Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | /** \mainpage V8 API Reference Guide |
| 6 | * |
| 7 | * V8 is Google's open source JavaScript engine. |
| 8 | * |
| 9 | * This set of documents provides reference material generated from the |
| 10 | * V8 header file, include/v8.h. |
| 11 | * |
| 12 | * For other documentation see http://code.google.com/apis/v8/ |
| 13 | */ |
| 14 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | #ifndef INCLUDE_V8_H_ |
| 16 | #define INCLUDE_V8_H_ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | #include "v8-version.h" // NOLINT(build/include) |
| 23 | #include "v8config.h" // NOLINT(build/include) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 24 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | # define V8_EXPORT __declspec(dllexport) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | #elif USING_V8_SHARED |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | # define V8_EXPORT __declspec(dllimport) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | #else |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | # define V8_EXPORT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | #endif // BUILDING_V8_SHARED |
| 47 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | #else // V8_OS_WIN |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | #endif // V8_OS_WIN |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * The v8 JavaScript engine. |
| 65 | */ |
| 66 | namespace v8 { |
| 67 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | class AccessorSignature; |
| 69 | class Array; |
| 70 | class Boolean; |
| 71 | class BooleanObject; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 72 | class Context; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 73 | class CpuProfiler; |
| 74 | class Data; |
| 75 | class Date; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | class External; |
| 77 | class Function; |
| 78 | class FunctionTemplate; |
| 79 | class HeapProfiler; |
| 80 | class ImplementationUtilities; |
| 81 | class Int32; |
| 82 | class Integer; |
| 83 | class Isolate; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 84 | template <class T> |
| 85 | class Maybe; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | class Name; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 87 | class Number; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 88 | class NumberObject; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | class Object; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | class ObjectOperationDescriptor; |
| 91 | class ObjectTemplate; |
| 92 | class Platform; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | class Primitive; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 94 | class Promise; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | class Proxy; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | class RawOperationDescriptor; |
| 97 | class Script; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 98 | class SharedArrayBuffer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | class Signature; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 100 | class StartupData; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | class StackFrame; |
| 102 | class StackTrace; |
| 103 | class String; |
| 104 | class StringObject; |
| 105 | class Symbol; |
| 106 | class SymbolObject; |
| 107 | class Private; |
| 108 | class Uint32; |
| 109 | class Utils; |
| 110 | class Value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | template <class T> class Local; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | template <class T> |
| 113 | class MaybeLocal; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 114 | template <class T> class Eternal; |
| 115 | template<class T> class NonCopyablePersistentTraits; |
| 116 | template<class T> class PersistentBase; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | template <class T, class M = NonCopyablePersistentTraits<T> > |
| 118 | class Persistent; |
| 119 | template <class T> |
| 120 | class Global; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | template<class K, class V, class T> class PersistentValueMap; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | template <class K, class V, class T> |
| 123 | class PersistentValueMapBase; |
| 124 | template <class K, class V, class T> |
| 125 | class GlobalValueMap; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | template<class V, class T> class PersistentValueVector; |
| 127 | template<class T, class P> class WeakCallbackObject; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | class FunctionTemplate; |
| 129 | class ObjectTemplate; |
| 130 | class Data; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 131 | template<typename T> class FunctionCallbackInfo; |
| 132 | template<typename T> class PropertyCallbackInfo; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 133 | class StackTrace; |
| 134 | class StackFrame; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | class Isolate; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | class CallHandlerHelper; |
| 137 | class EscapableHandleScope; |
| 138 | template<typename T> class ReturnValue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 140 | namespace experimental { |
| 141 | class FastAccessorBuilder; |
| 142 | } // namespace experimental |
| 143 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | namespace internal { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | class Arguments; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 146 | class Heap; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | class HeapObject; |
| 148 | class Isolate; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | class Object; |
| 150 | struct StreamedSource; |
| 151 | template<typename T> class CustomArguments; |
| 152 | class PropertyCallbackArguments; |
| 153 | class FunctionCallbackArguments; |
| 154 | class GlobalHandles; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 155 | } // namespace internal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 156 | |
| 157 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 159 | * General purpose unique identifier. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | class UniqueId { |
| 162 | public: |
| 163 | explicit UniqueId(intptr_t data) |
| 164 | : data_(data) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 166 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 182 | // --- Handles --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 184 | #define TYPE_CHECK(T, S) \ |
| 185 | while (false) { \ |
| 186 | *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 189 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | /** |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 211 | * a Local<Object>); the value will still be governed by a handle |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | * behind the scenes and the same rules apply to these values as to |
| 213 | * their handles. |
| 214 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 215 | template <class T> |
| 216 | class Local { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 218 | V8_INLINE Local() : val_(0) {} |
| 219 | template <class S> |
| 220 | V8_INLINE Local(Local<S> that) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | : val_(reinterpret_cast<T*>(*that)) { |
| 222 | /** |
| 223 | * This check fails when trying to convert between incompatible |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 224 | * handles. For example, converting from a Local<String> to a |
| 225 | * Local<Number>. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | */ |
| 227 | TYPE_CHECK(T, S); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns true if the handle is empty. |
| 232 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | V8_INLINE bool IsEmpty() const { return val_ == 0; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | /** |
| 236 | * Sets the handle to be empty. IsEmpty() will then return true. |
| 237 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 238 | V8_INLINE void Clear() { val_ = 0; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 239 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | V8_INLINE T* operator->() const { return val_; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 241 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 242 | V8_INLINE T* operator*() const { return val_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 250 | template <class S> |
| 251 | V8_INLINE bool operator==(const Local<S>& that) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 252 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 274 | template <class S> |
| 275 | V8_INLINE bool operator!=(const Local<S>& that) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | return !operator==(that); |
| 277 | } |
| 278 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 279 | template <class S> V8_INLINE bool operator!=( |
| 280 | const Persistent<S>& that) const { |
| 281 | return !operator==(that); |
| 282 | } |
| 283 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | template <class S> V8_INLINE static Local<T> Cast(Local<S> that) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | #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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 292 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 294 | template <class S> V8_INLINE Local<S> As() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 295 | return Local<S>::Cast(*this); |
| 296 | } |
| 297 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 298 | /** |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 303 | V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 304 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 312 | template<class F> friend class Local; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 313 | template <class F> |
| 314 | friend class MaybeLocal; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 315 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 320 | friend class Private; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 321 | template<class F> friend class internal::CustomArguments; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 322 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 326 | friend class HandleScope; |
| 327 | friend class EscapableHandleScope; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 328 | template <class F1, class F2, class F3> |
| 329 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 330 | template<class F1, class F2> friend class PersistentValueVector; |
| 331 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 332 | explicit V8_INLINE Local(T* that) : val_(that) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 333 | V8_INLINE static Local<T> New(Isolate* isolate, T* that); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 334 | T* val_; |
| 335 | }; |
| 336 | |
| 337 | |
| 338 | #if !defined(V8_IMMINENT_DEPRECATION_WARNINGS) |
| 339 | // Local is an alias for Local for historical reasons. |
| 340 | template <class T> |
| 341 | using 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 | */ |
| 355 | template <class T> |
| 356 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | |
| 386 | // Eternal handles are set-once handles that live for the life of the isolate. |
| 387 | template <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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 405 | static const int kInternalFieldsInWeakCallback = 2; |
| 406 | |
| 407 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 408 | template <typename T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 409 | class WeakCallbackInfo { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 410 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 411 | typedef void (*Callback)(const WeakCallbackInfo<T>& data); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 412 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 413 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 423 | V8_INLINE T* GetParameter() const { return parameter_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 424 | V8_INLINE void* GetInternalField(int index) const; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 425 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 426 | 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 435 | V8_DEPRECATED("Not realiable once SetSecondPassCallback() was used.", |
| 436 | bool IsFirstPass() const) { |
| 437 | return callback_ != nullptr; |
| 438 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 439 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 447 | |
| 448 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 449 | Isolate* isolate_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 450 | T* parameter_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 451 | Callback* callback_; |
| 452 | void* internal_fields_[kInternalFieldsInWeakCallback]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | |
| 456 | template <class T, class P> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 457 | class WeakCallbackData { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 458 | public: |
| 459 | typedef void (*Callback)(const WeakCallbackData<T, P>& data); |
| 460 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 461 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 466 | V8_INLINE Local<T> GetValue() const { return handle_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 467 | |
| 468 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 469 | Isolate* isolate_; |
| 470 | P* parameter_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 471 | Local<T> handle_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 475 | // TODO(dcarney): delete this with WeakCallbackData |
| 476 | template <class T> |
| 477 | using PhantomCallbackData = WeakCallbackInfo<T>; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 478 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 479 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 480 | enum class WeakCallbackType { kParameter, kInternalFields }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 481 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 486 | * allocated, a PersistentBase handle remains valid until it is explicitly |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 487 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 492 | * cell can be created using the constructor or PersistentBase::Reset and |
| 493 | * existing handles can be disposed using PersistentBase::Reset. |
| 494 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 496 | template <class T> class PersistentBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 497 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 499 | * If non-empty, destroy the underlying storage cell |
| 500 | * IsEmpty() will return true after this call. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 501 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 502 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 508 | V8_INLINE void Reset(Isolate* isolate, const Local<S>& other); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 509 | |
| 510 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 511 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 513 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 514 | template <class S> |
| 515 | V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other); |
| 516 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 517 | V8_INLINE bool IsEmpty() const { return val_ == NULL; } |
| 518 | V8_INLINE void Empty() { val_ = 0; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 519 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 520 | V8_INLINE Local<T> Get(Isolate* isolate) const { |
| 521 | return Local<T>::New(isolate, *this); |
| 522 | } |
| 523 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 524 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 528 | if (a == NULL) return b == NULL; |
| 529 | if (b == NULL) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 530 | return *a == *b; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 533 | template <class S> |
| 534 | V8_INLINE bool operator==(const Local<S>& that) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 535 | internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); |
| 536 | internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 537 | if (a == NULL) return b == NULL; |
| 538 | if (b == NULL) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 539 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 547 | template <class S> |
| 548 | V8_INLINE bool operator!=(const Local<S>& that) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 549 | return !operator==(that); |
| 550 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 551 | |
| 552 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 553 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 558 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 559 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 564 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 565 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 570 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 571 | // 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 578 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 584 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 585 | template <typename P> |
| 586 | V8_INLINE void SetWeak(P* parameter, |
| 587 | typename WeakCallbackInfo<P>::Callback callback, |
| 588 | WeakCallbackType type); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 589 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 590 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 614 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 621 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 644 | template<class F> friend class Local; |
| 645 | template<class F1, class F2> friend class Persistent; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 646 | template <class F> |
| 647 | friend class Global; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 648 | template<class F> friend class PersistentBase; |
| 649 | template<class F> friend class ReturnValue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 650 | template <class F1, class F2, class F3> |
| 651 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 652 | template<class F1, class F2> friend class PersistentValueVector; |
| 653 | friend class Object; |
| 654 | |
| 655 | explicit V8_INLINE PersistentBase(T* val) : val_(val) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 656 | PersistentBase(const PersistentBase& other) = delete; // NOLINT |
| 657 | void operator=(const PersistentBase&) = delete; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 658 | 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 | */ |
| 670 | template<class T> |
| 671 | class 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 | */ |
| 691 | template<class T> |
| 692 | struct 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 | */ |
| 711 | template <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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 718 | * Construct a Persistent from a Local. |
| 719 | * When the Local is non-empty, a new storage cell is created |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 720 | * pointing to the same object, and no flags are set. |
| 721 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 722 | template <class S> |
| 723 | V8_INLINE Persistent(Isolate* isolate, Local<S> that) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 724 | : 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 771 | #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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 774 | if (!that.IsEmpty()) T::Cast(*that); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 775 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 776 | return reinterpret_cast<Persistent<T>&>(that); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 779 | // TODO(dcarney): this is pretty useless, fix or remove |
| 780 | template <class S> V8_INLINE Persistent<S>& As() { // NOLINT |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 781 | return Persistent<S>::Cast(*this); |
| 782 | } |
| 783 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 784 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 785 | friend class Isolate; |
| 786 | friend class Utils; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 787 | template<class F> friend class Local; |
| 788 | template<class F1, class F2> friend class Persistent; |
| 789 | template<class F> friend class ReturnValue; |
| 790 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 791 | explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 792 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 803 | template <class T> |
| 804 | class Global : public PersistentBase<T> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 805 | public: |
| 806 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 807 | * A Global with no storage cell. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 808 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 809 | V8_INLINE Global() : PersistentBase<T>(nullptr) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 810 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 811 | * Construct a Global from a Local. |
| 812 | * When the Local is non-empty, a new storage cell is created |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 813 | * pointing to the same object, and no flags are set. |
| 814 | */ |
| 815 | template <class S> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 816 | V8_INLINE Global(Isolate* isolate, Local<S> that) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 817 | : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) { |
| 818 | TYPE_CHECK(T, S); |
| 819 | } |
| 820 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 821 | * Construct a Global from a PersistentBase. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 822 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 826 | V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that) |
| 827 | : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 828 | TYPE_CHECK(T, S); |
| 829 | } |
| 830 | /** |
| 831 | * Move constructor. |
| 832 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 833 | V8_INLINE Global(Global&& other) : PersistentBase<T>(other.val_) { // NOLINT |
| 834 | other.val_ = nullptr; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 835 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 836 | V8_INLINE ~Global() { this->Reset(); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 837 | /** |
| 838 | * Move via assignment. |
| 839 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 840 | template <class S> |
| 841 | V8_INLINE Global& operator=(Global<S>&& rhs) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 842 | TYPE_CHECK(T, S); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 843 | if (this != &rhs) { |
| 844 | this->Reset(); |
| 845 | this->val_ = rhs.val_; |
| 846 | rhs.val_ = nullptr; |
| 847 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 848 | return *this; |
| 849 | } |
| 850 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 851 | * Pass allows returning uniques from functions, etc. |
| 852 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 853 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 859 | |
| 860 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 861 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 866 | }; |
| 867 | |
| 868 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 869 | // UniquePersistent is an alias for Global for historical reason. |
| 870 | template <class T> |
| 871 | using UniquePersistent = Global<T>; |
| 872 | |
| 873 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 874 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 888 | class V8_EXPORT HandleScope { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 889 | public: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 890 | explicit HandleScope(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 891 | |
| 892 | ~HandleScope(); |
| 893 | |
| 894 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 895 | * Counts the number of allocated handles. |
| 896 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 897 | static int NumberOfHandles(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 898 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 899 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 910 | |
| 911 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 912 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 918 | HandleScope(const HandleScope&); |
| 919 | void operator=(const HandleScope&); |
| 920 | void* operator new(size_t size); |
| 921 | void operator delete(void*, size_t); |
| 922 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 923 | internal::Isolate* isolate_; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 924 | internal::Object** prev_next_; |
| 925 | internal::Object** prev_limit_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 926 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 927 | // Local::New uses CreateHandle with an Isolate* parameter. |
| 928 | template<class F> friend class Local; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 929 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 930 | // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with |
| 931 | // a HeapObject* in their shortcuts. |
| 932 | friend class Object; |
| 933 | friend class Context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 934 | }; |
| 935 | |
| 936 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 937 | /** |
| 938 | * A HandleScope which first allocates a handle in the current scope |
| 939 | * which will be later filled with the escape value. |
| 940 | */ |
| 941 | class V8_EXPORT EscapableHandleScope : public HandleScope { |
| 942 | public: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 943 | explicit EscapableHandleScope(Isolate* isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 944 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 970 | class V8_EXPORT SealHandleScope { |
| 971 | public: |
| 972 | SealHandleScope(Isolate* isolate); |
| 973 | ~SealHandleScope(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 974 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 975 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 982 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 983 | internal::Isolate* isolate_; |
| 984 | internal::Object** prev_limit_; |
| 985 | int prev_sealed_level_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 986 | }; |
| 987 | |
| 988 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 989 | // --- Special objects --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 990 | |
| 991 | |
| 992 | /** |
| 993 | * The superclass of values and API object templates. |
| 994 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 995 | class V8_EXPORT Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 996 | private: |
| 997 | Data(); |
| 998 | }; |
| 999 | |
| 1000 | |
| 1001 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1002 | * The optional attributes of ScriptOrigin. |
| 1003 | */ |
| 1004 | class 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1034 | * The origin, within a file, of a script. |
| 1035 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1036 | class ScriptOrigin { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1037 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1038 | V8_INLINE ScriptOrigin( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1039 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1057 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1058 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1064 | }; |
| 1065 | |
| 1066 | |
| 1067 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1068 | * A compiled JavaScript script, not yet tied to a Context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1069 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1070 | class V8_EXPORT UnboundScript { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1071 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1072 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1073 | * Binds the script to the currently entered context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1074 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1075 | Local<Script> BindToCurrentContext(); |
| 1076 | |
| 1077 | int GetId(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1078 | Local<Value> GetScriptName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1079 | |
| 1080 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1081 | * Data read from magic sourceURL comments. |
| 1082 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1083 | Local<Value> GetSourceURL(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1084 | /** |
| 1085 | * Data read from magic sourceMappingURL comments. |
| 1086 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1087 | Local<Value> GetSourceMappingURL(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1088 | |
| 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 | */ |
| 1103 | class V8_EXPORT Script { |
| 1104 | public: |
| 1105 | /** |
| 1106 | * A shorthand for ScriptCompiler::Compile(). |
| 1107 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1108 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1115 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1116 | static Local<Script> V8_DEPRECATE_SOON("Use maybe version", |
| 1117 | Compile(Local<String> source, |
| 1118 | Local<String> file_name)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1119 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1123 | * UnboundScript::BindToCurrentContext()). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1124 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1125 | V8_DEPRECATE_SOON("Use maybe version", Local<Value> Run()); |
| 1126 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1127 | |
| 1128 | /** |
| 1129 | * Returns the corresponding context-unbound script. |
| 1130 | */ |
| 1131 | Local<UnboundScript> GetUnboundScript(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1132 | }; |
| 1133 | |
| 1134 | |
| 1135 | /** |
| 1136 | * For compiling scripts. |
| 1137 | */ |
| 1138 | class 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1153 | CachedData() |
| 1154 | : data(NULL), |
| 1155 | length(0), |
| 1156 | rejected(false), |
| 1157 | buffer_policy(BufferNotOwned) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1158 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1170 | bool rejected; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1171 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1205 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1210 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1221 | class V8_EXPORT ExternalSourceStream { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1222 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1243 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1260 | }; |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1306 | kConsumeCodeCache |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1307 | }; |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1317 | * |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1318 | * \param source Script source code. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1319 | * \return Compiled script object (context independent; for running it must be |
| 1320 | * bound to a context). |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1321 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1322 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1327 | Isolate* isolate, Source* source, |
| 1328 | CompileOptions options = kNoCompileOptions); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1329 | |
| 1330 | /** |
| 1331 | * Compiles the specified script (bound to current context). |
| 1332 | * |
| 1333 | * \param source Script source code. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1334 | * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() |
| 1335 | * using pre_data speeds compilation if it's done multiple times. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1336 | * Owned by caller, no references are kept when this function returns. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1337 | * \return Compiled script object, bound to the context that was active |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1338 | * when this function was called. When run it will always use this |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1339 | * context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1340 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1341 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1347 | CompileOptions options = kNoCompileOptions); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1348 | |
| 1349 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1350 | * 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 Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1356 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1357 | * 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 Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1359 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1360 | static ScriptStreamingTask* StartStreamingScript( |
| 1361 | Isolate* isolate, StreamedSource* source, |
| 1362 | CompileOptions options = kNoCompileOptions); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1363 | |
| 1364 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1365 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1370 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1371 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1379 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1399 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1439 | }; |
| 1440 | |
| 1441 | |
| 1442 | /** |
| 1443 | * An error message. |
| 1444 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1445 | class V8_EXPORT Message { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1446 | public: |
| 1447 | Local<String> Get() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1448 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1452 | |
| 1453 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1454 | * Returns the origin for the script from where the function causing the |
| 1455 | * error originates. |
| 1456 | */ |
| 1457 | ScriptOrigin GetScriptOrigin() const; |
| 1458 | |
| 1459 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1460 | * Returns the resource name for the script from where the function causing |
| 1461 | * the error originates. |
| 1462 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1463 | Local<Value> GetScriptResourceName() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1464 | |
| 1465 | /** |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1466 | * Exception stack trace. By default stack traces are not captured for |
| 1467 | * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows |
| 1468 | * to change this option. |
| 1469 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1470 | Local<StackTrace> GetStackTrace() const; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1471 | |
| 1472 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1473 | * Returns the number, 1-based, of the line where the error occurred. |
| 1474 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1475 | V8_DEPRECATE_SOON("Use maybe version", int GetLineNumber() const); |
| 1476 | V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1477 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1494 | V8_DEPRECATE_SOON("Use maybe version", int GetStartColumn() const); |
| 1495 | V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1496 | |
| 1497 | /** |
| 1498 | * Returns the index within the line of the last character where |
| 1499 | * the error occurred. |
| 1500 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1501 | V8_DEPRECATED("Use maybe version", int GetEndColumn() const); |
| 1502 | V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1503 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1504 | /** |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1509 | bool IsOpaque() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1510 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1511 | // TODO(1245381): Print to a string instead of on a FILE. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1512 | static void PrintCurrentStackTrace(Isolate* isolate, FILE* out); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1513 | |
| 1514 | static const int kNoLineNumberInfo = 0; |
| 1515 | static const int kNoColumnInfo = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1516 | static const int kNoScriptIdInfo = 0; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1517 | }; |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1525 | class V8_EXPORT StackTrace { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1526 | 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 Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1538 | kScriptNameOrSourceURL = 1 << 6, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1539 | kScriptId = 1 << 7, |
| 1540 | kExposeFramesAcrossSecurityOrigins = 1 << 8, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1541 | kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1542 | kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1543 | }; |
| 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1561 | * Grab a snapshot of the current JavaScript execution stack. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1562 | * |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1568 | Isolate* isolate, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1569 | int frame_limit, |
| 1570 | StackTraceOptions options = kOverview); |
| 1571 | }; |
| 1572 | |
| 1573 | |
| 1574 | /** |
| 1575 | * A single JavaScript stack frame. |
| 1576 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1577 | class V8_EXPORT StackFrame { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1578 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1597 | * 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 Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1605 | * 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 Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1611 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1613 | * is undefined and its source ends with //# sourceURL=... string or |
| 1614 | * deprecated //@ sourceURL=... string. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1615 | */ |
| 1616 | Local<String> GetScriptNameOrSourceURL() const; |
| 1617 | |
| 1618 | /** |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1619 | * 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1630 | * Returns whether or not the associated function is called as a |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1631 | * constructor via "new". |
| 1632 | */ |
| 1633 | bool IsConstructor() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1634 | }; |
| 1635 | |
| 1636 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1637 | // A StateTag represents a possible state of the VM. |
| 1638 | enum 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. |
| 1643 | struct 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. |
| 1652 | struct SampleInfo { |
| 1653 | size_t frames_count; |
| 1654 | StateTag vm_state; |
| 1655 | }; |
| 1656 | |
| 1657 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1658 | /** |
| 1659 | * A JSON Parser. |
| 1660 | */ |
| 1661 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1670 | 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 | */ |
| 1682 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1689 | }; |
| 1690 | |
| 1691 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1692 | // --- Value --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1693 | |
| 1694 | |
| 1695 | /** |
| 1696 | * The superclass of all JavaScript values and objects. |
| 1697 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1698 | class V8_EXPORT Value : public Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1699 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1700 | /** |
| 1701 | * Returns true if this value is the undefined value. See ECMA-262 |
| 1702 | * 4.3.10. |
| 1703 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1704 | V8_INLINE bool IsUndefined() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1705 | |
| 1706 | /** |
| 1707 | * Returns true if this value is the null value. See ECMA-262 |
| 1708 | * 4.3.11. |
| 1709 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1710 | V8_INLINE bool IsNull() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1711 | |
| 1712 | /** |
| 1713 | * Returns true if this value is true. |
| 1714 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1715 | bool IsTrue() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1716 | |
| 1717 | /** |
| 1718 | * Returns true if this value is false. |
| 1719 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1720 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1727 | |
| 1728 | /** |
| 1729 | * Returns true if this value is an instance of the String type. |
| 1730 | * See ECMA-262 8.4. |
| 1731 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1732 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1739 | |
| 1740 | /** |
| 1741 | * Returns true if this value is a function. |
| 1742 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1743 | bool IsFunction() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1744 | |
| 1745 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1746 | * Returns true if this value is an array. Note that it will return false for |
| 1747 | * an Proxy for an array. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1748 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1749 | bool IsArray() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1750 | |
| 1751 | /** |
| 1752 | * Returns true if this value is an object. |
| 1753 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1754 | bool IsObject() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1755 | |
| 1756 | /** |
| 1757 | * Returns true if this value is boolean. |
| 1758 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1759 | bool IsBoolean() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1760 | |
| 1761 | /** |
| 1762 | * Returns true if this value is a number. |
| 1763 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1764 | bool IsNumber() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1765 | |
| 1766 | /** |
| 1767 | * Returns true if this value is external. |
| 1768 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1769 | bool IsExternal() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1770 | |
| 1771 | /** |
| 1772 | * Returns true if this value is a 32-bit signed integer. |
| 1773 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1774 | bool IsInt32() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1775 | |
| 1776 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1777 | * Returns true if this value is a 32-bit unsigned integer. |
| 1778 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1779 | bool IsUint32() const; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1780 | |
| 1781 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1782 | * Returns true if this value is a Date. |
| 1783 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1784 | bool IsDate() const; |
| 1785 | |
| 1786 | /** |
| 1787 | * Returns true if this value is an Arguments object. |
| 1788 | */ |
| 1789 | bool IsArgumentsObject() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1790 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1791 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1792 | * Returns true if this value is a Boolean object. |
| 1793 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1794 | bool IsBooleanObject() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1795 | |
| 1796 | /** |
| 1797 | * Returns true if this value is a Number object. |
| 1798 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1799 | bool IsNumberObject() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1800 | |
| 1801 | /** |
| 1802 | * Returns true if this value is a String object. |
| 1803 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1804 | 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1811 | |
| 1812 | /** |
| 1813 | * Returns true if this value is a NativeError. |
| 1814 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1815 | bool IsNativeError() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1816 | |
| 1817 | /** |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1818 | * Returns true if this value is a RegExp. |
| 1819 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1820 | bool IsRegExp() const; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1821 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1822 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1823 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1835 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1842 | */ |
| 1843 | bool IsMap() const; |
| 1844 | |
| 1845 | /** |
| 1846 | * Returns true if this value is a Set. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1847 | */ |
| 1848 | bool IsSet() const; |
| 1849 | |
| 1850 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1851 | * Returns true if this value is a Map Iterator. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1852 | */ |
| 1853 | bool IsMapIterator() const; |
| 1854 | |
| 1855 | /** |
| 1856 | * Returns true if this value is a Set Iterator. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1857 | */ |
| 1858 | bool IsSetIterator() const; |
| 1859 | |
| 1860 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1861 | * Returns true if this value is a WeakMap. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1862 | */ |
| 1863 | bool IsWeakMap() const; |
| 1864 | |
| 1865 | /** |
| 1866 | * Returns true if this value is a WeakSet. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1867 | */ |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1943 | * Returns true if this value is a SIMD Float32x4. |
| 1944 | * This is an experimental feature. |
| 1945 | */ |
| 1946 | bool IsFloat32x4() const; |
| 1947 | |
| 1948 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1949 | * Returns true if this value is a DataView. |
| 1950 | * This is an experimental feature. |
| 1951 | */ |
| 1952 | bool IsDataView() const; |
| 1953 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1954 | /** |
| 1955 | * Returns true if this value is a SharedArrayBuffer. |
| 1956 | * This is an experimental feature. |
| 1957 | */ |
| 1958 | bool IsSharedArrayBuffer() const; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1959 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1960 | /** |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2010 | |
| 2011 | /** |
| 2012 | * Attempts to convert a string to an array index. |
| 2013 | * Returns an empty handle if the conversion fails. |
| 2014 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2015 | V8_DEPRECATED("Use maybe version", Local<Uint32> ToArrayIndex() const); |
| 2016 | V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex( |
| 2017 | Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2018 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2019 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2032 | |
| 2033 | /** JS == */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2034 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2039 | |
| 2040 | template <class T> V8_INLINE static Value* Cast(T* value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2041 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2042 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2043 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2049 | }; |
| 2050 | |
| 2051 | |
| 2052 | /** |
| 2053 | * The superclass of primitive values. See ECMA-262 4.3.2. |
| 2054 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2055 | class V8_EXPORT Primitive : public Value { }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2056 | |
| 2057 | |
| 2058 | /** |
| 2059 | * A primitive boolean value (ECMA-262, 4.3.14). Either the true |
| 2060 | * or false value. |
| 2061 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2062 | class V8_EXPORT Boolean : public Primitive { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2063 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2064 | bool Value() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2065 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2070 | }; |
| 2071 | |
| 2072 | |
| 2073 | /** |
| 2074 | * A superclass for symbols and strings. |
| 2075 | */ |
| 2076 | class V8_EXPORT Name : public Primitive { |
| 2077 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2078 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2087 | V8_INLINE static Name* Cast(v8::Value* obj); |
| 2088 | private: |
| 2089 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2090 | }; |
| 2091 | |
| 2092 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2093 | enum class NewStringType { kNormal, kInternalized }; |
| 2094 | |
| 2095 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2096 | /** |
| 2097 | * A JavaScript string value (ECMA-262, 4.3.17). |
| 2098 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2099 | class V8_EXPORT String : public Name { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2100 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2101 | static const int kMaxLength = (1 << 28) - 16; |
| 2102 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2103 | enum Encoding { |
| 2104 | UNKNOWN_ENCODING = 0x1, |
| 2105 | TWO_BYTE_ENCODING = 0x0, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2106 | ONE_BYTE_ENCODING = 0x4 |
| 2107 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2108 | /** |
| 2109 | * Returns the number of characters in this string. |
| 2110 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2111 | int Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2112 | |
| 2113 | /** |
| 2114 | * Returns the number of bytes in the UTF-8 encoded |
| 2115 | * representation of this string. |
| 2116 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2117 | int Utf8Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2118 | |
| 2119 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2120 | * Returns whether this string is known to contain only one byte data. |
| 2121 | * Does not read the string. |
| 2122 | * False negatives are possible. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2123 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2124 | 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2131 | |
| 2132 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2133 | * 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 Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2139 | * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop |
| 2140 | * before the end of the buffer. |
| 2141 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2142 | * 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 Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2148 | * \param length The number of characters to copy from the string. For |
| 2149 | * WriteUtf8 the number of bytes in the buffer. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2150 | * \param nchars_ref The number of characters written, can be NULL. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2151 | * \param options Various options that might affect performance of this or |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2152 | * subsequent operations. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2153 | * \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 Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2155 | * including the null terminator (if written). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2156 | */ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2157 | enum WriteOptions { |
| 2158 | NO_OPTIONS = 0, |
| 2159 | HINT_MANY_WRITES_EXPECTED = 1, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2160 | NO_NULL_TERMINATION = 2, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2161 | 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2166 | }; |
| 2167 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2168 | // 16-bit character codes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2169 | 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 Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2178 | // UTF-8 encoded characters. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2179 | int WriteUtf8(char* buffer, |
| 2180 | int length = -1, |
| 2181 | int* nchars_ref = NULL, |
| 2182 | int options = NO_OPTIONS) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2183 | |
| 2184 | /** |
| 2185 | * A zero length string. |
| 2186 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2187 | V8_INLINE static v8::Local<v8::String> Empty(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2188 | |
| 2189 | /** |
| 2190 | * Returns true if the string is external |
| 2191 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2192 | bool IsExternal() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2193 | |
| 2194 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2195 | * Returns true if the string is both external and one-byte. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2196 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2197 | bool IsExternalOneByte() const; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2198 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2199 | class V8_EXPORT ExternalStringResourceBase { // NOLINT |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2200 | public: |
| 2201 | virtual ~ExternalStringResourceBase() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2202 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2203 | virtual bool IsCompressible() const { return false; } |
| 2204 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2205 | protected: |
| 2206 | ExternalStringResourceBase() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2207 | |
| 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2216 | private: |
| 2217 | // Disallow copying and assigning. |
| 2218 | ExternalStringResourceBase(const ExternalStringResourceBase&); |
| 2219 | void operator=(const ExternalStringResourceBase&); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2220 | |
| 2221 | friend class v8::internal::Heap; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2222 | }; |
| 2223 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2224 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2230 | class V8_EXPORT ExternalStringResource |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2231 | : public ExternalStringResourceBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2232 | public: |
| 2233 | /** |
| 2234 | * Override the destructor to manage the life cycle of the underlying |
| 2235 | * buffer. |
| 2236 | */ |
| 2237 | virtual ~ExternalStringResource() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2238 | |
| 2239 | /** |
| 2240 | * The string data from the underlying buffer. |
| 2241 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2242 | virtual const uint16_t* data() const = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2243 | |
| 2244 | /** |
| 2245 | * The length of the string. That is, the number of two-byte characters. |
| 2246 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2247 | virtual size_t length() const = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2248 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2249 | protected: |
| 2250 | ExternalStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2251 | }; |
| 2252 | |
| 2253 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2254 | * An ExternalOneByteStringResource is a wrapper around an one-byte |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2255 | * string buffer that resides outside V8's heap. Implement an |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2256 | * ExternalOneByteStringResource to manage the life cycle of the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2257 | * underlying buffer. Note that the string data must be immutable |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2258 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2261 | */ |
| 2262 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2263 | class V8_EXPORT ExternalOneByteStringResource |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2264 | : public ExternalStringResourceBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2265 | public: |
| 2266 | /** |
| 2267 | * Override the destructor to manage the life cycle of the underlying |
| 2268 | * buffer. |
| 2269 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2270 | virtual ~ExternalOneByteStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2271 | /** The string data from the underlying buffer.*/ |
| 2272 | virtual const char* data() const = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2273 | /** The number of Latin-1 characters in the string.*/ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2274 | virtual size_t length() const = 0; |
| 2275 | protected: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2276 | ExternalOneByteStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2277 | }; |
| 2278 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2279 | /** |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2287 | /** |
| 2288 | * Get the ExternalStringResource for an external string. Returns |
| 2289 | * NULL if IsExternal() doesn't return true. |
| 2290 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2291 | V8_INLINE ExternalStringResource* GetExternalStringResource() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2292 | |
| 2293 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2294 | * Get the ExternalOneByteStringResource for an external one-byte string. |
| 2295 | * Returns NULL if IsExternalOneByte() doesn't return true. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2296 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2297 | const ExternalOneByteStringResource* GetExternalOneByteStringResource() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2298 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2299 | V8_INLINE static String* Cast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2300 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2301 | // TODO(dcarney): remove with deprecation of New functions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2302 | enum NewStringType { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2303 | kNormalString = static_cast<int>(v8::NewStringType::kNormal), |
| 2304 | kInternalizedString = static_cast<int>(v8::NewStringType::kInternalized) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2305 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2306 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2307 | /** Allocates a new string from UTF-8 data.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2308 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2319 | |
| 2320 | /** Allocates a new string from Latin-1 data.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2321 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2331 | int length = -1); |
| 2332 | |
| 2333 | /** Allocates a new string from UTF-16 data.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2334 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2344 | int length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2345 | |
| 2346 | /** |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2347 | * Creates a new string by concatenating the left and the right strings |
| 2348 | * passed in as parameters. |
| 2349 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2350 | static Local<String> Concat(Local<String> left, Local<String> right); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2351 | |
| 2352 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2353 | * Creates a new external string using the data defined in the given |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2354 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2359 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2360 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2365 | |
| 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2370 | * character contents need to be equivalent to this string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2371 | * Returns true if the string has been changed to be an external string. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2372 | * The string is not modified if the operation fails. See NewExternal for |
| 2373 | * information on the lifetime of the resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2374 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2375 | bool MakeExternal(ExternalStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2376 | |
| 2377 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2378 | * Creates a new external string using the one-byte data defined in the given |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2379 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2384 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2385 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2391 | |
| 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2396 | * character contents need to be equivalent to this string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2397 | * Returns true if the string has been changed to be an external string. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2398 | * The string is not modified if the operation fails. See NewExternal for |
| 2399 | * information on the lifetime of the resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2400 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2401 | bool MakeExternal(ExternalOneByteStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2402 | |
| 2403 | /** |
| 2404 | * Returns true if this string can be made external. |
| 2405 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2406 | bool CanMakeExternal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2407 | |
| 2408 | /** |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2409 | * Converts an object to a UTF-8-encoded character array. Useful if |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2410 | * you want to print the object. If conversion to a string fails |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2411 | * (e.g. due to an exception in the toString() method of the object) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2412 | * then the length() method returns 0 and the * operator returns |
| 2413 | * NULL. |
| 2414 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2415 | class V8_EXPORT Utf8Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2416 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2417 | explicit Utf8Value(Local<v8::Value> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2418 | ~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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2432 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2437 | class V8_EXPORT Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2438 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2439 | explicit Value(Local<v8::Value> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2440 | ~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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2452 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2453 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2454 | 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 | */ |
| 2466 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2472 | static Local<Symbol> New(Isolate* isolate, |
| 2473 | Local<String> name = Local<String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2474 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2489 | static Local<Symbol> GetToStringTag(Isolate* isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2490 | static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2491 | |
| 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 | */ |
| 2505 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2511 | static Local<Private> New(Isolate* isolate, |
| 2512 | Local<String> name = Local<String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2513 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2521 | static Local<Private> ForApi(Isolate* isolate, Local<String> name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2522 | |
| 2523 | private: |
| 2524 | Private(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2525 | }; |
| 2526 | |
| 2527 | |
| 2528 | /** |
| 2529 | * A JavaScript number value (ECMA-262, 4.3.20) |
| 2530 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2531 | class V8_EXPORT Number : public Primitive { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2532 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2533 | double Value() const; |
| 2534 | static Local<Number> New(Isolate* isolate, double value); |
| 2535 | V8_INLINE static Number* Cast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2536 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2537 | Number(); |
| 2538 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2539 | }; |
| 2540 | |
| 2541 | |
| 2542 | /** |
| 2543 | * A JavaScript value representing a signed integer. |
| 2544 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2545 | class V8_EXPORT Integer : public Number { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2546 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2547 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2551 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2552 | Integer(); |
| 2553 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2554 | }; |
| 2555 | |
| 2556 | |
| 2557 | /** |
| 2558 | * A JavaScript value representing a 32-bit signed integer. |
| 2559 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2560 | class V8_EXPORT Int32 : public Integer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2561 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2562 | int32_t Value() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2563 | V8_INLINE static Int32* Cast(v8::Value* obj); |
| 2564 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2565 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2566 | Int32(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2567 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2568 | }; |
| 2569 | |
| 2570 | |
| 2571 | /** |
| 2572 | * A JavaScript value representing a 32-bit unsigned integer. |
| 2573 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2574 | class V8_EXPORT Uint32 : public Integer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2575 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2576 | uint32_t Value() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2577 | V8_INLINE static Uint32* Cast(v8::Value* obj); |
| 2578 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2579 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2580 | Uint32(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2581 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2582 | }; |
| 2583 | |
| 2584 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2585 | enum PropertyAttribute { |
| 2586 | None = 0, |
| 2587 | ReadOnly = 1 << 0, |
| 2588 | DontEnum = 1 << 1, |
| 2589 | DontDelete = 1 << 2 |
| 2590 | }; |
| 2591 | |
| 2592 | /** |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2593 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2597 | typedef void (*AccessorGetterCallback)( |
| 2598 | Local<String> property, |
| 2599 | const PropertyCallbackInfo<Value>& info); |
| 2600 | typedef void (*AccessorNameGetterCallback)( |
| 2601 | Local<Name> property, |
| 2602 | const PropertyCallbackInfo<Value>& info); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2603 | |
| 2604 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2605 | typedef void (*AccessorSetterCallback)( |
| 2606 | Local<String> property, |
| 2607 | Local<Value> value, |
| 2608 | const PropertyCallbackInfo<void>& info); |
| 2609 | typedef void (*AccessorNameSetterCallback)( |
| 2610 | Local<Name> property, |
| 2611 | Local<Value> value, |
| 2612 | const PropertyCallbackInfo<void>& info); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2613 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2622 | * TODO(dcarney): Remove PROHIBITS_OVERWRITING as it is now unused. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2623 | */ |
| 2624 | enum 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2633 | * A JavaScript object (ECMA-262, 4.3.3) |
| 2634 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2635 | class V8_EXPORT Object : public Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2636 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2637 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2641 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2642 | 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2670 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2671 | // Sets an own property on this object bypassing interceptors and |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2672 | // 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2679 | V8_DEPRECATED("Use CreateDataProperty / DefineOwnProperty", |
| 2680 | bool ForceSet(Local<Value> key, Local<Value> value, |
| 2681 | PropertyAttribute attribs = None)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 2682 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2686 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2687 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2690 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2691 | 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2694 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2695 | /** |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2700 | 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2704 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2705 | /** |
| 2706 | * Returns Object.getOwnPropertyDescriptor as per ES5 section 15.2.3.3. |
| 2707 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2708 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2712 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2713 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2716 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2717 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2720 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2721 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2723 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2724 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2727 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2728 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2749 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2750 | void SetAccessorProperty(Local<Name> name, Local<Function> getter, |
| 2751 | Local<Function> setter = Local<Function>(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2752 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2758 | * Note: Private properties are not inherited. Do not rely on this, since it |
| 2759 | * may change. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2760 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2761 | 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 Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2766 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2767 | /** |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2773 | V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetPropertyNames()); |
| 2774 | V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames( |
| 2775 | Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2776 | |
| 2777 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2778 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2782 | V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetOwnPropertyNames()); |
| 2783 | V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames( |
| 2784 | Local<Context> context); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2785 | |
| 2786 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2787 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2791 | Local<Value> GetPrototype(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2792 | |
| 2793 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2794 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2798 | 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 Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2801 | |
| 2802 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2803 | * Finds an instance of the given function template in the prototype |
| 2804 | * chain. |
| 2805 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2806 | Local<Object> FindInstanceInPrototypeChain(Local<FunctionTemplate> tmpl); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2807 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2813 | V8_DEPRECATED("Use maybe version", Local<String> ObjectProtoToString()); |
| 2814 | V8_WARN_UNUSED_RESULT MaybeLocal<String> ObjectProtoToString( |
| 2815 | Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2816 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2817 | /** |
| 2818 | * Returns the name of the function invoked as a constructor for this object. |
| 2819 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2820 | Local<String> GetConstructorName(); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2821 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2822 | /** Gets the number of internal fields for this Object. */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2823 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2834 | /** Sets the value in an internal field. */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2835 | void SetInternalField(int index, Local<Value> value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2836 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2837 | /** |
| 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2843 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2844 | /** 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2856 | |
| 2857 | // Testers for local properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2858 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2873 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2878 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2896 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2902 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2917 | |
| 2918 | /** Tests for a named lookup interceptor.*/ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2919 | bool HasNamedLookupInterceptor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2920 | |
| 2921 | /** Tests for an index lookup interceptor.*/ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2922 | bool HasIndexedLookupInterceptor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2923 | |
| 2924 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2925 | * Returns the identity hash for this object. The current implementation |
| 2926 | * uses a hidden property on the object to store the identity hash. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2927 | * |
| 2928 | * The return value will never be 0. Also, it is not guaranteed to be |
| 2929 | * unique. |
| 2930 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2931 | int GetIdentityHash(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2932 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2933 | 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2939 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2940 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2941 | * Clone this object with a fast but shallow copy. Values will point |
| 2942 | * to the same values as the original object. |
| 2943 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2944 | // TODO(dcarney): take an isolate and optionally bail out? |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2945 | Local<Object> Clone(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2946 | |
| 2947 | /** |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2948 | * Returns the context in which the object was created. |
| 2949 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2950 | Local<Context> CreationContext(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2951 | |
| 2952 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2953 | * Checks whether a callback is set by the |
| 2954 | * ObjectTemplate::SetCallAsFunctionHandler method. |
| 2955 | * When an Object is callable this method returns true. |
| 2956 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2957 | bool IsCallable(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2958 | |
| 2959 | /** |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2960 | * Call an Object as a function if a callback is set by the |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2961 | * ObjectTemplate::SetCallAsFunctionHandler method. |
| 2962 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2963 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2970 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2976 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2980 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2981 | /** |
| 2982 | * Return the isolate to which the Object belongs to. |
| 2983 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2984 | V8_DEPRECATE_SOON("Keep track of isolate correctly", Isolate* GetIsolate()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2985 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2986 | static Local<Object> New(Isolate* isolate); |
| 2987 | |
| 2988 | V8_INLINE static Object* Cast(Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2989 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2990 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2991 | Object(); |
| 2992 | static void CheckCast(Value* obj); |
| 2993 | Local<Value> SlowGetInternalField(int index); |
| 2994 | void* SlowGetAlignedPointerFromInternalField(int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2995 | }; |
| 2996 | |
| 2997 | |
| 2998 | /** |
| 2999 | * An instance of the built-in array constructor (ECMA-262, 15.4.2). |
| 3000 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3001 | class V8_EXPORT Array : public Object { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3002 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3003 | uint32_t Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3004 | |
| 3005 | /** |
| 3006 | * Clones an element at index |index|. Returns an empty |
| 3007 | * handle if cloning fails (for any reason). |
| 3008 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3009 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3014 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3015 | /** |
| 3016 | * Creates a JavaScript array with the given length. If the length |
| 3017 | * is negative the returned array will have length 0. |
| 3018 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3019 | static Local<Array> New(Isolate* isolate, int length = 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3020 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3021 | V8_INLINE static Array* Cast(Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3022 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3023 | Array(); |
| 3024 | static void CheckCast(Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3025 | }; |
| 3026 | |
| 3027 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3028 | /** |
| 3029 | * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1). |
| 3030 | */ |
| 3031 | class 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 | */ |
| 3067 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3096 | template<typename T> |
| 3097 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3103 | // 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3111 | // 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3131 | template <class F, class G, class H> |
| 3132 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3133 | 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 | */ |
| 3146 | template<typename T> |
| 3147 | class FunctionCallbackInfo { |
| 3148 | public: |
| 3149 | V8_INLINE int Length() const; |
| 3150 | V8_INLINE Local<Value> operator[](int i) const; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 3151 | V8_INLINE V8_DEPRECATED("Use Data() to explicitly pass Callee instead", |
| 3152 | Local<Function> Callee() const); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3153 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3180 | int is_construct_call_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3181 | }; |
| 3182 | |
| 3183 | |
| 3184 | /** |
| 3185 | * The information passed to a property callback about the context |
| 3186 | * of the property access. |
| 3187 | */ |
| 3188 | template<typename T> |
| 3189 | class 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 3196 | V8_INLINE bool ShouldThrowOnError() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3197 | // This shouldn't be public, but the arm compiler needs it. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 3198 | static const int kArgsLength = 7; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3199 | |
| 3200 | protected: |
| 3201 | friend class MacroAssembler; |
| 3202 | friend class internal::PropertyCallbackArguments; |
| 3203 | friend class internal::CustomArguments<PropertyCallbackInfo>; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 3204 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3211 | |
| 3212 | V8_INLINE PropertyCallbackInfo(internal::Object** args) : args_(args) {} |
| 3213 | internal::Object** args_; |
| 3214 | }; |
| 3215 | |
| 3216 | |
| 3217 | typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info); |
| 3218 | |
| 3219 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3220 | /** |
| 3221 | * A JavaScript function object (ECMA-262, 15.3). |
| 3222 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3223 | class V8_EXPORT Function : public Object { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3224 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3225 | /** |
| 3226 | * Create a function in the current execution context |
| 3227 | * for a given FunctionCallback. |
| 3228 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3229 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3237 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3238 | 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 Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3258 | |
| 3259 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3260 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3265 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3272 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3277 | Local<Value> GetDisplayName() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3278 | |
| 3279 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3280 | * Returns zero based line number of function body and |
| 3281 | * kLineOffsetNotFound if no information available. |
| 3282 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3283 | int GetScriptLineNumber() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3284 | /** |
| 3285 | * Returns zero based column number of function body and |
| 3286 | * kLineOffsetNotFound if no information available. |
| 3287 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3288 | 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3309 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3310 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3311 | 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 | */ |
| 3320 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3327 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3331 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3341 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3348 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3362 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3377 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3378 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3384 | V8_INLINE static Promise* Cast(Value* obj); |
| 3385 | |
| 3386 | private: |
| 3387 | Promise(); |
| 3388 | static void CheckCast(Value* obj); |
| 3389 | }; |
| 3390 | |
| 3391 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3392 | /** |
| 3393 | * An instance of the built-in Proxy constructor (ECMA-262, 6th Edition, |
| 3394 | * 26.2.1). |
| 3395 | */ |
| 3396 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3418 | #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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3423 | |
| 3424 | enum class ArrayBufferCreationMode { kInternalized, kExternalized }; |
| 3425 | |
| 3426 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3427 | /** |
| 3428 | * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5). |
| 3429 | * This API is experimental and may change significantly. |
| 3430 | */ |
| 3431 | class V8_EXPORT ArrayBuffer : public Object { |
| 3432 | public: |
| 3433 | /** |
| 3434 | * Allocator that V8 uses to allocate |ArrayBuffer|'s memory. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3435 | * The allocator is a global V8 setting. It has to be set via |
| 3436 | * Isolate::CreateParams. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3437 | * |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3468 | * Allocator::Allocate that is set via Isolate::CreateParams. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3469 | * |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3502 | * The created array buffer is by default immediately in externalized state. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3503 | * The memory block will not be reclaimed when a created ArrayBuffer |
| 3504 | * is garbage-collected. |
| 3505 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3506 | static Local<ArrayBuffer> New( |
| 3507 | Isolate* isolate, void* data, size_t byte_length, |
| 3508 | ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3509 | |
| 3510 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3511 | * Returns true if ArrayBuffer is externalized, that is, does not |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3512 | * own its memory block. |
| 3513 | */ |
| 3514 | bool IsExternal() const; |
| 3515 | |
| 3516 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3517 | * Returns true if this ArrayBuffer may be neutered. |
| 3518 | */ |
| 3519 | bool IsNeuterable() const; |
| 3520 | |
| 3521 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3522 | * 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3525 | * ArrayBuffer should have been externalized and must be neuterable. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3526 | */ |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3536 | * that has been set via Isolate::CreateParams. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3537 | */ |
| 3538 | Contents Externalize(); |
| 3539 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3540 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3552 | 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 | */ |
| 3574 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3589 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3606 | 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 | */ |
| 3622 | class 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 | */ |
| 3642 | class V8_EXPORT Uint8Array : public TypedArray { |
| 3643 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3644 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3647 | 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 | */ |
| 3660 | class V8_EXPORT Uint8ClampedArray : public TypedArray { |
| 3661 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3662 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3667 | 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 | */ |
| 3678 | class V8_EXPORT Int8Array : public TypedArray { |
| 3679 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3680 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3684 | 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 | */ |
| 3696 | class V8_EXPORT Uint16Array : public TypedArray { |
| 3697 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3698 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3702 | 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 | */ |
| 3714 | class V8_EXPORT Int16Array : public TypedArray { |
| 3715 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3716 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3719 | 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 | */ |
| 3732 | class V8_EXPORT Uint32Array : public TypedArray { |
| 3733 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3734 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3738 | 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 | */ |
| 3750 | class V8_EXPORT Int32Array : public TypedArray { |
| 3751 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3752 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3755 | 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 | */ |
| 3768 | class V8_EXPORT Float32Array : public TypedArray { |
| 3769 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3770 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3774 | 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 | */ |
| 3786 | class V8_EXPORT Float64Array : public TypedArray { |
| 3787 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3788 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3792 | 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 | */ |
| 3804 | class V8_EXPORT DataView : public ArrayBufferView { |
| 3805 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3806 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3809 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3815 | }; |
| 3816 | |
| 3817 | |
| 3818 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3819 | * An instance of the built-in SharedArrayBuffer constructor. |
| 3820 | * This API is experimental and may change significantly. |
| 3821 | */ |
| 3822 | class 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3918 | * An instance of the built-in Date constructor (ECMA-262, 15.9). |
| 3919 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3920 | class V8_EXPORT Date : public Object { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3921 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3922 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3926 | |
| 3927 | /** |
| 3928 | * A specialization of Value::NumberValue that is more efficient |
| 3929 | * because we know the structure of this object. |
| 3930 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3931 | double ValueOf() const; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3932 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3933 | V8_INLINE static Date* Cast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3934 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3947 | static void DateTimeConfigurationChangeNotification(Isolate* isolate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3948 | |
| 3949 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3950 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3951 | }; |
| 3952 | |
| 3953 | |
| 3954 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3955 | * A Number object (ECMA-262, 4.3.21). |
| 3956 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3957 | class V8_EXPORT NumberObject : public Object { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3958 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3959 | static Local<Value> New(Isolate* isolate, double value); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3960 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3961 | double ValueOf() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3962 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3963 | V8_INLINE static NumberObject* Cast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3964 | |
| 3965 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3966 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3967 | }; |
| 3968 | |
| 3969 | |
| 3970 | /** |
| 3971 | * A Boolean object (ECMA-262, 4.3.15). |
| 3972 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3973 | class V8_EXPORT BooleanObject : public Object { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3974 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3975 | static Local<Value> New(Isolate* isolate, bool value); |
| 3976 | V8_DEPRECATED("Pass an isolate", static Local<Value> New(bool value)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3977 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3978 | bool ValueOf() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3979 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3980 | V8_INLINE static BooleanObject* Cast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3981 | |
| 3982 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3983 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3984 | }; |
| 3985 | |
| 3986 | |
| 3987 | /** |
| 3988 | * A String object (ECMA-262, 4.3.18). |
| 3989 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3990 | class V8_EXPORT StringObject : public Object { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3991 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3992 | static Local<Value> New(Local<String> value); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3993 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3994 | Local<String> ValueOf() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3995 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3996 | V8_INLINE static StringObject* Cast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3997 | |
| 3998 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3999 | 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 | */ |
| 4008 | class V8_EXPORT SymbolObject : public Object { |
| 4009 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4010 | static Local<Value> New(Isolate* isolate, Local<Symbol> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4011 | |
| 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4018 | }; |
| 4019 | |
| 4020 | |
| 4021 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4022 | * An instance of the built-in RegExp constructor (ECMA-262, 15.10). |
| 4023 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4024 | class V8_EXPORT RegExp : public Object { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4025 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4034 | kMultiline = 4, |
| 4035 | kSticky = 8, |
| 4036 | kUnicode = 16 |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4037 | }; |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4049 | 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4055 | |
| 4056 | /** |
| 4057 | * Returns the value of the source property: a string representing |
| 4058 | * the regular expression. |
| 4059 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4060 | Local<String> GetSource() const; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4061 | |
| 4062 | /** |
| 4063 | * Returns the flags bit field. |
| 4064 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4065 | Flags GetFlags() const; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4066 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4067 | V8_INLINE static RegExp* Cast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4068 | |
| 4069 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4070 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4071 | }; |
| 4072 | |
| 4073 | |
| 4074 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4075 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4077 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4078 | class V8_EXPORT External : public Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4079 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4080 | static Local<External> New(Isolate* isolate, void* value); |
| 4081 | V8_INLINE static External* Cast(Value* obj); |
| 4082 | void* Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4083 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4084 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4085 | }; |
| 4086 | |
| 4087 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4088 | #define V8_INTRINSICS_LIST(F) F(ArrayProto_values, array_values_iterator) |
| 4089 | |
| 4090 | enum 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4097 | // --- Templates --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4098 | |
| 4099 | |
| 4100 | /** |
| 4101 | * The superclass of object and function templates. |
| 4102 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4103 | class V8_EXPORT Template : public Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4104 | public: |
| 4105 | /** Adds a property to each instance created by this template.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4106 | void Set(Local<Name> name, Local<Data> value, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4107 | PropertyAttribute attributes = None); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4108 | V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4109 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4144 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4158 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4159 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4165 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4166 | private: |
| 4167 | Template(); |
| 4168 | |
| 4169 | friend class ObjectTemplate; |
| 4170 | friend class FunctionTemplate; |
| 4171 | }; |
| 4172 | |
| 4173 | |
| 4174 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4175 | * NamedProperty[Getter|Setter] are used as interceptors on object. |
| 4176 | * See ObjectTemplate::SetNamedPropertyHandler. |
| 4177 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4178 | typedef void (*NamedPropertyGetterCallback)( |
| 4179 | Local<String> property, |
| 4180 | const PropertyCallbackInfo<Value>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4181 | |
| 4182 | |
| 4183 | /** |
| 4184 | * Returns the value if the setter intercepts the request. |
| 4185 | * Otherwise, returns an empty handle. |
| 4186 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4187 | typedef void (*NamedPropertySetterCallback)( |
| 4188 | Local<String> property, |
| 4189 | Local<Value> value, |
| 4190 | const PropertyCallbackInfo<Value>& info); |
| 4191 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4192 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4193 | /** |
| 4194 | * Returns a non-empty handle if the interceptor intercepts the request. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4195 | * The result is an integer encoding property attributes (like v8::None, |
| 4196 | * v8::DontEnum, etc.) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4197 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4198 | typedef void (*NamedPropertyQueryCallback)( |
| 4199 | Local<String> property, |
| 4200 | const PropertyCallbackInfo<Integer>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4201 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4208 | typedef void (*NamedPropertyDeleterCallback)( |
| 4209 | Local<String> property, |
| 4210 | const PropertyCallbackInfo<Boolean>& info); |
| 4211 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4212 | |
| 4213 | /** |
| 4214 | * Returns an array containing the names of the properties the named |
| 4215 | * property getter intercepts. |
| 4216 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4217 | typedef void (*NamedPropertyEnumeratorCallback)( |
| 4218 | const PropertyCallbackInfo<Array>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4219 | |
| 4220 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4221 | // 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 | */ |
| 4227 | typedef 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 | */ |
| 4235 | typedef 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 | */ |
| 4245 | typedef 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 | */ |
| 4254 | typedef 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 | */ |
| 4262 | typedef void (*GenericNamedPropertyEnumeratorCallback)( |
| 4263 | const PropertyCallbackInfo<Array>& info); |
| 4264 | |
| 4265 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4266 | /** |
| 4267 | * Returns the value of the property if the getter intercepts the |
| 4268 | * request. Otherwise, returns an empty handle. |
| 4269 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4270 | typedef void (*IndexedPropertyGetterCallback)( |
| 4271 | uint32_t index, |
| 4272 | const PropertyCallbackInfo<Value>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4273 | |
| 4274 | |
| 4275 | /** |
| 4276 | * Returns the value if the setter intercepts the request. |
| 4277 | * Otherwise, returns an empty handle. |
| 4278 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4279 | typedef void (*IndexedPropertySetterCallback)( |
| 4280 | uint32_t index, |
| 4281 | Local<Value> value, |
| 4282 | const PropertyCallbackInfo<Value>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4283 | |
| 4284 | |
| 4285 | /** |
| 4286 | * Returns a non-empty handle if the interceptor intercepts the request. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4287 | * The result is an integer encoding property attributes. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4288 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4289 | typedef void (*IndexedPropertyQueryCallback)( |
| 4290 | uint32_t index, |
| 4291 | const PropertyCallbackInfo<Integer>& info); |
| 4292 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4293 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4299 | typedef void (*IndexedPropertyDeleterCallback)( |
| 4300 | uint32_t index, |
| 4301 | const PropertyCallbackInfo<Boolean>& info); |
| 4302 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4303 | |
| 4304 | /** |
| 4305 | * Returns an array containing the indices of the properties the |
| 4306 | * indexed property getter intercepts. |
| 4307 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4308 | typedef void (*IndexedPropertyEnumeratorCallback)( |
| 4309 | const PropertyCallbackInfo<Array>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4310 | |
| 4311 | |
| 4312 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4313 | * Access type specification. |
| 4314 | */ |
| 4315 | enum AccessType { |
| 4316 | ACCESS_GET, |
| 4317 | ACCESS_SET, |
| 4318 | ACCESS_HAS, |
| 4319 | ACCESS_DELETE, |
| 4320 | ACCESS_KEYS |
| 4321 | }; |
| 4322 | |
| 4323 | |
| 4324 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4325 | * Returns true if the given context should be allowed to access the given |
| 4326 | * object. |
| 4327 | */ |
| 4328 | typedef bool (*AccessCheckCallback)(Local<Context> accessing_context, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 4329 | Local<Object> accessed_object, |
| 4330 | Local<Value> data); |
| 4331 | typedef bool (*DeprecatedAccessCheckCallback)(Local<Context> accessing_context, |
| 4332 | Local<Object> accessed_object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4333 | |
| 4334 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4335 | * Returns true if cross-context access should be allowed to the named |
| 4336 | * property with the given key on the host object. |
| 4337 | */ |
| 4338 | typedef 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 | */ |
| 4348 | typedef 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4362 | * Any modification of a FunctionTemplate after first instantiation will trigger |
| 4363 | *a crash. |
| 4364 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4365 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4449 | class V8_EXPORT FunctionTemplate : public Template { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4450 | public: |
| 4451 | /** Creates a function template.*/ |
| 4452 | static Local<FunctionTemplate> New( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4453 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4466 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4467 | /** Returns the unique function instance in the current execution context.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4468 | V8_DEPRECATE_SOON("Use maybe version", Local<Function> GetFunction()); |
| 4469 | V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction( |
| 4470 | Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4471 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4477 | void SetCallHandler( |
| 4478 | FunctionCallback callback, Local<Value> data = Local<Value>(), |
| 4479 | experimental::FastAccessorBuilder* fast_handler = nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4480 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4481 | /** Set the predefined length property for the FunctionTemplate. */ |
| 4482 | void SetLength(int length); |
| 4483 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4484 | /** Get the InstanceTemplate. */ |
| 4485 | Local<ObjectTemplate> InstanceTemplate(); |
| 4486 | |
| 4487 | /** Causes the function template to inherit from a parent function template.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4488 | void Inherit(Local<FunctionTemplate> parent); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4489 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4496 | /** |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4501 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4509 | |
| 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 Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4525 | * Sets the ReadOnly flag in the attributes of the 'prototype' property |
| 4526 | * of functions created from this FunctionTemplate to true. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4527 | */ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4528 | void ReadOnlyPrototype(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4529 | |
| 4530 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4531 | * Removes the prototype property from functions created from this |
| 4532 | * FunctionTemplate. |
| 4533 | */ |
| 4534 | void RemovePrototype(); |
| 4535 | |
| 4536 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4537 | * Returns true if the given object is an instance of this function |
| 4538 | * template. |
| 4539 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4540 | bool HasInstance(Local<Value> object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4541 | |
| 4542 | private: |
| 4543 | FunctionTemplate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4544 | friend class Context; |
| 4545 | friend class ObjectTemplate; |
| 4546 | }; |
| 4547 | |
| 4548 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4549 | enum 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4560 | |
| 4561 | |
| 4562 | struct 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4570 | Local<Value> data = Local<Value>(), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4571 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4585 | Local<Value> data; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4586 | PropertyHandlerFlags flags; |
| 4587 | }; |
| 4588 | |
| 4589 | |
| 4590 | struct 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4598 | Local<Value> data = Local<Value>(), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4599 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4613 | Local<Value> data; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4614 | PropertyHandlerFlags flags; |
| 4615 | }; |
| 4616 | |
| 4617 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4618 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4624 | class V8_EXPORT ObjectTemplate : public Template { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4625 | public: |
| 4626 | /** Creates an ObjectTemplate. */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4627 | static Local<ObjectTemplate> New( |
| 4628 | Isolate* isolate, |
| 4629 | Local<FunctionTemplate> constructor = Local<FunctionTemplate>()); |
| 4630 | static V8_DEPRECATED("Use isolate version", Local<ObjectTemplate> New()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4631 | |
| 4632 | /** Creates a new instance of this template.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4633 | V8_DEPRECATE_SOON("Use maybe version", Local<Object> NewInstance()); |
| 4634 | V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4635 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4659 | * \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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4664 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4665 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4675 | |
| 4676 | /** |
| 4677 | * Sets a named property handler on the object template. |
| 4678 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4679 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4681 | * accessing the property directly on the JavaScript object. |
| 4682 | * |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4683 | * Note that new code should use the second version that can intercept |
| 4684 | * symbol-named properties as well as string-named properties. |
| 4685 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4686 | * \param getter The callback to invoke when getting a property. |
| 4687 | * \param setter The callback to invoke when setting a property. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4688 | * \param query The callback to invoke to check if a property is present, |
| 4689 | * and if present, get its attributes. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4690 | * \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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4696 | // TODO(dcarney): deprecate |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4697 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4703 | void SetHandler(const NamedPropertyHandlerConfiguration& configuration); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4704 | |
| 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4714 | * \param query The callback to invoke to check if an object has a property. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4715 | * \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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4721 | void SetHandler(const IndexedPropertyHandlerConfiguration& configuration); |
| 4722 | // TODO(dcarney): deprecate |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4723 | void SetIndexedPropertyHandler( |
| 4724 | IndexedPropertyGetterCallback getter, |
| 4725 | IndexedPropertySetterCallback setter = 0, |
| 4726 | IndexedPropertyQueryCallback query = 0, |
| 4727 | IndexedPropertyDeleterCallback deleter = 0, |
| 4728 | IndexedPropertyEnumeratorCallback enumerator = 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4729 | Local<Value> data = Local<Value>()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4730 | SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query, |
| 4731 | deleter, enumerator, data)); |
| 4732 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4733 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4739 | void SetCallAsFunctionHandler(FunctionCallback callback, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4740 | Local<Value> data = Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4741 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4753 | * Sets access check callback on the object template and enables access |
| 4754 | * checks. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4755 | * |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4759 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4760 | void SetAccessCheckCallback(AccessCheckCallback callback, |
| 4761 | Local<Value> data = Local<Value>()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 4762 | V8_DEPRECATED( |
| 4763 | "Use SetAccessCheckCallback with new AccessCheckCallback signature.", |
| 4764 | void SetAccessCheckCallback(DeprecatedAccessCheckCallback callback, |
| 4765 | Local<Value> data = Local<Value>())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4766 | |
| 4767 | V8_DEPRECATED( |
| 4768 | "Use SetAccessCheckCallback instead", |
| 4769 | void SetAccessCheckCallbacks(NamedSecurityCallback named_handler, |
| 4770 | IndexedSecurityCallback indexed_handler, |
| 4771 | Local<Value> data = Local<Value>())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4772 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4787 | static Local<ObjectTemplate> New(internal::Isolate* isolate, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4788 | Local<FunctionTemplate> constructor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4789 | friend class FunctionTemplate; |
| 4790 | }; |
| 4791 | |
| 4792 | |
| 4793 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4794 | * A Signature specifies which receiver is valid for a function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4795 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4796 | class V8_EXPORT Signature : public Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4797 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4798 | static Local<Signature> New( |
| 4799 | Isolate* isolate, |
| 4800 | Local<FunctionTemplate> receiver = Local<FunctionTemplate>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4801 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4802 | private: |
| 4803 | Signature(); |
| 4804 | }; |
| 4805 | |
| 4806 | |
| 4807 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4808 | * An AccessorSignature specifies which receivers are valid parameters |
| 4809 | * to an accessor callback. |
| 4810 | */ |
| 4811 | class V8_EXPORT AccessorSignature : public Data { |
| 4812 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4813 | static Local<AccessorSignature> New( |
| 4814 | Isolate* isolate, |
| 4815 | Local<FunctionTemplate> receiver = Local<FunctionTemplate>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4816 | |
| 4817 | private: |
| 4818 | AccessorSignature(); |
| 4819 | }; |
| 4820 | |
| 4821 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4822 | // --- Extensions --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4823 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4824 | class V8_EXPORT ExternalOneByteStringResourceImpl |
| 4825 | : public String::ExternalOneByteStringResource { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4826 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4827 | ExternalOneByteStringResourceImpl() : data_(0), length_(0) {} |
| 4828 | ExternalOneByteStringResourceImpl(const char* data, size_t length) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4829 | : 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4837 | |
| 4838 | /** |
| 4839 | * Ignore |
| 4840 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4841 | class V8_EXPORT Extension { // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4842 | public: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4843 | // Note that the strings passed into this constructor must live as long |
| 4844 | // as the Extension itself. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4845 | Extension(const char* name, |
| 4846 | const char* source = 0, |
| 4847 | int dep_count = 0, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4848 | const char** deps = 0, |
| 4849 | int source_length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4850 | virtual ~Extension() { } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4851 | virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( |
| 4852 | v8::Isolate* isolate, v8::Local<v8::String> name) { |
| 4853 | return v8::Local<v8::FunctionTemplate>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4856 | const char* name() const { return name_; } |
| 4857 | size_t source_length() const { return source_length_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4858 | const String::ExternalOneByteStringResource* source() const { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4859 | return &source_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4860 | 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4867 | size_t source_length_; // expected to initialize before source_ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4868 | ExternalOneByteStringResourceImpl source_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4869 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4879 | void V8_EXPORT RegisterExtension(Extension* extension); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4880 | |
| 4881 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4882 | // --- Statics --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4883 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4884 | V8_INLINE Local<Primitive> Undefined(Isolate* isolate); |
| 4885 | V8_INLINE Local<Primitive> Null(Isolate* isolate); |
| 4886 | V8_INLINE Local<Boolean> True(Isolate* isolate); |
| 4887 | V8_INLINE Local<Boolean> False(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4888 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4899 | class V8_EXPORT ResourceConstraints { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4900 | public: |
| 4901 | ResourceConstraints(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4902 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4911 | */ |
| 4912 | void ConfigureDefaults(uint64_t physical_memory, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4913 | uint64_t virtual_memory_limit); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4914 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4917 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4919 | int max_executable_size() const { return max_executable_size_; } |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 4920 | void set_max_executable_size(int value) { max_executable_size_ = value; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4921 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4924 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4929 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4930 | int max_semi_space_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4931 | int max_old_space_size_; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 4932 | int max_executable_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4933 | uint32_t* stack_limit_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4934 | size_t code_range_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4935 | }; |
| 4936 | |
| 4937 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4938 | // --- Exceptions --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4939 | |
| 4940 | |
| 4941 | typedef void (*FatalErrorCallback)(const char* location, const char* message); |
| 4942 | |
| 4943 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4944 | typedef void (*MessageCallback)(Local<Message> message, Local<Value> error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4945 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4946 | // --- Tracing --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4947 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4948 | typedef void (*LogEventCallback)(const char* name, int event); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4949 | |
| 4950 | /** |
| 4951 | * Create new error objects by calling the corresponding error object |
| 4952 | * constructor with the message. |
| 4953 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4954 | class V8_EXPORT Exception { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4955 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4956 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4961 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4967 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4970 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4975 | static Local<StackTrace> GetStackTrace(Local<Value> exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4976 | }; |
| 4977 | |
| 4978 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4979 | // --- Counters Callbacks --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4980 | |
| 4981 | typedef int* (*CounterLookupCallback)(const char* name); |
| 4982 | |
| 4983 | typedef void* (*CreateHistogramCallback)(const char* name, |
| 4984 | int min, |
| 4985 | int max, |
| 4986 | size_t buckets); |
| 4987 | |
| 4988 | typedef void (*AddHistogramSampleCallback)(void* histogram, int sample); |
| 4989 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4990 | // --- Memory Allocation Callback --- |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4991 | enum ObjectSpace { |
| 4992 | kObjectSpaceNewSpace = 1 << 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4993 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5000 | }; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5001 | |
| 5002 | enum AllocationAction { |
| 5003 | kAllocationActionAllocate = 1 << 0, |
| 5004 | kAllocationActionFree = 1 << 1, |
| 5005 | kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree |
| 5006 | }; |
| 5007 | |
| 5008 | typedef void (*MemoryAllocationCallback)(ObjectSpace space, |
| 5009 | AllocationAction action, |
| 5010 | int size); |
| 5011 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 5012 | // --- Enter/Leave Script Callback --- |
| 5013 | typedef void (*BeforeCallEnteredCallback)(Isolate*); |
| 5014 | typedef void (*CallCompletedCallback)(Isolate*); |
| 5015 | typedef void (*DeprecatedCallCompletedCallback)(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5016 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5017 | // --- Promise Reject Callback --- |
| 5018 | enum PromiseRejectEvent { |
| 5019 | kPromiseRejectWithNoHandler = 0, |
| 5020 | kPromiseHandlerAddedAfterReject = 1 |
| 5021 | }; |
| 5022 | |
| 5023 | class PromiseRejectMessage { |
| 5024 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5025 | PromiseRejectMessage(Local<Promise> promise, PromiseRejectEvent event, |
| 5026 | Local<Value> value, Local<StackTrace> stack_trace) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5027 | : promise_(promise), |
| 5028 | event_(event), |
| 5029 | value_(value), |
| 5030 | stack_trace_(stack_trace) {} |
| 5031 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5032 | V8_INLINE Local<Promise> GetPromise() const { return promise_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5033 | V8_INLINE PromiseRejectEvent GetEvent() const { return event_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5034 | V8_INLINE Local<Value> GetValue() const { return value_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5035 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5036 | V8_DEPRECATED("Use v8::Exception::CreateMessage(GetValue())->GetStackTrace()", |
| 5037 | V8_INLINE Local<StackTrace> GetStackTrace() const) { |
| 5038 | return stack_trace_; |
| 5039 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5040 | |
| 5041 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5042 | Local<Promise> promise_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5043 | PromiseRejectEvent event_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5044 | Local<Value> value_; |
| 5045 | Local<StackTrace> stack_trace_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5046 | }; |
| 5047 | |
| 5048 | typedef void (*PromiseRejectCallback)(PromiseRejectMessage message); |
| 5049 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5050 | // --- Microtask Callback --- |
| 5051 | typedef void (*MicrotaskCallback)(void* data); |
| 5052 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5053 | // --- Failed Access Check Callback --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5054 | typedef void (*FailedAccessCheckCallback)(Local<Object> target, |
| 5055 | AccessType type, |
| 5056 | Local<Value> data); |
| 5057 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5058 | // --- AllowCodeGenerationFromStrings callbacks --- |
| 5059 | |
| 5060 | /** |
| 5061 | * Callback to check if code generation from strings is allowed. See |
| 5062 | * Context::AllowCodeGenerationFromStrings. |
| 5063 | */ |
| 5064 | typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context); |
| 5065 | |
| 5066 | // --- Garbage Collection Callbacks --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5067 | |
| 5068 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5069 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5074 | */ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5075 | enum GCType { |
| 5076 | kGCTypeScavenge = 1 << 0, |
| 5077 | kGCTypeMarkSweepCompact = 1 << 1, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5078 | kGCTypeIncrementalMarking = 1 << 2, |
| 5079 | kGCTypeProcessWeakCallbacks = 1 << 3, |
| 5080 | kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact | |
| 5081 | kGCTypeIncrementalMarking | kGCTypeProcessWeakCallbacks |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5082 | }; |
| 5083 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 5084 | /** |
| 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5096 | enum GCCallbackFlags { |
| 5097 | kNoGCCallbackFlags = 0, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5098 | kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5099 | kGCCallbackFlagForced = 1 << 2, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 5100 | kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3, |
| 5101 | kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5102 | }; |
| 5103 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5104 | typedef void (*GCCallback)(GCType type, GCCallbackFlags flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5105 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5106 | typedef void (*InterruptCallback)(Isolate* isolate, void* data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5107 | |
| 5108 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5109 | /** |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5110 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5115 | class V8_EXPORT HeapStatistics { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5116 | public: |
| 5117 | HeapStatistics(); |
| 5118 | size_t total_heap_size() { return total_heap_size_; } |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 5119 | size_t total_heap_size_executable() { return total_heap_size_executable_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5120 | size_t total_physical_size() { return total_physical_size_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5121 | size_t total_available_size() { return total_available_size_; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5122 | size_t used_heap_size() { return used_heap_size_; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5123 | size_t heap_size_limit() { return heap_size_limit_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5124 | size_t does_zap_garbage() { return does_zap_garbage_; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5125 | |
| 5126 | private: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5127 | size_t total_heap_size_; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 5128 | size_t total_heap_size_executable_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5129 | size_t total_physical_size_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5130 | size_t total_available_size_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5131 | size_t used_heap_size_; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5132 | size_t heap_size_limit_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5133 | bool does_zap_garbage_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5134 | |
| 5135 | friend class V8; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5136 | friend class Isolate; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5137 | }; |
| 5138 | |
| 5139 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5140 | class 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 | |
| 5160 | class 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5178 | class RetainedObjectInfo; |
| 5179 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5180 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5181 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5182 | * 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5191 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5192 | typedef 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 | */ |
| 5200 | struct 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5223 | Local<UnboundScript> script; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5224 | // 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 | */ |
| 5262 | enum 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 | */ |
| 5274 | typedef void (*JitCodeEventHandler)(const JitCodeEvent* event); |
| 5275 | |
| 5276 | |
| 5277 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5278 | * Interface for iterating through all external resources in the heap. |
| 5279 | */ |
| 5280 | class V8_EXPORT ExternalResourceVisitor { // NOLINT |
| 5281 | public: |
| 5282 | virtual ~ExternalResourceVisitor() {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5283 | virtual void VisitExternalString(Local<String> string) {} |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5284 | }; |
| 5285 | |
| 5286 | |
| 5287 | /** |
| 5288 | * Interface for iterating through all the persistent handles in the heap. |
| 5289 | */ |
| 5290 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5299 | * 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 | */ |
| 5306 | class V8_EXPORT Isolate { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5307 | public: |
| 5308 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5309 | * Initial configuration parameters for a new Isolate. |
| 5310 | */ |
| 5311 | struct CreateParams { |
| 5312 | CreateParams() |
| 5313 | : entry_hook(NULL), |
| 5314 | code_event_handler(NULL), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5315 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5320 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5342 | * Explicitly specify a startup snapshot blob. The embedder owns the blob. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5343 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5344 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5367 | }; |
| 5368 | |
| 5369 | |
| 5370 | /** |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5371 | * Stack-allocated class which sets the isolate for all operations |
| 5372 | * executed within a local scope. |
| 5373 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5374 | class V8_EXPORT Scope { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5375 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5390 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5458 | * Features reported via the SetUseCounterCallback callback. Do not change |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5459 | * assigned numbers of existing items; add new features to the end of this |
| 5460 | * list. |
| 5461 | */ |
| 5462 | enum UseCounterFeature { |
| 5463 | kUseAsm = 0, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5464 | kBreakIterator = 1, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5465 | 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 5483 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5495 | kUseCounterFeatureCount // This enum value must be last. |
| 5496 | }; |
| 5497 | |
| 5498 | typedef void (*UseCounterCallback)(Isolate* isolate, |
| 5499 | UseCounterFeature feature); |
| 5500 | |
| 5501 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5502 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5508 | * |
| 5509 | * V8::Initialize() must have run prior to this. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5510 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5511 | static Isolate* New(const CreateParams& params); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5512 | |
| 5513 | /** |
| 5514 | * Returns the entered isolate for the current thread or NULL in |
| 5515 | * case there is no current isolate. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5516 | * |
| 5517 | * This method must not be invoked before V8::Initialize() was invoked. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5518 | */ |
| 5519 | static Isolate* GetCurrent(); |
| 5520 | |
| 5521 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5522 | * 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5535 | * 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5561 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5562 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5571 | * Associate embedder-specific data with the isolate. |slot| has to be |
| 5572 | * between 0 and GetNumberOfDataSlots() - 1. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5573 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5574 | V8_INLINE void SetData(uint32_t slot, void* data); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5575 | |
| 5576 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5577 | * Retrieve embedder-specific data from the isolate. |
| 5578 | * Returns NULL if SetData has never been called for the given |slot|. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5579 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5580 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5594 | * 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5628 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5643 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5674 | /** |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5678 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5685 | V8_DEPRECATE_SOON( |
| 5686 | "Calling context concept is not compatible with tail calls, and will be " |
| 5687 | "removed.", |
| 5688 | Local<Context> GetCallingContext()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5689 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5690 | /** Returns the last context entered through V8's C++ API. */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5691 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5734 | typedef void (*GCCallback)(Isolate* isolate, GCType type, |
| 5735 | GCCallbackFlags flags); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5736 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5746 | void AddGCPrologueCallback(GCCallback callback, |
| 5747 | GCType gc_type_filter = kGCTypeAll); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5748 | |
| 5749 | /** |
| 5750 | * This function removes callback which was installed by |
| 5751 | * AddGCPrologueCallback function. |
| 5752 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5753 | void RemoveGCPrologueCallback(GCCallback callback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5754 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5764 | void AddGCEpilogueCallback(GCCallback callback, |
| 5765 | GCType gc_type_filter = kGCTypeAll); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5766 | |
| 5767 | /** |
| 5768 | * This function removes callback which was installed by |
| 5769 | * AddGCEpilogueCallback function. |
| 5770 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5771 | void RemoveGCEpilogueCallback(GCCallback callback); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5772 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5808 | /** |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5812 | * There may be a number of interrupt requests in flight. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5813 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5819 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5824 | * collection performance. Use IdleNotificationDeadline() or |
| 5825 | * LowMemoryNotification() instead to influence the garbage collection |
| 5826 | * schedule. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5827 | */ |
| 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 5836 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5849 | * 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 5856 | V8_DEPRECATE_SOON( |
| 5857 | "Use callback with parameter", |
| 5858 | void AddCallCompletedCallback(DeprecatedCallCompletedCallback callback)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5859 | |
| 5860 | /** |
| 5861 | * Removes callback that was installed by AddCallCompletedCallback. |
| 5862 | */ |
| 5863 | void RemoveCallCompletedCallback(CallCompletedCallback callback); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 5864 | V8_DEPRECATE_SOON( |
| 5865 | "Use callback with parameter", |
| 5866 | void RemoveCallCompletedCallback( |
| 5867 | DeprecatedCallCompletedCallback callback)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5868 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5875 | /** |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5884 | void EnqueueMicrotask(Local<Function> microtask); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5885 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5925 | * V8 uses the notification to perform garbage collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5926 | * This call can be used repeatedly if the embedder remains idle. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5927 | * Returns true if the embedder should stop calling IdleNotificationDeadline |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5928 | * 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5931 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5936 | */ |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5937 | bool IdleNotificationDeadline(double deadline_in_seconds); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5938 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5939 | V8_DEPRECATED("use IdleNotificationDeadline()", |
| 5940 | bool IdleNotification(int idle_time_in_ms)); |
| 5941 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5942 | /** |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5953 | * |
| 5954 | * The optional parameter |dependant_context| specifies whether the disposed |
| 5955 | * context was depending on state from other contexts or not. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5956 | */ |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5957 | int ContextDisposedNotification(bool dependant_context = true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5958 | |
| 5959 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5960 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5972 | * 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6006 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6007 | /** |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6048 | Local<Value> data = Local<Value>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6049 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6100 | /** |
| 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6107 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6108 | template <class K, class V, class Traits> |
| 6109 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6110 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6111 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6117 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6121 | void ReportExternalAllocationLimitReached(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6122 | }; |
| 6123 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6124 | class V8_EXPORT StartupData { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6125 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6126 | const char* data; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6127 | int raw_size; |
| 6128 | }; |
| 6129 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6130 | |
| 6131 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6132 | * EntropySource is used as a callback function when v8 needs a source |
| 6133 | * of entropy. |
| 6134 | */ |
| 6135 | typedef bool (*EntropySource)(unsigned char* buffer, size_t length); |
| 6136 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6137 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6143 | * |
| 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6150 | */ |
| 6151 | typedef uintptr_t (*ReturnAddressLocationResolver)( |
| 6152 | uintptr_t return_addr_location); |
| 6153 | |
| 6154 | |
| 6155 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6156 | * Container class for static utility functions. |
| 6157 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6158 | class V8_EXPORT V8 { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6159 | public: |
| 6160 | /** Set the callback to invoke in case of fatal errors. */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6161 | V8_INLINE static V8_DEPRECATED( |
| 6162 | "Use isolate version", |
| 6163 | void SetFatalErrorHandler(FatalErrorCallback that)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6164 | |
| 6165 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6166 | * Set the callback to invoke to check if code generation from |
| 6167 | * strings should be allowed. |
| 6168 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6169 | V8_INLINE static V8_DEPRECATED( |
| 6170 | "Use isolate version", void SetAllowCodeGenerationFromStringsCallback( |
| 6171 | AllowCodeGenerationFromStringsCallback that)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6172 | |
| 6173 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6174 | * Check if V8 is dead and therefore unusable. This is the case after |
| 6175 | * fatal errors such as out-of-memory situations. |
| 6176 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6177 | V8_INLINE static V8_DEPRECATED("Use isolate version", bool IsDead()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6178 | |
| 6179 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6180 | * 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6198 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6202 | static StartupData CreateSnapshotDataBlob(const char* custom_source = NULL); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6203 | |
| 6204 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6205 | * Adds a message listener. |
| 6206 | * |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6207 | * The same message listener can be added more than once and in that |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6208 | * case it will be called more than once for each message. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6209 | * |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6212 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6213 | V8_INLINE static V8_DEPRECATED( |
| 6214 | "Use isolate version", |
| 6215 | bool AddMessageListener(MessageCallback that, |
| 6216 | Local<Value> data = Local<Value>())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6217 | |
| 6218 | /** |
| 6219 | * Remove all message listeners from the specified callback function. |
| 6220 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6221 | V8_INLINE static V8_DEPRECATED( |
| 6222 | "Use isolate version", void RemoveMessageListeners(MessageCallback that)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6223 | |
| 6224 | /** |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6225 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6228 | 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 Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6233 | |
| 6234 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6235 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6249 | /** Callback function for reporting failed access checks.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6250 | V8_INLINE static V8_DEPRECATED( |
| 6251 | "Use isolate version", |
| 6252 | void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6253 | |
| 6254 | /** |
| 6255 | * Enables the host application to receive a notification before a |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6256 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6264 | static V8_DEPRECATED( |
| 6265 | "Use isolate version", |
| 6266 | void AddGCPrologueCallback(GCCallback callback, |
| 6267 | GCType gc_type_filter = kGCTypeAll)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6268 | |
| 6269 | /** |
| 6270 | * This function removes callback which was installed by |
| 6271 | * AddGCPrologueCallback function. |
| 6272 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6273 | V8_INLINE static V8_DEPRECATED( |
| 6274 | "Use isolate version", |
| 6275 | void RemoveGCPrologueCallback(GCCallback callback)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6276 | |
| 6277 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6278 | * Enables the host application to receive a notification after a |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6279 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6287 | static V8_DEPRECATED( |
| 6288 | "Use isolate version", |
| 6289 | void AddGCEpilogueCallback(GCCallback callback, |
| 6290 | GCType gc_type_filter = kGCTypeAll)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6291 | |
| 6292 | /** |
| 6293 | * This function removes callback which was installed by |
| 6294 | * AddGCEpilogueCallback function. |
| 6295 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6296 | V8_INLINE static V8_DEPRECATED( |
| 6297 | "Use isolate version", |
| 6298 | void RemoveGCEpilogueCallback(GCCallback callback)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6299 | |
| 6300 | /** |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6301 | * Enables the host application to provide a mechanism to be notified |
| 6302 | * and perform custom logging when V8 Allocates Executable Memory. |
| 6303 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6304 | V8_INLINE static V8_DEPRECATED( |
| 6305 | "Use isolate version", |
| 6306 | void AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 6307 | ObjectSpace space, |
| 6308 | AllocationAction action)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6309 | |
| 6310 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6311 | * Removes callback that was installed by AddMemoryAllocationCallback. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6312 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6313 | V8_INLINE static V8_DEPRECATED( |
| 6314 | "Use isolate version", |
| 6315 | void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6316 | |
| 6317 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6318 | * Initializes V8. This function needs to be called before the first Isolate |
| 6319 | * is created. It always returns true. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6320 | */ |
| 6321 | static bool Initialize(); |
| 6322 | |
| 6323 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6324 | * 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6330 | * 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6337 | * Forcefully terminate the current thread of JavaScript execution |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6338 | * in the given isolate. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6339 | * |
| 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6342 | * |
| 6343 | * \param isolate The isolate in which to terminate the current JS execution. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6344 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6345 | V8_INLINE static V8_DEPRECATED("Use isolate version", |
| 6346 | void TerminateExecution(Isolate* isolate)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6347 | |
| 6348 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6349 | * 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6355 | * |
| 6356 | * \param isolate The isolate in which to check. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6357 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6358 | V8_INLINE static V8_DEPRECATED( |
| 6359 | "Use isolate version", |
| 6360 | bool IsExecutionTerminating(Isolate* isolate = NULL)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6361 | |
| 6362 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6363 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6378 | V8_INLINE static V8_DEPRECATED( |
| 6379 | "Use isolate version", void CancelTerminateExecution(Isolate* isolate)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6380 | |
| 6381 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6382 | * 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6392 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6393 | * Iterates through all external resources referenced from current isolate |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6394 | * heap. GC is not invoked prior to iterating, therefore there is no |
| 6395 | * guarantee that visited objects are still alive. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6396 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6397 | V8_INLINE static V8_DEPRECATED( |
| 6398 | "Use isolate version", |
| 6399 | void VisitExternalResources(ExternalResourceVisitor* visitor)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6400 | |
| 6401 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6402 | * Iterates through all the persistent handles in the current isolate's heap |
| 6403 | * that have class_ids. |
| 6404 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6405 | V8_INLINE static V8_DEPRECATED( |
| 6406 | "Use isolate version", |
| 6407 | void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6408 | |
| 6409 | /** |
| 6410 | * Iterates through all the persistent handles in isolate's heap that have |
| 6411 | * class_ids. |
| 6412 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6413 | V8_INLINE static V8_DEPRECATED( |
| 6414 | "Use isolate version", |
| 6415 | void VisitHandlesWithClassIds(Isolate* isolate, |
| 6416 | PersistentHandleVisitor* visitor)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6417 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6425 | V8_INLINE static V8_DEPRECATED( |
| 6426 | "Use isolate version", |
| 6427 | void VisitHandlesForPartialDependence(Isolate* isolate, |
| 6428 | PersistentHandleVisitor* visitor)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6429 | |
| 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6433 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6434 | * If V8 was compiled with the ICU data in an external file, the location |
| 6435 | * of the data file has to be provided. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6436 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6437 | static bool InitializeICU(const char* icu_data_file = NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6438 | |
| 6439 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6440 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6459 | * Sets the v8::Platform to use. This should be invoked before V8 is |
| 6460 | * initialized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6461 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6462 | static void InitializePlatform(Platform* platform); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6463 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6464 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6465 | * Clears all references to the v8::Platform. This should be invoked after |
| 6466 | * V8 was disposed. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6467 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6468 | static void ShutdownPlatform(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6469 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6470 | private: |
| 6471 | V8(); |
| 6472 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6473 | static internal::Object** GlobalizeReference(internal::Isolate* isolate, |
| 6474 | internal::Object** handle); |
| 6475 | static internal::Object** CopyPersistent(internal::Object** handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6476 | static void DisposeGlobal(internal::Object** global_handle); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6477 | typedef WeakCallbackData<Value, void>::Callback WeakCallback; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6478 | static void MakeWeak(internal::Object** global_handle, void* data, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6479 | WeakCallback weak_callback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6480 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6489 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6494 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6495 | static void FromJustIsNothing(); |
| 6496 | static void ToLocalEmpty(); |
| 6497 | static void InternalFieldOutOfBounds(int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6498 | template <class T> friend class Local; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6499 | template <class T> |
| 6500 | friend class MaybeLocal; |
| 6501 | template <class T> |
| 6502 | friend class Maybe; |
| 6503 | template <class T> |
| 6504 | friend class WeakCallbackInfo; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6505 | template <class T> friend class Eternal; |
| 6506 | template <class T> friend class PersistentBase; |
| 6507 | template <class T, class M> friend class Persistent; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6508 | friend class Context; |
| 6509 | }; |
| 6510 | |
| 6511 | |
| 6512 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6513 | * 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 | */ |
| 6522 | template <class T> |
| 6523 | class 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 | |
| 6561 | template <class T> |
| 6562 | inline Maybe<T> Nothing() { |
| 6563 | return Maybe<T>(); |
| 6564 | } |
| 6565 | |
| 6566 | |
| 6567 | template <class T> |
| 6568 | inline Maybe<T> Just(const T& t) { |
| 6569 | return Maybe<T>(t); |
| 6570 | } |
| 6571 | |
| 6572 | |
| 6573 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6574 | * An external exception handler. |
| 6575 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6576 | class V8_EXPORT TryCatch { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6577 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6578 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6579 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6582 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6583 | V8_DEPRECATED("Use isolate version", TryCatch()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6584 | |
| 6585 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6586 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6593 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6603 | * For certain types of exceptions, it makes no sense to continue execution. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6604 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6605 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6609 | */ |
| 6610 | bool CanContinue() const; |
| 6611 | |
| 6612 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6613 | * 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6627 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6633 | Local<Value> ReThrow(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6634 | |
| 6635 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6636 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6647 | V8_DEPRECATE_SOON("Use maybe version.", Local<Value> StackTrace() const); |
| 6648 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace( |
| 6649 | Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6650 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6662 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6664 | * |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6689 | /** |
| 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6705 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6706 | 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6714 | v8::internal::Isolate* isolate_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6715 | v8::TryCatch* next_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6716 | void* exception_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6717 | void* message_obj_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6718 | void* js_stack_comparable_address_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6719 | bool is_verbose_ : 1; |
| 6720 | bool can_continue_ : 1; |
| 6721 | bool capture_message_ : 1; |
| 6722 | bool rethrow_ : 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6723 | bool has_terminated_ : 1; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6724 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6725 | friend class v8::internal::Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6726 | }; |
| 6727 | |
| 6728 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6729 | // --- Context --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6730 | |
| 6731 | |
| 6732 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6733 | * A container for extension names. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6734 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6735 | class V8_EXPORT ExtensionConfiguration { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6736 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6737 | ExtensionConfiguration() : name_count_(0), names_(NULL) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6738 | ExtensionConfiguration(int name_count, const char* names[]) |
| 6739 | : name_count_(name_count), names_(names) { } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6740 | |
| 6741 | const char** begin() const { return &names_[0]; } |
| 6742 | const char** end() const { return &names_[name_count_]; } |
| 6743 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6744 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6745 | const int name_count_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6746 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6754 | class V8_EXPORT Context { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6755 | public: |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6756 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6757 | * Returns the global proxy object. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6758 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6759 | * 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 Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6762 | * https://wiki.mozilla.org/Gecko:SplitWindow). |
| 6763 | * |
| 6764 | * Please note that changes to global proxy object prototype most probably |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6765 | * would break VM---v8 expects only global object as a prototype of global |
| 6766 | * proxy object. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6767 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6768 | 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 Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 6776 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6777 | * Creates a new context and returns a handle to the newly allocated |
| 6778 | * context. |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 6779 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6780 | * \param isolate The isolate in which to create the context. |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 6781 | * |
| 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 Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6793 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6794 | static Local<Context> New( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6795 | Isolate* isolate, ExtensionConfiguration* extensions = NULL, |
| 6796 | Local<ObjectTemplate> global_template = Local<ObjectTemplate>(), |
| 6797 | Local<Value> global_object = Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6798 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6799 | /** |
| 6800 | * Sets the security token for the context. To access an object in |
| 6801 | * another context, the security tokens must match. |
| 6802 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6803 | void SetSecurityToken(Local<Value> token); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6804 | |
| 6805 | /** Restores the security token to the default value. */ |
| 6806 | void UseDefaultSecurityToken(); |
| 6807 | |
| 6808 | /** Returns the security token of this context.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6809 | Local<Value> GetSecurityToken(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6810 | |
| 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6825 | /** Returns an isolate associated with a current context. */ |
| 6826 | v8::Isolate* GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6827 | |
| 6828 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6829 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6836 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6839 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6840 | V8_INLINE Local<Value> GetEmbedderData(int index); |
| 6841 | |
| 6842 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6843 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6851 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6855 | void SetEmbedderData(int index, Local<Value> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6856 | |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6871 | |
| 6872 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6873 | * 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 Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6888 | * 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6894 | * 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6898 | void SetErrorMessageForCodeGenerationFromStrings(Local<String> message); |
| 6899 | |
| 6900 | /** |
| 6901 | * Estimate the memory in bytes retained by this context. |
| 6902 | */ |
| 6903 | size_t EstimatedSize(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6904 | |
| 6905 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6906 | * Stack-allocated class which sets the execution context for all |
| 6907 | * operations executed within a local scope. |
| 6908 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6909 | class Scope { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6910 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6911 | explicit V8_INLINE Scope(Local<Context> context) : context_(context) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6912 | context_->Enter(); |
| 6913 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6914 | V8_INLINE ~Scope() { context_->Exit(); } |
| 6915 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6916 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6917 | Local<Context> context_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6918 | }; |
| 6919 | |
| 6920 | private: |
| 6921 | friend class Value; |
| 6922 | friend class Script; |
| 6923 | friend class Object; |
| 6924 | friend class Function; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6925 | |
| 6926 | Local<Value> SlowGetEmbedderData(int index); |
| 6927 | void* SlowGetAlignedPointerFromEmbedderData(int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6928 | }; |
| 6929 | |
| 6930 | |
| 6931 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6932 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6940 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6941 | * 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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6945 | * |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6946 | * Sample usage: |
| 6947 | * \code |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6948 | * ... |
| 6949 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6950 | * v8::Locker locker(isolate); |
| 6951 | * v8::Isolate::Scope isolate_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6952 | * ... |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6953 | * // Code using V8 and isolate goes here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6954 | * ... |
| 6955 | * } // Destructor called here |
| 6956 | * \endcode |
| 6957 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6958 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6961 | * |
| 6962 | * \code |
| 6963 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6964 | * isolate->Exit(); |
| 6965 | * v8::Unlocker unlocker(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6966 | * ... |
| 6967 | * // Code not using V8 goes here while V8 can run in another thread. |
| 6968 | * ... |
| 6969 | * } // Destructor called here. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6970 | * isolate->Enter(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6971 | * \endcode |
| 6972 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6973 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6975 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6976 | * 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6981 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6982 | * An unlocker will unlock several lockers if it has to and reinstate the |
| 6983 | * correct depth of locking on its destruction, e.g.: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6984 | * |
| 6985 | * \code |
| 6986 | * // V8 not locked. |
| 6987 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6988 | * v8::Locker locker(isolate); |
| 6989 | * Isolate::Scope isolate_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6990 | * // V8 locked. |
| 6991 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6992 | * v8::Locker another_locker(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6993 | * // V8 still locked (2 levels). |
| 6994 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6995 | * isolate->Exit(); |
| 6996 | * v8::Unlocker unlocker(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6997 | * // V8 not locked. |
| 6998 | * } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6999 | * isolate->Enter(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7000 | * // V8 locked again (2 levels). |
| 7001 | * } |
| 7002 | * // V8 still locked (1 level). |
| 7003 | * } |
| 7004 | * // V8 Now no longer locked. |
| 7005 | * \endcode |
| 7006 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7007 | class V8_EXPORT Unlocker { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7008 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7009 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7010 | * Initialize Unlocker for a given Isolate. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7011 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7012 | V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); } |
| 7013 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7014 | ~Unlocker(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7015 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7016 | void Initialize(Isolate* isolate); |
| 7017 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7018 | internal::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7019 | }; |
| 7020 | |
| 7021 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7022 | class V8_EXPORT Locker { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7023 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7024 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7025 | * Initialize Locker for a given Isolate. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7026 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7027 | V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); } |
| 7028 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7029 | ~Locker(); |
| 7030 | |
| 7031 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7032 | * Returns whether or not the locker for a given isolate, is locked by the |
| 7033 | * current thread. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7034 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7035 | static bool IsLocked(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7036 | |
| 7037 | /** |
| 7038 | * Returns whether v8::Locker is being used by this V8 instance. |
| 7039 | */ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7040 | static bool IsActive(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7041 | |
| 7042 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7043 | void Initialize(Isolate* isolate); |
| 7044 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7045 | bool has_lock_; |
| 7046 | bool top_level_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7047 | internal::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7048 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7049 | // Disallow copying and assigning. |
| 7050 | Locker(const Locker&); |
| 7051 | void operator=(const Locker&); |
| 7052 | }; |
| 7053 | |
| 7054 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7055 | // --- Implementation --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7056 | |
| 7057 | |
| 7058 | namespace internal { |
| 7059 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7060 | const int kApiPointerSize = sizeof(void*); // NOLINT |
| 7061 | const int kApiIntSize = sizeof(int); // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7062 | const int kApiInt64Size = sizeof(int64_t); // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7063 | |
| 7064 | // Tag information for HeapObject. |
| 7065 | const int kHeapObjectTag = 1; |
| 7066 | const int kHeapObjectTagSize = 2; |
| 7067 | const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; |
| 7068 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7069 | // Tag information for Smi. |
| 7070 | const int kSmiTag = 0; |
| 7071 | const int kSmiTagSize = 1; |
| 7072 | const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1; |
| 7073 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7074 | template <size_t ptr_size> struct SmiTagging; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7075 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7076 | template<int kSmiShiftSize> |
| 7077 | V8_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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7084 | // Smi constants for 32-bit systems. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7085 | template <> struct SmiTagging<4> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7086 | 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7090 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7094 | 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7111 | }; |
| 7112 | |
| 7113 | // Smi constants for 64-bit systems. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7114 | template <> struct SmiTagging<8> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7115 | 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7119 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7123 | 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7130 | }; |
| 7131 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7132 | typedef SmiTagging<kApiPointerSize> PlatformSmiTagging; |
| 7133 | const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize; |
| 7134 | const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7135 | V8_INLINE static bool SmiValuesAre31Bits() { return kSmiValueSize == 31; } |
| 7136 | V8_INLINE static bool SmiValuesAre32Bits() { return kSmiValueSize == 32; } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7137 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7138 | /** |
| 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 | */ |
| 7143 | class Internals { |
| 7144 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7145 | // These values match non-compiler-dependent values defined within |
| 7146 | // the implementation of v8. |
| 7147 | static const int kHeapObjectMapOffset = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7148 | static const int kMapInstanceTypeAndBitFieldOffset = |
| 7149 | 1 * kApiPointerSize + kApiIntSize; |
| 7150 | static const int kStringResourceOffset = 3 * kApiPointerSize; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7151 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7152 | static const int kOddballKindOffset = 4 * kApiPointerSize; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7153 | static const int kForeignAddressOffset = kApiPointerSize; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7154 | static const int kJSObjectHeaderSize = 3 * kApiPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7155 | static const int kFixedArrayHeaderSize = 2 * kApiPointerSize; |
| 7156 | static const int kContextHeaderSize = 2 * kApiPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7157 | static const int kContextEmbedderDataIndex = 5; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7158 | static const int kFullStringRepresentationMask = 0x07; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7159 | static const int kStringEncodingMask = 0x4; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7160 | static const int kExternalTwoByteRepresentationTag = 0x02; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7161 | static const int kExternalOneByteRepresentationTag = 0x06; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7162 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7163 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7175 | static const int kEmptyStringRootIndex = 10; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7176 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7183 | static const int kNodeStateMask = 0x7; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7184 | static const int kNodeStateIsWeakValue = 2; |
| 7185 | static const int kNodeStateIsPendingValue = 3; |
| 7186 | static const int kNodeStateIsNearDeathValue = 4; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7187 | static const int kNodeIsIndependentShift = 3; |
| 7188 | static const int kNodeIsPartiallyDependentShift = 4; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7189 | static const int kNodeIsActiveShift = 4; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7190 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 7191 | static const int kJSObjectType = 0xb5; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 7192 | static const int kFirstNonstringType = 0x80; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7193 | static const int kOddballType = 0x83; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7194 | static const int kForeignType = 0x87; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7195 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7196 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7209 | return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) == |
| 7210 | kHeapObjectTag); |
| 7211 | } |
| 7212 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7213 | V8_INLINE static int SmiValue(const internal::Object* value) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7214 | return PlatformSmiTagging::SmiToInt(value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7217 | 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7226 | typedef internal::Object O; |
| 7227 | O* map = ReadField<O*>(obj, kHeapObjectMapOffset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7228 | // 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7231 | } |
| 7232 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7233 | V8_INLINE static int GetOddballKind(const internal::Object* obj) { |
| 7234 | typedef internal::Object O; |
| 7235 | return SmiValue(ReadField<O*>(obj, kOddballKindOffset)); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7236 | } |
| 7237 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7238 | V8_INLINE static bool IsExternalTwoByteString(int instance_type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7239 | int representation = (instance_type & kFullStringRepresentationMask); |
| 7240 | return representation == kExternalTwoByteRepresentationTag; |
| 7241 | } |
| 7242 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7243 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7246 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7248 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7269 | uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7270 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7306 | }; |
| 7307 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7308 | } // namespace internal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7309 | |
| 7310 | |
| 7311 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7312 | Local<T> Local<T>::New(Isolate* isolate, Local<T> that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7313 | return New(isolate, that.val_); |
| 7314 | } |
| 7315 | |
| 7316 | template <class T> |
| 7317 | Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) { |
| 7318 | return New(isolate, that.val_); |
| 7319 | } |
| 7320 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7321 | |
| 7322 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7323 | Local<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 | |
| 7332 | template<class T> |
| 7333 | template<class S> |
| 7334 | void 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 | |
| 7340 | template<class T> |
| 7341 | Local<T> Eternal<T>::Get(Isolate* isolate) { |
| 7342 | return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7343 | } |
| 7344 | |
| 7345 | |
| 7346 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7347 | Local<T> MaybeLocal<T>::ToLocalChecked() { |
| 7348 | if (V8_UNLIKELY(val_ == nullptr)) V8::ToLocalEmpty(); |
| 7349 | return Local<T>(val_); |
| 7350 | } |
| 7351 | |
| 7352 | |
| 7353 | template <class T> |
| 7354 | void* 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 | |
| 7364 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7365 | T* 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 | |
| 7374 | template <class T, class M> |
| 7375 | template <class S, class M2> |
| 7376 | void 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 | |
| 7386 | template <class T> |
| 7387 | bool PersistentBase<T>::IsIndependent() const { |
| 7388 | typedef internal::Internals I; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7389 | if (this->IsEmpty()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7390 | return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_), |
| 7391 | I::kNodeIsIndependentShift); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7392 | } |
| 7393 | |
| 7394 | |
| 7395 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7396 | bool PersistentBase<T>::IsNearDeath() const { |
| 7397 | typedef internal::Internals I; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7398 | if (this->IsEmpty()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7399 | 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
| 7405 | |
| 7406 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7407 | bool 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 | |
| 7415 | template <class T> |
| 7416 | void PersistentBase<T>::Reset() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7417 | if (this->IsEmpty()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7418 | V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_)); |
| 7419 | val_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7420 | } |
| 7421 | |
| 7422 | |
| 7423 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7424 | template <class S> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7425 | void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7426 | TYPE_CHECK(T, S); |
| 7427 | Reset(); |
| 7428 | if (other.IsEmpty()) return; |
| 7429 | this->val_ = New(isolate, other.val_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7430 | } |
| 7431 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7432 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7433 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7434 | template <class S> |
| 7435 | void 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 Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7443 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7444 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7445 | template <typename S, typename P> |
| 7446 | void 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7451 | V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7452 | reinterpret_cast<Callback>(callback)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7453 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7454 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7455 | |
| 7456 | template <class T> |
| 7457 | template <typename P> |
| 7458 | void PersistentBase<T>::SetWeak( |
| 7459 | P* parameter, |
| 7460 | typename WeakCallbackData<T, P>::Callback callback) { |
| 7461 | SetWeak<T, P>(parameter, callback); |
| 7462 | } |
| 7463 | |
| 7464 | |
| 7465 | template <class T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7466 | template <typename P> |
| 7467 | void PersistentBase<T>::SetPhantom( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7468 | 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7474 | } |
| 7475 | |
| 7476 | |
| 7477 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7478 | template <typename P> |
| 7479 | V8_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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7485 | } |
| 7486 | |
| 7487 | |
| 7488 | template <class T> |
| 7489 | template <typename P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7490 | P* PersistentBase<T>::ClearWeak() { |
| 7491 | return reinterpret_cast<P*>( |
| 7492 | V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_))); |
| 7493 | } |
| 7494 | |
| 7495 | |
| 7496 | template <class T> |
| 7497 | void 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 | |
| 7506 | template <class T> |
| 7507 | void 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7516 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7517 | void 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 | |
| 7525 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7526 | void 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 | |
| 7535 | template <class T> |
| 7536 | uint16_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 | |
| 7545 | template<typename T> |
| 7546 | ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {} |
| 7547 | |
| 7548 | template<typename T> |
| 7549 | template<typename S> |
| 7550 | void 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7559 | template <typename T> |
| 7560 | template <typename S> |
| 7561 | void 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 | |
| 7570 | template <typename T> |
| 7571 | template <typename S> |
| 7572 | void ReturnValue<T>::Set(const Local<S> handle) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7573 | 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 | |
| 7581 | template<typename T> |
| 7582 | void ReturnValue<T>::Set(double i) { |
| 7583 | TYPE_CHECK(T, Number); |
| 7584 | Set(Number::New(GetIsolate(), i)); |
| 7585 | } |
| 7586 | |
| 7587 | template<typename T> |
| 7588 | void 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 | |
| 7598 | template<typename T> |
| 7599 | void 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 | |
| 7610 | template<typename T> |
| 7611 | void 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 | |
| 7623 | template<typename T> |
| 7624 | void ReturnValue<T>::SetNull() { |
| 7625 | TYPE_CHECK(T, Primitive); |
| 7626 | typedef internal::Internals I; |
| 7627 | *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex); |
| 7628 | } |
| 7629 | |
| 7630 | template<typename T> |
| 7631 | void ReturnValue<T>::SetUndefined() { |
| 7632 | TYPE_CHECK(T, Primitive); |
| 7633 | typedef internal::Internals I; |
| 7634 | *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex); |
| 7635 | } |
| 7636 | |
| 7637 | template<typename T> |
| 7638 | void ReturnValue<T>::SetEmptyString() { |
| 7639 | TYPE_CHECK(T, String); |
| 7640 | typedef internal::Internals I; |
| 7641 | *value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex); |
| 7642 | } |
| 7643 | |
| 7644 | template<typename T> |
| 7645 | Isolate* 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 | |
| 7650 | template<typename T> |
| 7651 | template<typename S> |
| 7652 | void ReturnValue<T>::Set(S* whatever) { |
| 7653 | // Uncompilable to prevent inadvertent misuse. |
| 7654 | TYPE_CHECK(S*, Primitive); |
| 7655 | } |
| 7656 | |
| 7657 | template<typename T> |
| 7658 | internal::Object* ReturnValue<T>::GetDefaultValue() { |
| 7659 | // Default value is always the pointer below value_ on the stack. |
| 7660 | return value_[-1]; |
| 7661 | } |
| 7662 | |
| 7663 | |
| 7664 | template<typename T> |
| 7665 | FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args, |
| 7666 | internal::Object** values, |
| 7667 | int length, |
| 7668 | bool is_construct_call) |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7669 | : implicit_args_(implicit_args), |
| 7670 | values_(values), |
| 7671 | length_(length), |
| 7672 | is_construct_call_(is_construct_call) { } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7673 | |
| 7674 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7675 | template<typename T> |
| 7676 | Local<Value> FunctionCallbackInfo<T>::operator[](int i) const { |
| 7677 | if (i < 0 || length_ <= i) return Local<Value>(*Undefined(GetIsolate())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7678 | return Local<Value>(reinterpret_cast<Value*>(values_ - i)); |
| 7679 | } |
| 7680 | |
| 7681 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7682 | template<typename T> |
| 7683 | Local<Function> FunctionCallbackInfo<T>::Callee() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7684 | return Local<Function>(reinterpret_cast<Function*>( |
| 7685 | &implicit_args_[kCalleeIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7686 | } |
| 7687 | |
| 7688 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7689 | template<typename T> |
| 7690 | Local<Object> FunctionCallbackInfo<T>::This() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7691 | return Local<Object>(reinterpret_cast<Object*>(values_ + 1)); |
| 7692 | } |
| 7693 | |
| 7694 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7695 | template<typename T> |
| 7696 | Local<Object> FunctionCallbackInfo<T>::Holder() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7697 | return Local<Object>(reinterpret_cast<Object*>( |
| 7698 | &implicit_args_[kHolderIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7699 | } |
| 7700 | |
| 7701 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7702 | template<typename T> |
| 7703 | Local<Value> FunctionCallbackInfo<T>::Data() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7704 | return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7705 | } |
| 7706 | |
| 7707 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7708 | template<typename T> |
| 7709 | Isolate* FunctionCallbackInfo<T>::GetIsolate() const { |
| 7710 | return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]); |
| 7711 | } |
| 7712 | |
| 7713 | |
| 7714 | template<typename T> |
| 7715 | ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const { |
| 7716 | return ReturnValue<T>(&implicit_args_[kReturnValueIndex]); |
| 7717 | } |
| 7718 | |
| 7719 | |
| 7720 | template<typename T> |
| 7721 | bool FunctionCallbackInfo<T>::IsConstructCall() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7722 | return is_construct_call_ & 0x1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7723 | } |
| 7724 | |
| 7725 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7726 | template<typename T> |
| 7727 | int FunctionCallbackInfo<T>::Length() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7728 | return length_; |
| 7729 | } |
| 7730 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7731 | ScriptOrigin::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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7749 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7750 | Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7751 | |
| 7752 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7753 | Local<Integer> ScriptOrigin::ResourceLineOffset() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7754 | return resource_line_offset_; |
| 7755 | } |
| 7756 | |
| 7757 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7758 | Local<Integer> ScriptOrigin::ResourceColumnOffset() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7759 | return resource_column_offset_; |
| 7760 | } |
| 7761 | |
| 7762 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7763 | Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7764 | |
| 7765 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7766 | Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7767 | |
| 7768 | |
| 7769 | ScriptCompiler::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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7775 | resource_options(origin.Options()), |
| 7776 | source_map_url(origin.SourceMapUrl()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7777 | cached_data(data) {} |
| 7778 | |
| 7779 | |
| 7780 | ScriptCompiler::Source::Source(Local<String> string, |
| 7781 | CachedData* data) |
| 7782 | : source_string(string), cached_data(data) {} |
| 7783 | |
| 7784 | |
| 7785 | ScriptCompiler::Source::~Source() { |
| 7786 | delete cached_data; |
| 7787 | } |
| 7788 | |
| 7789 | |
| 7790 | const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData() |
| 7791 | const { |
| 7792 | return cached_data; |
| 7793 | } |
| 7794 | |
| 7795 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7796 | Local<Boolean> Boolean::New(Isolate* isolate, bool value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7797 | return value ? True(isolate) : False(isolate); |
| 7798 | } |
| 7799 | |
| 7800 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7801 | void 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | |
| 7808 | Local<Value> Object::GetInternalField(int index) { |
| 7809 | #ifndef V8_ENABLE_CHECKS |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7810 | typedef internal::Object O; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7811 | typedef internal::HeapObject HO; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7812 | typedef internal::Internals I; |
| 7813 | O* obj = *reinterpret_cast<O**>(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7814 | // 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 Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7816 | if (I::GetInstanceType(obj) == I::kJSObjectType) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7817 | int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7818 | O* value = I::ReadField<O*>(obj, offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7819 | O** result = HandleScope::CreateHandle(reinterpret_cast<HO*>(obj), value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7820 | return Local<Value>(reinterpret_cast<Value*>(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7821 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7822 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7823 | return SlowGetInternalField(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7824 | } |
| 7825 | |
| 7826 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7827 | void* Object::GetAlignedPointerFromInternalField(int index) { |
| 7828 | #ifndef V8_ENABLE_CHECKS |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7829 | typedef internal::Object O; |
| 7830 | typedef internal::Internals I; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7831 | O* obj = *reinterpret_cast<O**>(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7832 | // 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) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7835 | int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7836 | return I::ReadField<void*>(obj, offset); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7837 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7838 | #endif |
| 7839 | return SlowGetAlignedPointerFromInternalField(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7840 | } |
| 7841 | |
| 7842 | |
| 7843 | String* 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7851 | Local<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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7860 | String::ExternalStringResource* String::GetExternalStringResource() const { |
| 7861 | typedef internal::Object O; |
| 7862 | typedef internal::Internals I; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7863 | O* obj = *reinterpret_cast<O* const*>(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7864 | String::ExternalStringResource* result; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7865 | if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7866 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7878 | String::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 | |
| 7898 | bool Value::IsUndefined() const { |
| 7899 | #ifdef V8_ENABLE_CHECKS |
| 7900 | return FullIsUndefined(); |
| 7901 | #else |
| 7902 | return QuickIsUndefined(); |
| 7903 | #endif |
| 7904 | } |
| 7905 | |
| 7906 | bool 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 | |
| 7916 | bool Value::IsNull() const { |
| 7917 | #ifdef V8_ENABLE_CHECKS |
| 7918 | return FullIsNull(); |
| 7919 | #else |
| 7920 | return QuickIsNull(); |
| 7921 | #endif |
| 7922 | } |
| 7923 | |
| 7924 | bool 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7934 | bool Value::IsString() const { |
| 7935 | #ifdef V8_ENABLE_CHECKS |
| 7936 | return FullIsString(); |
| 7937 | #else |
| 7938 | return QuickIsString(); |
| 7939 | #endif |
| 7940 | } |
| 7941 | |
| 7942 | bool Value::QuickIsString() const { |
| 7943 | typedef internal::Object O; |
| 7944 | typedef internal::Internals I; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7945 | O* obj = *reinterpret_cast<O* const*>(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7946 | if (!I::HasHeapObjectTag(obj)) return false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7947 | return (I::GetInstanceType(obj) < I::kFirstNonstringType); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7951 | template <class T> Value* Value::Cast(T* value) { |
| 7952 | return static_cast<Value*>(value); |
| 7953 | } |
| 7954 | |
| 7955 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7956 | Local<Boolean> Value::ToBoolean() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7957 | return ToBoolean(Isolate::GetCurrent()->GetCurrentContext()) |
| 7958 | .FromMaybe(Local<Boolean>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7959 | } |
| 7960 | |
| 7961 | |
| 7962 | Local<Number> Value::ToNumber() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7963 | return ToNumber(Isolate::GetCurrent()->GetCurrentContext()) |
| 7964 | .FromMaybe(Local<Number>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7965 | } |
| 7966 | |
| 7967 | |
| 7968 | Local<String> Value::ToString() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7969 | return ToString(Isolate::GetCurrent()->GetCurrentContext()) |
| 7970 | .FromMaybe(Local<String>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | |
| 7974 | Local<String> Value::ToDetailString() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7975 | return ToDetailString(Isolate::GetCurrent()->GetCurrentContext()) |
| 7976 | .FromMaybe(Local<String>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7977 | } |
| 7978 | |
| 7979 | |
| 7980 | Local<Object> Value::ToObject() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7981 | return ToObject(Isolate::GetCurrent()->GetCurrentContext()) |
| 7982 | .FromMaybe(Local<Object>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7983 | } |
| 7984 | |
| 7985 | |
| 7986 | Local<Integer> Value::ToInteger() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7987 | return ToInteger(Isolate::GetCurrent()->GetCurrentContext()) |
| 7988 | .FromMaybe(Local<Integer>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7989 | } |
| 7990 | |
| 7991 | |
| 7992 | Local<Uint32> Value::ToUint32() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7993 | return ToUint32(Isolate::GetCurrent()->GetCurrentContext()) |
| 7994 | .FromMaybe(Local<Uint32>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7995 | } |
| 7996 | |
| 7997 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7998 | Local<Int32> Value::ToInt32() const { |
| 7999 | return ToInt32(Isolate::GetCurrent()->GetCurrentContext()) |
| 8000 | .FromMaybe(Local<Int32>()); |
| 8001 | } |
| 8002 | |
| 8003 | |
| 8004 | Boolean* Boolean::Cast(v8::Value* value) { |
| 8005 | #ifdef V8_ENABLE_CHECKS |
| 8006 | CheckCast(value); |
| 8007 | #endif |
| 8008 | return static_cast<Boolean*>(value); |
| 8009 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8010 | |
| 8011 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8012 | Name* Name::Cast(v8::Value* value) { |
| 8013 | #ifdef V8_ENABLE_CHECKS |
| 8014 | CheckCast(value); |
| 8015 | #endif |
| 8016 | return static_cast<Name*>(value); |
| 8017 | } |
| 8018 | |
| 8019 | |
| 8020 | Symbol* 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8028 | Number* Number::Cast(v8::Value* value) { |
| 8029 | #ifdef V8_ENABLE_CHECKS |
| 8030 | CheckCast(value); |
| 8031 | #endif |
| 8032 | return static_cast<Number*>(value); |
| 8033 | } |
| 8034 | |
| 8035 | |
| 8036 | Integer* 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8044 | Int32* Int32::Cast(v8::Value* value) { |
| 8045 | #ifdef V8_ENABLE_CHECKS |
| 8046 | CheckCast(value); |
| 8047 | #endif |
| 8048 | return static_cast<Int32*>(value); |
| 8049 | } |
| 8050 | |
| 8051 | |
| 8052 | Uint32* 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8060 | Date* 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8068 | StringObject* 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8076 | SymbolObject* 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 Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8084 | NumberObject* NumberObject::Cast(v8::Value* value) { |
| 8085 | #ifdef V8_ENABLE_CHECKS |
| 8086 | CheckCast(value); |
| 8087 | #endif |
| 8088 | return static_cast<NumberObject*>(value); |
| 8089 | } |
| 8090 | |
| 8091 | |
| 8092 | BooleanObject* 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 Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 8100 | RegExp* 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8108 | Object* Object::Cast(v8::Value* value) { |
| 8109 | #ifdef V8_ENABLE_CHECKS |
| 8110 | CheckCast(value); |
| 8111 | #endif |
| 8112 | return static_cast<Object*>(value); |
| 8113 | } |
| 8114 | |
| 8115 | |
| 8116 | Array* 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8124 | Map* Map::Cast(v8::Value* value) { |
| 8125 | #ifdef V8_ENABLE_CHECKS |
| 8126 | CheckCast(value); |
| 8127 | #endif |
| 8128 | return static_cast<Map*>(value); |
| 8129 | } |
| 8130 | |
| 8131 | |
| 8132 | Set* 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8140 | Promise* 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8148 | Proxy* 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8156 | Promise::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 | |
| 8164 | ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) { |
| 8165 | #ifdef V8_ENABLE_CHECKS |
| 8166 | CheckCast(value); |
| 8167 | #endif |
| 8168 | return static_cast<ArrayBuffer*>(value); |
| 8169 | } |
| 8170 | |
| 8171 | |
| 8172 | ArrayBufferView* ArrayBufferView::Cast(v8::Value* value) { |
| 8173 | #ifdef V8_ENABLE_CHECKS |
| 8174 | CheckCast(value); |
| 8175 | #endif |
| 8176 | return static_cast<ArrayBufferView*>(value); |
| 8177 | } |
| 8178 | |
| 8179 | |
| 8180 | TypedArray* TypedArray::Cast(v8::Value* value) { |
| 8181 | #ifdef V8_ENABLE_CHECKS |
| 8182 | CheckCast(value); |
| 8183 | #endif |
| 8184 | return static_cast<TypedArray*>(value); |
| 8185 | } |
| 8186 | |
| 8187 | |
| 8188 | Uint8Array* Uint8Array::Cast(v8::Value* value) { |
| 8189 | #ifdef V8_ENABLE_CHECKS |
| 8190 | CheckCast(value); |
| 8191 | #endif |
| 8192 | return static_cast<Uint8Array*>(value); |
| 8193 | } |
| 8194 | |
| 8195 | |
| 8196 | Int8Array* Int8Array::Cast(v8::Value* value) { |
| 8197 | #ifdef V8_ENABLE_CHECKS |
| 8198 | CheckCast(value); |
| 8199 | #endif |
| 8200 | return static_cast<Int8Array*>(value); |
| 8201 | } |
| 8202 | |
| 8203 | |
| 8204 | Uint16Array* Uint16Array::Cast(v8::Value* value) { |
| 8205 | #ifdef V8_ENABLE_CHECKS |
| 8206 | CheckCast(value); |
| 8207 | #endif |
| 8208 | return static_cast<Uint16Array*>(value); |
| 8209 | } |
| 8210 | |
| 8211 | |
| 8212 | Int16Array* Int16Array::Cast(v8::Value* value) { |
| 8213 | #ifdef V8_ENABLE_CHECKS |
| 8214 | CheckCast(value); |
| 8215 | #endif |
| 8216 | return static_cast<Int16Array*>(value); |
| 8217 | } |
| 8218 | |
| 8219 | |
| 8220 | Uint32Array* Uint32Array::Cast(v8::Value* value) { |
| 8221 | #ifdef V8_ENABLE_CHECKS |
| 8222 | CheckCast(value); |
| 8223 | #endif |
| 8224 | return static_cast<Uint32Array*>(value); |
| 8225 | } |
| 8226 | |
| 8227 | |
| 8228 | Int32Array* Int32Array::Cast(v8::Value* value) { |
| 8229 | #ifdef V8_ENABLE_CHECKS |
| 8230 | CheckCast(value); |
| 8231 | #endif |
| 8232 | return static_cast<Int32Array*>(value); |
| 8233 | } |
| 8234 | |
| 8235 | |
| 8236 | Float32Array* Float32Array::Cast(v8::Value* value) { |
| 8237 | #ifdef V8_ENABLE_CHECKS |
| 8238 | CheckCast(value); |
| 8239 | #endif |
| 8240 | return static_cast<Float32Array*>(value); |
| 8241 | } |
| 8242 | |
| 8243 | |
| 8244 | Float64Array* Float64Array::Cast(v8::Value* value) { |
| 8245 | #ifdef V8_ENABLE_CHECKS |
| 8246 | CheckCast(value); |
| 8247 | #endif |
| 8248 | return static_cast<Float64Array*>(value); |
| 8249 | } |
| 8250 | |
| 8251 | |
| 8252 | Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) { |
| 8253 | #ifdef V8_ENABLE_CHECKS |
| 8254 | CheckCast(value); |
| 8255 | #endif |
| 8256 | return static_cast<Uint8ClampedArray*>(value); |
| 8257 | } |
| 8258 | |
| 8259 | |
| 8260 | DataView* 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8268 | SharedArrayBuffer* 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8276 | Function* Function::Cast(v8::Value* value) { |
| 8277 | #ifdef V8_ENABLE_CHECKS |
| 8278 | CheckCast(value); |
| 8279 | #endif |
| 8280 | return static_cast<Function*>(value); |
| 8281 | } |
| 8282 | |
| 8283 | |
| 8284 | External* 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8292 | template<typename T> |
| 8293 | Isolate* PropertyCallbackInfo<T>::GetIsolate() const { |
| 8294 | return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
| 8297 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8298 | template<typename T> |
| 8299 | Local<Value> PropertyCallbackInfo<T>::Data() const { |
| 8300 | return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8301 | } |
| 8302 | |
| 8303 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8304 | template<typename T> |
| 8305 | Local<Object> PropertyCallbackInfo<T>::This() const { |
| 8306 | return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex])); |
| 8307 | } |
| 8308 | |
| 8309 | |
| 8310 | template<typename T> |
| 8311 | Local<Object> PropertyCallbackInfo<T>::Holder() const { |
| 8312 | return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex])); |
| 8313 | } |
| 8314 | |
| 8315 | |
| 8316 | template<typename T> |
| 8317 | ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const { |
| 8318 | return ReturnValue<T>(&args_[kReturnValueIndex]); |
| 8319 | } |
| 8320 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 8321 | template <typename T> |
| 8322 | bool PropertyCallbackInfo<T>::ShouldThrowOnError() const { |
| 8323 | typedef internal::Internals I; |
| 8324 | return args_[kShouldThrowOnErrorIndex] != I::IntToSmi(0); |
| 8325 | } |
| 8326 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8327 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8328 | Local<Primitive> Undefined(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8329 | typedef internal::Object* S; |
| 8330 | typedef internal::Internals I; |
| 8331 | I::CheckInitialized(isolate); |
| 8332 | S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8333 | return Local<Primitive>(reinterpret_cast<Primitive*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8334 | } |
| 8335 | |
| 8336 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8337 | Local<Primitive> Null(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8338 | typedef internal::Object* S; |
| 8339 | typedef internal::Internals I; |
| 8340 | I::CheckInitialized(isolate); |
| 8341 | S* slot = I::GetRoot(isolate, I::kNullValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8342 | return Local<Primitive>(reinterpret_cast<Primitive*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8343 | } |
| 8344 | |
| 8345 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8346 | Local<Boolean> True(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8347 | typedef internal::Object* S; |
| 8348 | typedef internal::Internals I; |
| 8349 | I::CheckInitialized(isolate); |
| 8350 | S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8351 | return Local<Boolean>(reinterpret_cast<Boolean*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8352 | } |
| 8353 | |
| 8354 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8355 | Local<Boolean> False(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8356 | typedef internal::Object* S; |
| 8357 | typedef internal::Internals I; |
| 8358 | I::CheckInitialized(isolate); |
| 8359 | S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8360 | return Local<Boolean>(reinterpret_cast<Boolean*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8361 | } |
| 8362 | |
| 8363 | |
| 8364 | void Isolate::SetData(uint32_t slot, void* data) { |
| 8365 | typedef internal::Internals I; |
| 8366 | I::SetEmbedderData(this, slot, data); |
| 8367 | } |
| 8368 | |
| 8369 | |
| 8370 | void* Isolate::GetData(uint32_t slot) { |
| 8371 | typedef internal::Internals I; |
| 8372 | return I::GetEmbedderData(this, slot); |
| 8373 | } |
| 8374 | |
| 8375 | |
| 8376 | uint32_t Isolate::GetNumberOfDataSlots() { |
| 8377 | typedef internal::Internals I; |
| 8378 | return I::kNumIsolateDataSlots; |
| 8379 | } |
| 8380 | |
| 8381 | |
| 8382 | int64_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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8396 | ReportExternalAllocationLimitReached(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8397 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8398 | *amount_of_external_allocated_memory = amount; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8399 | return *amount_of_external_allocated_memory; |
| 8400 | } |
| 8401 | |
| 8402 | |
| 8403 | template<typename T> |
| 8404 | void 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 | |
| 8411 | template<typename T> |
| 8412 | void 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 | |
| 8420 | template<typename T, typename S> |
| 8421 | void 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 | |
| 8430 | Local<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 | |
| 8445 | void* 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8452 | } |
| 8453 | |
| 8454 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8455 | void V8::SetAllowCodeGenerationFromStringsCallback( |
| 8456 | AllowCodeGenerationFromStringsCallback callback) { |
| 8457 | Isolate* isolate = Isolate::GetCurrent(); |
| 8458 | isolate->SetAllowCodeGenerationFromStringsCallback(callback); |
| 8459 | } |
| 8460 | |
| 8461 | |
| 8462 | bool V8::IsDead() { |
| 8463 | Isolate* isolate = Isolate::GetCurrent(); |
| 8464 | return isolate->IsDead(); |
| 8465 | } |
| 8466 | |
| 8467 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8468 | bool V8::AddMessageListener(MessageCallback that, Local<Value> data) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8469 | Isolate* isolate = Isolate::GetCurrent(); |
| 8470 | return isolate->AddMessageListener(that, data); |
| 8471 | } |
| 8472 | |
| 8473 | |
| 8474 | void V8::RemoveMessageListeners(MessageCallback that) { |
| 8475 | Isolate* isolate = Isolate::GetCurrent(); |
| 8476 | isolate->RemoveMessageListeners(that); |
| 8477 | } |
| 8478 | |
| 8479 | |
| 8480 | void V8::SetFailedAccessCheckCallbackFunction( |
| 8481 | FailedAccessCheckCallback callback) { |
| 8482 | Isolate* isolate = Isolate::GetCurrent(); |
| 8483 | isolate->SetFailedAccessCheckCallbackFunction(callback); |
| 8484 | } |
| 8485 | |
| 8486 | |
| 8487 | void 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 | |
| 8495 | void V8::SetFatalErrorHandler(FatalErrorCallback callback) { |
| 8496 | Isolate* isolate = Isolate::GetCurrent(); |
| 8497 | isolate->SetFatalErrorHandler(callback); |
| 8498 | } |
| 8499 | |
| 8500 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8501 | void V8::RemoveGCPrologueCallback(GCCallback callback) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8502 | Isolate* isolate = Isolate::GetCurrent(); |
| 8503 | isolate->RemoveGCPrologueCallback( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8504 | reinterpret_cast<v8::Isolate::GCCallback>(callback)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8505 | } |
| 8506 | |
| 8507 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8508 | void V8::RemoveGCEpilogueCallback(GCCallback callback) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8509 | Isolate* isolate = Isolate::GetCurrent(); |
| 8510 | isolate->RemoveGCEpilogueCallback( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8511 | reinterpret_cast<v8::Isolate::GCCallback>(callback)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8512 | } |
| 8513 | |
| 8514 | |
| 8515 | void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 8516 | ObjectSpace space, |
| 8517 | AllocationAction action) { |
| 8518 | Isolate* isolate = Isolate::GetCurrent(); |
| 8519 | isolate->AddMemoryAllocationCallback(callback, space, action); |
| 8520 | } |
| 8521 | |
| 8522 | |
| 8523 | void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) { |
| 8524 | Isolate* isolate = Isolate::GetCurrent(); |
| 8525 | isolate->RemoveMemoryAllocationCallback(callback); |
| 8526 | } |
| 8527 | |
| 8528 | |
| 8529 | void V8::TerminateExecution(Isolate* isolate) { isolate->TerminateExecution(); } |
| 8530 | |
| 8531 | |
| 8532 | bool V8::IsExecutionTerminating(Isolate* isolate) { |
| 8533 | if (isolate == NULL) { |
| 8534 | isolate = Isolate::GetCurrent(); |
| 8535 | } |
| 8536 | return isolate->IsExecutionTerminating(); |
| 8537 | } |
| 8538 | |
| 8539 | |
| 8540 | void V8::CancelTerminateExecution(Isolate* isolate) { |
| 8541 | isolate->CancelTerminateExecution(); |
| 8542 | } |
| 8543 | |
| 8544 | |
| 8545 | void V8::VisitExternalResources(ExternalResourceVisitor* visitor) { |
| 8546 | Isolate* isolate = Isolate::GetCurrent(); |
| 8547 | isolate->VisitExternalResources(visitor); |
| 8548 | } |
| 8549 | |
| 8550 | |
| 8551 | void V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) { |
| 8552 | Isolate* isolate = Isolate::GetCurrent(); |
| 8553 | isolate->VisitHandlesWithClassIds(visitor); |
| 8554 | } |
| 8555 | |
| 8556 | |
| 8557 | void V8::VisitHandlesWithClassIds(Isolate* isolate, |
| 8558 | PersistentHandleVisitor* visitor) { |
| 8559 | isolate->VisitHandlesWithClassIds(visitor); |
| 8560 | } |
| 8561 | |
| 8562 | |
| 8563 | void V8::VisitHandlesForPartialDependence(Isolate* isolate, |
| 8564 | PersistentHandleVisitor* visitor) { |
| 8565 | isolate->VisitHandlesForPartialDependence(visitor); |
| 8566 | } |
| 8567 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8568 | /** |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8583 | #undef TYPE_CHECK |
| 8584 | |
| 8585 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8586 | #endif // INCLUDE_V8_H_ |