blob: 9ccbc6eb1863bdefe9fbb88a3149cbcb9956d47c [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>
21
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#include "v8-version.h" // NOLINT(build/include)
23#include "v8config.h" // NOLINT(build/include)
Steve Blocka7e24c12009-10-30 11:49:00 +000024
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025// We reserve the V8_* prefix for macros defined in V8 public API and
26// assume there are no name conflicts with the embedder's code.
27
28#ifdef V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000029
30// Setup for Windows DLL export/import. When building the V8 DLL the
31// BUILDING_V8_SHARED needs to be defined. When building a program which uses
32// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
33// static library or building a program which uses the V8 static library neither
34// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
Steve Blocka7e24c12009-10-30 11:49:00 +000035#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
36#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
37 build configuration to ensure that at most one of these is set
38#endif
39
40#ifdef BUILDING_V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041# define V8_EXPORT __declspec(dllexport)
Steve Blocka7e24c12009-10-30 11:49:00 +000042#elif USING_V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043# define V8_EXPORT __declspec(dllimport)
Steve Blocka7e24c12009-10-30 11:49:00 +000044#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045# define V8_EXPORT
Steve Blocka7e24c12009-10-30 11:49:00 +000046#endif // BUILDING_V8_SHARED
47
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048#else // V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000049
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050// Setup for Linux shared library export.
51#if V8_HAS_ATTRIBUTE_VISIBILITY && defined(V8_SHARED)
52# ifdef BUILDING_V8_SHARED
53# define V8_EXPORT __attribute__ ((visibility("default")))
54# else
55# define V8_EXPORT
56# endif
57#else
58# define V8_EXPORT
59#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000060
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061#endif // V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000062
63/**
64 * The v8 JavaScript engine.
65 */
66namespace v8 {
67
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068class AccessorSignature;
69class Array;
70class Boolean;
71class BooleanObject;
Steve Blocka7e24c12009-10-30 11:49:00 +000072class Context;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073class CpuProfiler;
74class Data;
75class Date;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076class External;
77class Function;
78class FunctionTemplate;
79class HeapProfiler;
80class ImplementationUtilities;
81class Int32;
82class Integer;
83class Isolate;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084template <class T>
85class Maybe;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086class Name;
Steve Blocka7e24c12009-10-30 11:49:00 +000087class Number;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000088class NumberObject;
Steve Blocka7e24c12009-10-30 11:49:00 +000089class Object;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090class ObjectOperationDescriptor;
91class ObjectTemplate;
92class Platform;
Steve Blocka7e24c12009-10-30 11:49:00 +000093class Primitive;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040094class Promise;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000095class Proxy;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096class RawOperationDescriptor;
97class Script;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098class SharedArrayBuffer;
Steve Blocka7e24c12009-10-30 11:49:00 +000099class Signature;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100class StartupData;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101class StackFrame;
102class StackTrace;
103class String;
104class StringObject;
105class Symbol;
106class SymbolObject;
107class Private;
108class Uint32;
109class Utils;
110class Value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000111template <class T> class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112template <class T>
113class MaybeLocal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114template <class T> class Eternal;
115template<class T> class NonCopyablePersistentTraits;
116template<class T> class PersistentBase;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117template <class T, class M = NonCopyablePersistentTraits<T> >
118class Persistent;
119template <class T>
120class Global;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121template<class K, class V, class T> class PersistentValueMap;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122template <class K, class V, class T>
123class PersistentValueMapBase;
124template <class K, class V, class T>
125class GlobalValueMap;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126template<class V, class T> class PersistentValueVector;
127template<class T, class P> class WeakCallbackObject;
Steve Blocka7e24c12009-10-30 11:49:00 +0000128class FunctionTemplate;
129class ObjectTemplate;
130class Data;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131template<typename T> class FunctionCallbackInfo;
132template<typename T> class PropertyCallbackInfo;
Kristian Monsen25f61362010-05-21 11:50:48 +0100133class StackTrace;
134class StackFrame;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135class Isolate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136class CallHandlerHelper;
137class EscapableHandleScope;
138template<typename T> class ReturnValue;
Steve Blocka7e24c12009-10-30 11:49:00 +0000139
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140namespace experimental {
141class FastAccessorBuilder;
142} // namespace experimental
143
Steve Blocka7e24c12009-10-30 11:49:00 +0000144namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +0000145class Arguments;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100146class Heap;
Steve Block44f0eee2011-05-26 01:26:41 +0100147class HeapObject;
148class Isolate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149class Object;
150struct StreamedSource;
151template<typename T> class CustomArguments;
152class PropertyCallbackArguments;
153class FunctionCallbackArguments;
154class GlobalHandles;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155} // namespace internal
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
157
Steve Blocka7e24c12009-10-30 11:49:00 +0000158/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 * General purpose unique identifier.
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161class UniqueId {
162 public:
163 explicit UniqueId(intptr_t data)
164 : data_(data) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 bool operator==(const UniqueId& other) const {
167 return data_ == other.data_;
168 }
169
170 bool operator!=(const UniqueId& other) const {
171 return data_ != other.data_;
172 }
173
174 bool operator<(const UniqueId& other) const {
175 return data_ < other.data_;
176 }
177
178 private:
179 intptr_t data_;
180};
Steve Blocka7e24c12009-10-30 11:49:00 +0000181
Ben Murdoch257744e2011-11-30 15:57:28 +0000182// --- Handles ---
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100184#define TYPE_CHECK(T, S) \
185 while (false) { \
186 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 }
188
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189
Steve Blocka7e24c12009-10-30 11:49:00 +0000190/**
191 * An object reference managed by the v8 garbage collector.
192 *
193 * All objects returned from v8 have to be tracked by the garbage
194 * collector so that it knows that the objects are still alive. Also,
195 * because the garbage collector may move objects, it is unsafe to
196 * point directly to an object. Instead, all objects are stored in
197 * handles which are known by the garbage collector and updated
198 * whenever an object moves. Handles should always be passed by value
199 * (except in cases like out-parameters) and they should never be
200 * allocated on the heap.
201 *
202 * There are two types of handles: local and persistent handles.
203 * Local handles are light-weight and transient and typically used in
204 * local operations. They are managed by HandleScopes. Persistent
205 * handles can be used when storing objects across several independent
206 * operations and have to be explicitly deallocated when they're no
207 * longer used.
208 *
209 * It is safe to extract the object stored in the handle by
210 * dereferencing the handle (for instance, to extract the Object* from
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000211 * a Local<Object>); the value will still be governed by a handle
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 * behind the scenes and the same rules apply to these values as to
213 * their handles.
214 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215template <class T>
216class Local {
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218 V8_INLINE Local() : val_(0) {}
219 template <class S>
220 V8_INLINE Local(Local<S> that)
Steve Blocka7e24c12009-10-30 11:49:00 +0000221 : val_(reinterpret_cast<T*>(*that)) {
222 /**
223 * This check fails when trying to convert between incompatible
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000224 * handles. For example, converting from a Local<String> to a
225 * Local<Number>.
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 */
227 TYPE_CHECK(T, S);
228 }
229
230 /**
231 * Returns true if the handle is empty.
232 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 V8_INLINE bool IsEmpty() const { return val_ == 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000234
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 /**
236 * Sets the handle to be empty. IsEmpty() will then return true.
237 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 V8_INLINE void Clear() { val_ = 0; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000239
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 V8_INLINE T* operator->() const { return val_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000241
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 V8_INLINE T* operator*() const { return val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000243
244 /**
245 * Checks whether two handles are the same.
246 * Returns true if both are empty, or if the objects
247 * to which they refer are identical.
248 * The handles' references are not checked.
249 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 template <class S>
251 V8_INLINE bool operator==(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
253 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
254 if (a == 0) return b == 0;
255 if (b == 0) return false;
256 return *a == *b;
257 }
258
259 template <class S> V8_INLINE bool operator==(
260 const PersistentBase<S>& that) const {
261 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
262 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 if (a == 0) return b == 0;
264 if (b == 0) return false;
265 return *a == *b;
266 }
267
268 /**
269 * Checks whether two handles are different.
270 * Returns true if only one of the handles is empty, or if
271 * the objects to which they refer are different.
272 * The handles' references are not checked.
273 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000274 template <class S>
275 V8_INLINE bool operator!=(const Local<S>& that) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 return !operator==(that);
277 }
278
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 template <class S> V8_INLINE bool operator!=(
280 const Persistent<S>& that) const {
281 return !operator==(that);
282 }
283
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 template <class S> V8_INLINE static Local<T> Cast(Local<S> that) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000285#ifdef V8_ENABLE_CHECKS
286 // If we're going to perform the type check then we have to check
287 // that the handle isn't empty before doing the checked cast.
288 if (that.IsEmpty()) return Local<T>();
289#endif
290 return Local<T>(T::Cast(*that));
291 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000292
Steve Blocka7e24c12009-10-30 11:49:00 +0000293
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 template <class S> V8_INLINE Local<S> As() {
Steve Block6ded16b2010-05-10 14:33:55 +0100295 return Local<S>::Cast(*this);
296 }
297
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 /**
299 * Create a local handle for the content of another handle.
300 * The referee is kept alive by the local handle even when
301 * the original handle is destroyed/disposed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303 V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304 V8_INLINE static Local<T> New(Isolate* isolate,
305 const PersistentBase<T>& that);
306
307 private:
308 friend class Utils;
309 template<class F> friend class Eternal;
310 template<class F> friend class PersistentBase;
311 template<class F, class M> friend class Persistent;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 template<class F> friend class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313 template <class F>
314 friend class MaybeLocal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 template<class F> friend class FunctionCallbackInfo;
316 template<class F> friend class PropertyCallbackInfo;
317 friend class String;
318 friend class Object;
319 friend class Context;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320 friend class Private;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321 template<class F> friend class internal::CustomArguments;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322 friend Local<Primitive> Undefined(Isolate* isolate);
323 friend Local<Primitive> Null(Isolate* isolate);
324 friend Local<Boolean> True(Isolate* isolate);
325 friend Local<Boolean> False(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 friend class HandleScope;
327 friend class EscapableHandleScope;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000328 template <class F1, class F2, class F3>
329 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 template<class F1, class F2> friend class PersistentValueVector;
331
Ben Murdoch097c5b22016-05-18 11:27:45 +0100332 explicit V8_INLINE Local(T* that) : val_(that) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 V8_INLINE static Local<T> New(Isolate* isolate, T* that);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000334 T* val_;
335};
336
337
338#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
339// Local is an alias for Local for historical reasons.
340template <class T>
341using Handle = Local<T>;
342#endif
343
344
345/**
346 * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether
347 * the Local<> is empty before it can be used.
348 *
349 * If an API method returns a MaybeLocal<>, the API method can potentially fail
350 * either because an exception is thrown, or because an exception is pending,
351 * e.g. because a previous API call threw an exception that hasn't been caught
352 * yet, or because a TerminateExecution exception was thrown. In that case, an
353 * empty MaybeLocal is returned.
354 */
355template <class T>
356class MaybeLocal {
357 public:
358 V8_INLINE MaybeLocal() : val_(nullptr) {}
359 template <class S>
360 V8_INLINE MaybeLocal(Local<S> that)
361 : val_(reinterpret_cast<T*>(*that)) {
362 TYPE_CHECK(T, S);
363 }
364
365 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
366
367 template <class S>
368 V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const {
369 out->val_ = IsEmpty() ? nullptr : this->val_;
370 return !IsEmpty();
371 }
372
373 // Will crash if the MaybeLocal<> is empty.
374 V8_INLINE Local<T> ToLocalChecked();
375
376 template <class S>
377 V8_INLINE Local<S> FromMaybe(Local<S> default_value) const {
378 return IsEmpty() ? default_value : Local<S>(val_);
379 }
380
381 private:
382 T* val_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383};
384
385
386// Eternal handles are set-once handles that live for the life of the isolate.
387template <class T> class Eternal {
388 public:
389 V8_INLINE Eternal() : index_(kInitialValue) { }
390 template<class S>
391 V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : index_(kInitialValue) {
392 Set(isolate, handle);
393 }
394 // Can only be safely called if already set.
395 V8_INLINE Local<T> Get(Isolate* isolate);
396 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; }
397 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
398
399 private:
400 static const int kInitialValue = -1;
401 int index_;
402};
403
404
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405static const int kInternalFieldsInWeakCallback = 2;
406
407
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400408template <typename T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409class WeakCallbackInfo {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400410 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411 typedef void (*Callback)(const WeakCallbackInfo<T>& data);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413 WeakCallbackInfo(Isolate* isolate, T* parameter,
414 void* internal_fields[kInternalFieldsInWeakCallback],
415 Callback* callback)
416 : isolate_(isolate), parameter_(parameter), callback_(callback) {
417 for (int i = 0; i < kInternalFieldsInWeakCallback; ++i) {
418 internal_fields_[i] = internal_fields[i];
419 }
420 }
421
422 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400423 V8_INLINE T* GetParameter() const { return parameter_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000424 V8_INLINE void* GetInternalField(int index) const;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400425
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000426 V8_INLINE V8_DEPRECATED("use indexed version",
427 void* GetInternalField1() const) {
428 return internal_fields_[0];
429 }
430 V8_INLINE V8_DEPRECATED("use indexed version",
431 void* GetInternalField2() const) {
432 return internal_fields_[1];
433 }
434
Ben Murdoch097c5b22016-05-18 11:27:45 +0100435 V8_DEPRECATED("Not realiable once SetSecondPassCallback() was used.",
436 bool IsFirstPass() const) {
437 return callback_ != nullptr;
438 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000439
440 // When first called, the embedder MUST Reset() the Global which triggered the
441 // callback. The Global itself is unusable for anything else. No v8 other api
442 // calls may be called in the first callback. Should additional work be
443 // required, the embedder must set a second pass callback, which will be
444 // called after all the initial callbacks are processed.
445 // Calling SetSecondPassCallback on the second pass will immediately crash.
446 void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400447
448 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000449 Isolate* isolate_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400450 T* parameter_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000451 Callback* callback_;
452 void* internal_fields_[kInternalFieldsInWeakCallback];
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400453};
454
455
456template <class T, class P>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000457class WeakCallbackData {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 public:
459 typedef void (*Callback)(const WeakCallbackData<T, P>& data);
460
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 WeakCallbackData(Isolate* isolate, P* parameter, Local<T> handle)
462 : isolate_(isolate), parameter_(parameter), handle_(handle) {}
463
464 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
465 V8_INLINE P* GetParameter() const { return parameter_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000466 V8_INLINE Local<T> GetValue() const { return handle_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467
468 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 Isolate* isolate_;
470 P* parameter_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471 Local<T> handle_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400472};
473
474
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475// TODO(dcarney): delete this with WeakCallbackData
476template <class T>
477using PhantomCallbackData = WeakCallbackInfo<T>;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400478
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400479
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000480enum class WeakCallbackType { kParameter, kInternalFields };
Steve Blocka7e24c12009-10-30 11:49:00 +0000481
482
483/**
484 * An object reference that is independent of any handle scope. Where
485 * a Local handle only lives as long as the HandleScope in which it was
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 * allocated, a PersistentBase handle remains valid until it is explicitly
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 * disposed.
488 *
489 * A persistent handle contains a reference to a storage cell within
490 * the v8 engine which holds an object value and which is updated by
491 * the garbage collector whenever the object is moved. A new storage
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000492 * cell can be created using the constructor or PersistentBase::Reset and
493 * existing handles can be disposed using PersistentBase::Reset.
494 *
Steve Blocka7e24c12009-10-30 11:49:00 +0000495 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000496template <class T> class PersistentBase {
Steve Blocka7e24c12009-10-30 11:49:00 +0000497 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499 * If non-empty, destroy the underlying storage cell
500 * IsEmpty() will return true after this call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000502 V8_INLINE void Reset();
503 /**
504 * If non-empty, destroy the underlying storage cell
505 * and create a new one with the contents of other if other is non empty
506 */
507 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000508 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
Steve Blocka7e24c12009-10-30 11:49:00 +0000509
510 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000511 * If non-empty, destroy the underlying storage cell
512 * and create a new one with the contents of other if other is non empty
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514 template <class S>
515 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
516
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400517 V8_INLINE bool IsEmpty() const { return val_ == NULL; }
518 V8_INLINE void Empty() { val_ = 0; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000520 V8_INLINE Local<T> Get(Isolate* isolate) const {
521 return Local<T>::New(isolate, *this);
522 }
523
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 template <class S>
525 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
526 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
527 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400528 if (a == NULL) return b == NULL;
529 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 return *a == *b;
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 }
532
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000533 template <class S>
534 V8_INLINE bool operator==(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
536 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400537 if (a == NULL) return b == NULL;
538 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 return *a == *b;
540 }
541
542 template <class S>
543 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
544 return !operator==(that);
545 }
546
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 template <class S>
548 V8_INLINE bool operator!=(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549 return !operator==(that);
550 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000551
552 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553 * Install a finalization callback on this object.
554 * NOTE: There is no guarantee as to *when* or even *if* the callback is
555 * invoked. The invocation is performed solely on a best effort basis.
556 * As always, GC-based finalization should *not* be relied upon for any
557 * critical form of resource management!
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000559 template <typename P>
560 V8_INLINE V8_DEPRECATED(
561 "use WeakCallbackInfo version",
562 void SetWeak(P* parameter,
563 typename WeakCallbackData<T, P>::Callback callback));
Steve Blocka7e24c12009-10-30 11:49:00 +0000564
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000565 template <typename S, typename P>
566 V8_INLINE V8_DEPRECATED(
567 "use WeakCallbackInfo version",
568 void SetWeak(P* parameter,
569 typename WeakCallbackData<S, P>::Callback callback));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400571 // Phantom persistents work like weak persistents, except that the pointer to
572 // the object being collected is not available in the finalization callback.
573 // This enables the garbage collector to collect the object and any objects
574 // it references transitively in one GC cycle. At the moment you can either
575 // specify a parameter for the callback or the location of two internal
576 // fields in the dying object.
577 template <typename P>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000578 V8_INLINE V8_DEPRECATED(
579 "use SetWeak",
580 void SetPhantom(P* parameter,
581 typename WeakCallbackInfo<P>::Callback callback,
582 int internal_field_index1 = -1,
583 int internal_field_index2 = -1));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400584
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000585 template <typename P>
586 V8_INLINE void SetWeak(P* parameter,
587 typename WeakCallbackInfo<P>::Callback callback,
588 WeakCallbackType type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400589
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 template<typename P>
591 V8_INLINE P* ClearWeak();
592
593 // TODO(dcarney): remove this.
594 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
595
596 /**
597 * Marks the reference to this object independent. Garbage collector is free
598 * to ignore any object groups containing this object. Weak callback for an
599 * independent handle should not assume that it will be preceded by a global
600 * GC prologue callback or followed by a global GC epilogue callback.
601 */
602 V8_INLINE void MarkIndependent();
603
604 /**
605 * Marks the reference to this object partially dependent. Partially dependent
606 * handles only depend on other partially dependent handles and these
607 * dependencies are provided through object groups. It provides a way to build
608 * smaller object groups for young objects that represent only a subset of all
609 * external dependencies. This mark is automatically cleared after each
610 * garbage collection.
611 */
612 V8_INLINE void MarkPartiallyDependent();
613
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000614 /**
615 * Marks the reference to this object as active. The scavenge garbage
616 * collection should not reclaim the objects marked as active.
617 * This bit is cleared after the each garbage collection pass.
618 */
619 V8_INLINE void MarkActive();
620
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000621 V8_INLINE bool IsIndependent() const;
622
623 /** Checks if the handle holds the only reference to an object. */
624 V8_INLINE bool IsNearDeath() const;
625
626 /** Returns true if the handle's reference is weak. */
627 V8_INLINE bool IsWeak() const;
628
629 /**
630 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
631 * description in v8-profiler.h for details.
632 */
633 V8_INLINE void SetWrapperClassId(uint16_t class_id);
634
635 /**
636 * Returns the class ID previously assigned to this handle or 0 if no class ID
637 * was previously assigned.
638 */
639 V8_INLINE uint16_t WrapperClassId() const;
640
641 private:
642 friend class Isolate;
643 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000644 template<class F> friend class Local;
645 template<class F1, class F2> friend class Persistent;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000646 template <class F>
647 friend class Global;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 template<class F> friend class PersistentBase;
649 template<class F> friend class ReturnValue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000650 template <class F1, class F2, class F3>
651 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652 template<class F1, class F2> friend class PersistentValueVector;
653 friend class Object;
654
655 explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000656 PersistentBase(const PersistentBase& other) = delete; // NOLINT
657 void operator=(const PersistentBase&) = delete;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000658 V8_INLINE static T* New(Isolate* isolate, T* that);
659
660 T* val_;
661};
662
663
664/**
665 * Default traits for Persistent. This class does not allow
666 * use of the copy constructor or assignment operator.
667 * At present kResetInDestructor is not set, but that will change in a future
668 * version.
669 */
670template<class T>
671class NonCopyablePersistentTraits {
672 public:
673 typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
674 static const bool kResetInDestructor = false;
675 template<class S, class M>
676 V8_INLINE static void Copy(const Persistent<S, M>& source,
677 NonCopyablePersistent* dest) {
678 Uncompilable<Object>();
679 }
680 // TODO(dcarney): come up with a good compile error here.
681 template<class O> V8_INLINE static void Uncompilable() {
682 TYPE_CHECK(O, Primitive);
683 }
684};
685
686
687/**
688 * Helper class traits to allow copying and assignment of Persistent.
689 * This will clone the contents of storage cell, but not any of the flags, etc.
690 */
691template<class T>
692struct CopyablePersistentTraits {
693 typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
694 static const bool kResetInDestructor = true;
695 template<class S, class M>
696 static V8_INLINE void Copy(const Persistent<S, M>& source,
697 CopyablePersistent* dest) {
698 // do nothing, just allow copy
699 }
700};
701
702
703/**
704 * A PersistentBase which allows copy and assignment.
705 *
706 * Copy, assignment and destructor bevavior is controlled by the traits
707 * class M.
708 *
709 * Note: Persistent class hierarchy is subject to future changes.
710 */
711template <class T, class M> class Persistent : public PersistentBase<T> {
712 public:
713 /**
714 * A Persistent with no storage cell.
715 */
716 V8_INLINE Persistent() : PersistentBase<T>(0) { }
717 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000718 * Construct a Persistent from a Local.
719 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000720 * pointing to the same object, and no flags are set.
721 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000722 template <class S>
723 V8_INLINE Persistent(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000724 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
725 TYPE_CHECK(T, S);
726 }
727 /**
728 * Construct a Persistent from a Persistent.
729 * When the Persistent is non-empty, a new storage cell is created
730 * pointing to the same object, and no flags are set.
731 */
732 template <class S, class M2>
733 V8_INLINE Persistent(Isolate* isolate, const Persistent<S, M2>& that)
734 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
735 TYPE_CHECK(T, S);
736 }
737 /**
738 * The copy constructors and assignment operator create a Persistent
739 * exactly as the Persistent constructor, but the Copy function from the
740 * traits class is called, allowing the setting of flags based on the
741 * copied Persistent.
742 */
743 V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(0) {
744 Copy(that);
745 }
746 template <class S, class M2>
747 V8_INLINE Persistent(const Persistent<S, M2>& that) : PersistentBase<T>(0) {
748 Copy(that);
749 }
750 V8_INLINE Persistent& operator=(const Persistent& that) { // NOLINT
751 Copy(that);
752 return *this;
753 }
754 template <class S, class M2>
755 V8_INLINE Persistent& operator=(const Persistent<S, M2>& that) { // NOLINT
756 Copy(that);
757 return *this;
758 }
759 /**
760 * The destructor will dispose the Persistent based on the
761 * kResetInDestructor flags in the traits class. Since not calling dispose
762 * can result in a memory leak, it is recommended to always set this flag.
763 */
764 V8_INLINE ~Persistent() {
765 if (M::kResetInDestructor) this->Reset();
766 }
767
768 // TODO(dcarney): this is pretty useless, fix or remove
769 template <class S>
770 V8_INLINE static Persistent<T>& Cast(Persistent<S>& that) { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000771#ifdef V8_ENABLE_CHECKS
772 // If we're going to perform the type check then we have to check
773 // that the handle isn't empty before doing the checked cast.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000774 if (!that.IsEmpty()) T::Cast(*that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000775#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 return reinterpret_cast<Persistent<T>&>(that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000777 }
778
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779 // TODO(dcarney): this is pretty useless, fix or remove
780 template <class S> V8_INLINE Persistent<S>& As() { // NOLINT
Steve Block6ded16b2010-05-10 14:33:55 +0100781 return Persistent<S>::Cast(*this);
782 }
783
Steve Blocka7e24c12009-10-30 11:49:00 +0000784 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000785 friend class Isolate;
786 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000787 template<class F> friend class Local;
788 template<class F1, class F2> friend class Persistent;
789 template<class F> friend class ReturnValue;
790
Ben Murdoch097c5b22016-05-18 11:27:45 +0100791 explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792 V8_INLINE T* operator*() const { return this->val_; }
793 template<class S, class M2>
794 V8_INLINE void Copy(const Persistent<S, M2>& that);
795};
796
797
798/**
799 * A PersistentBase which has move semantics.
800 *
801 * Note: Persistent class hierarchy is subject to future changes.
802 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000803template <class T>
804class Global : public PersistentBase<T> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 public:
806 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000807 * A Global with no storage cell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000808 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000809 V8_INLINE Global() : PersistentBase<T>(nullptr) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000810 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000811 * Construct a Global from a Local.
812 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813 * pointing to the same object, and no flags are set.
814 */
815 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000816 V8_INLINE Global(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000817 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
818 TYPE_CHECK(T, S);
819 }
820 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000821 * Construct a Global from a PersistentBase.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000822 * When the Persistent is non-empty, a new storage cell is created
823 * pointing to the same object, and no flags are set.
824 */
825 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000826 V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that)
827 : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 TYPE_CHECK(T, S);
829 }
830 /**
831 * Move constructor.
832 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000833 V8_INLINE Global(Global&& other) : PersistentBase<T>(other.val_) { // NOLINT
834 other.val_ = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000835 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000836 V8_INLINE ~Global() { this->Reset(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000837 /**
838 * Move via assignment.
839 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000840 template <class S>
841 V8_INLINE Global& operator=(Global<S>&& rhs) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000842 TYPE_CHECK(T, S);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000843 if (this != &rhs) {
844 this->Reset();
845 this->val_ = rhs.val_;
846 rhs.val_ = nullptr;
847 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848 return *this;
849 }
850 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000851 * Pass allows returning uniques from functions, etc.
852 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000853 Global Pass() { return static_cast<Global&&>(*this); } // NOLINT
854
855 /*
856 * For compatibility with Chromium's base::Bind (base::Passed).
857 */
858 typedef void MoveOnlyTypeForCPP03;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859
860 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000861 template <class F>
862 friend class ReturnValue;
863 Global(const Global&) = delete;
864 void operator=(const Global&) = delete;
865 V8_INLINE T* operator*() const { return this->val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000866};
867
868
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000869// UniquePersistent is an alias for Global for historical reason.
870template <class T>
871using UniquePersistent = Global<T>;
872
873
Steve Blocka7e24c12009-10-30 11:49:00 +0000874 /**
875 * A stack-allocated class that governs a number of local handles.
876 * After a handle scope has been created, all local handles will be
877 * allocated within that handle scope until either the handle scope is
878 * deleted or another handle scope is created. If there is already a
879 * handle scope and a new one is created, all allocations will take
880 * place in the new handle scope until it is deleted. After that,
881 * new handles will again be allocated in the original handle scope.
882 *
883 * After the handle scope of a local handle has been deleted the
884 * garbage collector will no longer track the object stored in the
885 * handle and may deallocate it. The behavior of accessing a handle
886 * for which the handle scope has been deleted is undefined.
887 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000888class V8_EXPORT HandleScope {
Steve Blocka7e24c12009-10-30 11:49:00 +0000889 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100890 explicit HandleScope(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000891
892 ~HandleScope();
893
894 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000895 * Counts the number of allocated handles.
896 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000897 static int NumberOfHandles(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000898
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899 V8_INLINE Isolate* GetIsolate() const {
900 return reinterpret_cast<Isolate*>(isolate_);
901 }
902
903 protected:
904 V8_INLINE HandleScope() {}
905
906 void Initialize(Isolate* isolate);
907
908 static internal::Object** CreateHandle(internal::Isolate* isolate,
909 internal::Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000910
911 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912 // Uses heap_object to obtain the current Isolate.
913 static internal::Object** CreateHandle(internal::HeapObject* heap_object,
914 internal::Object* value);
915
916 // Make it hard to create heap-allocated or illegal handle scopes by
917 // disallowing certain operations.
Steve Blocka7e24c12009-10-30 11:49:00 +0000918 HandleScope(const HandleScope&);
919 void operator=(const HandleScope&);
920 void* operator new(size_t size);
921 void operator delete(void*, size_t);
922
Steve Block44f0eee2011-05-26 01:26:41 +0100923 internal::Isolate* isolate_;
John Reck59135872010-11-02 12:39:01 -0700924 internal::Object** prev_next_;
925 internal::Object** prev_limit_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000926
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000927 // Local::New uses CreateHandle with an Isolate* parameter.
928 template<class F> friend class Local;
Steve Blocka7e24c12009-10-30 11:49:00 +0000929
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930 // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with
931 // a HeapObject* in their shortcuts.
932 friend class Object;
933 friend class Context;
Steve Blocka7e24c12009-10-30 11:49:00 +0000934};
935
936
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000937/**
938 * A HandleScope which first allocates a handle in the current scope
939 * which will be later filled with the escape value.
940 */
941class V8_EXPORT EscapableHandleScope : public HandleScope {
942 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100943 explicit EscapableHandleScope(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000944 V8_INLINE ~EscapableHandleScope() {}
945
946 /**
947 * Pushes the value into the previous scope and returns a handle to it.
948 * Cannot be called twice.
949 */
950 template <class T>
951 V8_INLINE Local<T> Escape(Local<T> value) {
952 internal::Object** slot =
953 Escape(reinterpret_cast<internal::Object**>(*value));
954 return Local<T>(reinterpret_cast<T*>(slot));
955 }
956
957 private:
958 internal::Object** Escape(internal::Object** escape_value);
959
960 // Make it hard to create heap-allocated or illegal handle scopes by
961 // disallowing certain operations.
962 EscapableHandleScope(const EscapableHandleScope&);
963 void operator=(const EscapableHandleScope&);
964 void* operator new(size_t size);
965 void operator delete(void*, size_t);
966
967 internal::Object** escape_slot_;
968};
969
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000970class V8_EXPORT SealHandleScope {
971 public:
972 SealHandleScope(Isolate* isolate);
973 ~SealHandleScope();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000974
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000975 private:
976 // Make it hard to create heap-allocated or illegal handle scopes by
977 // disallowing certain operations.
978 SealHandleScope(const SealHandleScope&);
979 void operator=(const SealHandleScope&);
980 void* operator new(size_t size);
981 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000983 internal::Isolate* isolate_;
984 internal::Object** prev_limit_;
985 int prev_sealed_level_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000986};
987
988
Ben Murdoch257744e2011-11-30 15:57:28 +0000989// --- Special objects ---
Steve Blocka7e24c12009-10-30 11:49:00 +0000990
991
992/**
993 * The superclass of values and API object templates.
994 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995class V8_EXPORT Data {
Steve Blocka7e24c12009-10-30 11:49:00 +0000996 private:
997 Data();
998};
999
1000
1001/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001002 * The optional attributes of ScriptOrigin.
1003 */
1004class ScriptOriginOptions {
1005 public:
1006 V8_INLINE ScriptOriginOptions(bool is_embedder_debug_script = false,
1007 bool is_shared_cross_origin = false,
1008 bool is_opaque = false)
1009 : flags_((is_embedder_debug_script ? kIsEmbedderDebugScript : 0) |
1010 (is_shared_cross_origin ? kIsSharedCrossOrigin : 0) |
1011 (is_opaque ? kIsOpaque : 0)) {}
1012 V8_INLINE ScriptOriginOptions(int flags)
1013 : flags_(flags &
1014 (kIsEmbedderDebugScript | kIsSharedCrossOrigin | kIsOpaque)) {}
1015 bool IsEmbedderDebugScript() const {
1016 return (flags_ & kIsEmbedderDebugScript) != 0;
1017 }
1018 bool IsSharedCrossOrigin() const {
1019 return (flags_ & kIsSharedCrossOrigin) != 0;
1020 }
1021 bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; }
1022 int Flags() const { return flags_; }
1023
1024 private:
1025 enum {
1026 kIsEmbedderDebugScript = 1,
1027 kIsSharedCrossOrigin = 1 << 1,
1028 kIsOpaque = 1 << 2
1029 };
1030 const int flags_;
1031};
1032
1033/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001034 * The origin, within a file, of a script.
1035 */
Steve Block8defd9f2010-07-08 12:39:36 +01001036class ScriptOrigin {
Steve Blocka7e24c12009-10-30 11:49:00 +00001037 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001038 V8_INLINE ScriptOrigin(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001039 Local<Value> resource_name,
1040 Local<Integer> resource_line_offset = Local<Integer>(),
1041 Local<Integer> resource_column_offset = Local<Integer>(),
1042 Local<Boolean> resource_is_shared_cross_origin = Local<Boolean>(),
1043 Local<Integer> script_id = Local<Integer>(),
1044 Local<Boolean> resource_is_embedder_debug_script = Local<Boolean>(),
1045 Local<Value> source_map_url = Local<Value>(),
1046 Local<Boolean> resource_is_opaque = Local<Boolean>());
1047 V8_INLINE Local<Value> ResourceName() const;
1048 V8_INLINE Local<Integer> ResourceLineOffset() const;
1049 V8_INLINE Local<Integer> ResourceColumnOffset() const;
1050 /**
1051 * Returns true for embedder's debugger scripts
1052 */
1053 V8_INLINE Local<Integer> ScriptID() const;
1054 V8_INLINE Local<Value> SourceMapUrl() const;
1055 V8_INLINE ScriptOriginOptions Options() const { return options_; }
1056
Steve Blocka7e24c12009-10-30 11:49:00 +00001057 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001058 Local<Value> resource_name_;
1059 Local<Integer> resource_line_offset_;
1060 Local<Integer> resource_column_offset_;
1061 ScriptOriginOptions options_;
1062 Local<Integer> script_id_;
1063 Local<Value> source_map_url_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001064};
1065
1066
1067/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001068 * A compiled JavaScript script, not yet tied to a Context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001070class V8_EXPORT UnboundScript {
Steve Blocka7e24c12009-10-30 11:49:00 +00001071 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001072 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001073 * Binds the script to the currently entered context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001074 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001075 Local<Script> BindToCurrentContext();
1076
1077 int GetId();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001078 Local<Value> GetScriptName();
Steve Blocka7e24c12009-10-30 11:49:00 +00001079
1080 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081 * Data read from magic sourceURL comments.
1082 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001083 Local<Value> GetSourceURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084 /**
1085 * Data read from magic sourceMappingURL comments.
1086 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001087 Local<Value> GetSourceMappingURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001088
1089 /**
1090 * Returns zero based line number of the code_pos location in the script.
1091 * -1 will be returned if no information available.
1092 */
1093 int GetLineNumber(int code_pos);
1094
1095 static const int kNoScriptId = 0;
1096};
1097
1098
1099/**
1100 * A compiled JavaScript script, tied to a Context which was active when the
1101 * script was compiled.
1102 */
1103class V8_EXPORT Script {
1104 public:
1105 /**
1106 * A shorthand for ScriptCompiler::Compile().
1107 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001108 static V8_DEPRECATE_SOON(
1109 "Use maybe version",
1110 Local<Script> Compile(Local<String> source,
1111 ScriptOrigin* origin = nullptr));
1112 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1113 Local<Context> context, Local<String> source,
1114 ScriptOrigin* origin = nullptr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001115
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001116 static Local<Script> V8_DEPRECATE_SOON("Use maybe version",
1117 Compile(Local<String> source,
1118 Local<String> file_name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001119
1120 /**
1121 * Runs the script returning the resulting value. It will be run in the
1122 * context in which it was created (ScriptCompiler::CompileBound or
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001123 * UnboundScript::BindToCurrentContext()).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001125 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Run());
1126 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127
1128 /**
1129 * Returns the corresponding context-unbound script.
1130 */
1131 Local<UnboundScript> GetUnboundScript();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001132};
1133
1134
1135/**
1136 * For compiling scripts.
1137 */
1138class V8_EXPORT ScriptCompiler {
1139 public:
1140 /**
1141 * Compilation data that the embedder can cache and pass back to speed up
1142 * future compilations. The data is produced if the CompilerOptions passed to
1143 * the compilation functions in ScriptCompiler contains produce_data_to_cache
1144 * = true. The data to cache can then can be retrieved from
1145 * UnboundScript.
1146 */
1147 struct V8_EXPORT CachedData {
1148 enum BufferPolicy {
1149 BufferNotOwned,
1150 BufferOwned
1151 };
1152
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001153 CachedData()
1154 : data(NULL),
1155 length(0),
1156 rejected(false),
1157 buffer_policy(BufferNotOwned) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001158
1159 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
1160 // data and guarantees that it stays alive until the CachedData object is
1161 // destroyed. If the policy is BufferOwned, the given data will be deleted
1162 // (with delete[]) when the CachedData object is destroyed.
1163 CachedData(const uint8_t* data, int length,
1164 BufferPolicy buffer_policy = BufferNotOwned);
1165 ~CachedData();
1166 // TODO(marja): Async compilation; add constructors which take a callback
1167 // which will be called when V8 no longer needs the data.
1168 const uint8_t* data;
1169 int length;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001170 bool rejected;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001171 BufferPolicy buffer_policy;
1172
1173 private:
1174 // Prevent copying. Not implemented.
1175 CachedData(const CachedData&);
1176 CachedData& operator=(const CachedData&);
1177 };
1178
1179 /**
1180 * Source code which can be then compiled to a UnboundScript or Script.
1181 */
1182 class Source {
1183 public:
1184 // Source takes ownership of CachedData.
1185 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
1186 CachedData* cached_data = NULL);
1187 V8_INLINE Source(Local<String> source_string,
1188 CachedData* cached_data = NULL);
1189 V8_INLINE ~Source();
1190
1191 // Ownership of the CachedData or its buffers is *not* transferred to the
1192 // caller. The CachedData object is alive as long as the Source object is
1193 // alive.
1194 V8_INLINE const CachedData* GetCachedData() const;
1195
1196 private:
1197 friend class ScriptCompiler;
1198 // Prevent copying. Not implemented.
1199 Source(const Source&);
1200 Source& operator=(const Source&);
1201
1202 Local<String> source_string;
1203
1204 // Origin information
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001205 Local<Value> resource_name;
1206 Local<Integer> resource_line_offset;
1207 Local<Integer> resource_column_offset;
1208 ScriptOriginOptions resource_options;
1209 Local<Value> source_map_url;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210
1211 // Cached data from previous compilation (if a kConsume*Cache flag is
1212 // set), or hold newly generated cache data (kProduce*Cache flags) are
1213 // set when calling a compile method.
1214 CachedData* cached_data;
1215 };
1216
1217 /**
1218 * For streaming incomplete script data to V8. The embedder should implement a
1219 * subclass of this class.
1220 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001221 class V8_EXPORT ExternalSourceStream {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 public:
1223 virtual ~ExternalSourceStream() {}
1224
1225 /**
1226 * V8 calls this to request the next chunk of data from the embedder. This
1227 * function will be called on a background thread, so it's OK to block and
1228 * wait for the data, if the embedder doesn't have data yet. Returns the
1229 * length of the data returned. When the data ends, GetMoreData should
1230 * return 0. Caller takes ownership of the data.
1231 *
1232 * When streaming UTF-8 data, V8 handles multi-byte characters split between
1233 * two data chunks, but doesn't handle multi-byte characters split between
1234 * more than two data chunks. The embedder can avoid this problem by always
1235 * returning at least 2 bytes of data.
1236 *
1237 * If the embedder wants to cancel the streaming, they should make the next
1238 * GetMoreData call return 0. V8 will interpret it as end of data (and most
1239 * probably, parsing will fail). The streaming task will return as soon as
1240 * V8 has parsed the data it received so far.
1241 */
1242 virtual size_t GetMoreData(const uint8_t** src) = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001243
1244 /**
1245 * V8 calls this method to set a 'bookmark' at the current position in
1246 * the source stream, for the purpose of (maybe) later calling
1247 * ResetToBookmark. If ResetToBookmark is called later, then subsequent
1248 * calls to GetMoreData should return the same data as they did when
1249 * SetBookmark was called earlier.
1250 *
1251 * The embedder may return 'false' to indicate it cannot provide this
1252 * functionality.
1253 */
1254 virtual bool SetBookmark();
1255
1256 /**
1257 * V8 calls this to return to a previously set bookmark.
1258 */
1259 virtual void ResetToBookmark();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001260 };
1261
1262
1263 /**
1264 * Source code which can be streamed into V8 in pieces. It will be parsed
1265 * while streaming. It can be compiled after the streaming is complete.
1266 * StreamedSource must be kept alive while the streaming task is ran (see
1267 * ScriptStreamingTask below).
1268 */
1269 class V8_EXPORT StreamedSource {
1270 public:
1271 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8 };
1272
1273 StreamedSource(ExternalSourceStream* source_stream, Encoding encoding);
1274 ~StreamedSource();
1275
1276 // Ownership of the CachedData or its buffers is *not* transferred to the
1277 // caller. The CachedData object is alive as long as the StreamedSource
1278 // object is alive.
1279 const CachedData* GetCachedData() const;
1280
1281 internal::StreamedSource* impl() const { return impl_; }
1282
1283 private:
1284 // Prevent copying. Not implemented.
1285 StreamedSource(const StreamedSource&);
1286 StreamedSource& operator=(const StreamedSource&);
1287
1288 internal::StreamedSource* impl_;
1289 };
1290
1291 /**
1292 * A streaming task which the embedder must run on a background thread to
1293 * stream scripts into V8. Returned by ScriptCompiler::StartStreamingScript.
1294 */
1295 class ScriptStreamingTask {
1296 public:
1297 virtual ~ScriptStreamingTask() {}
1298 virtual void Run() = 0;
1299 };
1300
1301 enum CompileOptions {
1302 kNoCompileOptions = 0,
1303 kProduceParserCache,
1304 kConsumeParserCache,
1305 kProduceCodeCache,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001306 kConsumeCodeCache
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001307 };
1308
1309 /**
1310 * Compiles the specified script (context-independent).
1311 * Cached data as part of the source object can be optionally produced to be
1312 * consumed later to speed up compilation of identical source scripts.
1313 *
1314 * Note that when producing cached data, the source must point to NULL for
1315 * cached data. When consuming cached data, the cached data must have been
1316 * produced by the same version of V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00001317 *
Andrei Popescu402d9372010-02-26 13:31:12 +00001318 * \param source Script source code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001319 * \return Compiled script object (context independent; for running it must be
1320 * bound to a context).
Andrei Popescu402d9372010-02-26 13:31:12 +00001321 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001322 static V8_DEPRECATED("Use maybe version",
1323 Local<UnboundScript> CompileUnbound(
1324 Isolate* isolate, Source* source,
1325 CompileOptions options = kNoCompileOptions));
1326 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001327 Isolate* isolate, Source* source,
1328 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001329
1330 /**
1331 * Compiles the specified script (bound to current context).
1332 *
1333 * \param source Script source code.
Andrei Popescu402d9372010-02-26 13:31:12 +00001334 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1335 * using pre_data speeds compilation if it's done multiple times.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336 * Owned by caller, no references are kept when this function returns.
Andrei Popescu402d9372010-02-26 13:31:12 +00001337 * \return Compiled script object, bound to the context that was active
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001338 * when this function was called. When run it will always use this
Andrei Popescu402d9372010-02-26 13:31:12 +00001339 * context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001340 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001341 static V8_DEPRECATED(
1342 "Use maybe version",
1343 Local<Script> Compile(Isolate* isolate, Source* source,
1344 CompileOptions options = kNoCompileOptions));
1345 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1346 Local<Context> context, Source* source,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001347 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001348
1349 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001350 * Returns a task which streams script data into V8, or NULL if the script
1351 * cannot be streamed. The user is responsible for running the task on a
1352 * background thread and deleting it. When ran, the task starts parsing the
1353 * script, and it will request data from the StreamedSource as needed. When
1354 * ScriptStreamingTask::Run exits, all data has been streamed and the script
1355 * can be compiled (see Compile below).
Andrei Popescu402d9372010-02-26 13:31:12 +00001356 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001357 * This API allows to start the streaming with as little data as possible, and
1358 * the remaining data (for example, the ScriptOrigin) is passed to Compile.
Andrei Popescu402d9372010-02-26 13:31:12 +00001359 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001360 static ScriptStreamingTask* StartStreamingScript(
1361 Isolate* isolate, StreamedSource* source,
1362 CompileOptions options = kNoCompileOptions);
Steve Blocka7e24c12009-10-30 11:49:00 +00001363
1364 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001365 * Compiles a streamed script (bound to current context).
1366 *
1367 * This can only be called after the streaming has finished
1368 * (ScriptStreamingTask has been run). V8 doesn't construct the source string
1369 * during streaming, so the embedder needs to pass the full source here.
Steve Blocka7e24c12009-10-30 11:49:00 +00001370 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001371 static V8_DEPRECATED("Use maybe version",
1372 Local<Script> Compile(Isolate* isolate,
1373 StreamedSource* source,
1374 Local<String> full_source_string,
1375 const ScriptOrigin& origin));
1376 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1377 Local<Context> context, StreamedSource* source,
1378 Local<String> full_source_string, const ScriptOrigin& origin);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001379
1380 /**
1381 * Return a version tag for CachedData for the current V8 version & flags.
1382 *
1383 * This value is meant only for determining whether a previously generated
1384 * CachedData instance is still valid; the tag has no other meaing.
1385 *
1386 * Background: The data carried by CachedData may depend on the exact
1387 * V8 version number or currently compiler flags. This means when
1388 * persisting CachedData, the embedder must take care to not pass in
1389 * data from another V8 version, or the same version with different
1390 * features enabled.
1391 *
1392 * The easiest way to do so is to clear the embedder's cache on any
1393 * such change.
1394 *
1395 * Alternatively, this tag can be stored alongside the cached data and
1396 * compared when it is being used.
1397 */
1398 static uint32_t CachedDataVersionTag();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001399
1400 /**
1401 * Compile an ES6 module.
1402 *
1403 * This is an unfinished experimental feature, and is only exposed
1404 * here for internal testing purposes.
1405 * Only parsing works at the moment. Do not use.
1406 *
1407 * TODO(adamk): Script is likely the wrong return value for this;
1408 * should return some new Module type.
1409 */
1410 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> CompileModule(
1411 Local<Context> context, Source* source,
1412 CompileOptions options = kNoCompileOptions);
1413
1414 /**
1415 * Compile a function for a given context. This is equivalent to running
1416 *
1417 * with (obj) {
1418 * return function(args) { ... }
1419 * }
1420 *
1421 * It is possible to specify multiple context extensions (obj in the above
1422 * example).
1423 */
1424 static V8_DEPRECATE_SOON("Use maybe version",
1425 Local<Function> CompileFunctionInContext(
1426 Isolate* isolate, Source* source,
1427 Local<Context> context, size_t arguments_count,
1428 Local<String> arguments[],
1429 size_t context_extension_count,
1430 Local<Object> context_extensions[]));
1431 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
1432 Local<Context> context, Source* source, size_t arguments_count,
1433 Local<String> arguments[], size_t context_extension_count,
1434 Local<Object> context_extensions[]);
1435
1436 private:
1437 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
1438 Isolate* isolate, Source* source, CompileOptions options, bool is_module);
Steve Blocka7e24c12009-10-30 11:49:00 +00001439};
1440
1441
1442/**
1443 * An error message.
1444 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445class V8_EXPORT Message {
Steve Blocka7e24c12009-10-30 11:49:00 +00001446 public:
1447 Local<String> Get() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001448
1449 V8_DEPRECATE_SOON("Use maybe version", Local<String> GetSourceLine() const);
1450 V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine(
1451 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001452
1453 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001454 * Returns the origin for the script from where the function causing the
1455 * error originates.
1456 */
1457 ScriptOrigin GetScriptOrigin() const;
1458
1459 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001460 * Returns the resource name for the script from where the function causing
1461 * the error originates.
1462 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001463 Local<Value> GetScriptResourceName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001464
1465 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001466 * Exception stack trace. By default stack traces are not captured for
1467 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1468 * to change this option.
1469 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001470 Local<StackTrace> GetStackTrace() const;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001471
1472 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001473 * Returns the number, 1-based, of the line where the error occurred.
1474 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001475 V8_DEPRECATE_SOON("Use maybe version", int GetLineNumber() const);
1476 V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001477
1478 /**
1479 * Returns the index within the script of the first character where
1480 * the error occurred.
1481 */
1482 int GetStartPosition() const;
1483
1484 /**
1485 * Returns the index within the script of the last character where
1486 * the error occurred.
1487 */
1488 int GetEndPosition() const;
1489
1490 /**
1491 * Returns the index within the line of the first character where
1492 * the error occurred.
1493 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001494 V8_DEPRECATE_SOON("Use maybe version", int GetStartColumn() const);
1495 V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001496
1497 /**
1498 * Returns the index within the line of the last character where
1499 * the error occurred.
1500 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001501 V8_DEPRECATED("Use maybe version", int GetEndColumn() const);
1502 V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001503
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001504 /**
1505 * Passes on the value set by the embedder when it fed the script from which
1506 * this Message was generated to V8.
1507 */
1508 bool IsSharedCrossOrigin() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001509 bool IsOpaque() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001510
Steve Blocka7e24c12009-10-30 11:49:00 +00001511 // TODO(1245381): Print to a string instead of on a FILE.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001512 static void PrintCurrentStackTrace(Isolate* isolate, FILE* out);
Kristian Monsen25f61362010-05-21 11:50:48 +01001513
1514 static const int kNoLineNumberInfo = 0;
1515 static const int kNoColumnInfo = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001516 static const int kNoScriptIdInfo = 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001517};
1518
1519
1520/**
1521 * Representation of a JavaScript stack trace. The information collected is a
1522 * snapshot of the execution stack and the information remains valid after
1523 * execution continues.
1524 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001525class V8_EXPORT StackTrace {
Kristian Monsen25f61362010-05-21 11:50:48 +01001526 public:
1527 /**
1528 * Flags that determine what information is placed captured for each
1529 * StackFrame when grabbing the current stack trace.
1530 */
1531 enum StackTraceOptions {
1532 kLineNumber = 1,
1533 kColumnOffset = 1 << 1 | kLineNumber,
1534 kScriptName = 1 << 2,
1535 kFunctionName = 1 << 3,
1536 kIsEval = 1 << 4,
1537 kIsConstructor = 1 << 5,
Ben Murdochf87a2032010-10-22 12:50:53 +01001538 kScriptNameOrSourceURL = 1 << 6,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001539 kScriptId = 1 << 7,
1540 kExposeFramesAcrossSecurityOrigins = 1 << 8,
Kristian Monsen25f61362010-05-21 11:50:48 +01001541 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
Ben Murdochf87a2032010-10-22 12:50:53 +01001542 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
Kristian Monsen25f61362010-05-21 11:50:48 +01001543 };
1544
1545 /**
1546 * Returns a StackFrame at a particular index.
1547 */
1548 Local<StackFrame> GetFrame(uint32_t index) const;
1549
1550 /**
1551 * Returns the number of StackFrames.
1552 */
1553 int GetFrameCount() const;
1554
1555 /**
1556 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1557 */
1558 Local<Array> AsArray();
1559
1560 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001561 * Grab a snapshot of the current JavaScript execution stack.
Kristian Monsen25f61362010-05-21 11:50:48 +01001562 *
1563 * \param frame_limit The maximum number of stack frames we want to capture.
1564 * \param options Enumerates the set of things we will capture for each
1565 * StackFrame.
1566 */
1567 static Local<StackTrace> CurrentStackTrace(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001568 Isolate* isolate,
Kristian Monsen25f61362010-05-21 11:50:48 +01001569 int frame_limit,
1570 StackTraceOptions options = kOverview);
1571};
1572
1573
1574/**
1575 * A single JavaScript stack frame.
1576 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001577class V8_EXPORT StackFrame {
Kristian Monsen25f61362010-05-21 11:50:48 +01001578 public:
1579 /**
1580 * Returns the number, 1-based, of the line for the associate function call.
1581 * This method will return Message::kNoLineNumberInfo if it is unable to
1582 * retrieve the line number, or if kLineNumber was not passed as an option
1583 * when capturing the StackTrace.
1584 */
1585 int GetLineNumber() const;
1586
1587 /**
1588 * Returns the 1-based column offset on the line for the associated function
1589 * call.
1590 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1591 * the column number, or if kColumnOffset was not passed as an option when
1592 * capturing the StackTrace.
1593 */
1594 int GetColumn() const;
1595
1596 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001597 * Returns the id of the script for the function for this StackFrame.
1598 * This method will return Message::kNoScriptIdInfo if it is unable to
1599 * retrieve the script id, or if kScriptId was not passed as an option when
1600 * capturing the StackTrace.
1601 */
1602 int GetScriptId() const;
1603
1604 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001605 * Returns the name of the resource that contains the script for the
1606 * function for this StackFrame.
1607 */
1608 Local<String> GetScriptName() const;
1609
1610 /**
Ben Murdochf87a2032010-10-22 12:50:53 +01001611 * Returns the name of the resource that contains the script for the
1612 * function for this StackFrame or sourceURL value if the script name
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001613 * is undefined and its source ends with //# sourceURL=... string or
1614 * deprecated //@ sourceURL=... string.
Ben Murdochf87a2032010-10-22 12:50:53 +01001615 */
1616 Local<String> GetScriptNameOrSourceURL() const;
1617
1618 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001619 * Returns the name of the function associated with this stack frame.
1620 */
1621 Local<String> GetFunctionName() const;
1622
1623 /**
1624 * Returns whether or not the associated function is compiled via a call to
1625 * eval().
1626 */
1627 bool IsEval() const;
1628
1629 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001630 * Returns whether or not the associated function is called as a
Kristian Monsen25f61362010-05-21 11:50:48 +01001631 * constructor via "new".
1632 */
1633 bool IsConstructor() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001634};
1635
1636
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001637// A StateTag represents a possible state of the VM.
1638enum StateTag { JS, GC, COMPILER, OTHER, EXTERNAL, IDLE };
1639
1640
1641// A RegisterState represents the current state of registers used
1642// by the sampling profiler API.
1643struct RegisterState {
1644 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {}
1645 void* pc; // Instruction pointer.
1646 void* sp; // Stack pointer.
1647 void* fp; // Frame pointer.
1648};
1649
1650
1651// The output structure filled up by GetStackSample API function.
1652struct SampleInfo {
1653 size_t frames_count;
1654 StateTag vm_state;
1655};
1656
1657
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001658/**
1659 * A JSON Parser.
1660 */
1661class V8_EXPORT JSON {
1662 public:
1663 /**
1664 * Tries to parse the string |json_string| and returns it as value if
1665 * successful.
1666 *
1667 * \param json_string The string to parse.
1668 * \return The corresponding value if successfully parsed.
1669 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001670 static V8_DEPRECATED("Use maybe version",
1671 Local<Value> Parse(Local<String> json_string));
1672 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse(
1673 Isolate* isolate, Local<String> json_string);
1674};
1675
1676
1677/**
1678 * A map whose keys are referenced weakly. It is similar to JavaScript WeakMap
1679 * but can be created without entering a v8::Context and hence shouldn't
1680 * escape to JavaScript.
1681 */
1682class V8_EXPORT NativeWeakMap : public Data {
1683 public:
1684 static Local<NativeWeakMap> New(Isolate* isolate);
1685 void Set(Local<Value> key, Local<Value> value);
1686 Local<Value> Get(Local<Value> key);
1687 bool Has(Local<Value> key);
1688 bool Delete(Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001689};
1690
1691
Ben Murdoch257744e2011-11-30 15:57:28 +00001692// --- Value ---
Steve Blocka7e24c12009-10-30 11:49:00 +00001693
1694
1695/**
1696 * The superclass of all JavaScript values and objects.
1697 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698class V8_EXPORT Value : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00001699 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001700 /**
1701 * Returns true if this value is the undefined value. See ECMA-262
1702 * 4.3.10.
1703 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001704 V8_INLINE bool IsUndefined() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001705
1706 /**
1707 * Returns true if this value is the null value. See ECMA-262
1708 * 4.3.11.
1709 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001710 V8_INLINE bool IsNull() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001711
1712 /**
1713 * Returns true if this value is true.
1714 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001715 bool IsTrue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001716
1717 /**
1718 * Returns true if this value is false.
1719 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001720 bool IsFalse() const;
1721
1722 /**
1723 * Returns true if this value is a symbol or a string.
1724 * This is an experimental feature.
1725 */
1726 bool IsName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001727
1728 /**
1729 * Returns true if this value is an instance of the String type.
1730 * See ECMA-262 8.4.
1731 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001732 V8_INLINE bool IsString() const;
1733
1734 /**
1735 * Returns true if this value is a symbol.
1736 * This is an experimental feature.
1737 */
1738 bool IsSymbol() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001739
1740 /**
1741 * Returns true if this value is a function.
1742 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 bool IsFunction() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001744
1745 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001746 * Returns true if this value is an array. Note that it will return false for
1747 * an Proxy for an array.
Steve Blocka7e24c12009-10-30 11:49:00 +00001748 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001749 bool IsArray() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001750
1751 /**
1752 * Returns true if this value is an object.
1753 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001754 bool IsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001755
1756 /**
1757 * Returns true if this value is boolean.
1758 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001759 bool IsBoolean() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001760
1761 /**
1762 * Returns true if this value is a number.
1763 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001764 bool IsNumber() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001765
1766 /**
1767 * Returns true if this value is external.
1768 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001769 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001770
1771 /**
1772 * Returns true if this value is a 32-bit signed integer.
1773 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001774 bool IsInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001775
1776 /**
Steve Block6ded16b2010-05-10 14:33:55 +01001777 * Returns true if this value is a 32-bit unsigned integer.
1778 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001779 bool IsUint32() const;
Steve Block6ded16b2010-05-10 14:33:55 +01001780
1781 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001782 * Returns true if this value is a Date.
1783 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001784 bool IsDate() const;
1785
1786 /**
1787 * Returns true if this value is an Arguments object.
1788 */
1789 bool IsArgumentsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001790
Iain Merrick75681382010-08-19 15:07:18 +01001791 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001792 * Returns true if this value is a Boolean object.
1793 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001794 bool IsBooleanObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001795
1796 /**
1797 * Returns true if this value is a Number object.
1798 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001799 bool IsNumberObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001800
1801 /**
1802 * Returns true if this value is a String object.
1803 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001804 bool IsStringObject() const;
1805
1806 /**
1807 * Returns true if this value is a Symbol object.
1808 * This is an experimental feature.
1809 */
1810 bool IsSymbolObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001811
1812 /**
1813 * Returns true if this value is a NativeError.
1814 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001815 bool IsNativeError() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001816
1817 /**
Iain Merrick75681382010-08-19 15:07:18 +01001818 * Returns true if this value is a RegExp.
1819 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001820 bool IsRegExp() const;
Iain Merrick75681382010-08-19 15:07:18 +01001821
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001822 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001823 * Returns true if this value is a Generator function.
1824 * This is an experimental feature.
1825 */
1826 bool IsGeneratorFunction() const;
1827
1828 /**
1829 * Returns true if this value is a Generator object (iterator).
1830 * This is an experimental feature.
1831 */
1832 bool IsGeneratorObject() const;
1833
1834 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001835 * Returns true if this value is a Promise.
1836 * This is an experimental feature.
1837 */
1838 bool IsPromise() const;
1839
1840 /**
1841 * Returns true if this value is a Map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001842 */
1843 bool IsMap() const;
1844
1845 /**
1846 * Returns true if this value is a Set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001847 */
1848 bool IsSet() const;
1849
1850 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001851 * Returns true if this value is a Map Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001852 */
1853 bool IsMapIterator() const;
1854
1855 /**
1856 * Returns true if this value is a Set Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001857 */
1858 bool IsSetIterator() const;
1859
1860 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001861 * Returns true if this value is a WeakMap.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001862 */
1863 bool IsWeakMap() const;
1864
1865 /**
1866 * Returns true if this value is a WeakSet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001867 */
1868 bool IsWeakSet() const;
1869
1870 /**
1871 * Returns true if this value is an ArrayBuffer.
1872 * This is an experimental feature.
1873 */
1874 bool IsArrayBuffer() const;
1875
1876 /**
1877 * Returns true if this value is an ArrayBufferView.
1878 * This is an experimental feature.
1879 */
1880 bool IsArrayBufferView() const;
1881
1882 /**
1883 * Returns true if this value is one of TypedArrays.
1884 * This is an experimental feature.
1885 */
1886 bool IsTypedArray() const;
1887
1888 /**
1889 * Returns true if this value is an Uint8Array.
1890 * This is an experimental feature.
1891 */
1892 bool IsUint8Array() const;
1893
1894 /**
1895 * Returns true if this value is an Uint8ClampedArray.
1896 * This is an experimental feature.
1897 */
1898 bool IsUint8ClampedArray() const;
1899
1900 /**
1901 * Returns true if this value is an Int8Array.
1902 * This is an experimental feature.
1903 */
1904 bool IsInt8Array() const;
1905
1906 /**
1907 * Returns true if this value is an Uint16Array.
1908 * This is an experimental feature.
1909 */
1910 bool IsUint16Array() const;
1911
1912 /**
1913 * Returns true if this value is an Int16Array.
1914 * This is an experimental feature.
1915 */
1916 bool IsInt16Array() const;
1917
1918 /**
1919 * Returns true if this value is an Uint32Array.
1920 * This is an experimental feature.
1921 */
1922 bool IsUint32Array() const;
1923
1924 /**
1925 * Returns true if this value is an Int32Array.
1926 * This is an experimental feature.
1927 */
1928 bool IsInt32Array() const;
1929
1930 /**
1931 * Returns true if this value is a Float32Array.
1932 * This is an experimental feature.
1933 */
1934 bool IsFloat32Array() const;
1935
1936 /**
1937 * Returns true if this value is a Float64Array.
1938 * This is an experimental feature.
1939 */
1940 bool IsFloat64Array() const;
1941
1942 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001943 * Returns true if this value is a SIMD Float32x4.
1944 * This is an experimental feature.
1945 */
1946 bool IsFloat32x4() const;
1947
1948 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001949 * Returns true if this value is a DataView.
1950 * This is an experimental feature.
1951 */
1952 bool IsDataView() const;
1953
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001954 /**
1955 * Returns true if this value is a SharedArrayBuffer.
1956 * This is an experimental feature.
1957 */
1958 bool IsSharedArrayBuffer() const;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001959
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001960 /**
1961 * Returns true if this value is a JavaScript Proxy.
1962 */
1963 bool IsProxy() const;
1964
1965
1966 V8_WARN_UNUSED_RESULT MaybeLocal<Boolean> ToBoolean(
1967 Local<Context> context) const;
1968 V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber(
1969 Local<Context> context) const;
1970 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString(
1971 Local<Context> context) const;
1972 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString(
1973 Local<Context> context) const;
1974 V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
1975 Local<Context> context) const;
1976 V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger(
1977 Local<Context> context) const;
1978 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32(
1979 Local<Context> context) const;
1980 V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const;
1981
1982 V8_DEPRECATE_SOON("Use maybe version",
1983 Local<Boolean> ToBoolean(Isolate* isolate) const);
1984 V8_DEPRECATE_SOON("Use maybe version",
1985 Local<Number> ToNumber(Isolate* isolate) const);
1986 V8_DEPRECATE_SOON("Use maybe version",
1987 Local<String> ToString(Isolate* isolate) const);
1988 V8_DEPRECATED("Use maybe version",
1989 Local<String> ToDetailString(Isolate* isolate) const);
1990 V8_DEPRECATE_SOON("Use maybe version",
1991 Local<Object> ToObject(Isolate* isolate) const);
1992 V8_DEPRECATE_SOON("Use maybe version",
1993 Local<Integer> ToInteger(Isolate* isolate) const);
1994 V8_DEPRECATED("Use maybe version",
1995 Local<Uint32> ToUint32(Isolate* isolate) const);
1996 V8_DEPRECATE_SOON("Use maybe version",
1997 Local<Int32> ToInt32(Isolate* isolate) const);
1998
1999 inline V8_DEPRECATE_SOON("Use maybe version",
2000 Local<Boolean> ToBoolean() const);
2001 inline V8_DEPRECATED("Use maybe version", Local<Number> ToNumber() const);
2002 inline V8_DEPRECATE_SOON("Use maybe version", Local<String> ToString() const);
2003 inline V8_DEPRECATED("Use maybe version",
2004 Local<String> ToDetailString() const);
2005 inline V8_DEPRECATE_SOON("Use maybe version", Local<Object> ToObject() const);
2006 inline V8_DEPRECATE_SOON("Use maybe version",
2007 Local<Integer> ToInteger() const);
2008 inline V8_DEPRECATED("Use maybe version", Local<Uint32> ToUint32() const);
2009 inline V8_DEPRECATED("Use maybe version", Local<Int32> ToInt32() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00002010
2011 /**
2012 * Attempts to convert a string to an array index.
2013 * Returns an empty handle if the conversion fails.
2014 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002015 V8_DEPRECATED("Use maybe version", Local<Uint32> ToArrayIndex() const);
2016 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex(
2017 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002018
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002019 V8_WARN_UNUSED_RESULT Maybe<bool> BooleanValue(Local<Context> context) const;
2020 V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
2021 V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
2022 Local<Context> context) const;
2023 V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
2024 Local<Context> context) const;
2025 V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
2026
2027 V8_DEPRECATE_SOON("Use maybe version", bool BooleanValue() const);
2028 V8_DEPRECATE_SOON("Use maybe version", double NumberValue() const);
2029 V8_DEPRECATE_SOON("Use maybe version", int64_t IntegerValue() const);
2030 V8_DEPRECATE_SOON("Use maybe version", uint32_t Uint32Value() const);
2031 V8_DEPRECATE_SOON("Use maybe version", int32_t Int32Value() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00002032
2033 /** JS == */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002034 V8_DEPRECATE_SOON("Use maybe version", bool Equals(Local<Value> that) const);
2035 V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
2036 Local<Value> that) const;
2037 bool StrictEquals(Local<Value> that) const;
2038 bool SameValue(Local<Value> that) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002039
2040 template <class T> V8_INLINE static Value* Cast(T* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002041
Steve Blocka7e24c12009-10-30 11:49:00 +00002042 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002043 V8_INLINE bool QuickIsUndefined() const;
2044 V8_INLINE bool QuickIsNull() const;
2045 V8_INLINE bool QuickIsString() const;
2046 bool FullIsUndefined() const;
2047 bool FullIsNull() const;
2048 bool FullIsString() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002049};
2050
2051
2052/**
2053 * The superclass of primitive values. See ECMA-262 4.3.2.
2054 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002055class V8_EXPORT Primitive : public Value { };
Steve Blocka7e24c12009-10-30 11:49:00 +00002056
2057
2058/**
2059 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
2060 * or false value.
2061 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002062class V8_EXPORT Boolean : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002063 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002064 bool Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002065 V8_INLINE static Boolean* Cast(v8::Value* obj);
2066 V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value);
2067
2068 private:
2069 static void CheckCast(v8::Value* obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002070};
2071
2072
2073/**
2074 * A superclass for symbols and strings.
2075 */
2076class V8_EXPORT Name : public Primitive {
2077 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002078 /**
2079 * Returns the identity hash for this object. The current implementation
2080 * uses an inline property on the object to store the identity hash.
2081 *
2082 * The return value will never be 0. Also, it is not guaranteed to be
2083 * unique.
2084 */
2085 int GetIdentityHash();
2086
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002087 V8_INLINE static Name* Cast(v8::Value* obj);
2088 private:
2089 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002090};
2091
2092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002093enum class NewStringType { kNormal, kInternalized };
2094
2095
Steve Blocka7e24c12009-10-30 11:49:00 +00002096/**
2097 * A JavaScript string value (ECMA-262, 4.3.17).
2098 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002099class V8_EXPORT String : public Name {
Steve Blocka7e24c12009-10-30 11:49:00 +00002100 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002101 static const int kMaxLength = (1 << 28) - 16;
2102
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002103 enum Encoding {
2104 UNKNOWN_ENCODING = 0x1,
2105 TWO_BYTE_ENCODING = 0x0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002106 ONE_BYTE_ENCODING = 0x4
2107 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002108 /**
2109 * Returns the number of characters in this string.
2110 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002111 int Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002112
2113 /**
2114 * Returns the number of bytes in the UTF-8 encoded
2115 * representation of this string.
2116 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002117 int Utf8Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002118
2119 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002120 * Returns whether this string is known to contain only one byte data.
2121 * Does not read the string.
2122 * False negatives are possible.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002123 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002124 bool IsOneByte() const;
2125
2126 /**
2127 * Returns whether this string contain only one byte data.
2128 * Will read the entire string in some cases.
2129 */
2130 bool ContainsOnlyOneByte() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002131
2132 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002133 * Write the contents of the string to an external buffer.
2134 * If no arguments are given, expects the buffer to be large
2135 * enough to hold the entire string and NULL terminator. Copies
2136 * the contents of the string and the NULL terminator into the
2137 * buffer.
2138 *
Ben Murdochb0fe1622011-05-05 13:52:32 +01002139 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
2140 * before the end of the buffer.
2141 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002142 * Copies up to length characters into the output buffer.
2143 * Only null-terminates if there is enough space in the buffer.
2144 *
2145 * \param buffer The buffer into which the string will be copied.
2146 * \param start The starting position within the string at which
2147 * copying begins.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002148 * \param length The number of characters to copy from the string. For
2149 * WriteUtf8 the number of bytes in the buffer.
Steve Block6ded16b2010-05-10 14:33:55 +01002150 * \param nchars_ref The number of characters written, can be NULL.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002151 * \param options Various options that might affect performance of this or
Steve Block6ded16b2010-05-10 14:33:55 +01002152 * subsequent operations.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002153 * \return The number of characters copied to the buffer excluding the null
2154 * terminator. For WriteUtf8: The number of bytes copied to the buffer
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002155 * including the null terminator (if written).
Steve Blocka7e24c12009-10-30 11:49:00 +00002156 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002157 enum WriteOptions {
2158 NO_OPTIONS = 0,
2159 HINT_MANY_WRITES_EXPECTED = 1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002160 NO_NULL_TERMINATION = 2,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002161 PRESERVE_ONE_BYTE_NULL = 4,
2162 // Used by WriteUtf8 to replace orphan surrogate code units with the
2163 // unicode replacement character. Needs to be set to guarantee valid UTF-8
2164 // output.
2165 REPLACE_INVALID_UTF8 = 8
Steve Block6ded16b2010-05-10 14:33:55 +01002166 };
2167
Ben Murdoch589d6972011-11-30 16:04:58 +00002168 // 16-bit character codes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002169 int Write(uint16_t* buffer,
2170 int start = 0,
2171 int length = -1,
2172 int options = NO_OPTIONS) const;
2173 // One byte characters.
2174 int WriteOneByte(uint8_t* buffer,
2175 int start = 0,
2176 int length = -1,
2177 int options = NO_OPTIONS) const;
Ben Murdoch589d6972011-11-30 16:04:58 +00002178 // UTF-8 encoded characters.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002179 int WriteUtf8(char* buffer,
2180 int length = -1,
2181 int* nchars_ref = NULL,
2182 int options = NO_OPTIONS) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002183
2184 /**
2185 * A zero length string.
2186 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002187 V8_INLINE static v8::Local<v8::String> Empty(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002188
2189 /**
2190 * Returns true if the string is external
2191 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002192 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002193
2194 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002195 * Returns true if the string is both external and one-byte.
Steve Blocka7e24c12009-10-30 11:49:00 +00002196 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002197 bool IsExternalOneByte() const;
Leon Clarkee46be812010-01-19 14:06:41 +00002198
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002199 class V8_EXPORT ExternalStringResourceBase { // NOLINT
Leon Clarkee46be812010-01-19 14:06:41 +00002200 public:
2201 virtual ~ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002202
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002203 virtual bool IsCompressible() const { return false; }
2204
Leon Clarkee46be812010-01-19 14:06:41 +00002205 protected:
2206 ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002207
2208 /**
2209 * Internally V8 will call this Dispose method when the external string
2210 * resource is no longer needed. The default implementation will use the
2211 * delete operator. This method can be overridden in subclasses to
2212 * control how allocated external string resources are disposed.
2213 */
2214 virtual void Dispose() { delete this; }
2215
Leon Clarkee46be812010-01-19 14:06:41 +00002216 private:
2217 // Disallow copying and assigning.
2218 ExternalStringResourceBase(const ExternalStringResourceBase&);
2219 void operator=(const ExternalStringResourceBase&);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002220
2221 friend class v8::internal::Heap;
Leon Clarkee46be812010-01-19 14:06:41 +00002222 };
2223
Steve Blocka7e24c12009-10-30 11:49:00 +00002224 /**
2225 * An ExternalStringResource is a wrapper around a two-byte string
2226 * buffer that resides outside V8's heap. Implement an
2227 * ExternalStringResource to manage the life cycle of the underlying
2228 * buffer. Note that the string data must be immutable.
2229 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002230 class V8_EXPORT ExternalStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002231 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002232 public:
2233 /**
2234 * Override the destructor to manage the life cycle of the underlying
2235 * buffer.
2236 */
2237 virtual ~ExternalStringResource() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002238
2239 /**
2240 * The string data from the underlying buffer.
2241 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002242 virtual const uint16_t* data() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002243
2244 /**
2245 * The length of the string. That is, the number of two-byte characters.
2246 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002247 virtual size_t length() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002248
Steve Blocka7e24c12009-10-30 11:49:00 +00002249 protected:
2250 ExternalStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002251 };
2252
2253 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002254 * An ExternalOneByteStringResource is a wrapper around an one-byte
Steve Blocka7e24c12009-10-30 11:49:00 +00002255 * string buffer that resides outside V8's heap. Implement an
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002256 * ExternalOneByteStringResource to manage the life cycle of the
Steve Blocka7e24c12009-10-30 11:49:00 +00002257 * underlying buffer. Note that the string data must be immutable
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002258 * and that the data must be Latin-1 and not UTF-8, which would require
2259 * special treatment internally in the engine and do not allow efficient
2260 * indexing. Use String::New or convert to 16 bit data for non-Latin1.
Steve Blocka7e24c12009-10-30 11:49:00 +00002261 */
2262
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002263 class V8_EXPORT ExternalOneByteStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002264 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 public:
2266 /**
2267 * Override the destructor to manage the life cycle of the underlying
2268 * buffer.
2269 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002270 virtual ~ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002271 /** The string data from the underlying buffer.*/
2272 virtual const char* data() const = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002273 /** The number of Latin-1 characters in the string.*/
Steve Blocka7e24c12009-10-30 11:49:00 +00002274 virtual size_t length() const = 0;
2275 protected:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002276 ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002277 };
2278
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002279 /**
2280 * If the string is an external string, return the ExternalStringResourceBase
2281 * regardless of the encoding, otherwise return NULL. The encoding of the
2282 * string is returned in encoding_out.
2283 */
2284 V8_INLINE ExternalStringResourceBase* GetExternalStringResourceBase(
2285 Encoding* encoding_out) const;
2286
Steve Blocka7e24c12009-10-30 11:49:00 +00002287 /**
2288 * Get the ExternalStringResource for an external string. Returns
2289 * NULL if IsExternal() doesn't return true.
2290 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002291 V8_INLINE ExternalStringResource* GetExternalStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002292
2293 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002294 * Get the ExternalOneByteStringResource for an external one-byte string.
2295 * Returns NULL if IsExternalOneByte() doesn't return true.
Steve Blocka7e24c12009-10-30 11:49:00 +00002296 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002297 const ExternalOneByteStringResource* GetExternalOneByteStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002298
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002299 V8_INLINE static String* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002300
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002301 // TODO(dcarney): remove with deprecation of New functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002302 enum NewStringType {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002303 kNormalString = static_cast<int>(v8::NewStringType::kNormal),
2304 kInternalizedString = static_cast<int>(v8::NewStringType::kInternalized)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002305 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002306
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002307 /** Allocates a new string from UTF-8 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002308 static V8_DEPRECATE_SOON(
2309 "Use maybe version",
2310 Local<String> NewFromUtf8(Isolate* isolate, const char* data,
2311 NewStringType type = kNormalString,
2312 int length = -1));
2313
2314 /** Allocates a new string from UTF-8 data. Only returns an empty value when
2315 * length > kMaxLength. **/
2316 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8(
2317 Isolate* isolate, const char* data, v8::NewStringType type,
2318 int length = -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002319
2320 /** Allocates a new string from Latin-1 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002321 static V8_DEPRECATED(
2322 "Use maybe version",
2323 Local<String> NewFromOneByte(Isolate* isolate, const uint8_t* data,
2324 NewStringType type = kNormalString,
2325 int length = -1));
2326
2327 /** Allocates a new string from Latin-1 data. Only returns an empty value
2328 * when length > kMaxLength. **/
2329 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte(
2330 Isolate* isolate, const uint8_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002331 int length = -1);
2332
2333 /** Allocates a new string from UTF-16 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002334 static V8_DEPRECATE_SOON(
2335 "Use maybe version",
2336 Local<String> NewFromTwoByte(Isolate* isolate, const uint16_t* data,
2337 NewStringType type = kNormalString,
2338 int length = -1));
2339
2340 /** Allocates a new string from UTF-16 data. Only returns an empty value when
2341 * length > kMaxLength. **/
2342 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte(
2343 Isolate* isolate, const uint16_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002344 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002345
2346 /**
Steve Block3ce2e202009-11-05 08:53:23 +00002347 * Creates a new string by concatenating the left and the right strings
2348 * passed in as parameters.
2349 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002350 static Local<String> Concat(Local<String> left, Local<String> right);
Steve Block3ce2e202009-11-05 08:53:23 +00002351
2352 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002353 * Creates a new external string using the data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002354 * resource. When the external string is no longer live on V8's heap the
2355 * resource will be disposed by calling its Dispose method. The caller of
2356 * this function should not otherwise delete or modify the resource. Neither
2357 * should the underlying buffer be deallocated or modified except through the
2358 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002359 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002360 static V8_DEPRECATED("Use maybe version",
2361 Local<String> NewExternal(
2362 Isolate* isolate, ExternalStringResource* resource));
2363 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte(
2364 Isolate* isolate, ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002365
2366 /**
2367 * Associate an external string resource with this string by transforming it
2368 * in place so that existing references to this string in the JavaScript heap
2369 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002370 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002371 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002372 * The string is not modified if the operation fails. See NewExternal for
2373 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002374 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002375 bool MakeExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002376
2377 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002378 * Creates a new external string using the one-byte data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002379 * resource. When the external string is no longer live on V8's heap the
2380 * resource will be disposed by calling its Dispose method. The caller of
2381 * this function should not otherwise delete or modify the resource. Neither
2382 * should the underlying buffer be deallocated or modified except through the
2383 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002384 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002385 static V8_DEPRECATE_SOON(
2386 "Use maybe version",
2387 Local<String> NewExternal(Isolate* isolate,
2388 ExternalOneByteStringResource* resource));
2389 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte(
2390 Isolate* isolate, ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002391
2392 /**
2393 * Associate an external string resource with this string by transforming it
2394 * in place so that existing references to this string in the JavaScript heap
2395 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002396 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002397 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002398 * The string is not modified if the operation fails. See NewExternal for
2399 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002400 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002401 bool MakeExternal(ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002402
2403 /**
2404 * Returns true if this string can be made external.
2405 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002406 bool CanMakeExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00002407
2408 /**
Ben Murdoch589d6972011-11-30 16:04:58 +00002409 * Converts an object to a UTF-8-encoded character array. Useful if
Steve Blocka7e24c12009-10-30 11:49:00 +00002410 * you want to print the object. If conversion to a string fails
Ben Murdoch589d6972011-11-30 16:04:58 +00002411 * (e.g. due to an exception in the toString() method of the object)
Steve Blocka7e24c12009-10-30 11:49:00 +00002412 * then the length() method returns 0 and the * operator returns
2413 * NULL.
2414 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002415 class V8_EXPORT Utf8Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002416 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002417 explicit Utf8Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002418 ~Utf8Value();
2419 char* operator*() { return str_; }
2420 const char* operator*() const { return str_; }
2421 int length() const { return length_; }
2422 private:
2423 char* str_;
2424 int length_;
2425
2426 // Disallow copying and assigning.
2427 Utf8Value(const Utf8Value&);
2428 void operator=(const Utf8Value&);
2429 };
2430
2431 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002432 * Converts an object to a two-byte string.
2433 * If conversion to a string fails (eg. due to an exception in the toString()
2434 * method of the object) then the length() method returns 0 and the * operator
2435 * returns NULL.
2436 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002437 class V8_EXPORT Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002438 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002439 explicit Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002440 ~Value();
2441 uint16_t* operator*() { return str_; }
2442 const uint16_t* operator*() const { return str_; }
2443 int length() const { return length_; }
2444 private:
2445 uint16_t* str_;
2446 int length_;
2447
2448 // Disallow copying and assigning.
2449 Value(const Value&);
2450 void operator=(const Value&);
2451 };
Steve Block3ce2e202009-11-05 08:53:23 +00002452
Steve Blocka7e24c12009-10-30 11:49:00 +00002453 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002454 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
2455 Encoding encoding) const;
2456 void VerifyExternalStringResource(ExternalStringResource* val) const;
2457 static void CheckCast(v8::Value* obj);
2458};
2459
2460
2461/**
2462 * A JavaScript symbol (ECMA-262 edition 6)
2463 *
2464 * This is an experimental feature. Use at your own risk.
2465 */
2466class V8_EXPORT Symbol : public Name {
2467 public:
2468 // Returns the print name string of the symbol, or undefined if none.
2469 Local<Value> Name() const;
2470
2471 // Create a symbol. If name is not empty, it will be used as the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002472 static Local<Symbol> New(Isolate* isolate,
2473 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002474
2475 // Access global symbol registry.
2476 // Note that symbols created this way are never collected, so
2477 // they should only be used for statically fixed properties.
2478 // Also, there is only one global name space for the names used as keys.
2479 // To minimize the potential for clashes, use qualified names as keys.
2480 static Local<Symbol> For(Isolate *isolate, Local<String> name);
2481
2482 // Retrieve a global symbol. Similar to |For|, but using a separate
2483 // registry that is not accessible by (and cannot clash with) JavaScript code.
2484 static Local<Symbol> ForApi(Isolate *isolate, Local<String> name);
2485
2486 // Well-known symbols
2487 static Local<Symbol> GetIterator(Isolate* isolate);
2488 static Local<Symbol> GetUnscopables(Isolate* isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002489 static Local<Symbol> GetToStringTag(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002490 static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002491
2492 V8_INLINE static Symbol* Cast(v8::Value* obj);
2493
2494 private:
2495 Symbol();
2496 static void CheckCast(v8::Value* obj);
2497};
2498
2499
2500/**
2501 * A private symbol
2502 *
2503 * This is an experimental feature. Use at your own risk.
2504 */
2505class V8_EXPORT Private : public Data {
2506 public:
2507 // Returns the print name string of the private symbol, or undefined if none.
2508 Local<Value> Name() const;
2509
2510 // Create a private symbol. If name is not empty, it will be the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002511 static Local<Private> New(Isolate* isolate,
2512 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002513
2514 // Retrieve a global private symbol. If a symbol with this name has not
2515 // been retrieved in the same isolate before, it is created.
2516 // Note that private symbols created this way are never collected, so
2517 // they should only be used for statically fixed properties.
2518 // Also, there is only one global name space for the names used as keys.
2519 // To minimize the potential for clashes, use qualified names as keys,
2520 // e.g., "Class#property".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002521 static Local<Private> ForApi(Isolate* isolate, Local<String> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002522
2523 private:
2524 Private();
Steve Blocka7e24c12009-10-30 11:49:00 +00002525};
2526
2527
2528/**
2529 * A JavaScript number value (ECMA-262, 4.3.20)
2530 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002531class V8_EXPORT Number : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002532 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002533 double Value() const;
2534 static Local<Number> New(Isolate* isolate, double value);
2535 V8_INLINE static Number* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002536 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002537 Number();
2538 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002539};
2540
2541
2542/**
2543 * A JavaScript value representing a signed integer.
2544 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002545class V8_EXPORT Integer : public Number {
Steve Blocka7e24c12009-10-30 11:49:00 +00002546 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002547 static Local<Integer> New(Isolate* isolate, int32_t value);
2548 static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value);
2549 int64_t Value() const;
2550 V8_INLINE static Integer* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002551 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002552 Integer();
2553 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002554};
2555
2556
2557/**
2558 * A JavaScript value representing a 32-bit signed integer.
2559 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002560class V8_EXPORT Int32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002561 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002562 int32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002563 V8_INLINE static Int32* Cast(v8::Value* obj);
2564
Steve Blocka7e24c12009-10-30 11:49:00 +00002565 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002566 Int32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002567 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002568};
2569
2570
2571/**
2572 * A JavaScript value representing a 32-bit unsigned integer.
2573 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002574class V8_EXPORT Uint32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002575 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002576 uint32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002577 V8_INLINE static Uint32* Cast(v8::Value* obj);
2578
Steve Blocka7e24c12009-10-30 11:49:00 +00002579 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002580 Uint32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002581 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002582};
2583
2584
Steve Blocka7e24c12009-10-30 11:49:00 +00002585enum PropertyAttribute {
2586 None = 0,
2587 ReadOnly = 1 << 0,
2588 DontEnum = 1 << 1,
2589 DontDelete = 1 << 2
2590};
2591
2592/**
Leon Clarkef7060e22010-06-03 12:02:55 +01002593 * Accessor[Getter|Setter] are used as callback functions when
2594 * setting|getting a particular property. See Object and ObjectTemplate's
2595 * method SetAccessor.
2596 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002597typedef void (*AccessorGetterCallback)(
2598 Local<String> property,
2599 const PropertyCallbackInfo<Value>& info);
2600typedef void (*AccessorNameGetterCallback)(
2601 Local<Name> property,
2602 const PropertyCallbackInfo<Value>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002603
2604
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002605typedef void (*AccessorSetterCallback)(
2606 Local<String> property,
2607 Local<Value> value,
2608 const PropertyCallbackInfo<void>& info);
2609typedef void (*AccessorNameSetterCallback)(
2610 Local<Name> property,
2611 Local<Value> value,
2612 const PropertyCallbackInfo<void>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002613
2614
2615/**
2616 * Access control specifications.
2617 *
2618 * Some accessors should be accessible across contexts. These
2619 * accessors have an explicit access control parameter which specifies
2620 * the kind of cross-context access that should be allowed.
2621 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002622 * TODO(dcarney): Remove PROHIBITS_OVERWRITING as it is now unused.
Leon Clarkef7060e22010-06-03 12:02:55 +01002623 */
2624enum AccessControl {
2625 DEFAULT = 0,
2626 ALL_CAN_READ = 1,
2627 ALL_CAN_WRITE = 1 << 1,
2628 PROHIBITS_OVERWRITING = 1 << 2
2629};
2630
2631
2632/**
Steve Blocka7e24c12009-10-30 11:49:00 +00002633 * A JavaScript object (ECMA-262, 4.3.3)
2634 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002635class V8_EXPORT Object : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002636 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002637 V8_DEPRECATE_SOON("Use maybe version",
2638 bool Set(Local<Value> key, Local<Value> value));
2639 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context,
2640 Local<Value> key, Local<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002641
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002642 V8_DEPRECATE_SOON("Use maybe version",
2643 bool Set(uint32_t index, Local<Value> value));
2644 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index,
2645 Local<Value> value);
2646
2647 // Implements CreateDataProperty (ECMA-262, 7.3.4).
2648 //
2649 // Defines a configurable, writable, enumerable property with the given value
2650 // on the object unless the property already exists and is not configurable
2651 // or the object is not extensible.
2652 //
2653 // Returns true on success.
2654 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2655 Local<Name> key,
2656 Local<Value> value);
2657 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2658 uint32_t index,
2659 Local<Value> value);
2660
2661 // Implements DefineOwnProperty.
2662 //
2663 // In general, CreateDataProperty will be faster, however, does not allow
2664 // for specifying attributes.
2665 //
2666 // Returns true on success.
2667 V8_WARN_UNUSED_RESULT Maybe<bool> DefineOwnProperty(
2668 Local<Context> context, Local<Name> key, Local<Value> value,
2669 PropertyAttribute attributes = None);
Steve Block6ded16b2010-05-10 14:33:55 +01002670
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002671 // Sets an own property on this object bypassing interceptors and
Steve Blocka7e24c12009-10-30 11:49:00 +00002672 // overriding accessors or read-only properties.
2673 //
2674 // Note that if the object has an interceptor the property will be set
2675 // locally, but since the interceptor takes precedence the local property
2676 // will only be returned if the interceptor doesn't return a value.
2677 //
2678 // Note also that this only works for named properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002679 V8_DEPRECATED("Use CreateDataProperty / DefineOwnProperty",
2680 bool ForceSet(Local<Value> key, Local<Value> value,
2681 PropertyAttribute attribs = None));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002682 V8_DEPRECATE_SOON("Use CreateDataProperty / DefineOwnProperty",
2683 Maybe<bool> ForceSet(Local<Context> context,
2684 Local<Value> key, Local<Value> value,
2685 PropertyAttribute attribs = None));
Steve Blocka7e24c12009-10-30 11:49:00 +00002686
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002687 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key));
2688 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2689 Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002690
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002691 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(uint32_t index));
2692 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2693 uint32_t index);
Steve Block6ded16b2010-05-10 14:33:55 +01002694
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002695 /**
2696 * Gets the property attributes of a property which can be None or
2697 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2698 * None when the property doesn't exist.
2699 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002700 V8_DEPRECATED("Use maybe version",
2701 PropertyAttribute GetPropertyAttributes(Local<Value> key));
2702 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetPropertyAttributes(
2703 Local<Context> context, Local<Value> key);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002704
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002705 /**
2706 * Returns Object.getOwnPropertyDescriptor as per ES5 section 15.2.3.3.
2707 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002708 V8_DEPRECATED("Use maybe version",
2709 Local<Value> GetOwnPropertyDescriptor(Local<String> key));
2710 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetOwnPropertyDescriptor(
2711 Local<Context> context, Local<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002712
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002713 V8_DEPRECATE_SOON("Use maybe version", bool Has(Local<Value> key));
2714 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
2715 Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002716
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002717 V8_DEPRECATE_SOON("Use maybe version", bool Delete(Local<Value> key));
2718 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2719 Maybe<bool> Delete(Local<Context> context, Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002720
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002721 V8_DEPRECATED("Use maybe version", bool Has(uint32_t index));
2722 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002723
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002724 V8_DEPRECATED("Use maybe version", bool Delete(uint32_t index));
2725 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2726 Maybe<bool> Delete(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002727
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002728 V8_DEPRECATED("Use maybe version",
2729 bool SetAccessor(Local<String> name,
2730 AccessorGetterCallback getter,
2731 AccessorSetterCallback setter = 0,
2732 Local<Value> data = Local<Value>(),
2733 AccessControl settings = DEFAULT,
2734 PropertyAttribute attribute = None));
2735 V8_DEPRECATED("Use maybe version",
2736 bool SetAccessor(Local<Name> name,
2737 AccessorNameGetterCallback getter,
2738 AccessorNameSetterCallback setter = 0,
2739 Local<Value> data = Local<Value>(),
2740 AccessControl settings = DEFAULT,
2741 PropertyAttribute attribute = None));
2742 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2743 Maybe<bool> SetAccessor(Local<Context> context, Local<Name> name,
2744 AccessorNameGetterCallback getter,
2745 AccessorNameSetterCallback setter = 0,
2746 MaybeLocal<Value> data = MaybeLocal<Value>(),
2747 AccessControl settings = DEFAULT,
2748 PropertyAttribute attribute = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00002749
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002750 void SetAccessorProperty(Local<Name> name, Local<Function> getter,
2751 Local<Function> setter = Local<Function>(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002752 PropertyAttribute attribute = None,
2753 AccessControl settings = DEFAULT);
2754
2755 /**
2756 * Functionality for private properties.
2757 * This is an experimental feature, use at your own risk.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002758 * Note: Private properties are not inherited. Do not rely on this, since it
2759 * may change.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002760 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002761 Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
2762 Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
2763 Local<Value> value);
2764 Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
2765 MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);
Leon Clarkef7060e22010-06-03 12:02:55 +01002766
Steve Blocka7e24c12009-10-30 11:49:00 +00002767 /**
2768 * Returns an array containing the names of the enumerable properties
2769 * of this object, including properties from prototype objects. The
2770 * array returned by this method contains the same values as would
2771 * be enumerated by a for-in statement over this object.
2772 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002773 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetPropertyNames());
2774 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames(
2775 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00002776
2777 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002778 * This function has the same functionality as GetPropertyNames but
2779 * the returned array doesn't contain the names of properties from
2780 * prototype objects.
2781 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002782 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetOwnPropertyNames());
2783 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
2784 Local<Context> context);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002785
2786 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002787 * Get the prototype object. This does not skip objects marked to
2788 * be skipped by __proto__ and it does not consult the security
2789 * handler.
2790 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002791 Local<Value> GetPrototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00002792
2793 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00002794 * Set the prototype object. This does not skip objects marked to
2795 * be skipped by __proto__ and it does not consult the security
2796 * handler.
2797 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002798 V8_DEPRECATED("Use maybe version", bool SetPrototype(Local<Value> prototype));
2799 V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context,
2800 Local<Value> prototype);
Andrei Popescu402d9372010-02-26 13:31:12 +00002801
2802 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002803 * Finds an instance of the given function template in the prototype
2804 * chain.
2805 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002806 Local<Object> FindInstanceInPrototypeChain(Local<FunctionTemplate> tmpl);
Steve Blocka7e24c12009-10-30 11:49:00 +00002807
2808 /**
2809 * Call builtin Object.prototype.toString on this object.
2810 * This is different from Value::ToString() that may call
2811 * user-defined toString function. This one does not.
2812 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002813 V8_DEPRECATED("Use maybe version", Local<String> ObjectProtoToString());
2814 V8_WARN_UNUSED_RESULT MaybeLocal<String> ObjectProtoToString(
2815 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00002816
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002817 /**
2818 * Returns the name of the function invoked as a constructor for this object.
2819 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002820 Local<String> GetConstructorName();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002821
Steve Blocka7e24c12009-10-30 11:49:00 +00002822 /** Gets the number of internal fields for this Object. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002823 int InternalFieldCount();
2824
2825 /** Same as above, but works for Persistents */
2826 V8_INLINE static int InternalFieldCount(
2827 const PersistentBase<Object>& object) {
2828 return object.val_->InternalFieldCount();
2829 }
2830
2831 /** Gets the value from an internal field. */
2832 V8_INLINE Local<Value> GetInternalField(int index);
2833
Steve Blocka7e24c12009-10-30 11:49:00 +00002834 /** Sets the value in an internal field. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002835 void SetInternalField(int index, Local<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002836
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002837 /**
2838 * Gets a 2-byte-aligned native pointer from an internal field. This field
2839 * must have been set by SetAlignedPointerInInternalField, everything else
2840 * leads to undefined behavior.
2841 */
2842 V8_INLINE void* GetAlignedPointerFromInternalField(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00002843
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002844 /** Same as above, but works for Persistents */
2845 V8_INLINE static void* GetAlignedPointerFromInternalField(
2846 const PersistentBase<Object>& object, int index) {
2847 return object.val_->GetAlignedPointerFromInternalField(index);
2848 }
2849
2850 /**
2851 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2852 * a field, GetAlignedPointerFromInternalField must be used, everything else
2853 * leads to undefined behavior.
2854 */
2855 void SetAlignedPointerInInternalField(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002856
2857 // Testers for local properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002858 V8_DEPRECATED("Use maybe version", bool HasOwnProperty(Local<String> key));
2859 V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
2860 Local<Name> key);
2861 V8_DEPRECATE_SOON("Use maybe version",
2862 bool HasRealNamedProperty(Local<String> key));
2863 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedProperty(Local<Context> context,
2864 Local<Name> key);
2865 V8_DEPRECATE_SOON("Use maybe version",
2866 bool HasRealIndexedProperty(uint32_t index));
2867 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealIndexedProperty(
2868 Local<Context> context, uint32_t index);
2869 V8_DEPRECATE_SOON("Use maybe version",
2870 bool HasRealNamedCallbackProperty(Local<String> key));
2871 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedCallbackProperty(
2872 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002873
2874 /**
2875 * If result.IsEmpty() no real property was located in the prototype chain.
2876 * This means interceptors in the prototype chain are not called.
2877 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002878 V8_DEPRECATED(
2879 "Use maybe version",
2880 Local<Value> GetRealNamedPropertyInPrototypeChain(Local<String> key));
2881 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedPropertyInPrototypeChain(
2882 Local<Context> context, Local<Name> key);
2883
2884 /**
2885 * Gets the property attributes of a real property in the prototype chain,
2886 * which can be None or any combination of ReadOnly, DontEnum and DontDelete.
2887 * Interceptors in the prototype chain are not called.
2888 */
2889 V8_DEPRECATED(
2890 "Use maybe version",
2891 Maybe<PropertyAttribute> GetRealNamedPropertyAttributesInPrototypeChain(
2892 Local<String> key));
2893 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute>
2894 GetRealNamedPropertyAttributesInPrototypeChain(Local<Context> context,
2895 Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002896
2897 /**
2898 * If result.IsEmpty() no real property was located on the object or
2899 * in the prototype chain.
2900 * This means interceptors in the prototype chain are not called.
2901 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002902 V8_DEPRECATED("Use maybe version",
2903 Local<Value> GetRealNamedProperty(Local<String> key));
2904 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedProperty(
2905 Local<Context> context, Local<Name> key);
2906
2907 /**
2908 * Gets the property attributes of a real property which can be
2909 * None or any combination of ReadOnly, DontEnum and DontDelete.
2910 * Interceptors in the prototype chain are not called.
2911 */
2912 V8_DEPRECATED("Use maybe version",
2913 Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2914 Local<String> key));
2915 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2916 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002917
2918 /** Tests for a named lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002919 bool HasNamedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002920
2921 /** Tests for an index lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002922 bool HasIndexedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002923
2924 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002925 * Returns the identity hash for this object. The current implementation
2926 * uses a hidden property on the object to store the identity hash.
Steve Blocka7e24c12009-10-30 11:49:00 +00002927 *
2928 * The return value will never be 0. Also, it is not guaranteed to be
2929 * unique.
2930 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002931 int GetIdentityHash();
Steve Blocka7e24c12009-10-30 11:49:00 +00002932
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002933 V8_DEPRECATED("Use v8::Object::SetPrivate instead.",
2934 bool SetHiddenValue(Local<String> key, Local<Value> value));
2935 V8_DEPRECATED("Use v8::Object::GetPrivate instead.",
2936 Local<Value> GetHiddenValue(Local<String> key));
2937 V8_DEPRECATED("Use v8::Object::DeletePrivate instead.",
2938 bool DeleteHiddenValue(Local<String> key));
Steve Block3ce2e202009-11-05 08:53:23 +00002939
Steve Blocka7e24c12009-10-30 11:49:00 +00002940 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002941 * Clone this object with a fast but shallow copy. Values will point
2942 * to the same values as the original object.
2943 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002944 // TODO(dcarney): take an isolate and optionally bail out?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002945 Local<Object> Clone();
Steve Blocka7e24c12009-10-30 11:49:00 +00002946
2947 /**
Ben Murdoch8b112d22011-06-08 16:22:53 +01002948 * Returns the context in which the object was created.
2949 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002950 Local<Context> CreationContext();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002951
2952 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002953 * Checks whether a callback is set by the
2954 * ObjectTemplate::SetCallAsFunctionHandler method.
2955 * When an Object is callable this method returns true.
2956 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002957 bool IsCallable();
Ben Murdoch257744e2011-11-30 15:57:28 +00002958
2959 /**
Ben Murdoch589d6972011-11-30 16:04:58 +00002960 * Call an Object as a function if a callback is set by the
Ben Murdoch257744e2011-11-30 15:57:28 +00002961 * ObjectTemplate::SetCallAsFunctionHandler method.
2962 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002963 V8_DEPRECATED("Use maybe version",
2964 Local<Value> CallAsFunction(Local<Value> recv, int argc,
2965 Local<Value> argv[]));
2966 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsFunction(Local<Context> context,
2967 Local<Value> recv,
2968 int argc,
2969 Local<Value> argv[]);
Ben Murdoch257744e2011-11-30 15:57:28 +00002970
2971 /**
2972 * Call an Object as a constructor if a callback is set by the
2973 * ObjectTemplate::SetCallAsFunctionHandler method.
2974 * Note: This method behaves like the Function::NewInstance method.
2975 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002976 V8_DEPRECATED("Use maybe version",
2977 Local<Value> CallAsConstructor(int argc, Local<Value> argv[]));
2978 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsConstructor(
2979 Local<Context> context, int argc, Local<Value> argv[]);
Ben Murdoch257744e2011-11-30 15:57:28 +00002980
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002981 /**
2982 * Return the isolate to which the Object belongs to.
2983 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002984 V8_DEPRECATE_SOON("Keep track of isolate correctly", Isolate* GetIsolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002985
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002986 static Local<Object> New(Isolate* isolate);
2987
2988 V8_INLINE static Object* Cast(Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002989
Steve Blocka7e24c12009-10-30 11:49:00 +00002990 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002991 Object();
2992 static void CheckCast(Value* obj);
2993 Local<Value> SlowGetInternalField(int index);
2994 void* SlowGetAlignedPointerFromInternalField(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002995};
2996
2997
2998/**
2999 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
3000 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003001class V8_EXPORT Array : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00003002 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003003 uint32_t Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00003004
3005 /**
3006 * Clones an element at index |index|. Returns an empty
3007 * handle if cloning fails (for any reason).
3008 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003009 V8_DEPRECATED("Cloning is not supported.",
3010 Local<Object> CloneElementAt(uint32_t index));
3011 V8_DEPRECATED("Cloning is not supported.",
3012 MaybeLocal<Object> CloneElementAt(Local<Context> context,
3013 uint32_t index));
Steve Blocka7e24c12009-10-30 11:49:00 +00003014
Steve Block44f0eee2011-05-26 01:26:41 +01003015 /**
3016 * Creates a JavaScript array with the given length. If the length
3017 * is negative the returned array will have length 0.
3018 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003019 static Local<Array> New(Isolate* isolate, int length = 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003020
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003021 V8_INLINE static Array* Cast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003022 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003023 Array();
3024 static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003025};
3026
3027
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003028/**
3029 * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1).
3030 */
3031class V8_EXPORT Map : public Object {
3032 public:
3033 size_t Size() const;
3034 void Clear();
3035 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
3036 Local<Value> key);
3037 V8_WARN_UNUSED_RESULT MaybeLocal<Map> Set(Local<Context> context,
3038 Local<Value> key,
3039 Local<Value> value);
3040 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3041 Local<Value> key);
3042 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
3043 Local<Value> key);
3044
3045 /**
3046 * Returns an array of length Size() * 2, where index N is the Nth key and
3047 * index N + 1 is the Nth value.
3048 */
3049 Local<Array> AsArray() const;
3050
3051 /**
3052 * Creates a new empty Map.
3053 */
3054 static Local<Map> New(Isolate* isolate);
3055
3056 V8_INLINE static Map* Cast(Value* obj);
3057
3058 private:
3059 Map();
3060 static void CheckCast(Value* obj);
3061};
3062
3063
3064/**
3065 * An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1).
3066 */
3067class V8_EXPORT Set : public Object {
3068 public:
3069 size_t Size() const;
3070 void Clear();
3071 V8_WARN_UNUSED_RESULT MaybeLocal<Set> Add(Local<Context> context,
3072 Local<Value> key);
3073 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3074 Local<Value> key);
3075 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
3076 Local<Value> key);
3077
3078 /**
3079 * Returns an array of the keys in this Set.
3080 */
3081 Local<Array> AsArray() const;
3082
3083 /**
3084 * Creates a new empty Set.
3085 */
3086 static Local<Set> New(Isolate* isolate);
3087
3088 V8_INLINE static Set* Cast(Value* obj);
3089
3090 private:
3091 Set();
3092 static void CheckCast(Value* obj);
3093};
3094
3095
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003096template<typename T>
3097class ReturnValue {
3098 public:
3099 template <class S> V8_INLINE ReturnValue(const ReturnValue<S>& that)
3100 : value_(that.value_) {
3101 TYPE_CHECK(T, S);
3102 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003103 // Local setters
3104 template <typename S>
3105 V8_INLINE V8_DEPRECATE_SOON("Use Global<> instead",
3106 void Set(const Persistent<S>& handle));
3107 template <typename S>
3108 V8_INLINE void Set(const Global<S>& handle);
3109 template <typename S>
3110 V8_INLINE void Set(const Local<S> handle);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003111 // Fast primitive setters
3112 V8_INLINE void Set(bool value);
3113 V8_INLINE void Set(double i);
3114 V8_INLINE void Set(int32_t i);
3115 V8_INLINE void Set(uint32_t i);
3116 // Fast JS primitive setters
3117 V8_INLINE void SetNull();
3118 V8_INLINE void SetUndefined();
3119 V8_INLINE void SetEmptyString();
3120 // Convenience getter for Isolate
3121 V8_INLINE Isolate* GetIsolate();
3122
3123 // Pointer setter: Uncompilable to prevent inadvertent misuse.
3124 template <typename S>
3125 V8_INLINE void Set(S* whatever);
3126
3127 private:
3128 template<class F> friend class ReturnValue;
3129 template<class F> friend class FunctionCallbackInfo;
3130 template<class F> friend class PropertyCallbackInfo;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003131 template <class F, class G, class H>
3132 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003133 V8_INLINE void SetInternal(internal::Object* value) { *value_ = value; }
3134 V8_INLINE internal::Object* GetDefaultValue();
3135 V8_INLINE explicit ReturnValue(internal::Object** slot);
3136 internal::Object** value_;
3137};
3138
3139
3140/**
3141 * The argument information given to function call callbacks. This
3142 * class provides access to information about the context of the call,
3143 * including the receiver, the number and values of arguments, and
3144 * the holder of the function.
3145 */
3146template<typename T>
3147class FunctionCallbackInfo {
3148 public:
3149 V8_INLINE int Length() const;
3150 V8_INLINE Local<Value> operator[](int i) const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003151 V8_INLINE V8_DEPRECATED("Use Data() to explicitly pass Callee instead",
3152 Local<Function> Callee() const);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003153 V8_INLINE Local<Object> This() const;
3154 V8_INLINE Local<Object> Holder() const;
3155 V8_INLINE bool IsConstructCall() const;
3156 V8_INLINE Local<Value> Data() const;
3157 V8_INLINE Isolate* GetIsolate() const;
3158 V8_INLINE ReturnValue<T> GetReturnValue() const;
3159 // This shouldn't be public, but the arm compiler needs it.
3160 static const int kArgsLength = 7;
3161
3162 protected:
3163 friend class internal::FunctionCallbackArguments;
3164 friend class internal::CustomArguments<FunctionCallbackInfo>;
3165 static const int kHolderIndex = 0;
3166 static const int kIsolateIndex = 1;
3167 static const int kReturnValueDefaultValueIndex = 2;
3168 static const int kReturnValueIndex = 3;
3169 static const int kDataIndex = 4;
3170 static const int kCalleeIndex = 5;
3171 static const int kContextSaveIndex = 6;
3172
3173 V8_INLINE FunctionCallbackInfo(internal::Object** implicit_args,
3174 internal::Object** values,
3175 int length,
3176 bool is_construct_call);
3177 internal::Object** implicit_args_;
3178 internal::Object** values_;
3179 int length_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003180 int is_construct_call_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003181};
3182
3183
3184/**
3185 * The information passed to a property callback about the context
3186 * of the property access.
3187 */
3188template<typename T>
3189class PropertyCallbackInfo {
3190 public:
3191 V8_INLINE Isolate* GetIsolate() const;
3192 V8_INLINE Local<Value> Data() const;
3193 V8_INLINE Local<Object> This() const;
3194 V8_INLINE Local<Object> Holder() const;
3195 V8_INLINE ReturnValue<T> GetReturnValue() const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003196 V8_INLINE bool ShouldThrowOnError() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003197 // This shouldn't be public, but the arm compiler needs it.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003198 static const int kArgsLength = 7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003199
3200 protected:
3201 friend class MacroAssembler;
3202 friend class internal::PropertyCallbackArguments;
3203 friend class internal::CustomArguments<PropertyCallbackInfo>;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003204 static const int kShouldThrowOnErrorIndex = 0;
3205 static const int kHolderIndex = 1;
3206 static const int kIsolateIndex = 2;
3207 static const int kReturnValueDefaultValueIndex = 3;
3208 static const int kReturnValueIndex = 4;
3209 static const int kDataIndex = 5;
3210 static const int kThisIndex = 6;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003211
3212 V8_INLINE PropertyCallbackInfo(internal::Object** args) : args_(args) {}
3213 internal::Object** args_;
3214};
3215
3216
3217typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
3218
3219
Steve Blocka7e24c12009-10-30 11:49:00 +00003220/**
3221 * A JavaScript function object (ECMA-262, 15.3).
3222 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003223class V8_EXPORT Function : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00003224 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003225 /**
3226 * Create a function in the current execution context
3227 * for a given FunctionCallback.
3228 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003229 static MaybeLocal<Function> New(Local<Context> context,
3230 FunctionCallback callback,
3231 Local<Value> data = Local<Value>(),
3232 int length = 0);
3233 static V8_DEPRECATE_SOON(
3234 "Use maybe version",
3235 Local<Function> New(Isolate* isolate, FunctionCallback callback,
3236 Local<Value> data = Local<Value>(), int length = 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003237
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003238 V8_DEPRECATED("Use maybe version",
3239 Local<Object> NewInstance(int argc, Local<Value> argv[]) const);
3240 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
3241 Local<Context> context, int argc, Local<Value> argv[]) const;
3242
3243 V8_DEPRECATED("Use maybe version", Local<Object> NewInstance() const);
3244 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
3245 Local<Context> context) const {
3246 return NewInstance(context, 0, nullptr);
3247 }
3248
3249 V8_DEPRECATE_SOON("Use maybe version",
3250 Local<Value> Call(Local<Value> recv, int argc,
3251 Local<Value> argv[]));
3252 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context,
3253 Local<Value> recv, int argc,
3254 Local<Value> argv[]);
3255
3256 void SetName(Local<String> name);
3257 Local<Value> GetName() const;
Andrei Popescu402d9372010-02-26 13:31:12 +00003258
3259 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003260 * Name inferred from variable or property assignment of this function.
3261 * Used to facilitate debugging and profiling of JavaScript code written
3262 * in an OO style, where many functions are anonymous but are assigned
3263 * to object properties.
3264 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003265 Local<Value> GetInferredName() const;
3266
3267 /**
3268 * displayName if it is set, otherwise name if it is configured, otherwise
3269 * function name, otherwise inferred name.
3270 */
3271 Local<Value> GetDebugName() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003272
3273 /**
3274 * User-defined name assigned to the "displayName" property of this function.
3275 * Used to facilitate debugging and profiling of JavaScript code.
3276 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003277 Local<Value> GetDisplayName() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003278
3279 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00003280 * Returns zero based line number of function body and
3281 * kLineOffsetNotFound if no information available.
3282 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003283 int GetScriptLineNumber() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003284 /**
3285 * Returns zero based column number of function body and
3286 * kLineOffsetNotFound if no information available.
3287 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003288 int GetScriptColumnNumber() const;
3289
3290 /**
3291 * Tells whether this function is builtin.
3292 */
3293 bool IsBuiltin() const;
3294
3295 /**
3296 * Returns scriptId.
3297 */
3298 int ScriptId() const;
3299
3300 /**
3301 * Returns the original function if this function is bound, else returns
3302 * v8::Undefined.
3303 */
3304 Local<Value> GetBoundFunction() const;
3305
3306 ScriptOrigin GetScriptOrigin() const;
3307 V8_INLINE static Function* Cast(Value* obj);
3308 static const int kLineOffsetNotFound;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003309
Steve Blocka7e24c12009-10-30 11:49:00 +00003310 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003311 Function();
3312 static void CheckCast(Value* obj);
3313};
3314
3315
3316/**
3317 * An instance of the built-in Promise constructor (ES6 draft).
3318 * This API is experimental. Only works with --harmony flag.
3319 */
3320class V8_EXPORT Promise : public Object {
3321 public:
3322 class V8_EXPORT Resolver : public Object {
3323 public:
3324 /**
3325 * Create a new resolver, along with an associated promise in pending state.
3326 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003327 static V8_DEPRECATE_SOON("Use maybe version",
3328 Local<Resolver> New(Isolate* isolate));
3329 static V8_WARN_UNUSED_RESULT MaybeLocal<Resolver> New(
3330 Local<Context> context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003331
3332 /**
3333 * Extract the associated promise.
3334 */
3335 Local<Promise> GetPromise();
3336
3337 /**
3338 * Resolve/reject the associated promise with a given value.
3339 * Ignored if the promise is no longer pending.
3340 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003341 V8_DEPRECATE_SOON("Use maybe version", void Resolve(Local<Value> value));
3342 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3343 Maybe<bool> Resolve(Local<Context> context, Local<Value> value);
3344
3345 V8_DEPRECATE_SOON("Use maybe version", void Reject(Local<Value> value));
3346 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3347 Maybe<bool> Reject(Local<Context> context, Local<Value> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003348
3349 V8_INLINE static Resolver* Cast(Value* obj);
3350
3351 private:
3352 Resolver();
3353 static void CheckCast(Value* obj);
3354 };
3355
3356 /**
3357 * Register a resolution/rejection handler with a promise.
3358 * The handler is given the respective resolution/rejection value as
3359 * an argument. If the promise is already resolved/rejected, the handler is
3360 * invoked at the end of turn.
3361 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003362 V8_DEPRECATED("Use maybe version of Then",
3363 Local<Promise> Chain(Local<Function> handler));
3364 V8_DEPRECATED("Use Then",
3365 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Chain(
3366 Local<Context> context, Local<Function> handler));
3367
3368 V8_DEPRECATED("Use maybe version",
3369 Local<Promise> Catch(Local<Function> handler));
3370 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Catch(Local<Context> context,
3371 Local<Function> handler);
3372
3373 V8_DEPRECATED("Use maybe version",
3374 Local<Promise> Then(Local<Function> handler));
3375 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Then(Local<Context> context,
3376 Local<Function> handler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003377
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003378 /**
3379 * Returns true if the promise has at least one derived promise, and
3380 * therefore resolve/reject handlers (including default handler).
3381 */
3382 bool HasHandler();
3383
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003384 V8_INLINE static Promise* Cast(Value* obj);
3385
3386 private:
3387 Promise();
3388 static void CheckCast(Value* obj);
3389};
3390
3391
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003392/**
3393 * An instance of the built-in Proxy constructor (ECMA-262, 6th Edition,
3394 * 26.2.1).
3395 */
3396class V8_EXPORT Proxy : public Object {
3397 public:
3398 Local<Object> GetTarget();
3399 Local<Value> GetHandler();
3400 bool IsRevoked();
3401 void Revoke();
3402
3403 /**
3404 * Creates a new empty Map.
3405 */
3406 static MaybeLocal<Proxy> New(Local<Context> context,
3407 Local<Object> local_target,
3408 Local<Object> local_handler);
3409
3410 V8_INLINE static Proxy* Cast(Value* obj);
3411
3412 private:
3413 Proxy();
3414 static void CheckCast(Value* obj);
3415};
3416
3417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003418#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
3419// The number of required internal fields can be defined by embedder.
3420#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
3421#endif
3422
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003423
3424enum class ArrayBufferCreationMode { kInternalized, kExternalized };
3425
3426
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003427/**
3428 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
3429 * This API is experimental and may change significantly.
3430 */
3431class V8_EXPORT ArrayBuffer : public Object {
3432 public:
3433 /**
3434 * Allocator that V8 uses to allocate |ArrayBuffer|'s memory.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003435 * The allocator is a global V8 setting. It has to be set via
3436 * Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003437 *
3438 * This API is experimental and may change significantly.
3439 */
3440 class V8_EXPORT Allocator { // NOLINT
3441 public:
3442 virtual ~Allocator() {}
3443
3444 /**
3445 * Allocate |length| bytes. Return NULL if allocation is not successful.
3446 * Memory should be initialized to zeroes.
3447 */
3448 virtual void* Allocate(size_t length) = 0;
3449
3450 /**
3451 * Allocate |length| bytes. Return NULL if allocation is not successful.
3452 * Memory does not have to be initialized.
3453 */
3454 virtual void* AllocateUninitialized(size_t length) = 0;
3455 /**
3456 * Free the memory block of size |length|, pointed to by |data|.
3457 * That memory is guaranteed to be previously allocated by |Allocate|.
3458 */
3459 virtual void Free(void* data, size_t length) = 0;
3460 };
3461
3462 /**
3463 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
3464 * returns an instance of this class, populated, with a pointer to data
3465 * and byte length.
3466 *
3467 * The Data pointer of ArrayBuffer::Contents is always allocated with
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003468 * Allocator::Allocate that is set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003469 *
3470 * This API is experimental and may change significantly.
3471 */
3472 class V8_EXPORT Contents { // NOLINT
3473 public:
3474 Contents() : data_(NULL), byte_length_(0) {}
3475
3476 void* Data() const { return data_; }
3477 size_t ByteLength() const { return byte_length_; }
3478
3479 private:
3480 void* data_;
3481 size_t byte_length_;
3482
3483 friend class ArrayBuffer;
3484 };
3485
3486
3487 /**
3488 * Data length in bytes.
3489 */
3490 size_t ByteLength() const;
3491
3492 /**
3493 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
3494 * Allocated memory will be owned by a created ArrayBuffer and
3495 * will be deallocated when it is garbage-collected,
3496 * unless the object is externalized.
3497 */
3498 static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
3499
3500 /**
3501 * Create a new ArrayBuffer over an existing memory block.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003502 * The created array buffer is by default immediately in externalized state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003503 * The memory block will not be reclaimed when a created ArrayBuffer
3504 * is garbage-collected.
3505 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003506 static Local<ArrayBuffer> New(
3507 Isolate* isolate, void* data, size_t byte_length,
3508 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003509
3510 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003511 * Returns true if ArrayBuffer is externalized, that is, does not
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003512 * own its memory block.
3513 */
3514 bool IsExternal() const;
3515
3516 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003517 * Returns true if this ArrayBuffer may be neutered.
3518 */
3519 bool IsNeuterable() const;
3520
3521 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003522 * Neuters this ArrayBuffer and all its views (typed arrays).
3523 * Neutering sets the byte length of the buffer and all typed arrays to zero,
3524 * preventing JavaScript from ever accessing underlying backing store.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003525 * ArrayBuffer should have been externalized and must be neuterable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003526 */
3527 void Neuter();
3528
3529 /**
3530 * Make this ArrayBuffer external. The pointer to underlying memory block
3531 * and byte length are returned as |Contents| structure. After ArrayBuffer
3532 * had been etxrenalized, it does no longer owns the memory block. The caller
3533 * should take steps to free memory when it is no longer needed.
3534 *
3535 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003536 * that has been set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003537 */
3538 Contents Externalize();
3539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003540 /**
3541 * Get a pointer to the ArrayBuffer's underlying memory block without
3542 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3543 * will become invalid as soon as the ArrayBuffer became garbage collected.
3544 *
3545 * The embedder should make sure to hold a strong reference to the
3546 * ArrayBuffer while accessing this pointer.
3547 *
3548 * The memory block is guaranteed to be allocated with |Allocator::Allocate|.
3549 */
3550 Contents GetContents();
3551
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003552 V8_INLINE static ArrayBuffer* Cast(Value* obj);
3553
3554 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3555
3556 private:
3557 ArrayBuffer();
3558 static void CheckCast(Value* obj);
3559};
3560
3561
3562#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
3563// The number of required internal fields can be defined by embedder.
3564#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
3565#endif
3566
3567
3568/**
3569 * A base class for an instance of one of "views" over ArrayBuffer,
3570 * including TypedArrays and DataView (ES6 draft 15.13).
3571 *
3572 * This API is experimental and may change significantly.
3573 */
3574class V8_EXPORT ArrayBufferView : public Object {
3575 public:
3576 /**
3577 * Returns underlying ArrayBuffer.
3578 */
3579 Local<ArrayBuffer> Buffer();
3580 /**
3581 * Byte offset in |Buffer|.
3582 */
3583 size_t ByteOffset();
3584 /**
3585 * Size of a view in bytes.
3586 */
3587 size_t ByteLength();
3588
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003589 /**
3590 * Copy the contents of the ArrayBufferView's buffer to an embedder defined
3591 * memory without additional overhead that calling ArrayBufferView::Buffer
3592 * might incur.
3593 *
3594 * Will write at most min(|byte_length|, ByteLength) bytes starting at
3595 * ByteOffset of the underling buffer to the memory starting at |dest|.
3596 * Returns the number of bytes actually written.
3597 */
3598 size_t CopyContents(void* dest, size_t byte_length);
3599
3600 /**
3601 * Returns true if ArrayBufferView's backing ArrayBuffer has already been
3602 * allocated.
3603 */
3604 bool HasBuffer() const;
3605
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003606 V8_INLINE static ArrayBufferView* Cast(Value* obj);
3607
3608 static const int kInternalFieldCount =
3609 V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
3610
3611 private:
3612 ArrayBufferView();
3613 static void CheckCast(Value* obj);
3614};
3615
3616
3617/**
3618 * A base class for an instance of TypedArray series of constructors
3619 * (ES6 draft 15.13.6).
3620 * This API is experimental and may change significantly.
3621 */
3622class V8_EXPORT TypedArray : public ArrayBufferView {
3623 public:
3624 /**
3625 * Number of elements in this typed array
3626 * (e.g. for Int16Array, |ByteLength|/2).
3627 */
3628 size_t Length();
3629
3630 V8_INLINE static TypedArray* Cast(Value* obj);
3631
3632 private:
3633 TypedArray();
3634 static void CheckCast(Value* obj);
3635};
3636
3637
3638/**
3639 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
3640 * This API is experimental and may change significantly.
3641 */
3642class V8_EXPORT Uint8Array : public TypedArray {
3643 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003644 static Local<Uint8Array> New(Local<ArrayBuffer> array_buffer,
3645 size_t byte_offset, size_t length);
3646 static Local<Uint8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003647 size_t byte_offset, size_t length);
3648 V8_INLINE static Uint8Array* Cast(Value* obj);
3649
3650 private:
3651 Uint8Array();
3652 static void CheckCast(Value* obj);
3653};
3654
3655
3656/**
3657 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
3658 * This API is experimental and may change significantly.
3659 */
3660class V8_EXPORT Uint8ClampedArray : public TypedArray {
3661 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003662 static Local<Uint8ClampedArray> New(Local<ArrayBuffer> array_buffer,
3663 size_t byte_offset, size_t length);
3664 static Local<Uint8ClampedArray> New(
3665 Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset,
3666 size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003667 V8_INLINE static Uint8ClampedArray* Cast(Value* obj);
3668
3669 private:
3670 Uint8ClampedArray();
3671 static void CheckCast(Value* obj);
3672};
3673
3674/**
3675 * An instance of Int8Array constructor (ES6 draft 15.13.6).
3676 * This API is experimental and may change significantly.
3677 */
3678class V8_EXPORT Int8Array : public TypedArray {
3679 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003680 static Local<Int8Array> New(Local<ArrayBuffer> array_buffer,
3681 size_t byte_offset, size_t length);
3682 static Local<Int8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3683 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003684 V8_INLINE static Int8Array* Cast(Value* obj);
3685
3686 private:
3687 Int8Array();
3688 static void CheckCast(Value* obj);
3689};
3690
3691
3692/**
3693 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
3694 * This API is experimental and may change significantly.
3695 */
3696class V8_EXPORT Uint16Array : public TypedArray {
3697 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003698 static Local<Uint16Array> New(Local<ArrayBuffer> array_buffer,
3699 size_t byte_offset, size_t length);
3700 static Local<Uint16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3701 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003702 V8_INLINE static Uint16Array* Cast(Value* obj);
3703
3704 private:
3705 Uint16Array();
3706 static void CheckCast(Value* obj);
3707};
3708
3709
3710/**
3711 * An instance of Int16Array constructor (ES6 draft 15.13.6).
3712 * This API is experimental and may change significantly.
3713 */
3714class V8_EXPORT Int16Array : public TypedArray {
3715 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003716 static Local<Int16Array> New(Local<ArrayBuffer> array_buffer,
3717 size_t byte_offset, size_t length);
3718 static Local<Int16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003719 size_t byte_offset, size_t length);
3720 V8_INLINE static Int16Array* Cast(Value* obj);
3721
3722 private:
3723 Int16Array();
3724 static void CheckCast(Value* obj);
3725};
3726
3727
3728/**
3729 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
3730 * This API is experimental and may change significantly.
3731 */
3732class V8_EXPORT Uint32Array : public TypedArray {
3733 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003734 static Local<Uint32Array> New(Local<ArrayBuffer> array_buffer,
3735 size_t byte_offset, size_t length);
3736 static Local<Uint32Array> 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 Uint32Array* Cast(Value* obj);
3739
3740 private:
3741 Uint32Array();
3742 static void CheckCast(Value* obj);
3743};
3744
3745
3746/**
3747 * An instance of Int32Array constructor (ES6 draft 15.13.6).
3748 * This API is experimental and may change significantly.
3749 */
3750class V8_EXPORT Int32Array : public TypedArray {
3751 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003752 static Local<Int32Array> New(Local<ArrayBuffer> array_buffer,
3753 size_t byte_offset, size_t length);
3754 static Local<Int32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003755 size_t byte_offset, size_t length);
3756 V8_INLINE static Int32Array* Cast(Value* obj);
3757
3758 private:
3759 Int32Array();
3760 static void CheckCast(Value* obj);
3761};
3762
3763
3764/**
3765 * An instance of Float32Array constructor (ES6 draft 15.13.6).
3766 * This API is experimental and may change significantly.
3767 */
3768class V8_EXPORT Float32Array : public TypedArray {
3769 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003770 static Local<Float32Array> New(Local<ArrayBuffer> array_buffer,
3771 size_t byte_offset, size_t length);
3772 static Local<Float32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3773 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003774 V8_INLINE static Float32Array* Cast(Value* obj);
3775
3776 private:
3777 Float32Array();
3778 static void CheckCast(Value* obj);
3779};
3780
3781
3782/**
3783 * An instance of Float64Array constructor (ES6 draft 15.13.6).
3784 * This API is experimental and may change significantly.
3785 */
3786class V8_EXPORT Float64Array : public TypedArray {
3787 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003788 static Local<Float64Array> New(Local<ArrayBuffer> array_buffer,
3789 size_t byte_offset, size_t length);
3790 static Local<Float64Array> 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 Float64Array* Cast(Value* obj);
3793
3794 private:
3795 Float64Array();
3796 static void CheckCast(Value* obj);
3797};
3798
3799
3800/**
3801 * An instance of DataView constructor (ES6 draft 15.13.7).
3802 * This API is experimental and may change significantly.
3803 */
3804class V8_EXPORT DataView : public ArrayBufferView {
3805 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003806 static Local<DataView> New(Local<ArrayBuffer> array_buffer,
3807 size_t byte_offset, size_t length);
3808 static Local<DataView> 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 DataView* Cast(Value* obj);
3811
3812 private:
3813 DataView();
3814 static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003815};
3816
3817
3818/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003819 * An instance of the built-in SharedArrayBuffer constructor.
3820 * This API is experimental and may change significantly.
3821 */
3822class V8_EXPORT SharedArrayBuffer : public Object {
3823 public:
3824 /**
3825 * The contents of an |SharedArrayBuffer|. Externalization of
3826 * |SharedArrayBuffer| returns an instance of this class, populated, with a
3827 * pointer to data and byte length.
3828 *
3829 * The Data pointer of SharedArrayBuffer::Contents is always allocated with
3830 * |ArrayBuffer::Allocator::Allocate| by the allocator specified in
3831 * v8::Isolate::CreateParams::array_buffer_allocator.
3832 *
3833 * This API is experimental and may change significantly.
3834 */
3835 class V8_EXPORT Contents { // NOLINT
3836 public:
3837 Contents() : data_(NULL), byte_length_(0) {}
3838
3839 void* Data() const { return data_; }
3840 size_t ByteLength() const { return byte_length_; }
3841
3842 private:
3843 void* data_;
3844 size_t byte_length_;
3845
3846 friend class SharedArrayBuffer;
3847 };
3848
3849
3850 /**
3851 * Data length in bytes.
3852 */
3853 size_t ByteLength() const;
3854
3855 /**
3856 * Create a new SharedArrayBuffer. Allocate |byte_length| bytes.
3857 * Allocated memory will be owned by a created SharedArrayBuffer and
3858 * will be deallocated when it is garbage-collected,
3859 * unless the object is externalized.
3860 */
3861 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
3862
3863 /**
3864 * Create a new SharedArrayBuffer over an existing memory block. The created
3865 * array buffer is immediately in externalized state unless otherwise
3866 * specified. The memory block will not be reclaimed when a created
3867 * SharedArrayBuffer is garbage-collected.
3868 */
3869 static Local<SharedArrayBuffer> New(
3870 Isolate* isolate, void* data, size_t byte_length,
3871 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
3872
3873 /**
3874 * Returns true if SharedArrayBuffer is externalized, that is, does not
3875 * own its memory block.
3876 */
3877 bool IsExternal() const;
3878
3879 /**
3880 * Make this SharedArrayBuffer external. The pointer to underlying memory
3881 * block and byte length are returned as |Contents| structure. After
3882 * SharedArrayBuffer had been etxrenalized, it does no longer owns the memory
3883 * block. The caller should take steps to free memory when it is no longer
3884 * needed.
3885 *
3886 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3887 * by the allocator specified in
3888 * v8::Isolate::CreateParams::array_buffer_allocator.
3889 *
3890 */
3891 Contents Externalize();
3892
3893 /**
3894 * Get a pointer to the ArrayBuffer's underlying memory block without
3895 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3896 * will become invalid as soon as the ArrayBuffer became garbage collected.
3897 *
3898 * The embedder should make sure to hold a strong reference to the
3899 * ArrayBuffer while accessing this pointer.
3900 *
3901 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3902 * by the allocator specified in
3903 * v8::Isolate::CreateParams::array_buffer_allocator.
3904 */
3905 Contents GetContents();
3906
3907 V8_INLINE static SharedArrayBuffer* Cast(Value* obj);
3908
3909 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3910
3911 private:
3912 SharedArrayBuffer();
3913 static void CheckCast(Value* obj);
3914};
3915
3916
3917/**
Ben Murdoch257744e2011-11-30 15:57:28 +00003918 * An instance of the built-in Date constructor (ECMA-262, 15.9).
3919 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003920class V8_EXPORT Date : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00003921 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003922 static V8_DEPRECATE_SOON("Use maybe version.",
3923 Local<Value> New(Isolate* isolate, double time));
3924 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> New(Local<Context> context,
3925 double time);
Ben Murdoch257744e2011-11-30 15:57:28 +00003926
3927 /**
3928 * A specialization of Value::NumberValue that is more efficient
3929 * because we know the structure of this object.
3930 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003931 double ValueOf() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00003932
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003933 V8_INLINE static Date* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00003934
3935 /**
3936 * Notification that the embedder has changed the time zone,
3937 * daylight savings time, or other date / time configuration
3938 * parameters. V8 keeps a cache of various values used for
3939 * date / time computation. This notification will reset
3940 * those cached values for the current context so that date /
3941 * time configuration changes would be reflected in the Date
3942 * object.
3943 *
3944 * This API should not be called more than needed as it will
3945 * negatively impact the performance of date operations.
3946 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003947 static void DateTimeConfigurationChangeNotification(Isolate* isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00003948
3949 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003950 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00003951};
3952
3953
3954/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003955 * A Number object (ECMA-262, 4.3.21).
3956 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003957class V8_EXPORT NumberObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003958 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003959 static Local<Value> New(Isolate* isolate, double value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003960
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003961 double ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003962
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003963 V8_INLINE static NumberObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003964
3965 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003966 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003967};
3968
3969
3970/**
3971 * A Boolean object (ECMA-262, 4.3.15).
3972 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003973class V8_EXPORT BooleanObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003974 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003975 static Local<Value> New(Isolate* isolate, bool value);
3976 V8_DEPRECATED("Pass an isolate", static Local<Value> New(bool value));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003977
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003978 bool ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003979
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003980 V8_INLINE static BooleanObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003981
3982 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003983 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003984};
3985
3986
3987/**
3988 * A String object (ECMA-262, 4.3.18).
3989 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003990class V8_EXPORT StringObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003991 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003992 static Local<Value> New(Local<String> value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003993
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003994 Local<String> ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003995
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003996 V8_INLINE static StringObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003997
3998 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003999 static void CheckCast(v8::Value* obj);
4000};
4001
4002
4003/**
4004 * A Symbol object (ECMA-262 edition 6).
4005 *
4006 * This is an experimental feature. Use at your own risk.
4007 */
4008class V8_EXPORT SymbolObject : public Object {
4009 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004010 static Local<Value> New(Isolate* isolate, Local<Symbol> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004011
4012 Local<Symbol> ValueOf() const;
4013
4014 V8_INLINE static SymbolObject* Cast(v8::Value* obj);
4015
4016 private:
4017 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004018};
4019
4020
4021/**
Ben Murdoch257744e2011-11-30 15:57:28 +00004022 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
4023 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004024class V8_EXPORT RegExp : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00004025 public:
4026 /**
4027 * Regular expression flag bits. They can be or'ed to enable a set
4028 * of flags.
4029 */
4030 enum Flags {
4031 kNone = 0,
4032 kGlobal = 1,
4033 kIgnoreCase = 2,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004034 kMultiline = 4,
4035 kSticky = 8,
4036 kUnicode = 16
Ben Murdoch257744e2011-11-30 15:57:28 +00004037 };
4038
4039 /**
4040 * Creates a regular expression from the given pattern string and
4041 * the flags bit field. May throw a JavaScript exception as
4042 * described in ECMA-262, 15.10.4.1.
4043 *
4044 * For example,
4045 * RegExp::New(v8::String::New("foo"),
4046 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
4047 * is equivalent to evaluating "/foo/gm".
4048 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004049 static V8_DEPRECATE_SOON("Use maybe version",
4050 Local<RegExp> New(Local<String> pattern,
4051 Flags flags));
4052 static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context,
4053 Local<String> pattern,
4054 Flags flags);
Ben Murdoch257744e2011-11-30 15:57:28 +00004055
4056 /**
4057 * Returns the value of the source property: a string representing
4058 * the regular expression.
4059 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004060 Local<String> GetSource() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004061
4062 /**
4063 * Returns the flags bit field.
4064 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004065 Flags GetFlags() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004066
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004067 V8_INLINE static RegExp* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004068
4069 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004070 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004071};
4072
4073
4074/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004075 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
4076 * to associate C++ data structures with JavaScript objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00004077 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004078class V8_EXPORT External : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00004079 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004080 static Local<External> New(Isolate* isolate, void* value);
4081 V8_INLINE static External* Cast(Value* obj);
4082 void* Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00004083 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004084 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004085};
4086
4087
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004088#define V8_INTRINSICS_LIST(F) F(ArrayProto_values, array_values_iterator)
4089
4090enum Intrinsic {
4091#define V8_DECL_INTRINSIC(name, iname) k##name,
4092 V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
4093#undef V8_DECL_INTRINSIC
4094};
4095
4096
Ben Murdoch257744e2011-11-30 15:57:28 +00004097// --- Templates ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004098
4099
4100/**
4101 * The superclass of object and function templates.
4102 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004103class V8_EXPORT Template : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004104 public:
4105 /** Adds a property to each instance created by this template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004106 void Set(Local<Name> name, Local<Data> value,
Steve Blocka7e24c12009-10-30 11:49:00 +00004107 PropertyAttribute attributes = None);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004108 V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004109
4110 void SetAccessorProperty(
4111 Local<Name> name,
4112 Local<FunctionTemplate> getter = Local<FunctionTemplate>(),
4113 Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
4114 PropertyAttribute attribute = None,
4115 AccessControl settings = DEFAULT);
4116
4117 /**
4118 * Whenever the property with the given name is accessed on objects
4119 * created from this Template the getter and setter callbacks
4120 * are called instead of getting and setting the property directly
4121 * on the JavaScript object.
4122 *
4123 * \param name The name of the property for which an accessor is added.
4124 * \param getter The callback to invoke when getting the property.
4125 * \param setter The callback to invoke when setting the property.
4126 * \param data A piece of data that will be passed to the getter and setter
4127 * callbacks whenever they are invoked.
4128 * \param settings Access control settings for the accessor. This is a bit
4129 * field consisting of one of more of
4130 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4131 * The default is to not allow cross-context access.
4132 * ALL_CAN_READ means that all cross-context reads are allowed.
4133 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4134 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4135 * cross-context access.
4136 * \param attribute The attributes of the property for which an accessor
4137 * is added.
4138 * \param signature The signature describes valid receivers for the accessor
4139 * and is used to perform implicit instance checks against them. If the
4140 * receiver is incompatible (i.e. is not an instance of the constructor as
4141 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4142 * thrown and no callback is invoked.
4143 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004144 void SetNativeDataProperty(
4145 Local<String> name, AccessorGetterCallback getter,
4146 AccessorSetterCallback setter = 0,
4147 // TODO(dcarney): gcc can't handle Local below
4148 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4149 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4150 AccessControl settings = DEFAULT);
4151 void SetNativeDataProperty(
4152 Local<Name> name, AccessorNameGetterCallback getter,
4153 AccessorNameSetterCallback setter = 0,
4154 // TODO(dcarney): gcc can't handle Local below
4155 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4156 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4157 AccessControl settings = DEFAULT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004158
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004159 /**
4160 * During template instantiation, sets the value with the intrinsic property
4161 * from the correct context.
4162 */
4163 void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
4164 PropertyAttribute attribute = None);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004165
Steve Blocka7e24c12009-10-30 11:49:00 +00004166 private:
4167 Template();
4168
4169 friend class ObjectTemplate;
4170 friend class FunctionTemplate;
4171};
4172
4173
4174/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004175 * NamedProperty[Getter|Setter] are used as interceptors on object.
4176 * See ObjectTemplate::SetNamedPropertyHandler.
4177 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004178typedef void (*NamedPropertyGetterCallback)(
4179 Local<String> property,
4180 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004181
4182
4183/**
4184 * Returns the value if the setter intercepts the request.
4185 * Otherwise, returns an empty handle.
4186 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004187typedef void (*NamedPropertySetterCallback)(
4188 Local<String> property,
4189 Local<Value> value,
4190 const PropertyCallbackInfo<Value>& info);
4191
Steve Blocka7e24c12009-10-30 11:49:00 +00004192
Steve Blocka7e24c12009-10-30 11:49:00 +00004193/**
4194 * Returns a non-empty handle if the interceptor intercepts the request.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004195 * The result is an integer encoding property attributes (like v8::None,
4196 * v8::DontEnum, etc.)
Steve Blocka7e24c12009-10-30 11:49:00 +00004197 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004198typedef void (*NamedPropertyQueryCallback)(
4199 Local<String> property,
4200 const PropertyCallbackInfo<Integer>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004201
4202
4203/**
4204 * Returns a non-empty handle if the deleter intercepts the request.
4205 * The return value is true if the property could be deleted and false
4206 * otherwise.
4207 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004208typedef void (*NamedPropertyDeleterCallback)(
4209 Local<String> property,
4210 const PropertyCallbackInfo<Boolean>& info);
4211
Steve Blocka7e24c12009-10-30 11:49:00 +00004212
4213/**
4214 * Returns an array containing the names of the properties the named
4215 * property getter intercepts.
4216 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004217typedef void (*NamedPropertyEnumeratorCallback)(
4218 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004219
4220
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004221// TODO(dcarney): Deprecate and remove previous typedefs, and replace
4222// GenericNamedPropertyFooCallback with just NamedPropertyFooCallback.
4223/**
4224 * GenericNamedProperty[Getter|Setter] are used as interceptors on object.
4225 * See ObjectTemplate::SetNamedPropertyHandler.
4226 */
4227typedef void (*GenericNamedPropertyGetterCallback)(
4228 Local<Name> property, const PropertyCallbackInfo<Value>& info);
4229
4230
4231/**
4232 * Returns the value if the setter intercepts the request.
4233 * Otherwise, returns an empty handle.
4234 */
4235typedef void (*GenericNamedPropertySetterCallback)(
4236 Local<Name> property, Local<Value> value,
4237 const PropertyCallbackInfo<Value>& info);
4238
4239
4240/**
4241 * Returns a non-empty handle if the interceptor intercepts the request.
4242 * The result is an integer encoding property attributes (like v8::None,
4243 * v8::DontEnum, etc.)
4244 */
4245typedef void (*GenericNamedPropertyQueryCallback)(
4246 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
4247
4248
4249/**
4250 * Returns a non-empty handle if the deleter intercepts the request.
4251 * The return value is true if the property could be deleted and false
4252 * otherwise.
4253 */
4254typedef void (*GenericNamedPropertyDeleterCallback)(
4255 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
4256
4257
4258/**
4259 * Returns an array containing the names of the properties the named
4260 * property getter intercepts.
4261 */
4262typedef void (*GenericNamedPropertyEnumeratorCallback)(
4263 const PropertyCallbackInfo<Array>& info);
4264
4265
Steve Blocka7e24c12009-10-30 11:49:00 +00004266/**
4267 * Returns the value of the property if the getter intercepts the
4268 * request. Otherwise, returns an empty handle.
4269 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004270typedef void (*IndexedPropertyGetterCallback)(
4271 uint32_t index,
4272 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004273
4274
4275/**
4276 * Returns the value if the setter intercepts the request.
4277 * Otherwise, returns an empty handle.
4278 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004279typedef void (*IndexedPropertySetterCallback)(
4280 uint32_t index,
4281 Local<Value> value,
4282 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004283
4284
4285/**
4286 * Returns a non-empty handle if the interceptor intercepts the request.
Iain Merrick75681382010-08-19 15:07:18 +01004287 * The result is an integer encoding property attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004288 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004289typedef void (*IndexedPropertyQueryCallback)(
4290 uint32_t index,
4291 const PropertyCallbackInfo<Integer>& info);
4292
Steve Blocka7e24c12009-10-30 11:49:00 +00004293
4294/**
4295 * Returns a non-empty handle if the deleter intercepts the request.
4296 * The return value is true if the property could be deleted and false
4297 * otherwise.
4298 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004299typedef void (*IndexedPropertyDeleterCallback)(
4300 uint32_t index,
4301 const PropertyCallbackInfo<Boolean>& info);
4302
Steve Blocka7e24c12009-10-30 11:49:00 +00004303
4304/**
4305 * Returns an array containing the indices of the properties the
4306 * indexed property getter intercepts.
4307 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004308typedef void (*IndexedPropertyEnumeratorCallback)(
4309 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004310
4311
4312/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004313 * Access type specification.
4314 */
4315enum AccessType {
4316 ACCESS_GET,
4317 ACCESS_SET,
4318 ACCESS_HAS,
4319 ACCESS_DELETE,
4320 ACCESS_KEYS
4321};
4322
4323
4324/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004325 * Returns true if the given context should be allowed to access the given
4326 * object.
4327 */
4328typedef bool (*AccessCheckCallback)(Local<Context> accessing_context,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004329 Local<Object> accessed_object,
4330 Local<Value> data);
4331typedef bool (*DeprecatedAccessCheckCallback)(Local<Context> accessing_context,
4332 Local<Object> accessed_object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004333
4334/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004335 * Returns true if cross-context access should be allowed to the named
4336 * property with the given key on the host object.
4337 */
4338typedef bool (*NamedSecurityCallback)(Local<Object> host,
4339 Local<Value> key,
4340 AccessType type,
4341 Local<Value> data);
4342
4343
4344/**
4345 * Returns true if cross-context access should be allowed to the indexed
4346 * property with the given index on the host object.
4347 */
4348typedef bool (*IndexedSecurityCallback)(Local<Object> host,
4349 uint32_t index,
4350 AccessType type,
4351 Local<Value> data);
4352
4353
4354/**
4355 * A FunctionTemplate is used to create functions at runtime. There
4356 * can only be one function created from a FunctionTemplate in a
4357 * context. The lifetime of the created function is equal to the
4358 * lifetime of the context. So in case the embedder needs to create
4359 * temporary functions that can be collected using Scripts is
4360 * preferred.
4361 *
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004362 * Any modification of a FunctionTemplate after first instantiation will trigger
4363 *a crash.
4364 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004365 * A FunctionTemplate can have properties, these properties are added to the
4366 * function object when it is created.
4367 *
4368 * A FunctionTemplate has a corresponding instance template which is
4369 * used to create object instances when the function is used as a
4370 * constructor. Properties added to the instance template are added to
4371 * each object instance.
4372 *
4373 * A FunctionTemplate can have a prototype template. The prototype template
4374 * is used to create the prototype object of the function.
4375 *
4376 * The following example shows how to use a FunctionTemplate:
4377 *
4378 * \code
4379 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
4380 * t->Set("func_property", v8::Number::New(1));
4381 *
4382 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
4383 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
4384 * proto_t->Set("proto_const", v8::Number::New(2));
4385 *
4386 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
4387 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
4388 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
4389 * instance_t->Set("instance_property", Number::New(3));
4390 *
4391 * v8::Local<v8::Function> function = t->GetFunction();
4392 * v8::Local<v8::Object> instance = function->NewInstance();
4393 * \endcode
4394 *
4395 * Let's use "function" as the JS variable name of the function object
4396 * and "instance" for the instance object created above. The function
4397 * and the instance will have the following properties:
4398 *
4399 * \code
4400 * func_property in function == true;
4401 * function.func_property == 1;
4402 *
4403 * function.prototype.proto_method() invokes 'InvokeCallback'
4404 * function.prototype.proto_const == 2;
4405 *
4406 * instance instanceof function == true;
4407 * instance.instance_accessor calls 'InstanceAccessorCallback'
4408 * instance.instance_property == 3;
4409 * \endcode
4410 *
4411 * A FunctionTemplate can inherit from another one by calling the
4412 * FunctionTemplate::Inherit method. The following graph illustrates
4413 * the semantics of inheritance:
4414 *
4415 * \code
4416 * FunctionTemplate Parent -> Parent() . prototype -> { }
4417 * ^ ^
4418 * | Inherit(Parent) | .__proto__
4419 * | |
4420 * FunctionTemplate Child -> Child() . prototype -> { }
4421 * \endcode
4422 *
4423 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
4424 * object of the Child() function has __proto__ pointing to the
4425 * Parent() function's prototype object. An instance of the Child
4426 * function has all properties on Parent's instance templates.
4427 *
4428 * Let Parent be the FunctionTemplate initialized in the previous
4429 * section and create a Child FunctionTemplate by:
4430 *
4431 * \code
4432 * Local<FunctionTemplate> parent = t;
4433 * Local<FunctionTemplate> child = FunctionTemplate::New();
4434 * child->Inherit(parent);
4435 *
4436 * Local<Function> child_function = child->GetFunction();
4437 * Local<Object> child_instance = child_function->NewInstance();
4438 * \endcode
4439 *
4440 * The Child function and Child instance will have the following
4441 * properties:
4442 *
4443 * \code
4444 * child_func.prototype.__proto__ == function.prototype;
4445 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
4446 * child_instance.instance_property == 3;
4447 * \endcode
4448 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004449class V8_EXPORT FunctionTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004450 public:
4451 /** Creates a function template.*/
4452 static Local<FunctionTemplate> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004453 Isolate* isolate, FunctionCallback callback = 0,
4454 Local<Value> data = Local<Value>(),
4455 Local<Signature> signature = Local<Signature>(), int length = 0);
4456
4457 /**
4458 * Creates a function template with a fast handler. If a fast handler is set,
4459 * the callback cannot be null.
4460 */
4461 static Local<FunctionTemplate> NewWithFastHandler(
4462 Isolate* isolate, FunctionCallback callback,
4463 experimental::FastAccessorBuilder* fast_handler = nullptr,
4464 Local<Value> data = Local<Value>(),
4465 Local<Signature> signature = Local<Signature>(), int length = 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004466
Steve Blocka7e24c12009-10-30 11:49:00 +00004467 /** Returns the unique function instance in the current execution context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004468 V8_DEPRECATE_SOON("Use maybe version", Local<Function> GetFunction());
4469 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
4470 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004471
4472 /**
4473 * Set the call-handler callback for a FunctionTemplate. This
4474 * callback is called whenever the function created from this
4475 * FunctionTemplate is called.
4476 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004477 void SetCallHandler(
4478 FunctionCallback callback, Local<Value> data = Local<Value>(),
4479 experimental::FastAccessorBuilder* fast_handler = nullptr);
Steve Blocka7e24c12009-10-30 11:49:00 +00004480
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004481 /** Set the predefined length property for the FunctionTemplate. */
4482 void SetLength(int length);
4483
Steve Blocka7e24c12009-10-30 11:49:00 +00004484 /** Get the InstanceTemplate. */
4485 Local<ObjectTemplate> InstanceTemplate();
4486
4487 /** Causes the function template to inherit from a parent function template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004488 void Inherit(Local<FunctionTemplate> parent);
Steve Blocka7e24c12009-10-30 11:49:00 +00004489
4490 /**
4491 * A PrototypeTemplate is the template used to create the prototype object
4492 * of the function created by this template.
4493 */
4494 Local<ObjectTemplate> PrototypeTemplate();
4495
Steve Blocka7e24c12009-10-30 11:49:00 +00004496 /**
4497 * Set the class name of the FunctionTemplate. This is used for
4498 * printing objects created with the function created from the
4499 * FunctionTemplate as its constructor.
4500 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004501 void SetClassName(Local<String> name);
4502
4503
4504 /**
4505 * When set to true, no access check will be performed on the receiver of a
4506 * function call. Currently defaults to true, but this is subject to change.
4507 */
4508 void SetAcceptAnyReceiver(bool value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004509
4510 /**
4511 * Determines whether the __proto__ accessor ignores instances of
4512 * the function template. If instances of the function template are
4513 * ignored, __proto__ skips all instances and instead returns the
4514 * next object in the prototype chain.
4515 *
4516 * Call with a value of true to make the __proto__ accessor ignore
4517 * instances of the function template. Call with a value of false
4518 * to make the __proto__ accessor not ignore instances of the
4519 * function template. By default, instances of a function template
4520 * are not ignored.
4521 */
4522 void SetHiddenPrototype(bool value);
4523
4524 /**
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004525 * Sets the ReadOnly flag in the attributes of the 'prototype' property
4526 * of functions created from this FunctionTemplate to true.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004527 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004528 void ReadOnlyPrototype();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004529
4530 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004531 * Removes the prototype property from functions created from this
4532 * FunctionTemplate.
4533 */
4534 void RemovePrototype();
4535
4536 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00004537 * Returns true if the given object is an instance of this function
4538 * template.
4539 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004540 bool HasInstance(Local<Value> object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004541
4542 private:
4543 FunctionTemplate();
Steve Blocka7e24c12009-10-30 11:49:00 +00004544 friend class Context;
4545 friend class ObjectTemplate;
4546};
4547
4548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004549enum class PropertyHandlerFlags {
4550 kNone = 0,
4551 // See ALL_CAN_READ above.
4552 kAllCanRead = 1,
4553 // Will not call into interceptor for properties on the receiver or prototype
4554 // chain. Currently only valid for named interceptors.
4555 kNonMasking = 1 << 1,
4556 // Will not call into interceptor for symbol lookup. Only meaningful for
4557 // named interceptors.
4558 kOnlyInterceptStrings = 1 << 2,
4559};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004560
4561
4562struct NamedPropertyHandlerConfiguration {
4563 NamedPropertyHandlerConfiguration(
4564 /** Note: getter is required **/
4565 GenericNamedPropertyGetterCallback getter = 0,
4566 GenericNamedPropertySetterCallback setter = 0,
4567 GenericNamedPropertyQueryCallback query = 0,
4568 GenericNamedPropertyDeleterCallback deleter = 0,
4569 GenericNamedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004570 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004571 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4572 : getter(getter),
4573 setter(setter),
4574 query(query),
4575 deleter(deleter),
4576 enumerator(enumerator),
4577 data(data),
4578 flags(flags) {}
4579
4580 GenericNamedPropertyGetterCallback getter;
4581 GenericNamedPropertySetterCallback setter;
4582 GenericNamedPropertyQueryCallback query;
4583 GenericNamedPropertyDeleterCallback deleter;
4584 GenericNamedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004585 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004586 PropertyHandlerFlags flags;
4587};
4588
4589
4590struct IndexedPropertyHandlerConfiguration {
4591 IndexedPropertyHandlerConfiguration(
4592 /** Note: getter is required **/
4593 IndexedPropertyGetterCallback getter = 0,
4594 IndexedPropertySetterCallback setter = 0,
4595 IndexedPropertyQueryCallback query = 0,
4596 IndexedPropertyDeleterCallback deleter = 0,
4597 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004598 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004599 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4600 : getter(getter),
4601 setter(setter),
4602 query(query),
4603 deleter(deleter),
4604 enumerator(enumerator),
4605 data(data),
4606 flags(flags) {}
4607
4608 IndexedPropertyGetterCallback getter;
4609 IndexedPropertySetterCallback setter;
4610 IndexedPropertyQueryCallback query;
4611 IndexedPropertyDeleterCallback deleter;
4612 IndexedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004613 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004614 PropertyHandlerFlags flags;
4615};
4616
4617
Steve Blocka7e24c12009-10-30 11:49:00 +00004618/**
4619 * An ObjectTemplate is used to create objects at runtime.
4620 *
4621 * Properties added to an ObjectTemplate are added to each object
4622 * created from the ObjectTemplate.
4623 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004624class V8_EXPORT ObjectTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004625 public:
4626 /** Creates an ObjectTemplate. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004627 static Local<ObjectTemplate> New(
4628 Isolate* isolate,
4629 Local<FunctionTemplate> constructor = Local<FunctionTemplate>());
4630 static V8_DEPRECATED("Use isolate version", Local<ObjectTemplate> New());
Steve Blocka7e24c12009-10-30 11:49:00 +00004631
4632 /** Creates a new instance of this template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004633 V8_DEPRECATE_SOON("Use maybe version", Local<Object> NewInstance());
4634 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004635
4636 /**
4637 * Sets an accessor on the object template.
4638 *
4639 * Whenever the property with the given name is accessed on objects
4640 * created from this ObjectTemplate the getter and setter callbacks
4641 * are called instead of getting and setting the property directly
4642 * on the JavaScript object.
4643 *
4644 * \param name The name of the property for which an accessor is added.
4645 * \param getter The callback to invoke when getting the property.
4646 * \param setter The callback to invoke when setting the property.
4647 * \param data A piece of data that will be passed to the getter and setter
4648 * callbacks whenever they are invoked.
4649 * \param settings Access control settings for the accessor. This is a bit
4650 * field consisting of one of more of
4651 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4652 * The default is to not allow cross-context access.
4653 * ALL_CAN_READ means that all cross-context reads are allowed.
4654 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4655 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4656 * cross-context access.
4657 * \param attribute The attributes of the property for which an accessor
4658 * is added.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004659 * \param signature The signature describes valid receivers for the accessor
4660 * and is used to perform implicit instance checks against them. If the
4661 * receiver is incompatible (i.e. is not an instance of the constructor as
4662 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4663 * thrown and no callback is invoked.
Steve Blocka7e24c12009-10-30 11:49:00 +00004664 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004665 void SetAccessor(
4666 Local<String> name, AccessorGetterCallback getter,
4667 AccessorSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4668 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4669 Local<AccessorSignature> signature = Local<AccessorSignature>());
4670 void SetAccessor(
4671 Local<Name> name, AccessorNameGetterCallback getter,
4672 AccessorNameSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4673 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4674 Local<AccessorSignature> signature = Local<AccessorSignature>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004675
4676 /**
4677 * Sets a named property handler on the object template.
4678 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004679 * Whenever a property whose name is a string is accessed on objects created
4680 * from this object template, the provided callback is invoked instead of
Steve Blocka7e24c12009-10-30 11:49:00 +00004681 * accessing the property directly on the JavaScript object.
4682 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004683 * Note that new code should use the second version that can intercept
4684 * symbol-named properties as well as string-named properties.
4685 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004686 * \param getter The callback to invoke when getting a property.
4687 * \param setter The callback to invoke when setting a property.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004688 * \param query The callback to invoke to check if a property is present,
4689 * and if present, get its attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004690 * \param deleter The callback to invoke when deleting a property.
4691 * \param enumerator The callback to invoke to enumerate all the named
4692 * properties of an object.
4693 * \param data A piece of data that will be passed to the callbacks
4694 * whenever they are invoked.
4695 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004696 // TODO(dcarney): deprecate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004697 void SetNamedPropertyHandler(NamedPropertyGetterCallback getter,
4698 NamedPropertySetterCallback setter = 0,
4699 NamedPropertyQueryCallback query = 0,
4700 NamedPropertyDeleterCallback deleter = 0,
4701 NamedPropertyEnumeratorCallback enumerator = 0,
4702 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004703 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
Steve Blocka7e24c12009-10-30 11:49:00 +00004704
4705 /**
4706 * Sets an indexed property handler on the object template.
4707 *
4708 * Whenever an indexed property is accessed on objects created from
4709 * this object template, the provided callback is invoked instead of
4710 * accessing the property directly on the JavaScript object.
4711 *
4712 * \param getter The callback to invoke when getting a property.
4713 * \param setter The callback to invoke when setting a property.
Ben Murdoch257744e2011-11-30 15:57:28 +00004714 * \param query The callback to invoke to check if an object has a property.
Steve Blocka7e24c12009-10-30 11:49:00 +00004715 * \param deleter The callback to invoke when deleting a property.
4716 * \param enumerator The callback to invoke to enumerate all the indexed
4717 * properties of an object.
4718 * \param data A piece of data that will be passed to the callbacks
4719 * whenever they are invoked.
4720 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004721 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
4722 // TODO(dcarney): deprecate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004723 void SetIndexedPropertyHandler(
4724 IndexedPropertyGetterCallback getter,
4725 IndexedPropertySetterCallback setter = 0,
4726 IndexedPropertyQueryCallback query = 0,
4727 IndexedPropertyDeleterCallback deleter = 0,
4728 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004729 Local<Value> data = Local<Value>()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004730 SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query,
4731 deleter, enumerator, data));
4732 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004733 /**
4734 * Sets the callback to be used when calling instances created from
4735 * this template as a function. If no callback is set, instances
4736 * behave like normal JavaScript objects that cannot be called as a
4737 * function.
4738 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004739 void SetCallAsFunctionHandler(FunctionCallback callback,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004740 Local<Value> data = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004741
4742 /**
4743 * Mark object instances of the template as undetectable.
4744 *
4745 * In many ways, undetectable objects behave as though they are not
4746 * there. They behave like 'undefined' in conditionals and when
4747 * printed. However, properties can be accessed and called as on
4748 * normal objects.
4749 */
4750 void MarkAsUndetectable();
4751
4752 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004753 * Sets access check callback on the object template and enables access
4754 * checks.
Steve Blocka7e24c12009-10-30 11:49:00 +00004755 *
4756 * When accessing properties on instances of this object template,
4757 * the access check callback will be called to determine whether or
4758 * not to allow cross-context access to the properties.
Steve Blocka7e24c12009-10-30 11:49:00 +00004759 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004760 void SetAccessCheckCallback(AccessCheckCallback callback,
4761 Local<Value> data = Local<Value>());
Ben Murdoch097c5b22016-05-18 11:27:45 +01004762 V8_DEPRECATED(
4763 "Use SetAccessCheckCallback with new AccessCheckCallback signature.",
4764 void SetAccessCheckCallback(DeprecatedAccessCheckCallback callback,
4765 Local<Value> data = Local<Value>()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004766
4767 V8_DEPRECATED(
4768 "Use SetAccessCheckCallback instead",
4769 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
4770 IndexedSecurityCallback indexed_handler,
4771 Local<Value> data = Local<Value>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00004772
4773 /**
4774 * Gets the number of internal fields for objects generated from
4775 * this template.
4776 */
4777 int InternalFieldCount();
4778
4779 /**
4780 * Sets the number of internal fields for objects generated from
4781 * this template.
4782 */
4783 void SetInternalFieldCount(int value);
4784
4785 private:
4786 ObjectTemplate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004787 static Local<ObjectTemplate> New(internal::Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004788 Local<FunctionTemplate> constructor);
Steve Blocka7e24c12009-10-30 11:49:00 +00004789 friend class FunctionTemplate;
4790};
4791
4792
4793/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004794 * A Signature specifies which receiver is valid for a function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004795 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004796class V8_EXPORT Signature : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004797 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004798 static Local<Signature> New(
4799 Isolate* isolate,
4800 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004801
Steve Blocka7e24c12009-10-30 11:49:00 +00004802 private:
4803 Signature();
4804};
4805
4806
4807/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004808 * An AccessorSignature specifies which receivers are valid parameters
4809 * to an accessor callback.
4810 */
4811class V8_EXPORT AccessorSignature : public Data {
4812 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004813 static Local<AccessorSignature> New(
4814 Isolate* isolate,
4815 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004816
4817 private:
4818 AccessorSignature();
4819};
4820
4821
Ben Murdoch257744e2011-11-30 15:57:28 +00004822// --- Extensions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004823
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004824class V8_EXPORT ExternalOneByteStringResourceImpl
4825 : public String::ExternalOneByteStringResource {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004826 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004827 ExternalOneByteStringResourceImpl() : data_(0), length_(0) {}
4828 ExternalOneByteStringResourceImpl(const char* data, size_t length)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004829 : data_(data), length_(length) {}
4830 const char* data() const { return data_; }
4831 size_t length() const { return length_; }
4832
4833 private:
4834 const char* data_;
4835 size_t length_;
4836};
Steve Blocka7e24c12009-10-30 11:49:00 +00004837
4838/**
4839 * Ignore
4840 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004841class V8_EXPORT Extension { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00004842 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004843 // Note that the strings passed into this constructor must live as long
4844 // as the Extension itself.
Steve Blocka7e24c12009-10-30 11:49:00 +00004845 Extension(const char* name,
4846 const char* source = 0,
4847 int dep_count = 0,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004848 const char** deps = 0,
4849 int source_length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00004850 virtual ~Extension() { }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004851 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
4852 v8::Isolate* isolate, v8::Local<v8::String> name) {
4853 return v8::Local<v8::FunctionTemplate>();
Steve Blocka7e24c12009-10-30 11:49:00 +00004854 }
4855
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004856 const char* name() const { return name_; }
4857 size_t source_length() const { return source_length_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004858 const String::ExternalOneByteStringResource* source() const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004859 return &source_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00004860 int dependency_count() { return dep_count_; }
4861 const char** dependencies() { return deps_; }
4862 void set_auto_enable(bool value) { auto_enable_ = value; }
4863 bool auto_enable() { return auto_enable_; }
4864
4865 private:
4866 const char* name_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004867 size_t source_length_; // expected to initialize before source_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004868 ExternalOneByteStringResourceImpl source_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004869 int dep_count_;
4870 const char** deps_;
4871 bool auto_enable_;
4872
4873 // Disallow copying and assigning.
4874 Extension(const Extension&);
4875 void operator=(const Extension&);
4876};
4877
4878
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004879void V8_EXPORT RegisterExtension(Extension* extension);
Steve Blocka7e24c12009-10-30 11:49:00 +00004880
4881
Ben Murdoch257744e2011-11-30 15:57:28 +00004882// --- Statics ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004883
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004884V8_INLINE Local<Primitive> Undefined(Isolate* isolate);
4885V8_INLINE Local<Primitive> Null(Isolate* isolate);
4886V8_INLINE Local<Boolean> True(Isolate* isolate);
4887V8_INLINE Local<Boolean> False(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004888
4889
4890/**
4891 * A set of constraints that specifies the limits of the runtime's memory use.
4892 * You must set the heap size before initializing the VM - the size cannot be
4893 * adjusted after the VM is initialized.
4894 *
4895 * If you are using threads then you should hold the V8::Locker lock while
4896 * setting the stack limit and you must set a non-default stack limit separately
4897 * for each thread.
4898 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004899class V8_EXPORT ResourceConstraints {
Steve Blocka7e24c12009-10-30 11:49:00 +00004900 public:
4901 ResourceConstraints();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004902
4903 /**
4904 * Configures the constraints with reasonable default values based on the
4905 * capabilities of the current device the VM is running on.
4906 *
4907 * \param physical_memory The total amount of physical memory on the current
4908 * device, in bytes.
4909 * \param virtual_memory_limit The amount of virtual memory on the current
4910 * device, in bytes, or zero, if there is no limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004911 */
4912 void ConfigureDefaults(uint64_t physical_memory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004913 uint64_t virtual_memory_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004914
4915 int max_semi_space_size() const { return max_semi_space_size_; }
4916 void set_max_semi_space_size(int value) { max_semi_space_size_ = value; }
Steve Blocka7e24c12009-10-30 11:49:00 +00004917 int max_old_space_size() const { return max_old_space_size_; }
4918 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004919 int max_executable_size() const { return max_executable_size_; }
Russell Brenner90bac252010-11-18 13:33:46 -08004920 void set_max_executable_size(int value) { max_executable_size_ = value; }
Steve Blocka7e24c12009-10-30 11:49:00 +00004921 uint32_t* stack_limit() const { return stack_limit_; }
4922 // Sets an address beyond which the VM's stack may not grow.
4923 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004924 size_t code_range_size() const { return code_range_size_; }
4925 void set_code_range_size(size_t value) {
4926 code_range_size_ = value;
4927 }
4928
Steve Blocka7e24c12009-10-30 11:49:00 +00004929 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004930 int max_semi_space_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004931 int max_old_space_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08004932 int max_executable_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004933 uint32_t* stack_limit_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004934 size_t code_range_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004935};
4936
4937
Ben Murdoch257744e2011-11-30 15:57:28 +00004938// --- Exceptions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004939
4940
4941typedef void (*FatalErrorCallback)(const char* location, const char* message);
4942
4943
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004944typedef void (*MessageCallback)(Local<Message> message, Local<Value> error);
Steve Blocka7e24c12009-10-30 11:49:00 +00004945
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004946// --- Tracing ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004947
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004948typedef void (*LogEventCallback)(const char* name, int event);
Steve Blocka7e24c12009-10-30 11:49:00 +00004949
4950/**
4951 * Create new error objects by calling the corresponding error object
4952 * constructor with the message.
4953 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004954class V8_EXPORT Exception {
Steve Blocka7e24c12009-10-30 11:49:00 +00004955 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004956 static Local<Value> RangeError(Local<String> message);
4957 static Local<Value> ReferenceError(Local<String> message);
4958 static Local<Value> SyntaxError(Local<String> message);
4959 static Local<Value> TypeError(Local<String> message);
4960 static Local<Value> Error(Local<String> message);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004961
4962 /**
4963 * Creates an error message for the given exception.
4964 * Will try to reconstruct the original stack trace from the exception value,
4965 * or capture the current stack trace if not available.
4966 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004967 static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
4968 V8_DEPRECATED("Use version with an Isolate*",
4969 static Local<Message> CreateMessage(Local<Value> exception));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004970
4971 /**
4972 * Returns the original stack trace that was captured at the creation time
4973 * of a given exception, or an empty handle if not available.
4974 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004975 static Local<StackTrace> GetStackTrace(Local<Value> exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00004976};
4977
4978
Ben Murdoch257744e2011-11-30 15:57:28 +00004979// --- Counters Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004980
4981typedef int* (*CounterLookupCallback)(const char* name);
4982
4983typedef void* (*CreateHistogramCallback)(const char* name,
4984 int min,
4985 int max,
4986 size_t buckets);
4987
4988typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
4989
Ben Murdoch257744e2011-11-30 15:57:28 +00004990// --- Memory Allocation Callback ---
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004991enum ObjectSpace {
4992 kObjectSpaceNewSpace = 1 << 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004993 kObjectSpaceOldSpace = 1 << 1,
4994 kObjectSpaceCodeSpace = 1 << 2,
4995 kObjectSpaceMapSpace = 1 << 3,
4996 kObjectSpaceLoSpace = 1 << 4,
4997 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldSpace |
4998 kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
4999 kObjectSpaceLoSpace
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005000};
Iain Merrick9ac36c92010-09-13 15:29:50 +01005001
5002 enum AllocationAction {
5003 kAllocationActionAllocate = 1 << 0,
5004 kAllocationActionFree = 1 << 1,
5005 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
5006 };
5007
5008typedef void (*MemoryAllocationCallback)(ObjectSpace space,
5009 AllocationAction action,
5010 int size);
5011
Ben Murdoch097c5b22016-05-18 11:27:45 +01005012// --- Enter/Leave Script Callback ---
5013typedef void (*BeforeCallEnteredCallback)(Isolate*);
5014typedef void (*CallCompletedCallback)(Isolate*);
5015typedef void (*DeprecatedCallCompletedCallback)();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005016
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005017// --- Promise Reject Callback ---
5018enum PromiseRejectEvent {
5019 kPromiseRejectWithNoHandler = 0,
5020 kPromiseHandlerAddedAfterReject = 1
5021};
5022
5023class PromiseRejectMessage {
5024 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005025 PromiseRejectMessage(Local<Promise> promise, PromiseRejectEvent event,
5026 Local<Value> value, Local<StackTrace> stack_trace)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005027 : promise_(promise),
5028 event_(event),
5029 value_(value),
5030 stack_trace_(stack_trace) {}
5031
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005032 V8_INLINE Local<Promise> GetPromise() const { return promise_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005033 V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005034 V8_INLINE Local<Value> GetValue() const { return value_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005035
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005036 V8_DEPRECATED("Use v8::Exception::CreateMessage(GetValue())->GetStackTrace()",
5037 V8_INLINE Local<StackTrace> GetStackTrace() const) {
5038 return stack_trace_;
5039 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005040
5041 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005042 Local<Promise> promise_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005043 PromiseRejectEvent event_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005044 Local<Value> value_;
5045 Local<StackTrace> stack_trace_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005046};
5047
5048typedef void (*PromiseRejectCallback)(PromiseRejectMessage message);
5049
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005050// --- Microtask Callback ---
5051typedef void (*MicrotaskCallback)(void* data);
5052
Ben Murdoch257744e2011-11-30 15:57:28 +00005053// --- Failed Access Check Callback ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005054typedef void (*FailedAccessCheckCallback)(Local<Object> target,
5055 AccessType type,
5056 Local<Value> data);
5057
Ben Murdoch257744e2011-11-30 15:57:28 +00005058// --- AllowCodeGenerationFromStrings callbacks ---
5059
5060/**
5061 * Callback to check if code generation from strings is allowed. See
5062 * Context::AllowCodeGenerationFromStrings.
5063 */
5064typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
5065
5066// --- Garbage Collection Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005067
5068/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005069 * Applications can register callback functions which will be called before and
5070 * after certain garbage collection operations. Allocations are not allowed in
5071 * the callback functions, you therefore cannot manipulate objects (set or
5072 * delete properties for example) since it is possible such operations will
5073 * result in the allocation of objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00005074 */
Steve Block6ded16b2010-05-10 14:33:55 +01005075enum GCType {
5076 kGCTypeScavenge = 1 << 0,
5077 kGCTypeMarkSweepCompact = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005078 kGCTypeIncrementalMarking = 1 << 2,
5079 kGCTypeProcessWeakCallbacks = 1 << 3,
5080 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact |
5081 kGCTypeIncrementalMarking | kGCTypeProcessWeakCallbacks
Steve Block6ded16b2010-05-10 14:33:55 +01005082};
5083
Ben Murdoch097c5b22016-05-18 11:27:45 +01005084/**
5085 * GCCallbackFlags is used to notify additional information about the GC
5086 * callback.
5087 * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for
5088 * constructing retained object infos.
5089 * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing.
5090 * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback
5091 * is called synchronously without getting posted to an idle task.
5092 * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called
5093 * in a phase where V8 is trying to collect all available garbage
5094 * (e.g., handling a low memory notification).
5095 */
Steve Block6ded16b2010-05-10 14:33:55 +01005096enum GCCallbackFlags {
5097 kNoGCCallbackFlags = 0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005098 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005099 kGCCallbackFlagForced = 1 << 2,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005100 kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3,
5101 kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4,
Steve Block6ded16b2010-05-10 14:33:55 +01005102};
5103
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005104typedef void (*GCCallback)(GCType type, GCCallbackFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01005105
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005106typedef void (*InterruptCallback)(Isolate* isolate, void* data);
Steve Blocka7e24c12009-10-30 11:49:00 +00005107
5108
Steve Blocka7e24c12009-10-30 11:49:00 +00005109/**
Steve Block3ce2e202009-11-05 08:53:23 +00005110 * Collection of V8 heap information.
5111 *
5112 * Instances of this class can be passed to v8::V8::HeapStatistics to
5113 * get heap statistics from V8.
5114 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005115class V8_EXPORT HeapStatistics {
Steve Block3ce2e202009-11-05 08:53:23 +00005116 public:
5117 HeapStatistics();
5118 size_t total_heap_size() { return total_heap_size_; }
Russell Brenner90bac252010-11-18 13:33:46 -08005119 size_t total_heap_size_executable() { return total_heap_size_executable_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005120 size_t total_physical_size() { return total_physical_size_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005121 size_t total_available_size() { return total_available_size_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005122 size_t used_heap_size() { return used_heap_size_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005123 size_t heap_size_limit() { return heap_size_limit_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005124 size_t does_zap_garbage() { return does_zap_garbage_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005125
5126 private:
Steve Block3ce2e202009-11-05 08:53:23 +00005127 size_t total_heap_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08005128 size_t total_heap_size_executable_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005129 size_t total_physical_size_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005130 size_t total_available_size_;
Steve Block3ce2e202009-11-05 08:53:23 +00005131 size_t used_heap_size_;
Ben Murdochb8e0da22011-05-16 14:20:40 +01005132 size_t heap_size_limit_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005133 bool does_zap_garbage_;
Steve Block3ce2e202009-11-05 08:53:23 +00005134
5135 friend class V8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005136 friend class Isolate;
Steve Block3ce2e202009-11-05 08:53:23 +00005137};
5138
5139
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005140class V8_EXPORT HeapSpaceStatistics {
5141 public:
5142 HeapSpaceStatistics();
5143 const char* space_name() { return space_name_; }
5144 size_t space_size() { return space_size_; }
5145 size_t space_used_size() { return space_used_size_; }
5146 size_t space_available_size() { return space_available_size_; }
5147 size_t physical_space_size() { return physical_space_size_; }
5148
5149 private:
5150 const char* space_name_;
5151 size_t space_size_;
5152 size_t space_used_size_;
5153 size_t space_available_size_;
5154 size_t physical_space_size_;
5155
5156 friend class Isolate;
5157};
5158
5159
5160class V8_EXPORT HeapObjectStatistics {
5161 public:
5162 HeapObjectStatistics();
5163 const char* object_type() { return object_type_; }
5164 const char* object_sub_type() { return object_sub_type_; }
5165 size_t object_count() { return object_count_; }
5166 size_t object_size() { return object_size_; }
5167
5168 private:
5169 const char* object_type_;
5170 const char* object_sub_type_;
5171 size_t object_count_;
5172 size_t object_size_;
5173
5174 friend class Isolate;
5175};
5176
5177
Steve Block44f0eee2011-05-26 01:26:41 +01005178class RetainedObjectInfo;
5179
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005180
Steve Block44f0eee2011-05-26 01:26:41 +01005181/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005182 * FunctionEntryHook is the type of the profile entry hook called at entry to
5183 * any generated function when function-level profiling is enabled.
5184 *
5185 * \param function the address of the function that's being entered.
5186 * \param return_addr_location points to a location on stack where the machine
5187 * return address resides. This can be used to identify the caller of
5188 * \p function, and/or modified to divert execution when \p function exits.
5189 *
5190 * \note the entry hook must not cause garbage collection.
Steve Block44f0eee2011-05-26 01:26:41 +01005191 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005192typedef void (*FunctionEntryHook)(uintptr_t function,
5193 uintptr_t return_addr_location);
5194
5195/**
5196 * A JIT code event is issued each time code is added, moved or removed.
5197 *
5198 * \note removal events are not currently issued.
5199 */
5200struct JitCodeEvent {
5201 enum EventType {
5202 CODE_ADDED,
5203 CODE_MOVED,
5204 CODE_REMOVED,
5205 CODE_ADD_LINE_POS_INFO,
5206 CODE_START_LINE_INFO_RECORDING,
5207 CODE_END_LINE_INFO_RECORDING
5208 };
5209 // Definition of the code position type. The "POSITION" type means the place
5210 // in the source code which are of interest when making stack traces to
5211 // pin-point the source location of a stack frame as close as possible.
5212 // The "STATEMENT_POSITION" means the place at the beginning of each
5213 // statement, and is used to indicate possible break locations.
5214 enum PositionType { POSITION, STATEMENT_POSITION };
5215
5216 // Type of event.
5217 EventType type;
5218 // Start of the instructions.
5219 void* code_start;
5220 // Size of the instructions.
5221 size_t code_len;
5222 // Script info for CODE_ADDED event.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005223 Local<UnboundScript> script;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005224 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
5225 // code line information which is returned from the
5226 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
5227 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
5228 void* user_data;
5229
5230 struct name_t {
5231 // Name of the object associated with the code, note that the string is not
5232 // zero-terminated.
5233 const char* str;
5234 // Number of chars in str.
5235 size_t len;
5236 };
5237
5238 struct line_info_t {
5239 // PC offset
5240 size_t offset;
5241 // Code postion
5242 size_t pos;
5243 // The position type.
5244 PositionType position_type;
5245 };
5246
5247 union {
5248 // Only valid for CODE_ADDED.
5249 struct name_t name;
5250
5251 // Only valid for CODE_ADD_LINE_POS_INFO
5252 struct line_info_t line_info;
5253
5254 // New location of instructions. Only valid for CODE_MOVED.
5255 void* new_code_start;
5256 };
5257};
5258
5259/**
5260 * Option flags passed to the SetJitCodeEventHandler function.
5261 */
5262enum JitCodeEventOptions {
5263 kJitCodeEventDefault = 0,
5264 // Generate callbacks for already existent code.
5265 kJitCodeEventEnumExisting = 1
5266};
5267
5268
5269/**
5270 * Callback function passed to SetJitCodeEventHandler.
5271 *
5272 * \param event code add, move or removal event.
5273 */
5274typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
5275
5276
5277/**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005278 * Interface for iterating through all external resources in the heap.
5279 */
5280class V8_EXPORT ExternalResourceVisitor { // NOLINT
5281 public:
5282 virtual ~ExternalResourceVisitor() {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005283 virtual void VisitExternalString(Local<String> string) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005284};
5285
5286
5287/**
5288 * Interface for iterating through all the persistent handles in the heap.
5289 */
5290class V8_EXPORT PersistentHandleVisitor { // NOLINT
5291 public:
5292 virtual ~PersistentHandleVisitor() {}
5293 virtual void VisitPersistentHandle(Persistent<Value>* value,
5294 uint16_t class_id) {}
5295};
5296
5297
5298/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005299 * Isolate represents an isolated instance of the V8 engine. V8 isolates have
5300 * completely separate states. Objects from one isolate must not be used in
5301 * other isolates. The embedder can create multiple isolates and use them in
5302 * parallel in multiple threads. An isolate can be entered by at most one
5303 * thread at any given time. The Locker/Unlocker API must be used to
5304 * synchronize.
5305 */
5306class V8_EXPORT Isolate {
Steve Block44f0eee2011-05-26 01:26:41 +01005307 public:
5308 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005309 * Initial configuration parameters for a new Isolate.
5310 */
5311 struct CreateParams {
5312 CreateParams()
5313 : entry_hook(NULL),
5314 code_event_handler(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005315 snapshot_blob(NULL),
5316 counter_lookup_callback(NULL),
5317 create_histogram_callback(NULL),
5318 add_histogram_sample_callback(NULL),
5319 array_buffer_allocator(NULL) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005320
5321 /**
5322 * The optional entry_hook allows the host application to provide the
5323 * address of a function that's invoked on entry to every V8-generated
5324 * function. Note that entry_hook is invoked at the very start of each
5325 * generated function. Furthermore, if an entry_hook is given, V8 will
5326 * always run without a context snapshot.
5327 */
5328 FunctionEntryHook entry_hook;
5329
5330 /**
5331 * Allows the host application to provide the address of a function that is
5332 * notified each time code is added, moved or removed.
5333 */
5334 JitCodeEventHandler code_event_handler;
5335
5336 /**
5337 * ResourceConstraints to use for the new Isolate.
5338 */
5339 ResourceConstraints constraints;
5340
5341 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005342 * Explicitly specify a startup snapshot blob. The embedder owns the blob.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005343 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005344 StartupData* snapshot_blob;
5345
5346
5347 /**
5348 * Enables the host application to provide a mechanism for recording
5349 * statistics counters.
5350 */
5351 CounterLookupCallback counter_lookup_callback;
5352
5353 /**
5354 * Enables the host application to provide a mechanism for recording
5355 * histograms. The CreateHistogram function returns a
5356 * histogram which will later be passed to the AddHistogramSample
5357 * function.
5358 */
5359 CreateHistogramCallback create_histogram_callback;
5360 AddHistogramSampleCallback add_histogram_sample_callback;
5361
5362 /**
5363 * The ArrayBuffer::Allocator to use for allocating and freeing the backing
5364 * store of ArrayBuffers.
5365 */
5366 ArrayBuffer::Allocator* array_buffer_allocator;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005367 };
5368
5369
5370 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005371 * Stack-allocated class which sets the isolate for all operations
5372 * executed within a local scope.
5373 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005374 class V8_EXPORT Scope {
Steve Block44f0eee2011-05-26 01:26:41 +01005375 public:
5376 explicit Scope(Isolate* isolate) : isolate_(isolate) {
5377 isolate->Enter();
5378 }
5379
5380 ~Scope() { isolate_->Exit(); }
5381
5382 private:
5383 Isolate* const isolate_;
5384
5385 // Prevent copying of Scope objects.
5386 Scope(const Scope&);
5387 Scope& operator=(const Scope&);
5388 };
5389
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005390
5391 /**
5392 * Assert that no Javascript code is invoked.
5393 */
5394 class V8_EXPORT DisallowJavascriptExecutionScope {
5395 public:
5396 enum OnFailure { CRASH_ON_FAILURE, THROW_ON_FAILURE };
5397
5398 DisallowJavascriptExecutionScope(Isolate* isolate, OnFailure on_failure);
5399 ~DisallowJavascriptExecutionScope();
5400
5401 private:
5402 bool on_failure_;
5403 void* internal_;
5404
5405 // Prevent copying of Scope objects.
5406 DisallowJavascriptExecutionScope(const DisallowJavascriptExecutionScope&);
5407 DisallowJavascriptExecutionScope& operator=(
5408 const DisallowJavascriptExecutionScope&);
5409 };
5410
5411
5412 /**
5413 * Introduce exception to DisallowJavascriptExecutionScope.
5414 */
5415 class V8_EXPORT AllowJavascriptExecutionScope {
5416 public:
5417 explicit AllowJavascriptExecutionScope(Isolate* isolate);
5418 ~AllowJavascriptExecutionScope();
5419
5420 private:
5421 void* internal_throws_;
5422 void* internal_assert_;
5423
5424 // Prevent copying of Scope objects.
5425 AllowJavascriptExecutionScope(const AllowJavascriptExecutionScope&);
5426 AllowJavascriptExecutionScope& operator=(
5427 const AllowJavascriptExecutionScope&);
5428 };
5429
5430 /**
5431 * Do not run microtasks while this scope is active, even if microtasks are
5432 * automatically executed otherwise.
5433 */
5434 class V8_EXPORT SuppressMicrotaskExecutionScope {
5435 public:
5436 explicit SuppressMicrotaskExecutionScope(Isolate* isolate);
5437 ~SuppressMicrotaskExecutionScope();
5438
5439 private:
5440 internal::Isolate* isolate_;
5441
5442 // Prevent copying of Scope objects.
5443 SuppressMicrotaskExecutionScope(const SuppressMicrotaskExecutionScope&);
5444 SuppressMicrotaskExecutionScope& operator=(
5445 const SuppressMicrotaskExecutionScope&);
5446 };
5447
5448 /**
5449 * Types of garbage collections that can be requested via
5450 * RequestGarbageCollectionForTesting.
5451 */
5452 enum GarbageCollectionType {
5453 kFullGarbageCollection,
5454 kMinorGarbageCollection
5455 };
5456
5457 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005458 * Features reported via the SetUseCounterCallback callback. Do not change
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005459 * assigned numbers of existing items; add new features to the end of this
5460 * list.
5461 */
5462 enum UseCounterFeature {
5463 kUseAsm = 0,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005464 kBreakIterator = 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005465 kLegacyConst = 2,
5466 kMarkDequeOverflow = 3,
5467 kStoreBufferOverflow = 4,
5468 kSlotsBufferOverflow = 5,
5469 kObjectObserve = 6,
5470 kForcedGC = 7,
5471 kSloppyMode = 8,
5472 kStrictMode = 9,
5473 kStrongMode = 10,
5474 kRegExpPrototypeStickyGetter = 11,
5475 kRegExpPrototypeToString = 12,
5476 kRegExpPrototypeUnicodeGetter = 13,
5477 kIntlV8Parse = 14,
5478 kIntlPattern = 15,
5479 kIntlResolved = 16,
5480 kPromiseChain = 17,
5481 kPromiseAccept = 18,
5482 kPromiseDefer = 19,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005483 kHtmlCommentInExternalScript = 20,
5484 kHtmlComment = 21,
5485 kSloppyModeBlockScopedFunctionRedefinition = 22,
5486 kForInInitializer = 23,
5487 kArrayProtectorDirtied = 24,
5488 kArraySpeciesModified = 25,
5489 kArrayPrototypeConstructorModified = 26,
5490 kArrayInstanceProtoModified = 27,
5491 kArrayInstanceConstructorModified = 28,
5492
5493 // If you add new values here, you'll also need to update V8Initializer.cpp
5494 // in Chromium.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005495 kUseCounterFeatureCount // This enum value must be last.
5496 };
5497
5498 typedef void (*UseCounterCallback)(Isolate* isolate,
5499 UseCounterFeature feature);
5500
5501
Steve Block44f0eee2011-05-26 01:26:41 +01005502 /**
5503 * Creates a new isolate. Does not change the currently entered
5504 * isolate.
5505 *
5506 * When an isolate is no longer used its resources should be freed
5507 * by calling Dispose(). Using the delete operator is not allowed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005508 *
5509 * V8::Initialize() must have run prior to this.
Steve Block44f0eee2011-05-26 01:26:41 +01005510 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005511 static Isolate* New(const CreateParams& params);
Steve Block44f0eee2011-05-26 01:26:41 +01005512
5513 /**
5514 * Returns the entered isolate for the current thread or NULL in
5515 * case there is no current isolate.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005516 *
5517 * This method must not be invoked before V8::Initialize() was invoked.
Steve Block44f0eee2011-05-26 01:26:41 +01005518 */
5519 static Isolate* GetCurrent();
5520
5521 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005522 * Custom callback used by embedders to help V8 determine if it should abort
5523 * when it throws and no internal handler is predicted to catch the
5524 * exception. If --abort-on-uncaught-exception is used on the command line,
5525 * then V8 will abort if either:
5526 * - no custom callback is set.
5527 * - the custom callback set returns true.
5528 * Otherwise, the custom callback will not be called and V8 will not abort.
5529 */
5530 typedef bool (*AbortOnUncaughtExceptionCallback)(Isolate*);
5531 void SetAbortOnUncaughtExceptionCallback(
5532 AbortOnUncaughtExceptionCallback callback);
5533
5534 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005535 * Methods below this point require holding a lock (using Locker) in
5536 * a multi-threaded environment.
5537 */
5538
5539 /**
5540 * Sets this isolate as the entered one for the current thread.
5541 * Saves the previously entered one (if any), so that it can be
5542 * restored when exiting. Re-entering an isolate is allowed.
5543 */
5544 void Enter();
5545
5546 /**
5547 * Exits this isolate by restoring the previously entered one in the
5548 * current thread. The isolate may still stay the same, if it was
5549 * entered more than once.
5550 *
5551 * Requires: this == Isolate::GetCurrent().
5552 */
5553 void Exit();
5554
5555 /**
5556 * Disposes the isolate. The isolate must not be entered by any
5557 * thread to be disposable.
5558 */
5559 void Dispose();
5560
Ben Murdoch257744e2011-11-30 15:57:28 +00005561 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005562 * Discards all V8 thread-specific data for the Isolate. Should be used
5563 * if a thread is terminating and it has used an Isolate that will outlive
5564 * the thread -- all thread-specific data for an Isolate is discarded when
5565 * an Isolate is disposed so this call is pointless if an Isolate is about
5566 * to be Disposed.
5567 */
5568 void DiscardThreadSpecificMetadata();
5569
5570 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005571 * Associate embedder-specific data with the isolate. |slot| has to be
5572 * between 0 and GetNumberOfDataSlots() - 1.
Ben Murdoch257744e2011-11-30 15:57:28 +00005573 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005574 V8_INLINE void SetData(uint32_t slot, void* data);
Ben Murdoch257744e2011-11-30 15:57:28 +00005575
5576 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005577 * Retrieve embedder-specific data from the isolate.
5578 * Returns NULL if SetData has never been called for the given |slot|.
Ben Murdoch257744e2011-11-30 15:57:28 +00005579 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005580 V8_INLINE void* GetData(uint32_t slot);
5581
5582 /**
5583 * Returns the maximum number of available embedder data slots. Valid slots
5584 * are in the range of 0 - GetNumberOfDataSlots() - 1.
5585 */
5586 V8_INLINE static uint32_t GetNumberOfDataSlots();
5587
5588 /**
5589 * Get statistics about the heap memory usage.
5590 */
5591 void GetHeapStatistics(HeapStatistics* heap_statistics);
5592
5593 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005594 * Returns the number of spaces in the heap.
5595 */
5596 size_t NumberOfHeapSpaces();
5597
5598 /**
5599 * Get the memory usage of a space in the heap.
5600 *
5601 * \param space_statistics The HeapSpaceStatistics object to fill in
5602 * statistics.
5603 * \param index The index of the space to get statistics from, which ranges
5604 * from 0 to NumberOfHeapSpaces() - 1.
5605 * \returns true on success.
5606 */
5607 bool GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
5608 size_t index);
5609
5610 /**
5611 * Returns the number of types of objects tracked in the heap at GC.
5612 */
5613 size_t NumberOfTrackedHeapObjectTypes();
5614
5615 /**
5616 * Get statistics about objects in the heap.
5617 *
5618 * \param object_statistics The HeapObjectStatistics object to fill in
5619 * statistics of objects of given type, which were live in the previous GC.
5620 * \param type_index The index of the type of object to fill details about,
5621 * which ranges from 0 to NumberOfTrackedHeapObjectTypes() - 1.
5622 * \returns true on success.
5623 */
5624 bool GetHeapObjectStatisticsAtLastGC(HeapObjectStatistics* object_statistics,
5625 size_t type_index);
5626
5627 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005628 * Get a call stack sample from the isolate.
5629 * \param state Execution state.
5630 * \param frames Caller allocated buffer to store stack frames.
5631 * \param frames_limit Maximum number of frames to capture. The buffer must
5632 * be large enough to hold the number of frames.
5633 * \param sample_info The sample info is filled up by the function
5634 * provides number of actual captured stack frames and
5635 * the current VM state.
5636 * \note GetStackSample should only be called when the JS thread is paused or
5637 * interrupted. Otherwise the behavior is undefined.
5638 */
5639 void GetStackSample(const RegisterState& state, void** frames,
5640 size_t frames_limit, SampleInfo* sample_info);
5641
5642 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005643 * Adjusts the amount of registered external memory. Used to give V8 an
5644 * indication of the amount of externally allocated memory that is kept alive
5645 * by JavaScript objects. V8 uses this to decide when to perform global
5646 * garbage collections. Registering externally allocated memory will trigger
5647 * global garbage collections more often than it would otherwise in an attempt
5648 * to garbage collect the JavaScript objects that keep the externally
5649 * allocated memory alive.
5650 *
5651 * \param change_in_bytes the change in externally allocated memory that is
5652 * kept alive by JavaScript objects.
5653 * \returns the adjusted value.
5654 */
5655 V8_INLINE int64_t
5656 AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes);
5657
5658 /**
5659 * Returns heap profiler for this isolate. Will return NULL until the isolate
5660 * is initialized.
5661 */
5662 HeapProfiler* GetHeapProfiler();
5663
5664 /**
5665 * Returns CPU profiler for this isolate. Will return NULL unless the isolate
5666 * is initialized. It is the embedder's responsibility to stop all CPU
5667 * profiling activities if it has started any.
5668 */
5669 CpuProfiler* GetCpuProfiler();
5670
5671 /** Returns true if this isolate has a current context. */
5672 bool InContext();
5673
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005674 /**
5675 * Returns the context of the currently running JavaScript, or the context
5676 * on the top of the stack if no JavaScript is running.
5677 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005678 Local<Context> GetCurrentContext();
5679
5680 /**
5681 * Returns the context of the calling JavaScript code. That is the
5682 * context of the top-most JavaScript frame. If there are no
5683 * JavaScript frames an empty handle is returned.
5684 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005685 V8_DEPRECATE_SOON(
5686 "Calling context concept is not compatible with tail calls, and will be "
5687 "removed.",
5688 Local<Context> GetCallingContext());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005689
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005690 /** Returns the last context entered through V8's C++ API. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005691 Local<Context> GetEnteredContext();
5692
5693 /**
5694 * Schedules an exception to be thrown when returning to JavaScript. When an
5695 * exception has been scheduled it is illegal to invoke any JavaScript
5696 * operation; the caller must return immediately and only after the exception
5697 * has been handled does it become legal to invoke JavaScript operations.
5698 */
5699 Local<Value> ThrowException(Local<Value> exception);
5700
5701 /**
5702 * Allows the host application to group objects together. If one
5703 * object in the group is alive, all objects in the group are alive.
5704 * After each garbage collection, object groups are removed. It is
5705 * intended to be used in the before-garbage-collection callback
5706 * function, for instance to simulate DOM tree connections among JS
5707 * wrapper objects. Object groups for all dependent handles need to
5708 * be provided for kGCTypeMarkSweepCompact collections, for all other
5709 * garbage collection types it is sufficient to provide object groups
5710 * for partially dependent handles only.
5711 */
5712 template<typename T> void SetObjectGroupId(const Persistent<T>& object,
5713 UniqueId id);
5714
5715 /**
5716 * Allows the host application to declare implicit references from an object
5717 * group to an object. If the objects of the object group are alive, the child
5718 * object is alive too. After each garbage collection, all implicit references
5719 * are removed. It is intended to be used in the before-garbage-collection
5720 * callback function.
5721 */
5722 template<typename T> void SetReferenceFromGroup(UniqueId id,
5723 const Persistent<T>& child);
5724
5725 /**
5726 * Allows the host application to declare implicit references from an object
5727 * to another object. If the parent object is alive, the child object is alive
5728 * too. After each garbage collection, all implicit references are removed. It
5729 * is intended to be used in the before-garbage-collection callback function.
5730 */
5731 template<typename T, typename S>
5732 void SetReference(const Persistent<T>& parent, const Persistent<S>& child);
5733
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005734 typedef void (*GCCallback)(Isolate* isolate, GCType type,
5735 GCCallbackFlags flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005736
5737 /**
5738 * Enables the host application to receive a notification before a
5739 * garbage collection. Allocations are allowed in the callback function,
5740 * but the callback is not re-entrant: if the allocation inside it will
5741 * trigger the garbage collection, the callback won't be called again.
5742 * It is possible to specify the GCType filter for your callback. But it is
5743 * not possible to register the same callback function two times with
5744 * different GCType filters.
5745 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005746 void AddGCPrologueCallback(GCCallback callback,
5747 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005748
5749 /**
5750 * This function removes callback which was installed by
5751 * AddGCPrologueCallback function.
5752 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005753 void RemoveGCPrologueCallback(GCCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005754
5755 /**
5756 * Enables the host application to receive a notification after a
5757 * garbage collection. Allocations are allowed in the callback function,
5758 * but the callback is not re-entrant: if the allocation inside it will
5759 * trigger the garbage collection, the callback won't be called again.
5760 * It is possible to specify the GCType filter for your callback. But it is
5761 * not possible to register the same callback function two times with
5762 * different GCType filters.
5763 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005764 void AddGCEpilogueCallback(GCCallback callback,
5765 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005766
5767 /**
5768 * This function removes callback which was installed by
5769 * AddGCEpilogueCallback function.
5770 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005771 void RemoveGCEpilogueCallback(GCCallback callback);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005772
5773 /**
5774 * Forcefully terminate the current thread of JavaScript execution
5775 * in the given isolate.
5776 *
5777 * This method can be used by any thread even if that thread has not
5778 * acquired the V8 lock with a Locker object.
5779 */
5780 void TerminateExecution();
5781
5782 /**
5783 * Is V8 terminating JavaScript execution.
5784 *
5785 * Returns true if JavaScript execution is currently terminating
5786 * because of a call to TerminateExecution. In that case there are
5787 * still JavaScript frames on the stack and the termination
5788 * exception is still active.
5789 */
5790 bool IsExecutionTerminating();
5791
5792 /**
5793 * Resume execution capability in the given isolate, whose execution
5794 * was previously forcefully terminated using TerminateExecution().
5795 *
5796 * When execution is forcefully terminated using TerminateExecution(),
5797 * the isolate can not resume execution until all JavaScript frames
5798 * have propagated the uncatchable exception which is generated. This
5799 * method allows the program embedding the engine to handle the
5800 * termination event and resume execution capability, even if
5801 * JavaScript frames remain on the stack.
5802 *
5803 * This method can be used by any thread even if that thread has not
5804 * acquired the V8 lock with a Locker object.
5805 */
5806 void CancelTerminateExecution();
5807
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005808 /**
5809 * Request V8 to interrupt long running JavaScript code and invoke
5810 * the given |callback| passing the given |data| to it. After |callback|
5811 * returns control will be returned to the JavaScript code.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005812 * There may be a number of interrupt requests in flight.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005813 * Can be called from another thread without acquiring a |Locker|.
5814 * Registered |callback| must not reenter interrupted Isolate.
5815 */
5816 void RequestInterrupt(InterruptCallback callback, void* data);
5817
5818 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005819 * Request garbage collection in this Isolate. It is only valid to call this
5820 * function if --expose_gc was specified.
5821 *
5822 * This should only be used for testing purposes and not to enforce a garbage
5823 * collection schedule. It has strong negative impact on the garbage
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005824 * collection performance. Use IdleNotificationDeadline() or
5825 * LowMemoryNotification() instead to influence the garbage collection
5826 * schedule.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005827 */
5828 void RequestGarbageCollectionForTesting(GarbageCollectionType type);
5829
5830 /**
5831 * Set the callback to invoke for logging event.
5832 */
5833 void SetEventLogger(LogEventCallback that);
5834
5835 /**
Ben Murdoch097c5b22016-05-18 11:27:45 +01005836 * Adds a callback to notify the host application right before a script
5837 * is about to run. If a script re-enters the runtime during executing, the
5838 * BeforeCallEnteredCallback is invoked for each re-entrance.
5839 * Executing scripts inside the callback will re-trigger the callback.
5840 */
5841 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
5842
5843 /**
5844 * Removes callback that was installed by AddBeforeCallEnteredCallback.
5845 */
5846 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
5847
5848 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005849 * Adds a callback to notify the host application when a script finished
5850 * running. If a script re-enters the runtime during executing, the
5851 * CallCompletedCallback is only invoked when the outer-most script
5852 * execution ends. Executing scripts inside the callback do not trigger
5853 * further callbacks.
5854 */
5855 void AddCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005856 V8_DEPRECATE_SOON(
5857 "Use callback with parameter",
5858 void AddCallCompletedCallback(DeprecatedCallCompletedCallback callback));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005859
5860 /**
5861 * Removes callback that was installed by AddCallCompletedCallback.
5862 */
5863 void RemoveCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005864 V8_DEPRECATE_SOON(
5865 "Use callback with parameter",
5866 void RemoveCallCompletedCallback(
5867 DeprecatedCallCompletedCallback callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005868
5869 /**
5870 * Set callback to notify about promise reject with no handler, or
5871 * revocation of such a previous notification once the handler is added.
5872 */
5873 void SetPromiseRejectCallback(PromiseRejectCallback callback);
5874
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005875 /**
5876 * Experimental: Runs the Microtask Work Queue until empty
5877 * Any exceptions thrown by microtask callbacks are swallowed.
5878 */
5879 void RunMicrotasks();
5880
5881 /**
5882 * Experimental: Enqueues the callback to the Microtask Work Queue
5883 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005884 void EnqueueMicrotask(Local<Function> microtask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005885
5886 /**
5887 * Experimental: Enqueues the callback to the Microtask Work Queue
5888 */
5889 void EnqueueMicrotask(MicrotaskCallback microtask, void* data = NULL);
5890
5891 /**
5892 * Experimental: Controls whether the Microtask Work Queue is automatically
5893 * run when the script call depth decrements to zero.
5894 */
5895 void SetAutorunMicrotasks(bool autorun);
5896
5897 /**
5898 * Experimental: Returns whether the Microtask Work Queue is automatically
5899 * run when the script call depth decrements to zero.
5900 */
5901 bool WillAutorunMicrotasks() const;
5902
5903 /**
5904 * Sets a callback for counting the number of times a feature of V8 is used.
5905 */
5906 void SetUseCounterCallback(UseCounterCallback callback);
5907
5908 /**
5909 * Enables the host application to provide a mechanism for recording
5910 * statistics counters.
5911 */
5912 void SetCounterFunction(CounterLookupCallback);
5913
5914 /**
5915 * Enables the host application to provide a mechanism for recording
5916 * histograms. The CreateHistogram function returns a
5917 * histogram which will later be passed to the AddHistogramSample
5918 * function.
5919 */
5920 void SetCreateHistogramFunction(CreateHistogramCallback);
5921 void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
5922
5923 /**
5924 * Optional notification that the embedder is idle.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005925 * V8 uses the notification to perform garbage collection.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005926 * This call can be used repeatedly if the embedder remains idle.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005927 * Returns true if the embedder should stop calling IdleNotificationDeadline
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005928 * until real work has been done. This indicates that V8 has done
5929 * as much cleanup as it will be able to do.
5930 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005931 * The deadline_in_seconds argument specifies the deadline V8 has to finish
5932 * garbage collection work. deadline_in_seconds is compared with
5933 * MonotonicallyIncreasingTime() and should be based on the same timebase as
5934 * that function. There is no guarantee that the actual work will be done
5935 * within the time limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005936 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005937 bool IdleNotificationDeadline(double deadline_in_seconds);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005938
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005939 V8_DEPRECATED("use IdleNotificationDeadline()",
5940 bool IdleNotification(int idle_time_in_ms));
5941
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005942 /**
5943 * Optional notification that the system is running low on memory.
5944 * V8 uses these notifications to attempt to free memory.
5945 */
5946 void LowMemoryNotification();
5947
5948 /**
5949 * Optional notification that a context has been disposed. V8 uses
5950 * these notifications to guide the GC heuristic. Returns the number
5951 * of context disposals - including this one - since the last time
5952 * V8 had a chance to clean up.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005953 *
5954 * The optional parameter |dependant_context| specifies whether the disposed
5955 * context was depending on state from other contexts or not.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005956 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005957 int ContextDisposedNotification(bool dependant_context = true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005958
5959 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005960 * Optional notification that the isolate switched to the foreground.
5961 * V8 uses these notifications to guide heuristics.
5962 */
5963 void IsolateInForegroundNotification();
5964
5965 /**
5966 * Optional notification that the isolate switched to the background.
5967 * V8 uses these notifications to guide heuristics.
5968 */
5969 void IsolateInBackgroundNotification();
5970
5971 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005972 * Allows the host application to provide the address of a function that is
5973 * notified each time code is added, moved or removed.
5974 *
5975 * \param options options for the JIT code event handler.
5976 * \param event_handler the JIT code event handler, which will be invoked
5977 * each time code is added, moved or removed.
5978 * \note \p event_handler won't get notified of existent code.
5979 * \note since code removal notifications are not currently issued, the
5980 * \p event_handler may get notifications of code that overlaps earlier
5981 * code notifications. This happens when code areas are reused, and the
5982 * earlier overlapping code areas should therefore be discarded.
5983 * \note the events passed to \p event_handler and the strings they point to
5984 * are not guaranteed to live past each call. The \p event_handler must
5985 * copy strings and other parameters it needs to keep around.
5986 * \note the set of events declared in JitCodeEvent::EventType is expected to
5987 * grow over time, and the JitCodeEvent structure is expected to accrue
5988 * new members. The \p event_handler function must ignore event codes
5989 * it does not recognize to maintain future compatibility.
5990 * \note Use Isolate::CreateParams to get events for code executed during
5991 * Isolate setup.
5992 */
5993 void SetJitCodeEventHandler(JitCodeEventOptions options,
5994 JitCodeEventHandler event_handler);
5995
5996 /**
5997 * Modifies the stack limit for this Isolate.
5998 *
5999 * \param stack_limit An address beyond which the Vm's stack may not grow.
6000 *
6001 * \note If you are using threads then you should hold the V8::Locker lock
6002 * while setting the stack limit and you must set a non-default stack
6003 * limit separately for each thread.
6004 */
6005 void SetStackLimit(uintptr_t stack_limit);
Ben Murdoch257744e2011-11-30 15:57:28 +00006006
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006007 /**
6008 * Returns a memory range that can potentially contain jitted code.
6009 *
6010 * On Win64, embedders are advised to install function table callbacks for
6011 * these ranges, as default SEH won't be able to unwind through jitted code.
6012 *
6013 * The first page of the code range is reserved for the embedder and is
6014 * committed, writable, and executable.
6015 *
6016 * Might be empty on other platforms.
6017 *
6018 * https://code.google.com/p/v8/issues/detail?id=3598
6019 */
6020 void GetCodeRange(void** start, size_t* length_in_bytes);
6021
6022 /** Set the callback to invoke in case of fatal errors. */
6023 void SetFatalErrorHandler(FatalErrorCallback that);
6024
6025 /**
6026 * Set the callback to invoke to check if code generation from
6027 * strings should be allowed.
6028 */
6029 void SetAllowCodeGenerationFromStringsCallback(
6030 AllowCodeGenerationFromStringsCallback callback);
6031
6032 /**
6033 * Check if V8 is dead and therefore unusable. This is the case after
6034 * fatal errors such as out-of-memory situations.
6035 */
6036 bool IsDead();
6037
6038 /**
6039 * Adds a message listener.
6040 *
6041 * The same message listener can be added more than once and in that
6042 * case it will be called more than once for each message.
6043 *
6044 * If data is specified, it will be passed to the callback when it is called.
6045 * Otherwise, the exception object will be passed to the callback instead.
6046 */
6047 bool AddMessageListener(MessageCallback that,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006048 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006049
6050 /**
6051 * Remove all message listeners from the specified callback function.
6052 */
6053 void RemoveMessageListeners(MessageCallback that);
6054
6055 /** Callback function for reporting failed access checks.*/
6056 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
6057
6058 /**
6059 * Tells V8 to capture current stack trace when uncaught exception occurs
6060 * and report it to the message listeners. The option is off by default.
6061 */
6062 void SetCaptureStackTraceForUncaughtExceptions(
6063 bool capture, int frame_limit = 10,
6064 StackTrace::StackTraceOptions options = StackTrace::kOverview);
6065
6066 /**
6067 * Enables the host application to provide a mechanism to be notified
6068 * and perform custom logging when V8 Allocates Executable Memory.
6069 */
6070 void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
6071 ObjectSpace space, AllocationAction action);
6072
6073 /**
6074 * Removes callback that was installed by AddMemoryAllocationCallback.
6075 */
6076 void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
6077
6078 /**
6079 * Iterates through all external resources referenced from current isolate
6080 * heap. GC is not invoked prior to iterating, therefore there is no
6081 * guarantee that visited objects are still alive.
6082 */
6083 void VisitExternalResources(ExternalResourceVisitor* visitor);
6084
6085 /**
6086 * Iterates through all the persistent handles in the current isolate's heap
6087 * that have class_ids.
6088 */
6089 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
6090
6091 /**
6092 * Iterates through all the persistent handles in the current isolate's heap
6093 * that have class_ids and are candidates to be marked as partially dependent
6094 * handles. This will visit handles to young objects created since the last
6095 * garbage collection but is free to visit an arbitrary superset of these
6096 * objects.
6097 */
6098 void VisitHandlesForPartialDependence(PersistentHandleVisitor* visitor);
6099
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006100 /**
6101 * Iterates through all the persistent handles in the current isolate's heap
6102 * that have class_ids and are weak to be marked as inactive if there is no
6103 * pending activity for the handle.
6104 */
6105 void VisitWeakHandles(PersistentHandleVisitor* visitor);
6106
Steve Block44f0eee2011-05-26 01:26:41 +01006107 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006108 template <class K, class V, class Traits>
6109 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006110
Steve Block44f0eee2011-05-26 01:26:41 +01006111 Isolate();
6112 Isolate(const Isolate&);
6113 ~Isolate();
6114 Isolate& operator=(const Isolate&);
6115 void* operator new(size_t size);
6116 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006117
6118 void SetObjectGroupId(internal::Object** object, UniqueId id);
6119 void SetReferenceFromGroup(UniqueId id, internal::Object** object);
6120 void SetReference(internal::Object** parent, internal::Object** child);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006121 void ReportExternalAllocationLimitReached();
Steve Block44f0eee2011-05-26 01:26:41 +01006122};
6123
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006124class V8_EXPORT StartupData {
Ben Murdoch257744e2011-11-30 15:57:28 +00006125 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006126 const char* data;
Ben Murdoch257744e2011-11-30 15:57:28 +00006127 int raw_size;
6128};
6129
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006130
6131/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006132 * EntropySource is used as a callback function when v8 needs a source
6133 * of entropy.
6134 */
6135typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
6136
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006137
6138/**
6139 * ReturnAddressLocationResolver is used as a callback function when v8 is
6140 * resolving the location of a return address on the stack. Profilers that
6141 * change the return address on the stack can use this to resolve the stack
6142 * location to whereever the profiler stashed the original return address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006143 *
6144 * \param return_addr_location points to a location on stack where a machine
6145 * return address resides.
6146 * \returns either return_addr_location, or else a pointer to the profiler's
6147 * copy of the original return address.
6148 *
6149 * \note the resolver function must not cause garbage collection.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006150 */
6151typedef uintptr_t (*ReturnAddressLocationResolver)(
6152 uintptr_t return_addr_location);
6153
6154
6155/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006156 * Container class for static utility functions.
6157 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006158class V8_EXPORT V8 {
Steve Blocka7e24c12009-10-30 11:49:00 +00006159 public:
6160 /** Set the callback to invoke in case of fatal errors. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006161 V8_INLINE static V8_DEPRECATED(
6162 "Use isolate version",
6163 void SetFatalErrorHandler(FatalErrorCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006164
6165 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00006166 * Set the callback to invoke to check if code generation from
6167 * strings should be allowed.
6168 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006169 V8_INLINE static V8_DEPRECATED(
6170 "Use isolate version", void SetAllowCodeGenerationFromStringsCallback(
6171 AllowCodeGenerationFromStringsCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006172
6173 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006174 * Check if V8 is dead and therefore unusable. This is the case after
6175 * fatal errors such as out-of-memory situations.
6176 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006177 V8_INLINE static V8_DEPRECATED("Use isolate version", bool IsDead());
Ben Murdoch257744e2011-11-30 15:57:28 +00006178
6179 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006180 * Hand startup data to V8, in case the embedder has chosen to build
6181 * V8 with external startup data.
6182 *
6183 * Note:
6184 * - By default the startup data is linked into the V8 library, in which
6185 * case this function is not meaningful.
6186 * - If this needs to be called, it needs to be called before V8
6187 * tries to make use of its built-ins.
6188 * - To avoid unnecessary copies of data, V8 will point directly into the
6189 * given data blob, so pretty please keep it around until V8 exit.
6190 * - Compression of the startup blob might be useful, but needs to
6191 * handled entirely on the embedders' side.
6192 * - The call will abort if the data is invalid.
6193 */
6194 static void SetNativesDataBlob(StartupData* startup_blob);
6195 static void SetSnapshotDataBlob(StartupData* startup_blob);
6196
6197 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006198 * Create a new isolate and context for the purpose of capturing a snapshot
6199 * Returns { NULL, 0 } on failure.
6200 * The caller owns the data array in the return value.
6201 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006202 static StartupData CreateSnapshotDataBlob(const char* custom_source = NULL);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006203
6204 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006205 * Adds a message listener.
6206 *
Ben Murdoch257744e2011-11-30 15:57:28 +00006207 * The same message listener can be added more than once and in that
Steve Blocka7e24c12009-10-30 11:49:00 +00006208 * case it will be called more than once for each message.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006209 *
6210 * If data is specified, it will be passed to the callback when it is called.
6211 * Otherwise, the exception object will be passed to the callback instead.
Steve Blocka7e24c12009-10-30 11:49:00 +00006212 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006213 V8_INLINE static V8_DEPRECATED(
6214 "Use isolate version",
6215 bool AddMessageListener(MessageCallback that,
6216 Local<Value> data = Local<Value>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00006217
6218 /**
6219 * Remove all message listeners from the specified callback function.
6220 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006221 V8_INLINE static V8_DEPRECATED(
6222 "Use isolate version", void RemoveMessageListeners(MessageCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006223
6224 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006225 * Tells V8 to capture current stack trace when uncaught exception occurs
6226 * and report it to the message listeners. The option is off by default.
6227 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006228 V8_INLINE static V8_DEPRECATED(
6229 "Use isolate version",
6230 void SetCaptureStackTraceForUncaughtExceptions(
6231 bool capture, int frame_limit = 10,
6232 StackTrace::StackTraceOptions options = StackTrace::kOverview));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006233
6234 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006235 * Sets V8 flags from a string.
6236 */
6237 static void SetFlagsFromString(const char* str, int length);
6238
6239 /**
6240 * Sets V8 flags from the command line.
6241 */
6242 static void SetFlagsFromCommandLine(int* argc,
6243 char** argv,
6244 bool remove_flags);
6245
6246 /** Get the version string. */
6247 static const char* GetVersion();
6248
Steve Blocka7e24c12009-10-30 11:49:00 +00006249 /** Callback function for reporting failed access checks.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006250 V8_INLINE static V8_DEPRECATED(
6251 "Use isolate version",
6252 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback));
Steve Blocka7e24c12009-10-30 11:49:00 +00006253
6254 /**
6255 * Enables the host application to receive a notification before a
Steve Block6ded16b2010-05-10 14:33:55 +01006256 * garbage collection. Allocations are not allowed in the
6257 * callback function, you therefore cannot manipulate objects (set
6258 * or delete properties for example) since it is possible such
6259 * operations will result in the allocation of objects. It is possible
6260 * to specify the GCType filter for your callback. But it is not possible to
6261 * register the same callback function two times with different
6262 * GCType filters.
6263 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006264 static V8_DEPRECATED(
6265 "Use isolate version",
6266 void AddGCPrologueCallback(GCCallback callback,
6267 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006268
6269 /**
6270 * This function removes callback which was installed by
6271 * AddGCPrologueCallback function.
6272 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006273 V8_INLINE static V8_DEPRECATED(
6274 "Use isolate version",
6275 void RemoveGCPrologueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006276
6277 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006278 * Enables the host application to receive a notification after a
Steve Block6ded16b2010-05-10 14:33:55 +01006279 * garbage collection. Allocations are not allowed in the
6280 * callback function, you therefore cannot manipulate objects (set
6281 * or delete properties for example) since it is possible such
6282 * operations will result in the allocation of objects. It is possible
6283 * to specify the GCType filter for your callback. But it is not possible to
6284 * register the same callback function two times with different
6285 * GCType filters.
6286 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006287 static V8_DEPRECATED(
6288 "Use isolate version",
6289 void AddGCEpilogueCallback(GCCallback callback,
6290 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006291
6292 /**
6293 * This function removes callback which was installed by
6294 * AddGCEpilogueCallback function.
6295 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006296 V8_INLINE static V8_DEPRECATED(
6297 "Use isolate version",
6298 void RemoveGCEpilogueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006299
6300 /**
Iain Merrick9ac36c92010-09-13 15:29:50 +01006301 * Enables the host application to provide a mechanism to be notified
6302 * and perform custom logging when V8 Allocates Executable Memory.
6303 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006304 V8_INLINE static V8_DEPRECATED(
6305 "Use isolate version",
6306 void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
6307 ObjectSpace space,
6308 AllocationAction action));
Iain Merrick9ac36c92010-09-13 15:29:50 +01006309
6310 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006311 * Removes callback that was installed by AddMemoryAllocationCallback.
Iain Merrick9ac36c92010-09-13 15:29:50 +01006312 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006313 V8_INLINE static V8_DEPRECATED(
6314 "Use isolate version",
6315 void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback));
Iain Merrick9ac36c92010-09-13 15:29:50 +01006316
6317 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006318 * Initializes V8. This function needs to be called before the first Isolate
6319 * is created. It always returns true.
Steve Blocka7e24c12009-10-30 11:49:00 +00006320 */
6321 static bool Initialize();
6322
6323 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006324 * Allows the host application to provide a callback which can be used
6325 * as a source of entropy for random number generators.
6326 */
6327 static void SetEntropySource(EntropySource source);
6328
6329 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006330 * Allows the host application to provide a callback that allows v8 to
6331 * cooperate with a profiler that rewrites return addresses on stack.
6332 */
6333 static void SetReturnAddressLocationResolver(
6334 ReturnAddressLocationResolver return_address_resolver);
6335
6336 /**
Steve Block44f0eee2011-05-26 01:26:41 +01006337 * Forcefully terminate the current thread of JavaScript execution
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006338 * in the given isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +00006339 *
6340 * This method can be used by any thread even if that thread has not
6341 * acquired the V8 lock with a Locker object.
Steve Block44f0eee2011-05-26 01:26:41 +01006342 *
6343 * \param isolate The isolate in which to terminate the current JS execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006344 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006345 V8_INLINE static V8_DEPRECATED("Use isolate version",
6346 void TerminateExecution(Isolate* isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00006347
6348 /**
Steve Block6ded16b2010-05-10 14:33:55 +01006349 * Is V8 terminating JavaScript execution.
6350 *
6351 * Returns true if JavaScript execution is currently terminating
6352 * because of a call to TerminateExecution. In that case there are
6353 * still JavaScript frames on the stack and the termination
6354 * exception is still active.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006355 *
6356 * \param isolate The isolate in which to check.
Steve Block6ded16b2010-05-10 14:33:55 +01006357 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006358 V8_INLINE static V8_DEPRECATED(
6359 "Use isolate version",
6360 bool IsExecutionTerminating(Isolate* isolate = NULL));
Steve Block6ded16b2010-05-10 14:33:55 +01006361
6362 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006363 * Resume execution capability in the given isolate, whose execution
6364 * was previously forcefully terminated using TerminateExecution().
6365 *
6366 * When execution is forcefully terminated using TerminateExecution(),
6367 * the isolate can not resume execution until all JavaScript frames
6368 * have propagated the uncatchable exception which is generated. This
6369 * method allows the program embedding the engine to handle the
6370 * termination event and resume execution capability, even if
6371 * JavaScript frames remain on the stack.
6372 *
6373 * This method can be used by any thread even if that thread has not
6374 * acquired the V8 lock with a Locker object.
6375 *
6376 * \param isolate The isolate in which to resume execution capability.
6377 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006378 V8_INLINE static V8_DEPRECATED(
6379 "Use isolate version", void CancelTerminateExecution(Isolate* isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006380
6381 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006382 * Releases any resources used by v8 and stops any utility threads
6383 * that may be running. Note that disposing v8 is permanent, it
6384 * cannot be reinitialized.
6385 *
6386 * It should generally not be necessary to dispose v8 before exiting
6387 * a process, this should happen automatically. It is only necessary
6388 * to use if the process needs the resources taken up by v8.
6389 */
6390 static bool Dispose();
6391
Steve Block3ce2e202009-11-05 08:53:23 +00006392 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006393 * Iterates through all external resources referenced from current isolate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006394 * heap. GC is not invoked prior to iterating, therefore there is no
6395 * guarantee that visited objects are still alive.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006396 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006397 V8_INLINE static V8_DEPRECATED(
6398 "Use isolate version",
6399 void VisitExternalResources(ExternalResourceVisitor* visitor));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006400
6401 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006402 * Iterates through all the persistent handles in the current isolate's heap
6403 * that have class_ids.
6404 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006405 V8_INLINE static V8_DEPRECATED(
6406 "Use isolate version",
6407 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006408
6409 /**
6410 * Iterates through all the persistent handles in isolate's heap that have
6411 * class_ids.
6412 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006413 V8_INLINE static V8_DEPRECATED(
6414 "Use isolate version",
6415 void VisitHandlesWithClassIds(Isolate* isolate,
6416 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006417
6418 /**
6419 * Iterates through all the persistent handles in the current isolate's heap
6420 * that have class_ids and are candidates to be marked as partially dependent
6421 * handles. This will visit handles to young objects created since the last
6422 * garbage collection but is free to visit an arbitrary superset of these
6423 * objects.
6424 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006425 V8_INLINE static V8_DEPRECATED(
6426 "Use isolate version",
6427 void VisitHandlesForPartialDependence(Isolate* isolate,
6428 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006429
6430 /**
6431 * Initialize the ICU library bundled with V8. The embedder should only
6432 * invoke this method when using the bundled ICU. Returns true on success.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006433 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006434 * If V8 was compiled with the ICU data in an external file, the location
6435 * of the data file has to be provided.
Steve Blocka7e24c12009-10-30 11:49:00 +00006436 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006437 static bool InitializeICU(const char* icu_data_file = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00006438
6439 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006440 * Initialize the external startup data. The embedder only needs to
6441 * invoke this method when external startup data was enabled in a build.
6442 *
6443 * If V8 was compiled with the startup data in an external file, then
6444 * V8 needs to be given those external files during startup. There are
6445 * three ways to do this:
6446 * - InitializeExternalStartupData(const char*)
6447 * This will look in the given directory for files "natives_blob.bin"
6448 * and "snapshot_blob.bin" - which is what the default build calls them.
6449 * - InitializeExternalStartupData(const char*, const char*)
6450 * As above, but will directly use the two given file names.
6451 * - Call SetNativesDataBlob, SetNativesDataBlob.
6452 * This will read the blobs from the given data structures and will
6453 * not perform any file IO.
6454 */
6455 static void InitializeExternalStartupData(const char* directory_path);
6456 static void InitializeExternalStartupData(const char* natives_blob,
6457 const char* snapshot_blob);
6458 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006459 * Sets the v8::Platform to use. This should be invoked before V8 is
6460 * initialized.
Steve Blocka7e24c12009-10-30 11:49:00 +00006461 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006462 static void InitializePlatform(Platform* platform);
Steve Blocka7e24c12009-10-30 11:49:00 +00006463
Steve Block6ded16b2010-05-10 14:33:55 +01006464 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006465 * Clears all references to the v8::Platform. This should be invoked after
6466 * V8 was disposed.
Steve Block6ded16b2010-05-10 14:33:55 +01006467 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006468 static void ShutdownPlatform();
Steve Block6ded16b2010-05-10 14:33:55 +01006469
Steve Blocka7e24c12009-10-30 11:49:00 +00006470 private:
6471 V8();
6472
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006473 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
6474 internal::Object** handle);
6475 static internal::Object** CopyPersistent(internal::Object** handle);
Steve Blocka7e24c12009-10-30 11:49:00 +00006476 static void DisposeGlobal(internal::Object** global_handle);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006477 typedef WeakCallbackData<Value, void>::Callback WeakCallback;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006478 static void MakeWeak(internal::Object** global_handle, void* data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006479 WeakCallback weak_callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006480 static void MakeWeak(internal::Object** global_handle, void* data,
6481 WeakCallbackInfo<void>::Callback weak_callback,
6482 WeakCallbackType type);
6483 static void MakeWeak(internal::Object** global_handle, void* data,
6484 // Must be 0 or -1.
6485 int internal_field_index1,
6486 // Must be 1 or -1.
6487 int internal_field_index2,
6488 WeakCallbackInfo<void>::Callback weak_callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006489 static void* ClearWeak(internal::Object** global_handle);
6490 static void Eternalize(Isolate* isolate,
6491 Value* handle,
6492 int* index);
6493 static Local<Value> GetEternal(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006494
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006495 static void FromJustIsNothing();
6496 static void ToLocalEmpty();
6497 static void InternalFieldOutOfBounds(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006498 template <class T> friend class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006499 template <class T>
6500 friend class MaybeLocal;
6501 template <class T>
6502 friend class Maybe;
6503 template <class T>
6504 friend class WeakCallbackInfo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006505 template <class T> friend class Eternal;
6506 template <class T> friend class PersistentBase;
6507 template <class T, class M> friend class Persistent;
Steve Blocka7e24c12009-10-30 11:49:00 +00006508 friend class Context;
6509};
6510
6511
6512/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006513 * A simple Maybe type, representing an object which may or may not have a
6514 * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html.
6515 *
6516 * If an API method returns a Maybe<>, the API method can potentially fail
6517 * either because an exception is thrown, or because an exception is pending,
6518 * e.g. because a previous API call threw an exception that hasn't been caught
6519 * yet, or because a TerminateExecution exception was thrown. In that case, a
6520 * "Nothing" value is returned.
6521 */
6522template <class T>
6523class Maybe {
6524 public:
6525 V8_INLINE bool IsNothing() const { return !has_value; }
6526 V8_INLINE bool IsJust() const { return has_value; }
6527
6528 // Will crash if the Maybe<> is nothing.
6529 V8_INLINE T FromJust() const {
6530 if (V8_UNLIKELY(!IsJust())) V8::FromJustIsNothing();
6531 return value;
6532 }
6533
6534 V8_INLINE T FromMaybe(const T& default_value) const {
6535 return has_value ? value : default_value;
6536 }
6537
6538 V8_INLINE bool operator==(const Maybe& other) const {
6539 return (IsJust() == other.IsJust()) &&
6540 (!IsJust() || FromJust() == other.FromJust());
6541 }
6542
6543 V8_INLINE bool operator!=(const Maybe& other) const {
6544 return !operator==(other);
6545 }
6546
6547 private:
6548 Maybe() : has_value(false) {}
6549 explicit Maybe(const T& t) : has_value(true), value(t) {}
6550
6551 bool has_value;
6552 T value;
6553
6554 template <class U>
6555 friend Maybe<U> Nothing();
6556 template <class U>
6557 friend Maybe<U> Just(const U& u);
6558};
6559
6560
6561template <class T>
6562inline Maybe<T> Nothing() {
6563 return Maybe<T>();
6564}
6565
6566
6567template <class T>
6568inline Maybe<T> Just(const T& t) {
6569 return Maybe<T>(t);
6570}
6571
6572
6573/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006574 * An external exception handler.
6575 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006576class V8_EXPORT TryCatch {
Steve Blocka7e24c12009-10-30 11:49:00 +00006577 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00006578 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006579 * Creates a new try/catch block and registers it with v8. Note that
6580 * all TryCatch blocks should be stack allocated because the memory
6581 * location itself is compared against JavaScript try/catch blocks.
Steve Blocka7e24c12009-10-30 11:49:00 +00006582 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006583 V8_DEPRECATED("Use isolate version", TryCatch());
Steve Blocka7e24c12009-10-30 11:49:00 +00006584
6585 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006586 * Creates a new try/catch block and registers it with v8. Note that
6587 * all TryCatch blocks should be stack allocated because the memory
6588 * location itself is compared against JavaScript try/catch blocks.
6589 */
6590 TryCatch(Isolate* isolate);
6591
6592 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006593 * Unregisters and deletes this try/catch block.
6594 */
6595 ~TryCatch();
6596
6597 /**
6598 * Returns true if an exception has been caught by this try/catch block.
6599 */
6600 bool HasCaught() const;
6601
6602 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006603 * For certain types of exceptions, it makes no sense to continue execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006604 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006605 * If CanContinue returns false, the correct action is to perform any C++
6606 * cleanup needed and then return. If CanContinue returns false and
6607 * HasTerminated returns true, it is possible to call
6608 * CancelTerminateExecution in order to continue calling into the engine.
Steve Blocka7e24c12009-10-30 11:49:00 +00006609 */
6610 bool CanContinue() const;
6611
6612 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006613 * Returns true if an exception has been caught due to script execution
6614 * being terminated.
6615 *
6616 * There is no JavaScript representation of an execution termination
6617 * exception. Such exceptions are thrown when the TerminateExecution
6618 * methods are called to terminate a long-running script.
6619 *
6620 * If such an exception has been thrown, HasTerminated will return true,
6621 * indicating that it is possible to call CancelTerminateExecution in order
6622 * to continue calling into the engine.
6623 */
6624 bool HasTerminated() const;
6625
6626 /**
Steve Blockd0582a62009-12-15 09:54:21 +00006627 * Throws the exception caught by this TryCatch in a way that avoids
6628 * it being caught again by this same TryCatch. As with ThrowException
6629 * it is illegal to execute any JavaScript operations after calling
6630 * ReThrow; the caller must return immediately to where the exception
6631 * is caught.
6632 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006633 Local<Value> ReThrow();
Steve Blockd0582a62009-12-15 09:54:21 +00006634
6635 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006636 * Returns the exception caught by this try/catch block. If no exception has
6637 * been caught an empty handle is returned.
6638 *
6639 * The returned handle is valid until this TryCatch block has been destroyed.
6640 */
6641 Local<Value> Exception() const;
6642
6643 /**
6644 * Returns the .stack property of the thrown object. If no .stack
6645 * property is present an empty handle is returned.
6646 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006647 V8_DEPRECATE_SOON("Use maybe version.", Local<Value> StackTrace() const);
6648 V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace(
6649 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006650
6651 /**
6652 * Returns the message associated with this exception. If there is
6653 * no message associated an empty handle is returned.
6654 *
6655 * The returned handle is valid until this TryCatch block has been
6656 * destroyed.
6657 */
6658 Local<v8::Message> Message() const;
6659
6660 /**
6661 * Clears any exceptions that may have been caught by this try/catch block.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006662 * After this method has been called, HasCaught() will return false. Cancels
6663 * the scheduled exception if it is caught and ReThrow() is not called before.
Steve Blocka7e24c12009-10-30 11:49:00 +00006664 *
6665 * It is not necessary to clear a try/catch block before using it again; if
6666 * another exception is thrown the previously caught exception will just be
6667 * overwritten. However, it is often a good idea since it makes it easier
6668 * to determine which operation threw a given exception.
6669 */
6670 void Reset();
6671
6672 /**
6673 * Set verbosity of the external exception handler.
6674 *
6675 * By default, exceptions that are caught by an external exception
6676 * handler are not reported. Call SetVerbose with true on an
6677 * external exception handler to have exceptions caught by the
6678 * handler reported as if they were not caught.
6679 */
6680 void SetVerbose(bool value);
6681
6682 /**
6683 * Set whether or not this TryCatch should capture a Message object
6684 * which holds source information about where the exception
6685 * occurred. True by default.
6686 */
6687 void SetCaptureMessage(bool value);
6688
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006689 /**
6690 * There are cases when the raw address of C++ TryCatch object cannot be
6691 * used for comparisons with addresses into the JS stack. The cases are:
6692 * 1) ARM, ARM64 and MIPS simulators which have separate JS stack.
6693 * 2) Address sanitizer allocates local C++ object in the heap when
6694 * UseAfterReturn mode is enabled.
6695 * This method returns address that can be used for comparisons with
6696 * addresses into the JS stack. When neither simulator nor ASAN's
6697 * UseAfterReturn is enabled, then the address returned will be the address
6698 * of the C++ try catch handler itself.
6699 */
6700 static void* JSStackComparableAddress(v8::TryCatch* handler) {
6701 if (handler == NULL) return NULL;
6702 return handler->js_stack_comparable_address_;
6703 }
6704
Steve Blockd0582a62009-12-15 09:54:21 +00006705 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006706 void ResetInternal();
6707
6708 // Make it hard to create heap-allocated TryCatch blocks.
6709 TryCatch(const TryCatch&);
6710 void operator=(const TryCatch&);
6711 void* operator new(size_t size);
6712 void operator delete(void*, size_t);
6713
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006714 v8::internal::Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006715 v8::TryCatch* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006716 void* exception_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006717 void* message_obj_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006718 void* js_stack_comparable_address_;
Steve Blockd0582a62009-12-15 09:54:21 +00006719 bool is_verbose_ : 1;
6720 bool can_continue_ : 1;
6721 bool capture_message_ : 1;
6722 bool rethrow_ : 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006723 bool has_terminated_ : 1;
Steve Blockd0582a62009-12-15 09:54:21 +00006724
Steve Block44f0eee2011-05-26 01:26:41 +01006725 friend class v8::internal::Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +00006726};
6727
6728
Ben Murdoch257744e2011-11-30 15:57:28 +00006729// --- Context ---
Steve Blocka7e24c12009-10-30 11:49:00 +00006730
6731
6732/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006733 * A container for extension names.
Steve Blocka7e24c12009-10-30 11:49:00 +00006734 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006735class V8_EXPORT ExtensionConfiguration {
Steve Blocka7e24c12009-10-30 11:49:00 +00006736 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006737 ExtensionConfiguration() : name_count_(0), names_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +00006738 ExtensionConfiguration(int name_count, const char* names[])
6739 : name_count_(name_count), names_(names) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006740
6741 const char** begin() const { return &names_[0]; }
6742 const char** end() const { return &names_[name_count_]; }
6743
Steve Blocka7e24c12009-10-30 11:49:00 +00006744 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006745 const int name_count_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006746 const char** names_;
6747};
6748
6749
6750/**
6751 * A sandboxed execution context with its own set of built-in objects
6752 * and functions.
6753 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006754class V8_EXPORT Context {
Steve Blocka7e24c12009-10-30 11:49:00 +00006755 public:
Steve Block1e0659c2011-05-24 12:43:12 +01006756 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006757 * Returns the global proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01006758 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006759 * Global proxy object is a thin wrapper whose prototype points to actual
6760 * context's global object with the properties like Object, etc. This is done
6761 * that way for security reasons (for more details see
Steve Block1e0659c2011-05-24 12:43:12 +01006762 * https://wiki.mozilla.org/Gecko:SplitWindow).
6763 *
6764 * Please note that changes to global proxy object prototype most probably
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006765 * would break VM---v8 expects only global object as a prototype of global
6766 * proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01006767 */
Steve Blocka7e24c12009-10-30 11:49:00 +00006768 Local<Object> Global();
6769
6770 /**
6771 * Detaches the global object from its context before
6772 * the global object can be reused to create a new context.
6773 */
6774 void DetachGlobal();
6775
Andrei Popescu74b3c142010-03-29 12:03:09 +01006776 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006777 * Creates a new context and returns a handle to the newly allocated
6778 * context.
Andrei Popescu74b3c142010-03-29 12:03:09 +01006779 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006780 * \param isolate The isolate in which to create the context.
Steve Block9fac8402011-05-12 15:51:54 +01006781 *
6782 * \param extensions An optional extension configuration containing
6783 * the extensions to be installed in the newly created context.
6784 *
6785 * \param global_template An optional object template from which the
6786 * global object for the newly created context will be created.
6787 *
6788 * \param global_object An optional global object to be reused for
6789 * the newly created context. This global object must have been
6790 * created by a previous call to Context::New with the same global
6791 * template. The state of the global object will be completely reset
6792 * and only object identify will remain.
Leon Clarkef7060e22010-06-03 12:02:55 +01006793 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006794 static Local<Context> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006795 Isolate* isolate, ExtensionConfiguration* extensions = NULL,
6796 Local<ObjectTemplate> global_template = Local<ObjectTemplate>(),
6797 Local<Value> global_object = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00006798
Steve Blocka7e24c12009-10-30 11:49:00 +00006799 /**
6800 * Sets the security token for the context. To access an object in
6801 * another context, the security tokens must match.
6802 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006803 void SetSecurityToken(Local<Value> token);
Steve Blocka7e24c12009-10-30 11:49:00 +00006804
6805 /** Restores the security token to the default value. */
6806 void UseDefaultSecurityToken();
6807
6808 /** Returns the security token of this context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006809 Local<Value> GetSecurityToken();
Steve Blocka7e24c12009-10-30 11:49:00 +00006810
6811 /**
6812 * Enter this context. After entering a context, all code compiled
6813 * and run is compiled and run in this context. If another context
6814 * is already entered, this old context is saved so it can be
6815 * restored when the new context is exited.
6816 */
6817 void Enter();
6818
6819 /**
6820 * Exit this context. Exiting the current context restores the
6821 * context that was in place when entering the current context.
6822 */
6823 void Exit();
6824
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006825 /** Returns an isolate associated with a current context. */
6826 v8::Isolate* GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00006827
6828 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006829 * The field at kDebugIdIndex is reserved for V8 debugger implementation.
6830 * The value is propagated to the scripts compiled in given Context and
6831 * can be used for filtering scripts.
6832 */
6833 enum EmbedderDataFields { kDebugIdIndex = 0 };
6834
6835 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006836 * Gets the embedder data with the given index, which must have been set by a
6837 * previous call to SetEmbedderData with the same index. Note that index 0
6838 * currently has a special meaning for Chrome's debugger.
Steve Blocka7e24c12009-10-30 11:49:00 +00006839 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006840 V8_INLINE Local<Value> GetEmbedderData(int index);
6841
6842 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006843 * Gets the binding object used by V8 extras. Extra natives get a reference
6844 * to this object and can use it to "export" functionality by adding
6845 * properties. Extra natives can also "import" functionality by accessing
6846 * properties added by the embedder using the V8 API.
6847 */
6848 Local<Object> GetExtrasBindingObject();
6849
6850 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006851 * Sets the embedder data with the given index, growing the data as
6852 * needed. Note that index 0 currently has a special meaning for Chrome's
6853 * debugger.
6854 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006855 void SetEmbedderData(int index, Local<Value> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006856
6857 /**
6858 * Gets a 2-byte-aligned native pointer from the embedder data with the given
6859 * index, which must have bees set by a previous call to
6860 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
6861 * currently has a special meaning for Chrome's debugger.
6862 */
6863 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
6864
6865 /**
6866 * Sets a 2-byte-aligned native pointer in the embedder data with the given
6867 * index, growing the data as needed. Note that index 0 currently has a
6868 * special meaning for Chrome's debugger.
6869 */
6870 void SetAlignedPointerInEmbedderData(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006871
6872 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00006873 * Control whether code generation from strings is allowed. Calling
6874 * this method with false will disable 'eval' and the 'Function'
6875 * constructor for code running in this context. If 'eval' or the
6876 * 'Function' constructor are used an exception will be thrown.
6877 *
6878 * If code generation from strings is not allowed the
6879 * V8::AllowCodeGenerationFromStrings callback will be invoked if
6880 * set before blocking the call to 'eval' or the 'Function'
6881 * constructor. If that callback returns true, the call will be
6882 * allowed, otherwise an exception will be thrown. If no callback is
6883 * set an exception will be thrown.
6884 */
6885 void AllowCodeGenerationFromStrings(bool allow);
6886
6887 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006888 * Returns true if code generation from strings is allowed for the context.
6889 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
6890 */
6891 bool IsCodeGenerationFromStringsAllowed();
6892
6893 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006894 * Sets the error description for the exception that is thrown when
6895 * code generation from strings is not allowed and 'eval' or the 'Function'
6896 * constructor are called.
6897 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006898 void SetErrorMessageForCodeGenerationFromStrings(Local<String> message);
6899
6900 /**
6901 * Estimate the memory in bytes retained by this context.
6902 */
6903 size_t EstimatedSize();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006904
6905 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006906 * Stack-allocated class which sets the execution context for all
6907 * operations executed within a local scope.
6908 */
Steve Block8defd9f2010-07-08 12:39:36 +01006909 class Scope {
Steve Blocka7e24c12009-10-30 11:49:00 +00006910 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006911 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006912 context_->Enter();
6913 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006914 V8_INLINE ~Scope() { context_->Exit(); }
6915
Steve Blocka7e24c12009-10-30 11:49:00 +00006916 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006917 Local<Context> context_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006918 };
6919
6920 private:
6921 friend class Value;
6922 friend class Script;
6923 friend class Object;
6924 friend class Function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006925
6926 Local<Value> SlowGetEmbedderData(int index);
6927 void* SlowGetAlignedPointerFromEmbedderData(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006928};
6929
6930
6931/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006932 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
6933 * to use any given V8 isolate, see the comments in the Isolate class. The
6934 * definition of 'using a V8 isolate' includes accessing handles or holding onto
6935 * object pointers obtained from V8 handles while in the particular V8 isolate.
6936 * It is up to the user of V8 to ensure, perhaps with locking, that this
6937 * constraint is not violated. In addition to any other synchronization
6938 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
6939 * used to signal thead switches to V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00006940 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006941 * v8::Locker is a scoped lock object. While it's active, i.e. between its
6942 * construction and destruction, the current thread is allowed to use the locked
6943 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
6944 * any time. In other words, the scope of a v8::Locker is a critical section.
Steve Block44f0eee2011-05-26 01:26:41 +01006945 *
Ben Murdoch257744e2011-11-30 15:57:28 +00006946 * Sample usage:
6947* \code
Steve Blocka7e24c12009-10-30 11:49:00 +00006948 * ...
6949 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00006950 * v8::Locker locker(isolate);
6951 * v8::Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00006952 * ...
Ben Murdoch257744e2011-11-30 15:57:28 +00006953 * // Code using V8 and isolate goes here.
Steve Blocka7e24c12009-10-30 11:49:00 +00006954 * ...
6955 * } // Destructor called here
6956 * \endcode
6957 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006958 * If you wish to stop using V8 in a thread A you can do this either by
6959 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
6960 * object:
Steve Blocka7e24c12009-10-30 11:49:00 +00006961 *
6962 * \code
6963 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00006964 * isolate->Exit();
6965 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00006966 * ...
6967 * // Code not using V8 goes here while V8 can run in another thread.
6968 * ...
6969 * } // Destructor called here.
Ben Murdoch257744e2011-11-30 15:57:28 +00006970 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00006971 * \endcode
6972 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006973 * The Unlocker object is intended for use in a long-running callback from V8,
6974 * where you want to release the V8 lock for other threads to use.
Steve Blocka7e24c12009-10-30 11:49:00 +00006975 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006976 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
6977 * given thread. This can be useful if you have code that can be called either
6978 * from code that holds the lock or from code that does not. The Unlocker is
6979 * not recursive so you can not have several Unlockers on the stack at once, and
6980 * you can not use an Unlocker in a thread that is not inside a Locker's scope.
Steve Blocka7e24c12009-10-30 11:49:00 +00006981 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006982 * An unlocker will unlock several lockers if it has to and reinstate the
6983 * correct depth of locking on its destruction, e.g.:
Steve Blocka7e24c12009-10-30 11:49:00 +00006984 *
6985 * \code
6986 * // V8 not locked.
6987 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00006988 * v8::Locker locker(isolate);
6989 * Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00006990 * // V8 locked.
6991 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00006992 * v8::Locker another_locker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00006993 * // V8 still locked (2 levels).
6994 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00006995 * isolate->Exit();
6996 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00006997 * // V8 not locked.
6998 * }
Ben Murdoch257744e2011-11-30 15:57:28 +00006999 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00007000 * // V8 locked again (2 levels).
7001 * }
7002 * // V8 still locked (1 level).
7003 * }
7004 * // V8 Now no longer locked.
7005 * \endcode
7006 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007007class V8_EXPORT Unlocker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007008 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007009 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007010 * Initialize Unlocker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007011 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007012 V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); }
7013
Steve Blocka7e24c12009-10-30 11:49:00 +00007014 ~Unlocker();
Ben Murdoch257744e2011-11-30 15:57:28 +00007015 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007016 void Initialize(Isolate* isolate);
7017
Ben Murdoch257744e2011-11-30 15:57:28 +00007018 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007019};
7020
7021
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007022class V8_EXPORT Locker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007023 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007024 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007025 * Initialize Locker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007026 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007027 V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); }
7028
Steve Blocka7e24c12009-10-30 11:49:00 +00007029 ~Locker();
7030
7031 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007032 * Returns whether or not the locker for a given isolate, is locked by the
7033 * current thread.
Steve Blocka7e24c12009-10-30 11:49:00 +00007034 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007035 static bool IsLocked(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007036
7037 /**
7038 * Returns whether v8::Locker is being used by this V8 instance.
7039 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007040 static bool IsActive();
Steve Blocka7e24c12009-10-30 11:49:00 +00007041
7042 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007043 void Initialize(Isolate* isolate);
7044
Steve Blocka7e24c12009-10-30 11:49:00 +00007045 bool has_lock_;
7046 bool top_level_;
Ben Murdoch257744e2011-11-30 15:57:28 +00007047 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007048
Steve Blocka7e24c12009-10-30 11:49:00 +00007049 // Disallow copying and assigning.
7050 Locker(const Locker&);
7051 void operator=(const Locker&);
7052};
7053
7054
Ben Murdoch257744e2011-11-30 15:57:28 +00007055// --- Implementation ---
Steve Blocka7e24c12009-10-30 11:49:00 +00007056
7057
7058namespace internal {
7059
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007060const int kApiPointerSize = sizeof(void*); // NOLINT
7061const int kApiIntSize = sizeof(int); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007062const int kApiInt64Size = sizeof(int64_t); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00007063
7064// Tag information for HeapObject.
7065const int kHeapObjectTag = 1;
7066const int kHeapObjectTagSize = 2;
7067const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
7068
Steve Blocka7e24c12009-10-30 11:49:00 +00007069// Tag information for Smi.
7070const int kSmiTag = 0;
7071const int kSmiTagSize = 1;
7072const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
7073
Ben Murdochb8e0da22011-05-16 14:20:40 +01007074template <size_t ptr_size> struct SmiTagging;
Steve Block3ce2e202009-11-05 08:53:23 +00007075
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007076template<int kSmiShiftSize>
7077V8_INLINE internal::Object* IntToSmi(int value) {
7078 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
7079 uintptr_t tagged_value =
7080 (static_cast<uintptr_t>(value) << smi_shift_bits) | kSmiTag;
7081 return reinterpret_cast<internal::Object*>(tagged_value);
7082}
7083
Steve Block3ce2e202009-11-05 08:53:23 +00007084// Smi constants for 32-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007085template <> struct SmiTagging<4> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007086 enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
7087 static int SmiShiftSize() { return kSmiShiftSize; }
7088 static int SmiValueSize() { return kSmiValueSize; }
7089 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007090 int shift_bits = kSmiTagSize + kSmiShiftSize;
7091 // Throw away top 32 bits and shift down (requires >> to be sign extending).
7092 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
7093 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007094 V8_INLINE static internal::Object* IntToSmi(int value) {
7095 return internal::IntToSmi<kSmiShiftSize>(value);
7096 }
7097 V8_INLINE static bool IsValidSmi(intptr_t value) {
7098 // To be representable as an tagged small integer, the two
7099 // most-significant bits of 'value' must be either 00 or 11 due to
7100 // sign-extension. To check this we add 01 to the two
7101 // most-significant bits, and check if the most-significant bit is 0
7102 //
7103 // CAUTION: The original code below:
7104 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
7105 // may lead to incorrect results according to the C language spec, and
7106 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
7107 // compiler may produce undefined results in case of signed integer
7108 // overflow. The computation must be done w/ unsigned ints.
7109 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
7110 }
Steve Block3ce2e202009-11-05 08:53:23 +00007111};
7112
7113// Smi constants for 64-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007114template <> struct SmiTagging<8> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007115 enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
7116 static int SmiShiftSize() { return kSmiShiftSize; }
7117 static int SmiValueSize() { return kSmiValueSize; }
7118 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007119 int shift_bits = kSmiTagSize + kSmiShiftSize;
7120 // Shift down and throw away top 32 bits.
7121 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
7122 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007123 V8_INLINE static internal::Object* IntToSmi(int value) {
7124 return internal::IntToSmi<kSmiShiftSize>(value);
7125 }
7126 V8_INLINE static bool IsValidSmi(intptr_t value) {
7127 // To be representable as a long smi, the value must be a 32-bit integer.
7128 return (value == static_cast<int32_t>(value));
7129 }
Steve Block3ce2e202009-11-05 08:53:23 +00007130};
7131
Ben Murdochb8e0da22011-05-16 14:20:40 +01007132typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
7133const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
7134const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007135V8_INLINE static bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
7136V8_INLINE static bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
Steve Blockd0582a62009-12-15 09:54:21 +00007137
Steve Blocka7e24c12009-10-30 11:49:00 +00007138/**
7139 * This class exports constants and functionality from within v8 that
7140 * is necessary to implement inline functions in the v8 api. Don't
7141 * depend on functions and constants defined here.
7142 */
7143class Internals {
7144 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00007145 // These values match non-compiler-dependent values defined within
7146 // the implementation of v8.
7147 static const int kHeapObjectMapOffset = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007148 static const int kMapInstanceTypeAndBitFieldOffset =
7149 1 * kApiPointerSize + kApiIntSize;
7150 static const int kStringResourceOffset = 3 * kApiPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00007151
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007152 static const int kOddballKindOffset = 4 * kApiPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00007153 static const int kForeignAddressOffset = kApiPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007154 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007155 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
7156 static const int kContextHeaderSize = 2 * kApiPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007157 static const int kContextEmbedderDataIndex = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00007158 static const int kFullStringRepresentationMask = 0x07;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007159 static const int kStringEncodingMask = 0x4;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007160 static const int kExternalTwoByteRepresentationTag = 0x02;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007161 static const int kExternalOneByteRepresentationTag = 0x06;
Steve Blocka7e24c12009-10-30 11:49:00 +00007162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007163 static const int kIsolateEmbedderDataOffset = 0 * kApiPointerSize;
7164 static const int kAmountOfExternalAllocatedMemoryOffset =
7165 4 * kApiPointerSize;
7166 static const int kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset =
7167 kAmountOfExternalAllocatedMemoryOffset + kApiInt64Size;
7168 static const int kIsolateRootsOffset =
7169 kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset + kApiInt64Size +
7170 kApiPointerSize;
7171 static const int kUndefinedValueRootIndex = 5;
7172 static const int kNullValueRootIndex = 7;
7173 static const int kTrueValueRootIndex = 8;
7174 static const int kFalseValueRootIndex = 9;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007175 static const int kEmptyStringRootIndex = 10;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007176
7177 // The external allocation limit should be below 256 MB on all architectures
7178 // to avoid that resource-constrained embedders run low on memory.
7179 static const int kExternalAllocationLimit = 192 * 1024 * 1024;
7180
7181 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
7182 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007183 static const int kNodeStateMask = 0x7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007184 static const int kNodeStateIsWeakValue = 2;
7185 static const int kNodeStateIsPendingValue = 3;
7186 static const int kNodeStateIsNearDeathValue = 4;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007187 static const int kNodeIsIndependentShift = 3;
7188 static const int kNodeIsPartiallyDependentShift = 4;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007189 static const int kNodeIsActiveShift = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007190
Ben Murdoch097c5b22016-05-18 11:27:45 +01007191 static const int kJSObjectType = 0xb5;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01007192 static const int kFirstNonstringType = 0x80;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007193 static const int kOddballType = 0x83;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007194 static const int kForeignType = 0x87;
Steve Blocka7e24c12009-10-30 11:49:00 +00007195
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007196 static const int kUndefinedOddballKind = 5;
7197 static const int kNullOddballKind = 3;
7198
7199 static const uint32_t kNumIsolateDataSlots = 4;
7200
7201 V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
7202 V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
7203#ifdef V8_ENABLE_CHECKS
7204 CheckInitializedImpl(isolate);
7205#endif
7206 }
7207
7208 V8_INLINE static bool HasHeapObjectTag(const internal::Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007209 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
7210 kHeapObjectTag);
7211 }
7212
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007213 V8_INLINE static int SmiValue(const internal::Object* value) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01007214 return PlatformSmiTagging::SmiToInt(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007215 }
7216
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007217 V8_INLINE static internal::Object* IntToSmi(int value) {
7218 return PlatformSmiTagging::IntToSmi(value);
7219 }
7220
7221 V8_INLINE static bool IsValidSmi(intptr_t value) {
7222 return PlatformSmiTagging::IsValidSmi(value);
7223 }
7224
7225 V8_INLINE static int GetInstanceType(const internal::Object* obj) {
Steve Block3ce2e202009-11-05 08:53:23 +00007226 typedef internal::Object O;
7227 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007228 // Map::InstanceType is defined so that it will always be loaded into
7229 // the LS 8 bits of one 16-bit word, regardless of endianess.
7230 return ReadField<uint16_t>(map, kMapInstanceTypeAndBitFieldOffset) & 0xff;
Steve Block3ce2e202009-11-05 08:53:23 +00007231 }
7232
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007233 V8_INLINE static int GetOddballKind(const internal::Object* obj) {
7234 typedef internal::Object O;
7235 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +01007236 }
7237
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007238 V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007239 int representation = (instance_type & kFullStringRepresentationMask);
7240 return representation == kExternalTwoByteRepresentationTag;
7241 }
7242
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007243 V8_INLINE static uint8_t GetNodeFlag(internal::Object** obj, int shift) {
7244 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7245 return *addr & static_cast<uint8_t>(1U << shift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007246 }
Steve Block44f0eee2011-05-26 01:26:41 +01007247
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007248 V8_INLINE static void UpdateNodeFlag(internal::Object** obj,
7249 bool value, int shift) {
7250 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7251 uint8_t mask = static_cast<uint8_t>(1U << shift);
7252 *addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
7253 }
7254
7255 V8_INLINE static uint8_t GetNodeState(internal::Object** obj) {
7256 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7257 return *addr & kNodeStateMask;
7258 }
7259
7260 V8_INLINE static void UpdateNodeState(internal::Object** obj,
7261 uint8_t value) {
7262 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7263 *addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
7264 }
7265
7266 V8_INLINE static void SetEmbedderData(v8::Isolate* isolate,
7267 uint32_t slot,
7268 void* data) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007269 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007270 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7271 *reinterpret_cast<void**>(addr) = data;
7272 }
7273
7274 V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
7275 uint32_t slot) {
7276 const uint8_t* addr = reinterpret_cast<const uint8_t*>(isolate) +
7277 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7278 return *reinterpret_cast<void* const*>(addr);
7279 }
7280
7281 V8_INLINE static internal::Object** GetRoot(v8::Isolate* isolate,
7282 int index) {
7283 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
7284 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
7285 }
7286
7287 template <typename T>
7288 V8_INLINE static T ReadField(const internal::Object* ptr, int offset) {
7289 const uint8_t* addr =
7290 reinterpret_cast<const uint8_t*>(ptr) + offset - kHeapObjectTag;
7291 return *reinterpret_cast<const T*>(addr);
7292 }
7293
7294 template <typename T>
7295 V8_INLINE static T ReadEmbedderData(const v8::Context* context, int index) {
7296 typedef internal::Object O;
7297 typedef internal::Internals I;
7298 O* ctx = *reinterpret_cast<O* const*>(context);
7299 int embedder_data_offset = I::kContextHeaderSize +
7300 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
7301 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
7302 int value_offset =
7303 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
7304 return I::ReadField<T>(embedder_data, value_offset);
7305 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007306};
7307
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007308} // namespace internal
Steve Blocka7e24c12009-10-30 11:49:00 +00007309
7310
7311template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007312Local<T> Local<T>::New(Isolate* isolate, Local<T> that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007313 return New(isolate, that.val_);
7314}
7315
7316template <class T>
7317Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) {
7318 return New(isolate, that.val_);
7319}
7320
Steve Blocka7e24c12009-10-30 11:49:00 +00007321
7322template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007323Local<T> Local<T>::New(Isolate* isolate, T* that) {
7324 if (that == NULL) return Local<T>();
7325 T* that_ptr = that;
7326 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
7327 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
7328 reinterpret_cast<internal::Isolate*>(isolate), *p)));
7329}
7330
7331
7332template<class T>
7333template<class S>
7334void Eternal<T>::Set(Isolate* isolate, Local<S> handle) {
7335 TYPE_CHECK(T, S);
7336 V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle), &this->index_);
7337}
7338
7339
7340template<class T>
7341Local<T> Eternal<T>::Get(Isolate* isolate) {
7342 return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_)));
Steve Blocka7e24c12009-10-30 11:49:00 +00007343}
7344
7345
7346template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007347Local<T> MaybeLocal<T>::ToLocalChecked() {
7348 if (V8_UNLIKELY(val_ == nullptr)) V8::ToLocalEmpty();
7349 return Local<T>(val_);
7350}
7351
7352
7353template <class T>
7354void* WeakCallbackInfo<T>::GetInternalField(int index) const {
7355#ifdef V8_ENABLE_CHECKS
7356 if (index < 0 || index >= kInternalFieldsInWeakCallback) {
7357 V8::InternalFieldOutOfBounds(index);
7358 }
7359#endif
7360 return internal_fields_[index];
7361}
7362
7363
7364template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007365T* PersistentBase<T>::New(Isolate* isolate, T* that) {
7366 if (that == NULL) return NULL;
7367 internal::Object** p = reinterpret_cast<internal::Object**>(that);
7368 return reinterpret_cast<T*>(
7369 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
7370 p));
7371}
7372
7373
7374template <class T, class M>
7375template <class S, class M2>
7376void Persistent<T, M>::Copy(const Persistent<S, M2>& that) {
7377 TYPE_CHECK(T, S);
7378 this->Reset();
7379 if (that.IsEmpty()) return;
7380 internal::Object** p = reinterpret_cast<internal::Object**>(that.val_);
7381 this->val_ = reinterpret_cast<T*>(V8::CopyPersistent(p));
7382 M::Copy(that, this);
7383}
7384
7385
7386template <class T>
7387bool PersistentBase<T>::IsIndependent() const {
7388 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007389 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007390 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7391 I::kNodeIsIndependentShift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007392}
7393
7394
7395template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007396bool PersistentBase<T>::IsNearDeath() const {
7397 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007398 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007399 uint8_t node_state =
7400 I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_));
7401 return node_state == I::kNodeStateIsNearDeathValue ||
7402 node_state == I::kNodeStateIsPendingValue;
Steve Blocka7e24c12009-10-30 11:49:00 +00007403}
7404
7405
7406template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007407bool PersistentBase<T>::IsWeak() const {
7408 typedef internal::Internals I;
7409 if (this->IsEmpty()) return false;
7410 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
7411 I::kNodeStateIsWeakValue;
7412}
7413
7414
7415template <class T>
7416void PersistentBase<T>::Reset() {
Steve Blocka7e24c12009-10-30 11:49:00 +00007417 if (this->IsEmpty()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007418 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
7419 val_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00007420}
7421
7422
7423template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007424template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007425void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007426 TYPE_CHECK(T, S);
7427 Reset();
7428 if (other.IsEmpty()) return;
7429 this->val_ = New(isolate, other.val_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007430}
7431
Steve Blocka7e24c12009-10-30 11:49:00 +00007432
Steve Block44f0eee2011-05-26 01:26:41 +01007433template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007434template <class S>
7435void PersistentBase<T>::Reset(Isolate* isolate,
7436 const PersistentBase<S>& other) {
7437 TYPE_CHECK(T, S);
7438 Reset();
7439 if (other.IsEmpty()) return;
7440 this->val_ = New(isolate, other.val_);
Ben Murdoch257744e2011-11-30 15:57:28 +00007441}
7442
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007443
Ben Murdoch257744e2011-11-30 15:57:28 +00007444template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007445template <typename S, typename P>
7446void PersistentBase<T>::SetWeak(
7447 P* parameter,
7448 typename WeakCallbackData<S, P>::Callback callback) {
7449 TYPE_CHECK(S, T);
7450 typedef typename WeakCallbackData<Value, void>::Callback Callback;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007451 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007452 reinterpret_cast<Callback>(callback));
Steve Block44f0eee2011-05-26 01:26:41 +01007453}
Steve Block8defd9f2010-07-08 12:39:36 +01007454
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007455
7456template <class T>
7457template <typename P>
7458void PersistentBase<T>::SetWeak(
7459 P* parameter,
7460 typename WeakCallbackData<T, P>::Callback callback) {
7461 SetWeak<T, P>(parameter, callback);
7462}
7463
7464
7465template <class T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007466template <typename P>
7467void PersistentBase<T>::SetPhantom(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007468 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7469 int internal_field_index1, int internal_field_index2) {
7470 typedef typename WeakCallbackInfo<void>::Callback Callback;
7471 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7472 internal_field_index1, internal_field_index2,
7473 reinterpret_cast<Callback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007474}
7475
7476
7477template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007478template <typename P>
7479V8_INLINE void PersistentBase<T>::SetWeak(
7480 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7481 WeakCallbackType type) {
7482 typedef typename WeakCallbackInfo<void>::Callback Callback;
7483 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7484 reinterpret_cast<Callback>(callback), type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007485}
7486
7487
7488template <class T>
7489template <typename P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007490P* PersistentBase<T>::ClearWeak() {
7491 return reinterpret_cast<P*>(
7492 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
7493}
7494
7495
7496template <class T>
7497void PersistentBase<T>::MarkIndependent() {
7498 typedef internal::Internals I;
7499 if (this->IsEmpty()) return;
7500 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7501 true,
7502 I::kNodeIsIndependentShift);
7503}
7504
7505
7506template <class T>
7507void PersistentBase<T>::MarkPartiallyDependent() {
7508 typedef internal::Internals I;
7509 if (this->IsEmpty()) return;
7510 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7511 true,
7512 I::kNodeIsPartiallyDependentShift);
7513}
7514
7515
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007516template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007517void PersistentBase<T>::MarkActive() {
7518 typedef internal::Internals I;
7519 if (this->IsEmpty()) return;
7520 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), true,
7521 I::kNodeIsActiveShift);
7522}
7523
7524
7525template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007526void PersistentBase<T>::SetWrapperClassId(uint16_t class_id) {
7527 typedef internal::Internals I;
7528 if (this->IsEmpty()) return;
7529 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7530 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7531 *reinterpret_cast<uint16_t*>(addr) = class_id;
7532}
7533
7534
7535template <class T>
7536uint16_t PersistentBase<T>::WrapperClassId() const {
7537 typedef internal::Internals I;
7538 if (this->IsEmpty()) return 0;
7539 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7540 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7541 return *reinterpret_cast<uint16_t*>(addr);
7542}
7543
7544
7545template<typename T>
7546ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
7547
7548template<typename T>
7549template<typename S>
7550void ReturnValue<T>::Set(const Persistent<S>& handle) {
7551 TYPE_CHECK(T, S);
7552 if (V8_UNLIKELY(handle.IsEmpty())) {
7553 *value_ = GetDefaultValue();
7554 } else {
7555 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7556 }
7557}
7558
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007559template <typename T>
7560template <typename S>
7561void ReturnValue<T>::Set(const Global<S>& handle) {
7562 TYPE_CHECK(T, S);
7563 if (V8_UNLIKELY(handle.IsEmpty())) {
7564 *value_ = GetDefaultValue();
7565 } else {
7566 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7567 }
7568}
7569
7570template <typename T>
7571template <typename S>
7572void ReturnValue<T>::Set(const Local<S> handle) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007573 TYPE_CHECK(T, S);
7574 if (V8_UNLIKELY(handle.IsEmpty())) {
7575 *value_ = GetDefaultValue();
7576 } else {
7577 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7578 }
7579}
7580
7581template<typename T>
7582void ReturnValue<T>::Set(double i) {
7583 TYPE_CHECK(T, Number);
7584 Set(Number::New(GetIsolate(), i));
7585}
7586
7587template<typename T>
7588void ReturnValue<T>::Set(int32_t i) {
7589 TYPE_CHECK(T, Integer);
7590 typedef internal::Internals I;
7591 if (V8_LIKELY(I::IsValidSmi(i))) {
7592 *value_ = I::IntToSmi(i);
7593 return;
7594 }
7595 Set(Integer::New(GetIsolate(), i));
7596}
7597
7598template<typename T>
7599void ReturnValue<T>::Set(uint32_t i) {
7600 TYPE_CHECK(T, Integer);
7601 // Can't simply use INT32_MAX here for whatever reason.
7602 bool fits_into_int32_t = (i & (1U << 31)) == 0;
7603 if (V8_LIKELY(fits_into_int32_t)) {
7604 Set(static_cast<int32_t>(i));
7605 return;
7606 }
7607 Set(Integer::NewFromUnsigned(GetIsolate(), i));
7608}
7609
7610template<typename T>
7611void ReturnValue<T>::Set(bool value) {
7612 TYPE_CHECK(T, Boolean);
7613 typedef internal::Internals I;
7614 int root_index;
7615 if (value) {
7616 root_index = I::kTrueValueRootIndex;
7617 } else {
7618 root_index = I::kFalseValueRootIndex;
7619 }
7620 *value_ = *I::GetRoot(GetIsolate(), root_index);
7621}
7622
7623template<typename T>
7624void ReturnValue<T>::SetNull() {
7625 TYPE_CHECK(T, Primitive);
7626 typedef internal::Internals I;
7627 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
7628}
7629
7630template<typename T>
7631void ReturnValue<T>::SetUndefined() {
7632 TYPE_CHECK(T, Primitive);
7633 typedef internal::Internals I;
7634 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
7635}
7636
7637template<typename T>
7638void ReturnValue<T>::SetEmptyString() {
7639 TYPE_CHECK(T, String);
7640 typedef internal::Internals I;
7641 *value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex);
7642}
7643
7644template<typename T>
7645Isolate* ReturnValue<T>::GetIsolate() {
7646 // Isolate is always the pointer below the default value on the stack.
7647 return *reinterpret_cast<Isolate**>(&value_[-2]);
7648}
7649
7650template<typename T>
7651template<typename S>
7652void ReturnValue<T>::Set(S* whatever) {
7653 // Uncompilable to prevent inadvertent misuse.
7654 TYPE_CHECK(S*, Primitive);
7655}
7656
7657template<typename T>
7658internal::Object* ReturnValue<T>::GetDefaultValue() {
7659 // Default value is always the pointer below value_ on the stack.
7660 return value_[-1];
7661}
7662
7663
7664template<typename T>
7665FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
7666 internal::Object** values,
7667 int length,
7668 bool is_construct_call)
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007669 : implicit_args_(implicit_args),
7670 values_(values),
7671 length_(length),
7672 is_construct_call_(is_construct_call) { }
Steve Block8defd9f2010-07-08 12:39:36 +01007673
7674
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007675template<typename T>
7676Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
7677 if (i < 0 || length_ <= i) return Local<Value>(*Undefined(GetIsolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +00007678 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
7679}
7680
7681
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007682template<typename T>
7683Local<Function> FunctionCallbackInfo<T>::Callee() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007684 return Local<Function>(reinterpret_cast<Function*>(
7685 &implicit_args_[kCalleeIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007686}
7687
7688
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007689template<typename T>
7690Local<Object> FunctionCallbackInfo<T>::This() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007691 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
7692}
7693
7694
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007695template<typename T>
7696Local<Object> FunctionCallbackInfo<T>::Holder() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007697 return Local<Object>(reinterpret_cast<Object*>(
7698 &implicit_args_[kHolderIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007699}
7700
7701
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007702template<typename T>
7703Local<Value> FunctionCallbackInfo<T>::Data() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007704 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007705}
7706
7707
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007708template<typename T>
7709Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
7710 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
7711}
7712
7713
7714template<typename T>
7715ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
7716 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
7717}
7718
7719
7720template<typename T>
7721bool FunctionCallbackInfo<T>::IsConstructCall() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007722 return is_construct_call_ & 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +00007723}
7724
7725
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007726template<typename T>
7727int FunctionCallbackInfo<T>::Length() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007728 return length_;
7729}
7730
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007731ScriptOrigin::ScriptOrigin(Local<Value> resource_name,
7732 Local<Integer> resource_line_offset,
7733 Local<Integer> resource_column_offset,
7734 Local<Boolean> resource_is_shared_cross_origin,
7735 Local<Integer> script_id,
7736 Local<Boolean> resource_is_embedder_debug_script,
7737 Local<Value> source_map_url,
7738 Local<Boolean> resource_is_opaque)
7739 : resource_name_(resource_name),
7740 resource_line_offset_(resource_line_offset),
7741 resource_column_offset_(resource_column_offset),
7742 options_(!resource_is_embedder_debug_script.IsEmpty() &&
7743 resource_is_embedder_debug_script->IsTrue(),
7744 !resource_is_shared_cross_origin.IsEmpty() &&
7745 resource_is_shared_cross_origin->IsTrue(),
7746 !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue()),
7747 script_id_(script_id),
7748 source_map_url_(source_map_url) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00007749
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007750Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00007751
7752
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007753Local<Integer> ScriptOrigin::ResourceLineOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007754 return resource_line_offset_;
7755}
7756
7757
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007758Local<Integer> ScriptOrigin::ResourceColumnOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007759 return resource_column_offset_;
7760}
7761
7762
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007763Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00007764
7765
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007766Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007767
7768
7769ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
7770 CachedData* data)
7771 : source_string(string),
7772 resource_name(origin.ResourceName()),
7773 resource_line_offset(origin.ResourceLineOffset()),
7774 resource_column_offset(origin.ResourceColumnOffset()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007775 resource_options(origin.Options()),
7776 source_map_url(origin.SourceMapUrl()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007777 cached_data(data) {}
7778
7779
7780ScriptCompiler::Source::Source(Local<String> string,
7781 CachedData* data)
7782 : source_string(string), cached_data(data) {}
7783
7784
7785ScriptCompiler::Source::~Source() {
7786 delete cached_data;
7787}
7788
7789
7790const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData()
7791 const {
7792 return cached_data;
7793}
7794
7795
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007796Local<Boolean> Boolean::New(Isolate* isolate, bool value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007797 return value ? True(isolate) : False(isolate);
7798}
7799
7800
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007801void Template::Set(Isolate* isolate, const char* name, v8::Local<Data> value) {
7802 Set(v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
7803 .ToLocalChecked(),
7804 value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007805}
7806
7807
7808Local<Value> Object::GetInternalField(int index) {
7809#ifndef V8_ENABLE_CHECKS
Steve Blocka7e24c12009-10-30 11:49:00 +00007810 typedef internal::Object O;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007811 typedef internal::HeapObject HO;
Steve Blocka7e24c12009-10-30 11:49:00 +00007812 typedef internal::Internals I;
7813 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007814 // Fast path: If the object is a plain JSObject, which is the common case, we
7815 // know where to find the internal fields and can return the value directly.
Steve Block3ce2e202009-11-05 08:53:23 +00007816 if (I::GetInstanceType(obj) == I::kJSObjectType) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007817 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Steve Blocka7e24c12009-10-30 11:49:00 +00007818 O* value = I::ReadField<O*>(obj, offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007819 O** result = HandleScope::CreateHandle(reinterpret_cast<HO*>(obj), value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007820 return Local<Value>(reinterpret_cast<Value*>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00007821 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007822#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007823 return SlowGetInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00007824}
7825
7826
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007827void* Object::GetAlignedPointerFromInternalField(int index) {
7828#ifndef V8_ENABLE_CHECKS
Steve Block3ce2e202009-11-05 08:53:23 +00007829 typedef internal::Object O;
7830 typedef internal::Internals I;
Steve Block3ce2e202009-11-05 08:53:23 +00007831 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007832 // Fast path: If the object is a plain JSObject, which is the common case, we
7833 // know where to find the internal fields and can return the value directly.
7834 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007835 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007836 return I::ReadField<void*>(obj, offset);
Steve Block3ce2e202009-11-05 08:53:23 +00007837 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007838#endif
7839 return SlowGetAlignedPointerFromInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00007840}
7841
7842
7843String* String::Cast(v8::Value* value) {
7844#ifdef V8_ENABLE_CHECKS
7845 CheckCast(value);
7846#endif
7847 return static_cast<String*>(value);
7848}
7849
7850
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007851Local<String> String::Empty(Isolate* isolate) {
7852 typedef internal::Object* S;
7853 typedef internal::Internals I;
7854 I::CheckInitialized(isolate);
7855 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
7856 return Local<String>(reinterpret_cast<String*>(slot));
7857}
7858
7859
Steve Blocka7e24c12009-10-30 11:49:00 +00007860String::ExternalStringResource* String::GetExternalStringResource() const {
7861 typedef internal::Object O;
7862 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007863 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00007864 String::ExternalStringResource* result;
Steve Block3ce2e202009-11-05 08:53:23 +00007865 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007866 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
7867 result = reinterpret_cast<String::ExternalStringResource*>(value);
7868 } else {
7869 result = NULL;
7870 }
7871#ifdef V8_ENABLE_CHECKS
7872 VerifyExternalStringResource(result);
7873#endif
7874 return result;
7875}
7876
7877
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007878String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
7879 String::Encoding* encoding_out) const {
7880 typedef internal::Object O;
7881 typedef internal::Internals I;
7882 O* obj = *reinterpret_cast<O* const*>(this);
7883 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
7884 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
7885 ExternalStringResourceBase* resource = NULL;
7886 if (type == I::kExternalOneByteRepresentationTag ||
7887 type == I::kExternalTwoByteRepresentationTag) {
7888 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
7889 resource = static_cast<ExternalStringResourceBase*>(value);
7890 }
7891#ifdef V8_ENABLE_CHECKS
7892 VerifyExternalStringResourceBase(resource, *encoding_out);
7893#endif
7894 return resource;
7895}
7896
7897
7898bool Value::IsUndefined() const {
7899#ifdef V8_ENABLE_CHECKS
7900 return FullIsUndefined();
7901#else
7902 return QuickIsUndefined();
7903#endif
7904}
7905
7906bool Value::QuickIsUndefined() const {
7907 typedef internal::Object O;
7908 typedef internal::Internals I;
7909 O* obj = *reinterpret_cast<O* const*>(this);
7910 if (!I::HasHeapObjectTag(obj)) return false;
7911 if (I::GetInstanceType(obj) != I::kOddballType) return false;
7912 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
7913}
7914
7915
7916bool Value::IsNull() const {
7917#ifdef V8_ENABLE_CHECKS
7918 return FullIsNull();
7919#else
7920 return QuickIsNull();
7921#endif
7922}
7923
7924bool Value::QuickIsNull() const {
7925 typedef internal::Object O;
7926 typedef internal::Internals I;
7927 O* obj = *reinterpret_cast<O* const*>(this);
7928 if (!I::HasHeapObjectTag(obj)) return false;
7929 if (I::GetInstanceType(obj) != I::kOddballType) return false;
7930 return (I::GetOddballKind(obj) == I::kNullOddballKind);
7931}
7932
7933
Steve Blocka7e24c12009-10-30 11:49:00 +00007934bool Value::IsString() const {
7935#ifdef V8_ENABLE_CHECKS
7936 return FullIsString();
7937#else
7938 return QuickIsString();
7939#endif
7940}
7941
7942bool Value::QuickIsString() const {
7943 typedef internal::Object O;
7944 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007945 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00007946 if (!I::HasHeapObjectTag(obj)) return false;
Steve Block3ce2e202009-11-05 08:53:23 +00007947 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
Steve Blocka7e24c12009-10-30 11:49:00 +00007948}
7949
7950
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007951template <class T> Value* Value::Cast(T* value) {
7952 return static_cast<Value*>(value);
7953}
7954
7955
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007956Local<Boolean> Value::ToBoolean() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007957 return ToBoolean(Isolate::GetCurrent()->GetCurrentContext())
7958 .FromMaybe(Local<Boolean>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007959}
7960
7961
7962Local<Number> Value::ToNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007963 return ToNumber(Isolate::GetCurrent()->GetCurrentContext())
7964 .FromMaybe(Local<Number>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007965}
7966
7967
7968Local<String> Value::ToString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007969 return ToString(Isolate::GetCurrent()->GetCurrentContext())
7970 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007971}
7972
7973
7974Local<String> Value::ToDetailString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007975 return ToDetailString(Isolate::GetCurrent()->GetCurrentContext())
7976 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007977}
7978
7979
7980Local<Object> Value::ToObject() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007981 return ToObject(Isolate::GetCurrent()->GetCurrentContext())
7982 .FromMaybe(Local<Object>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007983}
7984
7985
7986Local<Integer> Value::ToInteger() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007987 return ToInteger(Isolate::GetCurrent()->GetCurrentContext())
7988 .FromMaybe(Local<Integer>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007989}
7990
7991
7992Local<Uint32> Value::ToUint32() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007993 return ToUint32(Isolate::GetCurrent()->GetCurrentContext())
7994 .FromMaybe(Local<Uint32>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007995}
7996
7997
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007998Local<Int32> Value::ToInt32() const {
7999 return ToInt32(Isolate::GetCurrent()->GetCurrentContext())
8000 .FromMaybe(Local<Int32>());
8001}
8002
8003
8004Boolean* Boolean::Cast(v8::Value* value) {
8005#ifdef V8_ENABLE_CHECKS
8006 CheckCast(value);
8007#endif
8008 return static_cast<Boolean*>(value);
8009}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008010
8011
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008012Name* Name::Cast(v8::Value* value) {
8013#ifdef V8_ENABLE_CHECKS
8014 CheckCast(value);
8015#endif
8016 return static_cast<Name*>(value);
8017}
8018
8019
8020Symbol* Symbol::Cast(v8::Value* value) {
8021#ifdef V8_ENABLE_CHECKS
8022 CheckCast(value);
8023#endif
8024 return static_cast<Symbol*>(value);
8025}
8026
8027
Steve Blocka7e24c12009-10-30 11:49:00 +00008028Number* Number::Cast(v8::Value* value) {
8029#ifdef V8_ENABLE_CHECKS
8030 CheckCast(value);
8031#endif
8032 return static_cast<Number*>(value);
8033}
8034
8035
8036Integer* Integer::Cast(v8::Value* value) {
8037#ifdef V8_ENABLE_CHECKS
8038 CheckCast(value);
8039#endif
8040 return static_cast<Integer*>(value);
8041}
8042
8043
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008044Int32* Int32::Cast(v8::Value* value) {
8045#ifdef V8_ENABLE_CHECKS
8046 CheckCast(value);
8047#endif
8048 return static_cast<Int32*>(value);
8049}
8050
8051
8052Uint32* Uint32::Cast(v8::Value* value) {
8053#ifdef V8_ENABLE_CHECKS
8054 CheckCast(value);
8055#endif
8056 return static_cast<Uint32*>(value);
8057}
8058
8059
Steve Blocka7e24c12009-10-30 11:49:00 +00008060Date* Date::Cast(v8::Value* value) {
8061#ifdef V8_ENABLE_CHECKS
8062 CheckCast(value);
8063#endif
8064 return static_cast<Date*>(value);
8065}
8066
8067
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008068StringObject* StringObject::Cast(v8::Value* value) {
8069#ifdef V8_ENABLE_CHECKS
8070 CheckCast(value);
8071#endif
8072 return static_cast<StringObject*>(value);
8073}
8074
8075
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008076SymbolObject* SymbolObject::Cast(v8::Value* value) {
8077#ifdef V8_ENABLE_CHECKS
8078 CheckCast(value);
8079#endif
8080 return static_cast<SymbolObject*>(value);
8081}
8082
8083
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008084NumberObject* NumberObject::Cast(v8::Value* value) {
8085#ifdef V8_ENABLE_CHECKS
8086 CheckCast(value);
8087#endif
8088 return static_cast<NumberObject*>(value);
8089}
8090
8091
8092BooleanObject* BooleanObject::Cast(v8::Value* value) {
8093#ifdef V8_ENABLE_CHECKS
8094 CheckCast(value);
8095#endif
8096 return static_cast<BooleanObject*>(value);
8097}
8098
8099
Ben Murdochf87a2032010-10-22 12:50:53 +01008100RegExp* RegExp::Cast(v8::Value* value) {
8101#ifdef V8_ENABLE_CHECKS
8102 CheckCast(value);
8103#endif
8104 return static_cast<RegExp*>(value);
8105}
8106
8107
Steve Blocka7e24c12009-10-30 11:49:00 +00008108Object* Object::Cast(v8::Value* value) {
8109#ifdef V8_ENABLE_CHECKS
8110 CheckCast(value);
8111#endif
8112 return static_cast<Object*>(value);
8113}
8114
8115
8116Array* Array::Cast(v8::Value* value) {
8117#ifdef V8_ENABLE_CHECKS
8118 CheckCast(value);
8119#endif
8120 return static_cast<Array*>(value);
8121}
8122
8123
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008124Map* Map::Cast(v8::Value* value) {
8125#ifdef V8_ENABLE_CHECKS
8126 CheckCast(value);
8127#endif
8128 return static_cast<Map*>(value);
8129}
8130
8131
8132Set* Set::Cast(v8::Value* value) {
8133#ifdef V8_ENABLE_CHECKS
8134 CheckCast(value);
8135#endif
8136 return static_cast<Set*>(value);
8137}
8138
8139
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008140Promise* Promise::Cast(v8::Value* value) {
8141#ifdef V8_ENABLE_CHECKS
8142 CheckCast(value);
8143#endif
8144 return static_cast<Promise*>(value);
8145}
8146
8147
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008148Proxy* Proxy::Cast(v8::Value* value) {
8149#ifdef V8_ENABLE_CHECKS
8150 CheckCast(value);
8151#endif
8152 return static_cast<Proxy*>(value);
8153}
8154
8155
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008156Promise::Resolver* Promise::Resolver::Cast(v8::Value* value) {
8157#ifdef V8_ENABLE_CHECKS
8158 CheckCast(value);
8159#endif
8160 return static_cast<Promise::Resolver*>(value);
8161}
8162
8163
8164ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
8165#ifdef V8_ENABLE_CHECKS
8166 CheckCast(value);
8167#endif
8168 return static_cast<ArrayBuffer*>(value);
8169}
8170
8171
8172ArrayBufferView* ArrayBufferView::Cast(v8::Value* value) {
8173#ifdef V8_ENABLE_CHECKS
8174 CheckCast(value);
8175#endif
8176 return static_cast<ArrayBufferView*>(value);
8177}
8178
8179
8180TypedArray* TypedArray::Cast(v8::Value* value) {
8181#ifdef V8_ENABLE_CHECKS
8182 CheckCast(value);
8183#endif
8184 return static_cast<TypedArray*>(value);
8185}
8186
8187
8188Uint8Array* Uint8Array::Cast(v8::Value* value) {
8189#ifdef V8_ENABLE_CHECKS
8190 CheckCast(value);
8191#endif
8192 return static_cast<Uint8Array*>(value);
8193}
8194
8195
8196Int8Array* Int8Array::Cast(v8::Value* value) {
8197#ifdef V8_ENABLE_CHECKS
8198 CheckCast(value);
8199#endif
8200 return static_cast<Int8Array*>(value);
8201}
8202
8203
8204Uint16Array* Uint16Array::Cast(v8::Value* value) {
8205#ifdef V8_ENABLE_CHECKS
8206 CheckCast(value);
8207#endif
8208 return static_cast<Uint16Array*>(value);
8209}
8210
8211
8212Int16Array* Int16Array::Cast(v8::Value* value) {
8213#ifdef V8_ENABLE_CHECKS
8214 CheckCast(value);
8215#endif
8216 return static_cast<Int16Array*>(value);
8217}
8218
8219
8220Uint32Array* Uint32Array::Cast(v8::Value* value) {
8221#ifdef V8_ENABLE_CHECKS
8222 CheckCast(value);
8223#endif
8224 return static_cast<Uint32Array*>(value);
8225}
8226
8227
8228Int32Array* Int32Array::Cast(v8::Value* value) {
8229#ifdef V8_ENABLE_CHECKS
8230 CheckCast(value);
8231#endif
8232 return static_cast<Int32Array*>(value);
8233}
8234
8235
8236Float32Array* Float32Array::Cast(v8::Value* value) {
8237#ifdef V8_ENABLE_CHECKS
8238 CheckCast(value);
8239#endif
8240 return static_cast<Float32Array*>(value);
8241}
8242
8243
8244Float64Array* Float64Array::Cast(v8::Value* value) {
8245#ifdef V8_ENABLE_CHECKS
8246 CheckCast(value);
8247#endif
8248 return static_cast<Float64Array*>(value);
8249}
8250
8251
8252Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) {
8253#ifdef V8_ENABLE_CHECKS
8254 CheckCast(value);
8255#endif
8256 return static_cast<Uint8ClampedArray*>(value);
8257}
8258
8259
8260DataView* DataView::Cast(v8::Value* value) {
8261#ifdef V8_ENABLE_CHECKS
8262 CheckCast(value);
8263#endif
8264 return static_cast<DataView*>(value);
8265}
8266
8267
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008268SharedArrayBuffer* SharedArrayBuffer::Cast(v8::Value* value) {
8269#ifdef V8_ENABLE_CHECKS
8270 CheckCast(value);
8271#endif
8272 return static_cast<SharedArrayBuffer*>(value);
8273}
8274
8275
Steve Blocka7e24c12009-10-30 11:49:00 +00008276Function* Function::Cast(v8::Value* value) {
8277#ifdef V8_ENABLE_CHECKS
8278 CheckCast(value);
8279#endif
8280 return static_cast<Function*>(value);
8281}
8282
8283
8284External* External::Cast(v8::Value* value) {
8285#ifdef V8_ENABLE_CHECKS
8286 CheckCast(value);
8287#endif
8288 return static_cast<External*>(value);
8289}
8290
8291
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008292template<typename T>
8293Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
8294 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
Steve Blocka7e24c12009-10-30 11:49:00 +00008295}
8296
8297
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008298template<typename T>
8299Local<Value> PropertyCallbackInfo<T>::Data() const {
8300 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00008301}
8302
8303
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008304template<typename T>
8305Local<Object> PropertyCallbackInfo<T>::This() const {
8306 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
8307}
8308
8309
8310template<typename T>
8311Local<Object> PropertyCallbackInfo<T>::Holder() const {
8312 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
8313}
8314
8315
8316template<typename T>
8317ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
8318 return ReturnValue<T>(&args_[kReturnValueIndex]);
8319}
8320
Ben Murdoch097c5b22016-05-18 11:27:45 +01008321template <typename T>
8322bool PropertyCallbackInfo<T>::ShouldThrowOnError() const {
8323 typedef internal::Internals I;
8324 return args_[kShouldThrowOnErrorIndex] != I::IntToSmi(0);
8325}
8326
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008327
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008328Local<Primitive> Undefined(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008329 typedef internal::Object* S;
8330 typedef internal::Internals I;
8331 I::CheckInitialized(isolate);
8332 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008333 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008334}
8335
8336
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008337Local<Primitive> Null(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008338 typedef internal::Object* S;
8339 typedef internal::Internals I;
8340 I::CheckInitialized(isolate);
8341 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008342 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008343}
8344
8345
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008346Local<Boolean> True(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008347 typedef internal::Object* S;
8348 typedef internal::Internals I;
8349 I::CheckInitialized(isolate);
8350 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008351 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008352}
8353
8354
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008355Local<Boolean> False(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008356 typedef internal::Object* S;
8357 typedef internal::Internals I;
8358 I::CheckInitialized(isolate);
8359 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008360 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008361}
8362
8363
8364void Isolate::SetData(uint32_t slot, void* data) {
8365 typedef internal::Internals I;
8366 I::SetEmbedderData(this, slot, data);
8367}
8368
8369
8370void* Isolate::GetData(uint32_t slot) {
8371 typedef internal::Internals I;
8372 return I::GetEmbedderData(this, slot);
8373}
8374
8375
8376uint32_t Isolate::GetNumberOfDataSlots() {
8377 typedef internal::Internals I;
8378 return I::kNumIsolateDataSlots;
8379}
8380
8381
8382int64_t Isolate::AdjustAmountOfExternalAllocatedMemory(
8383 int64_t change_in_bytes) {
8384 typedef internal::Internals I;
8385 int64_t* amount_of_external_allocated_memory =
8386 reinterpret_cast<int64_t*>(reinterpret_cast<uint8_t*>(this) +
8387 I::kAmountOfExternalAllocatedMemoryOffset);
8388 int64_t* amount_of_external_allocated_memory_at_last_global_gc =
8389 reinterpret_cast<int64_t*>(
8390 reinterpret_cast<uint8_t*>(this) +
8391 I::kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset);
8392 int64_t amount = *amount_of_external_allocated_memory + change_in_bytes;
8393 if (change_in_bytes > 0 &&
8394 amount - *amount_of_external_allocated_memory_at_last_global_gc >
8395 I::kExternalAllocationLimit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008396 ReportExternalAllocationLimitReached();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008397 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008398 *amount_of_external_allocated_memory = amount;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008399 return *amount_of_external_allocated_memory;
8400}
8401
8402
8403template<typename T>
8404void Isolate::SetObjectGroupId(const Persistent<T>& object,
8405 UniqueId id) {
8406 TYPE_CHECK(Value, T);
8407 SetObjectGroupId(reinterpret_cast<v8::internal::Object**>(object.val_), id);
8408}
8409
8410
8411template<typename T>
8412void Isolate::SetReferenceFromGroup(UniqueId id,
8413 const Persistent<T>& object) {
8414 TYPE_CHECK(Value, T);
8415 SetReferenceFromGroup(id,
8416 reinterpret_cast<v8::internal::Object**>(object.val_));
8417}
8418
8419
8420template<typename T, typename S>
8421void Isolate::SetReference(const Persistent<T>& parent,
8422 const Persistent<S>& child) {
8423 TYPE_CHECK(Object, T);
8424 TYPE_CHECK(Value, S);
8425 SetReference(reinterpret_cast<v8::internal::Object**>(parent.val_),
8426 reinterpret_cast<v8::internal::Object**>(child.val_));
8427}
8428
8429
8430Local<Value> Context::GetEmbedderData(int index) {
8431#ifndef V8_ENABLE_CHECKS
8432 typedef internal::Object O;
8433 typedef internal::HeapObject HO;
8434 typedef internal::Internals I;
8435 HO* context = *reinterpret_cast<HO**>(this);
8436 O** result =
8437 HandleScope::CreateHandle(context, I::ReadEmbedderData<O*>(this, index));
8438 return Local<Value>(reinterpret_cast<Value*>(result));
8439#else
8440 return SlowGetEmbedderData(index);
8441#endif
8442}
8443
8444
8445void* Context::GetAlignedPointerFromEmbedderData(int index) {
8446#ifndef V8_ENABLE_CHECKS
8447 typedef internal::Internals I;
8448 return I::ReadEmbedderData<void*>(this, index);
8449#else
8450 return SlowGetAlignedPointerFromEmbedderData(index);
8451#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00008452}
8453
8454
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008455void V8::SetAllowCodeGenerationFromStringsCallback(
8456 AllowCodeGenerationFromStringsCallback callback) {
8457 Isolate* isolate = Isolate::GetCurrent();
8458 isolate->SetAllowCodeGenerationFromStringsCallback(callback);
8459}
8460
8461
8462bool V8::IsDead() {
8463 Isolate* isolate = Isolate::GetCurrent();
8464 return isolate->IsDead();
8465}
8466
8467
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008468bool V8::AddMessageListener(MessageCallback that, Local<Value> data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008469 Isolate* isolate = Isolate::GetCurrent();
8470 return isolate->AddMessageListener(that, data);
8471}
8472
8473
8474void V8::RemoveMessageListeners(MessageCallback that) {
8475 Isolate* isolate = Isolate::GetCurrent();
8476 isolate->RemoveMessageListeners(that);
8477}
8478
8479
8480void V8::SetFailedAccessCheckCallbackFunction(
8481 FailedAccessCheckCallback callback) {
8482 Isolate* isolate = Isolate::GetCurrent();
8483 isolate->SetFailedAccessCheckCallbackFunction(callback);
8484}
8485
8486
8487void V8::SetCaptureStackTraceForUncaughtExceptions(
8488 bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
8489 Isolate* isolate = Isolate::GetCurrent();
8490 isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
8491 options);
8492}
8493
8494
8495void V8::SetFatalErrorHandler(FatalErrorCallback callback) {
8496 Isolate* isolate = Isolate::GetCurrent();
8497 isolate->SetFatalErrorHandler(callback);
8498}
8499
8500
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008501void V8::RemoveGCPrologueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008502 Isolate* isolate = Isolate::GetCurrent();
8503 isolate->RemoveGCPrologueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008504 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008505}
8506
8507
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008508void V8::RemoveGCEpilogueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008509 Isolate* isolate = Isolate::GetCurrent();
8510 isolate->RemoveGCEpilogueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008511 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008512}
8513
8514
8515void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
8516 ObjectSpace space,
8517 AllocationAction action) {
8518 Isolate* isolate = Isolate::GetCurrent();
8519 isolate->AddMemoryAllocationCallback(callback, space, action);
8520}
8521
8522
8523void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
8524 Isolate* isolate = Isolate::GetCurrent();
8525 isolate->RemoveMemoryAllocationCallback(callback);
8526}
8527
8528
8529void V8::TerminateExecution(Isolate* isolate) { isolate->TerminateExecution(); }
8530
8531
8532bool V8::IsExecutionTerminating(Isolate* isolate) {
8533 if (isolate == NULL) {
8534 isolate = Isolate::GetCurrent();
8535 }
8536 return isolate->IsExecutionTerminating();
8537}
8538
8539
8540void V8::CancelTerminateExecution(Isolate* isolate) {
8541 isolate->CancelTerminateExecution();
8542}
8543
8544
8545void V8::VisitExternalResources(ExternalResourceVisitor* visitor) {
8546 Isolate* isolate = Isolate::GetCurrent();
8547 isolate->VisitExternalResources(visitor);
8548}
8549
8550
8551void V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
8552 Isolate* isolate = Isolate::GetCurrent();
8553 isolate->VisitHandlesWithClassIds(visitor);
8554}
8555
8556
8557void V8::VisitHandlesWithClassIds(Isolate* isolate,
8558 PersistentHandleVisitor* visitor) {
8559 isolate->VisitHandlesWithClassIds(visitor);
8560}
8561
8562
8563void V8::VisitHandlesForPartialDependence(Isolate* isolate,
8564 PersistentHandleVisitor* visitor) {
8565 isolate->VisitHandlesForPartialDependence(visitor);
8566}
8567
Steve Blocka7e24c12009-10-30 11:49:00 +00008568/**
8569 * \example shell.cc
8570 * A simple shell that takes a list of expressions on the
8571 * command-line and executes them.
8572 */
8573
8574
8575/**
8576 * \example process.cc
8577 */
8578
8579
8580} // namespace v8
8581
8582
Steve Blocka7e24c12009-10-30 11:49:00 +00008583#undef TYPE_CHECK
8584
8585
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008586#endif // INCLUDE_V8_H_