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> |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 21 | #include <utility> |
| 22 | #include <vector> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 23 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 24 | #include "v8-version.h" // NOLINT(build/include) |
| 25 | #include "v8config.h" // NOLINT(build/include) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 26 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | // We reserve the V8_* prefix for macros defined in V8 public API and |
| 28 | // assume there are no name conflicts with the embedder's code. |
| 29 | |
| 30 | #ifdef V8_OS_WIN |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | |
| 32 | // Setup for Windows DLL export/import. When building the V8 DLL the |
| 33 | // BUILDING_V8_SHARED needs to be defined. When building a program which uses |
| 34 | // the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8 |
| 35 | // static library or building a program which uses the V8 static library neither |
| 36 | // BUILDING_V8_SHARED nor USING_V8_SHARED should be defined. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED) |
| 38 | #error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\ |
| 39 | build configuration to ensure that at most one of these is set |
| 40 | #endif |
| 41 | |
| 42 | #ifdef BUILDING_V8_SHARED |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | # define V8_EXPORT __declspec(dllexport) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | #elif USING_V8_SHARED |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | # define V8_EXPORT __declspec(dllimport) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | #else |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | # define V8_EXPORT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | #endif // BUILDING_V8_SHARED |
| 49 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | #else // V8_OS_WIN |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | // Setup for Linux shared library export. |
| 53 | #if V8_HAS_ATTRIBUTE_VISIBILITY && defined(V8_SHARED) |
| 54 | # ifdef BUILDING_V8_SHARED |
| 55 | # define V8_EXPORT __attribute__ ((visibility("default"))) |
| 56 | # else |
| 57 | # define V8_EXPORT |
| 58 | # endif |
| 59 | #else |
| 60 | # define V8_EXPORT |
| 61 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | #endif // V8_OS_WIN |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * The v8 JavaScript engine. |
| 67 | */ |
| 68 | namespace v8 { |
| 69 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 70 | class AccessorSignature; |
| 71 | class Array; |
| 72 | class Boolean; |
| 73 | class BooleanObject; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | class Context; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | class CpuProfiler; |
| 76 | class Data; |
| 77 | class Date; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | class External; |
| 79 | class Function; |
| 80 | class FunctionTemplate; |
| 81 | class HeapProfiler; |
| 82 | class ImplementationUtilities; |
| 83 | class Int32; |
| 84 | class Integer; |
| 85 | class Isolate; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 86 | template <class T> |
| 87 | class Maybe; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | class Name; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | class Number; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 90 | class NumberObject; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | class Object; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | class ObjectOperationDescriptor; |
| 93 | class ObjectTemplate; |
| 94 | class Platform; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | class Primitive; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 96 | class Promise; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | class Proxy; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 98 | class RawOperationDescriptor; |
| 99 | class Script; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 100 | class SharedArrayBuffer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | class Signature; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | class StartupData; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 103 | class StackFrame; |
| 104 | class StackTrace; |
| 105 | class String; |
| 106 | class StringObject; |
| 107 | class Symbol; |
| 108 | class SymbolObject; |
| 109 | class Private; |
| 110 | class Uint32; |
| 111 | class Utils; |
| 112 | class Value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | template <class T> class Local; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 114 | template <class T> |
| 115 | class MaybeLocal; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | template <class T> class Eternal; |
| 117 | template<class T> class NonCopyablePersistentTraits; |
| 118 | template<class T> class PersistentBase; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 119 | template <class T, class M = NonCopyablePersistentTraits<T> > |
| 120 | class Persistent; |
| 121 | template <class T> |
| 122 | class Global; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | template<class K, class V, class T> class PersistentValueMap; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 124 | template <class K, class V, class T> |
| 125 | class PersistentValueMapBase; |
| 126 | template <class K, class V, class T> |
| 127 | class GlobalValueMap; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | template<class V, class T> class PersistentValueVector; |
| 129 | template<class T, class P> class WeakCallbackObject; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | class FunctionTemplate; |
| 131 | class ObjectTemplate; |
| 132 | class Data; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 133 | template<typename T> class FunctionCallbackInfo; |
| 134 | template<typename T> class PropertyCallbackInfo; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 135 | class StackTrace; |
| 136 | class StackFrame; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | class Isolate; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 138 | class CallHandlerHelper; |
| 139 | class EscapableHandleScope; |
| 140 | template<typename T> class ReturnValue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 142 | namespace experimental { |
| 143 | class FastAccessorBuilder; |
| 144 | } // namespace experimental |
| 145 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | namespace internal { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | class Arguments; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 148 | class Heap; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 149 | class HeapObject; |
| 150 | class Isolate; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | class Object; |
| 152 | struct StreamedSource; |
| 153 | template<typename T> class CustomArguments; |
| 154 | class PropertyCallbackArguments; |
| 155 | class FunctionCallbackArguments; |
| 156 | class GlobalHandles; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 157 | } // namespace internal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | |
| 159 | |
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 | * General purpose unique identifier. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 163 | class UniqueId { |
| 164 | public: |
| 165 | explicit UniqueId(intptr_t data) |
| 166 | : data_(data) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 168 | bool operator==(const UniqueId& other) const { |
| 169 | return data_ == other.data_; |
| 170 | } |
| 171 | |
| 172 | bool operator!=(const UniqueId& other) const { |
| 173 | return data_ != other.data_; |
| 174 | } |
| 175 | |
| 176 | bool operator<(const UniqueId& other) const { |
| 177 | return data_ < other.data_; |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | intptr_t data_; |
| 182 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 184 | // --- Handles --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 186 | #define TYPE_CHECK(T, S) \ |
| 187 | while (false) { \ |
| 188 | *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 191 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | /** |
| 193 | * An object reference managed by the v8 garbage collector. |
| 194 | * |
| 195 | * All objects returned from v8 have to be tracked by the garbage |
| 196 | * collector so that it knows that the objects are still alive. Also, |
| 197 | * because the garbage collector may move objects, it is unsafe to |
| 198 | * point directly to an object. Instead, all objects are stored in |
| 199 | * handles which are known by the garbage collector and updated |
| 200 | * whenever an object moves. Handles should always be passed by value |
| 201 | * (except in cases like out-parameters) and they should never be |
| 202 | * allocated on the heap. |
| 203 | * |
| 204 | * There are two types of handles: local and persistent handles. |
| 205 | * Local handles are light-weight and transient and typically used in |
| 206 | * local operations. They are managed by HandleScopes. Persistent |
| 207 | * handles can be used when storing objects across several independent |
| 208 | * operations and have to be explicitly deallocated when they're no |
| 209 | * longer used. |
| 210 | * |
| 211 | * It is safe to extract the object stored in the handle by |
| 212 | * dereferencing the handle (for instance, to extract the Object* from |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 213 | * a Local<Object>); the value will still be governed by a handle |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 214 | * behind the scenes and the same rules apply to these values as to |
| 215 | * their handles. |
| 216 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 217 | template <class T> |
| 218 | class Local { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 220 | V8_INLINE Local() : val_(0) {} |
| 221 | template <class S> |
| 222 | V8_INLINE Local(Local<S> that) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | : val_(reinterpret_cast<T*>(*that)) { |
| 224 | /** |
| 225 | * This check fails when trying to convert between incompatible |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 226 | * handles. For example, converting from a Local<String> to a |
| 227 | * Local<Number>. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | */ |
| 229 | TYPE_CHECK(T, S); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns true if the handle is empty. |
| 234 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 235 | V8_INLINE bool IsEmpty() const { return val_ == 0; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | /** |
| 238 | * Sets the handle to be empty. IsEmpty() will then return true. |
| 239 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | V8_INLINE void Clear() { val_ = 0; } |
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_; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 243 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 244 | V8_INLINE T* operator*() const { return val_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * Checks whether two handles are the same. |
| 248 | * Returns true if both are empty, or if the objects |
| 249 | * to which they refer are identical. |
| 250 | * The handles' references are not checked. |
| 251 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 252 | template <class S> |
| 253 | V8_INLINE bool operator==(const Local<S>& that) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 254 | internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); |
| 255 | internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); |
| 256 | if (a == 0) return b == 0; |
| 257 | if (b == 0) return false; |
| 258 | return *a == *b; |
| 259 | } |
| 260 | |
| 261 | template <class S> V8_INLINE bool operator==( |
| 262 | const PersistentBase<S>& that) const { |
| 263 | internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); |
| 264 | internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | if (a == 0) return b == 0; |
| 266 | if (b == 0) return false; |
| 267 | return *a == *b; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Checks whether two handles are different. |
| 272 | * Returns true if only one of the handles is empty, or if |
| 273 | * the objects to which they refer are different. |
| 274 | * The handles' references are not checked. |
| 275 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 276 | template <class S> |
| 277 | V8_INLINE bool operator!=(const Local<S>& that) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | return !operator==(that); |
| 279 | } |
| 280 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 281 | template <class S> V8_INLINE bool operator!=( |
| 282 | const Persistent<S>& that) const { |
| 283 | return !operator==(that); |
| 284 | } |
| 285 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 286 | template <class S> V8_INLINE static Local<T> Cast(Local<S> that) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 287 | #ifdef V8_ENABLE_CHECKS |
| 288 | // If we're going to perform the type check then we have to check |
| 289 | // that the handle isn't empty before doing the checked cast. |
| 290 | if (that.IsEmpty()) return Local<T>(); |
| 291 | #endif |
| 292 | return Local<T>(T::Cast(*that)); |
| 293 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 294 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | template <class S> V8_INLINE Local<S> As() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 297 | return Local<S>::Cast(*this); |
| 298 | } |
| 299 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 300 | /** |
| 301 | * Create a local handle for the content of another handle. |
| 302 | * The referee is kept alive by the local handle even when |
| 303 | * the original handle is destroyed/disposed. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 305 | V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 306 | V8_INLINE static Local<T> New(Isolate* isolate, |
| 307 | const PersistentBase<T>& that); |
| 308 | |
| 309 | private: |
| 310 | friend class Utils; |
| 311 | template<class F> friend class Eternal; |
| 312 | template<class F> friend class PersistentBase; |
| 313 | template<class F, class M> friend class Persistent; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 314 | template<class F> friend class Local; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 315 | template <class F> |
| 316 | friend class MaybeLocal; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 317 | template<class F> friend class FunctionCallbackInfo; |
| 318 | template<class F> friend class PropertyCallbackInfo; |
| 319 | friend class String; |
| 320 | friend class Object; |
| 321 | friend class Context; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 322 | friend class Private; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 323 | template<class F> friend class internal::CustomArguments; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 324 | friend Local<Primitive> Undefined(Isolate* isolate); |
| 325 | friend Local<Primitive> Null(Isolate* isolate); |
| 326 | friend Local<Boolean> True(Isolate* isolate); |
| 327 | friend Local<Boolean> False(Isolate* isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 328 | friend class HandleScope; |
| 329 | friend class EscapableHandleScope; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 330 | template <class F1, class F2, class F3> |
| 331 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 332 | template<class F1, class F2> friend class PersistentValueVector; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 333 | template <class F> |
| 334 | friend class ReturnValue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 335 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 336 | explicit V8_INLINE Local(T* that) : val_(that) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 337 | V8_INLINE static Local<T> New(Isolate* isolate, T* that); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 338 | T* val_; |
| 339 | }; |
| 340 | |
| 341 | |
| 342 | #if !defined(V8_IMMINENT_DEPRECATION_WARNINGS) |
| 343 | // Local is an alias for Local for historical reasons. |
| 344 | template <class T> |
| 345 | using Handle = Local<T>; |
| 346 | #endif |
| 347 | |
| 348 | |
| 349 | /** |
| 350 | * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether |
| 351 | * the Local<> is empty before it can be used. |
| 352 | * |
| 353 | * If an API method returns a MaybeLocal<>, the API method can potentially fail |
| 354 | * either because an exception is thrown, or because an exception is pending, |
| 355 | * e.g. because a previous API call threw an exception that hasn't been caught |
| 356 | * yet, or because a TerminateExecution exception was thrown. In that case, an |
| 357 | * empty MaybeLocal is returned. |
| 358 | */ |
| 359 | template <class T> |
| 360 | class MaybeLocal { |
| 361 | public: |
| 362 | V8_INLINE MaybeLocal() : val_(nullptr) {} |
| 363 | template <class S> |
| 364 | V8_INLINE MaybeLocal(Local<S> that) |
| 365 | : val_(reinterpret_cast<T*>(*that)) { |
| 366 | TYPE_CHECK(T, S); |
| 367 | } |
| 368 | |
| 369 | V8_INLINE bool IsEmpty() const { return val_ == nullptr; } |
| 370 | |
| 371 | template <class S> |
| 372 | V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const { |
| 373 | out->val_ = IsEmpty() ? nullptr : this->val_; |
| 374 | return !IsEmpty(); |
| 375 | } |
| 376 | |
| 377 | // Will crash if the MaybeLocal<> is empty. |
| 378 | V8_INLINE Local<T> ToLocalChecked(); |
| 379 | |
| 380 | template <class S> |
| 381 | V8_INLINE Local<S> FromMaybe(Local<S> default_value) const { |
| 382 | return IsEmpty() ? default_value : Local<S>(val_); |
| 383 | } |
| 384 | |
| 385 | private: |
| 386 | T* val_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | |
| 390 | // Eternal handles are set-once handles that live for the life of the isolate. |
| 391 | template <class T> class Eternal { |
| 392 | public: |
| 393 | V8_INLINE Eternal() : index_(kInitialValue) { } |
| 394 | template<class S> |
| 395 | V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : index_(kInitialValue) { |
| 396 | Set(isolate, handle); |
| 397 | } |
| 398 | // Can only be safely called if already set. |
| 399 | V8_INLINE Local<T> Get(Isolate* isolate); |
| 400 | V8_INLINE bool IsEmpty() { return index_ == kInitialValue; } |
| 401 | template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle); |
| 402 | |
| 403 | private: |
| 404 | static const int kInitialValue = -1; |
| 405 | int index_; |
| 406 | }; |
| 407 | |
| 408 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 409 | static const int kInternalFieldsInWeakCallback = 2; |
| 410 | |
| 411 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 412 | template <typename T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 413 | class WeakCallbackInfo { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 414 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 415 | typedef void (*Callback)(const WeakCallbackInfo<T>& data); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 416 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 417 | WeakCallbackInfo(Isolate* isolate, T* parameter, |
| 418 | void* internal_fields[kInternalFieldsInWeakCallback], |
| 419 | Callback* callback) |
| 420 | : isolate_(isolate), parameter_(parameter), callback_(callback) { |
| 421 | for (int i = 0; i < kInternalFieldsInWeakCallback; ++i) { |
| 422 | internal_fields_[i] = internal_fields[i]; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | V8_INLINE Isolate* GetIsolate() const { return isolate_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 427 | V8_INLINE T* GetParameter() const { return parameter_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 428 | V8_INLINE void* GetInternalField(int index) const; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 429 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 430 | V8_INLINE V8_DEPRECATED("use indexed version", |
| 431 | void* GetInternalField1() const) { |
| 432 | return internal_fields_[0]; |
| 433 | } |
| 434 | V8_INLINE V8_DEPRECATED("use indexed version", |
| 435 | void* GetInternalField2() const) { |
| 436 | return internal_fields_[1]; |
| 437 | } |
| 438 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 439 | V8_DEPRECATED("Not realiable once SetSecondPassCallback() was used.", |
| 440 | bool IsFirstPass() const) { |
| 441 | return callback_ != nullptr; |
| 442 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 443 | |
| 444 | // When first called, the embedder MUST Reset() the Global which triggered the |
| 445 | // callback. The Global itself is unusable for anything else. No v8 other api |
| 446 | // calls may be called in the first callback. Should additional work be |
| 447 | // required, the embedder must set a second pass callback, which will be |
| 448 | // called after all the initial callbacks are processed. |
| 449 | // Calling SetSecondPassCallback on the second pass will immediately crash. |
| 450 | void SetSecondPassCallback(Callback callback) const { *callback_ = callback; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 451 | |
| 452 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 453 | Isolate* isolate_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 454 | T* parameter_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 455 | Callback* callback_; |
| 456 | void* internal_fields_[kInternalFieldsInWeakCallback]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | |
| 460 | template <class T, class P> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 461 | class WeakCallbackData { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 462 | public: |
| 463 | typedef void (*Callback)(const WeakCallbackData<T, P>& data); |
| 464 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 465 | WeakCallbackData(Isolate* isolate, P* parameter, Local<T> handle) |
| 466 | : isolate_(isolate), parameter_(parameter), handle_(handle) {} |
| 467 | |
| 468 | V8_INLINE Isolate* GetIsolate() const { return isolate_; } |
| 469 | V8_INLINE P* GetParameter() const { return parameter_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 470 | V8_INLINE Local<T> GetValue() const { return handle_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 471 | |
| 472 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 473 | Isolate* isolate_; |
| 474 | P* parameter_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 475 | Local<T> handle_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 479 | // TODO(dcarney): delete this with WeakCallbackData |
| 480 | template <class T> |
| 481 | using PhantomCallbackData = WeakCallbackInfo<T>; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 482 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 483 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 484 | enum class WeakCallbackType { kParameter, kInternalFields }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 485 | |
| 486 | |
| 487 | /** |
| 488 | * An object reference that is independent of any handle scope. Where |
| 489 | * a Local handle only lives as long as the HandleScope in which it was |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 490 | * allocated, a PersistentBase handle remains valid until it is explicitly |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | * disposed. |
| 492 | * |
| 493 | * A persistent handle contains a reference to a storage cell within |
| 494 | * the v8 engine which holds an object value and which is updated by |
| 495 | * the garbage collector whenever the object is moved. A new storage |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 496 | * cell can be created using the constructor or PersistentBase::Reset and |
| 497 | * existing handles can be disposed using PersistentBase::Reset. |
| 498 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 499 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 500 | template <class T> class PersistentBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 501 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 503 | * If non-empty, destroy the underlying storage cell |
| 504 | * IsEmpty() will return true after this call. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 505 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 506 | V8_INLINE void Reset(); |
| 507 | /** |
| 508 | * If non-empty, destroy the underlying storage cell |
| 509 | * and create a new one with the contents of other if other is non empty |
| 510 | */ |
| 511 | template <class S> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 512 | V8_INLINE void Reset(Isolate* isolate, const Local<S>& other); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 513 | |
| 514 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 515 | * If non-empty, destroy the underlying storage cell |
| 516 | * and create a new one with the contents of other if other is non empty |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 517 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 518 | template <class S> |
| 519 | V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other); |
| 520 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 521 | V8_INLINE bool IsEmpty() const { return val_ == NULL; } |
| 522 | V8_INLINE void Empty() { val_ = 0; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 524 | V8_INLINE Local<T> Get(Isolate* isolate) const { |
| 525 | return Local<T>::New(isolate, *this); |
| 526 | } |
| 527 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 528 | template <class S> |
| 529 | V8_INLINE bool operator==(const PersistentBase<S>& that) const { |
| 530 | internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); |
| 531 | internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 532 | if (a == NULL) return b == NULL; |
| 533 | if (b == NULL) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 534 | return *a == *b; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 537 | template <class S> |
| 538 | V8_INLINE bool operator==(const Local<S>& that) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 539 | internal::Object** a = reinterpret_cast<internal::Object**>(this->val_); |
| 540 | internal::Object** b = reinterpret_cast<internal::Object**>(that.val_); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 541 | if (a == NULL) return b == NULL; |
| 542 | if (b == NULL) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 543 | return *a == *b; |
| 544 | } |
| 545 | |
| 546 | template <class S> |
| 547 | V8_INLINE bool operator!=(const PersistentBase<S>& that) const { |
| 548 | return !operator==(that); |
| 549 | } |
| 550 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 551 | template <class S> |
| 552 | V8_INLINE bool operator!=(const Local<S>& that) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 553 | return !operator==(that); |
| 554 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | |
| 556 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 557 | * Install a finalization callback on this object. |
| 558 | * NOTE: There is no guarantee as to *when* or even *if* the callback is |
| 559 | * invoked. The invocation is performed solely on a best effort basis. |
| 560 | * As always, GC-based finalization should *not* be relied upon for any |
| 561 | * critical form of resource management! |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 562 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 563 | template <typename P> |
| 564 | V8_INLINE V8_DEPRECATED( |
| 565 | "use WeakCallbackInfo version", |
| 566 | void SetWeak(P* parameter, |
| 567 | typename WeakCallbackData<T, P>::Callback callback)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 568 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 569 | template <typename S, typename P> |
| 570 | V8_INLINE V8_DEPRECATED( |
| 571 | "use WeakCallbackInfo version", |
| 572 | void SetWeak(P* parameter, |
| 573 | typename WeakCallbackData<S, P>::Callback callback)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 574 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 575 | // Phantom persistents work like weak persistents, except that the pointer to |
| 576 | // the object being collected is not available in the finalization callback. |
| 577 | // This enables the garbage collector to collect the object and any objects |
| 578 | // it references transitively in one GC cycle. At the moment you can either |
| 579 | // specify a parameter for the callback or the location of two internal |
| 580 | // fields in the dying object. |
| 581 | template <typename P> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 582 | V8_INLINE V8_DEPRECATED( |
| 583 | "use SetWeak", |
| 584 | void SetPhantom(P* parameter, |
| 585 | typename WeakCallbackInfo<P>::Callback callback, |
| 586 | int internal_field_index1 = -1, |
| 587 | int internal_field_index2 = -1)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 588 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 589 | template <typename P> |
| 590 | V8_INLINE void SetWeak(P* parameter, |
| 591 | typename WeakCallbackInfo<P>::Callback callback, |
| 592 | WeakCallbackType type); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 593 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 594 | template<typename P> |
| 595 | V8_INLINE P* ClearWeak(); |
| 596 | |
| 597 | // TODO(dcarney): remove this. |
| 598 | V8_INLINE void ClearWeak() { ClearWeak<void>(); } |
| 599 | |
| 600 | /** |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 601 | * Allows the embedder to tell the v8 garbage collector that a certain object |
| 602 | * is alive. Only allowed when the embedder is asked to trace its heap by |
| 603 | * EmbedderHeapTracer. |
| 604 | */ |
| 605 | V8_INLINE void RegisterExternalReference(Isolate* isolate); |
| 606 | |
| 607 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 608 | * Marks the reference to this object independent. Garbage collector is free |
| 609 | * to ignore any object groups containing this object. Weak callback for an |
| 610 | * independent handle should not assume that it will be preceded by a global |
| 611 | * GC prologue callback or followed by a global GC epilogue callback. |
| 612 | */ |
| 613 | V8_INLINE void MarkIndependent(); |
| 614 | |
| 615 | /** |
| 616 | * Marks the reference to this object partially dependent. Partially dependent |
| 617 | * handles only depend on other partially dependent handles and these |
| 618 | * dependencies are provided through object groups. It provides a way to build |
| 619 | * smaller object groups for young objects that represent only a subset of all |
| 620 | * external dependencies. This mark is automatically cleared after each |
| 621 | * garbage collection. |
| 622 | */ |
| 623 | V8_INLINE void MarkPartiallyDependent(); |
| 624 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 625 | /** |
| 626 | * Marks the reference to this object as active. The scavenge garbage |
| 627 | * collection should not reclaim the objects marked as active. |
| 628 | * This bit is cleared after the each garbage collection pass. |
| 629 | */ |
| 630 | V8_INLINE void MarkActive(); |
| 631 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 632 | V8_INLINE bool IsIndependent() const; |
| 633 | |
| 634 | /** Checks if the handle holds the only reference to an object. */ |
| 635 | V8_INLINE bool IsNearDeath() const; |
| 636 | |
| 637 | /** Returns true if the handle's reference is weak. */ |
| 638 | V8_INLINE bool IsWeak() const; |
| 639 | |
| 640 | /** |
| 641 | * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface |
| 642 | * description in v8-profiler.h for details. |
| 643 | */ |
| 644 | V8_INLINE void SetWrapperClassId(uint16_t class_id); |
| 645 | |
| 646 | /** |
| 647 | * Returns the class ID previously assigned to this handle or 0 if no class ID |
| 648 | * was previously assigned. |
| 649 | */ |
| 650 | V8_INLINE uint16_t WrapperClassId() const; |
| 651 | |
| 652 | private: |
| 653 | friend class Isolate; |
| 654 | friend class Utils; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 655 | template<class F> friend class Local; |
| 656 | template<class F1, class F2> friend class Persistent; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 657 | template <class F> |
| 658 | friend class Global; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 659 | template<class F> friend class PersistentBase; |
| 660 | template<class F> friend class ReturnValue; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 661 | template <class F1, class F2, class F3> |
| 662 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 663 | template<class F1, class F2> friend class PersistentValueVector; |
| 664 | friend class Object; |
| 665 | |
| 666 | explicit V8_INLINE PersistentBase(T* val) : val_(val) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 667 | PersistentBase(const PersistentBase& other) = delete; // NOLINT |
| 668 | void operator=(const PersistentBase&) = delete; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 669 | V8_INLINE static T* New(Isolate* isolate, T* that); |
| 670 | |
| 671 | T* val_; |
| 672 | }; |
| 673 | |
| 674 | |
| 675 | /** |
| 676 | * Default traits for Persistent. This class does not allow |
| 677 | * use of the copy constructor or assignment operator. |
| 678 | * At present kResetInDestructor is not set, but that will change in a future |
| 679 | * version. |
| 680 | */ |
| 681 | template<class T> |
| 682 | class NonCopyablePersistentTraits { |
| 683 | public: |
| 684 | typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent; |
| 685 | static const bool kResetInDestructor = false; |
| 686 | template<class S, class M> |
| 687 | V8_INLINE static void Copy(const Persistent<S, M>& source, |
| 688 | NonCopyablePersistent* dest) { |
| 689 | Uncompilable<Object>(); |
| 690 | } |
| 691 | // TODO(dcarney): come up with a good compile error here. |
| 692 | template<class O> V8_INLINE static void Uncompilable() { |
| 693 | TYPE_CHECK(O, Primitive); |
| 694 | } |
| 695 | }; |
| 696 | |
| 697 | |
| 698 | /** |
| 699 | * Helper class traits to allow copying and assignment of Persistent. |
| 700 | * This will clone the contents of storage cell, but not any of the flags, etc. |
| 701 | */ |
| 702 | template<class T> |
| 703 | struct CopyablePersistentTraits { |
| 704 | typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent; |
| 705 | static const bool kResetInDestructor = true; |
| 706 | template<class S, class M> |
| 707 | static V8_INLINE void Copy(const Persistent<S, M>& source, |
| 708 | CopyablePersistent* dest) { |
| 709 | // do nothing, just allow copy |
| 710 | } |
| 711 | }; |
| 712 | |
| 713 | |
| 714 | /** |
| 715 | * A PersistentBase which allows copy and assignment. |
| 716 | * |
| 717 | * Copy, assignment and destructor bevavior is controlled by the traits |
| 718 | * class M. |
| 719 | * |
| 720 | * Note: Persistent class hierarchy is subject to future changes. |
| 721 | */ |
| 722 | template <class T, class M> class Persistent : public PersistentBase<T> { |
| 723 | public: |
| 724 | /** |
| 725 | * A Persistent with no storage cell. |
| 726 | */ |
| 727 | V8_INLINE Persistent() : PersistentBase<T>(0) { } |
| 728 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 729 | * Construct a Persistent from a Local. |
| 730 | * When the Local is non-empty, a new storage cell is created |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 731 | * pointing to the same object, and no flags are set. |
| 732 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 733 | template <class S> |
| 734 | V8_INLINE Persistent(Isolate* isolate, Local<S> that) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 735 | : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) { |
| 736 | TYPE_CHECK(T, S); |
| 737 | } |
| 738 | /** |
| 739 | * Construct a Persistent from a Persistent. |
| 740 | * When the Persistent is non-empty, a new storage cell is created |
| 741 | * pointing to the same object, and no flags are set. |
| 742 | */ |
| 743 | template <class S, class M2> |
| 744 | V8_INLINE Persistent(Isolate* isolate, const Persistent<S, M2>& that) |
| 745 | : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) { |
| 746 | TYPE_CHECK(T, S); |
| 747 | } |
| 748 | /** |
| 749 | * The copy constructors and assignment operator create a Persistent |
| 750 | * exactly as the Persistent constructor, but the Copy function from the |
| 751 | * traits class is called, allowing the setting of flags based on the |
| 752 | * copied Persistent. |
| 753 | */ |
| 754 | V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(0) { |
| 755 | Copy(that); |
| 756 | } |
| 757 | template <class S, class M2> |
| 758 | V8_INLINE Persistent(const Persistent<S, M2>& that) : PersistentBase<T>(0) { |
| 759 | Copy(that); |
| 760 | } |
| 761 | V8_INLINE Persistent& operator=(const Persistent& that) { // NOLINT |
| 762 | Copy(that); |
| 763 | return *this; |
| 764 | } |
| 765 | template <class S, class M2> |
| 766 | V8_INLINE Persistent& operator=(const Persistent<S, M2>& that) { // NOLINT |
| 767 | Copy(that); |
| 768 | return *this; |
| 769 | } |
| 770 | /** |
| 771 | * The destructor will dispose the Persistent based on the |
| 772 | * kResetInDestructor flags in the traits class. Since not calling dispose |
| 773 | * can result in a memory leak, it is recommended to always set this flag. |
| 774 | */ |
| 775 | V8_INLINE ~Persistent() { |
| 776 | if (M::kResetInDestructor) this->Reset(); |
| 777 | } |
| 778 | |
| 779 | // TODO(dcarney): this is pretty useless, fix or remove |
| 780 | template <class S> |
| 781 | V8_INLINE static Persistent<T>& Cast(Persistent<S>& that) { // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 782 | #ifdef V8_ENABLE_CHECKS |
| 783 | // If we're going to perform the type check then we have to check |
| 784 | // that the handle isn't empty before doing the checked cast. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 785 | if (!that.IsEmpty()) T::Cast(*that); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 786 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 787 | return reinterpret_cast<Persistent<T>&>(that); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 790 | // TODO(dcarney): this is pretty useless, fix or remove |
| 791 | template <class S> V8_INLINE Persistent<S>& As() { // NOLINT |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 792 | return Persistent<S>::Cast(*this); |
| 793 | } |
| 794 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 795 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 796 | friend class Isolate; |
| 797 | friend class Utils; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 798 | template<class F> friend class Local; |
| 799 | template<class F1, class F2> friend class Persistent; |
| 800 | template<class F> friend class ReturnValue; |
| 801 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 802 | explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 803 | V8_INLINE T* operator*() const { return this->val_; } |
| 804 | template<class S, class M2> |
| 805 | V8_INLINE void Copy(const Persistent<S, M2>& that); |
| 806 | }; |
| 807 | |
| 808 | |
| 809 | /** |
| 810 | * A PersistentBase which has move semantics. |
| 811 | * |
| 812 | * Note: Persistent class hierarchy is subject to future changes. |
| 813 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 814 | template <class T> |
| 815 | class Global : public PersistentBase<T> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 816 | public: |
| 817 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 818 | * A Global with no storage cell. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 819 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 820 | V8_INLINE Global() : PersistentBase<T>(nullptr) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 821 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 822 | * Construct a Global from a Local. |
| 823 | * When the Local is non-empty, a new storage cell is created |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 824 | * pointing to the same object, and no flags are set. |
| 825 | */ |
| 826 | template <class S> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 827 | V8_INLINE Global(Isolate* isolate, Local<S> that) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 828 | : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) { |
| 829 | TYPE_CHECK(T, S); |
| 830 | } |
| 831 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 832 | * Construct a Global from a PersistentBase. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 833 | * When the Persistent is non-empty, a new storage cell is created |
| 834 | * pointing to the same object, and no flags are set. |
| 835 | */ |
| 836 | template <class S> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 837 | V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that) |
| 838 | : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 839 | TYPE_CHECK(T, S); |
| 840 | } |
| 841 | /** |
| 842 | * Move constructor. |
| 843 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 844 | V8_INLINE Global(Global&& other) : PersistentBase<T>(other.val_) { // NOLINT |
| 845 | other.val_ = nullptr; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 846 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 847 | V8_INLINE ~Global() { this->Reset(); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 848 | /** |
| 849 | * Move via assignment. |
| 850 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 851 | template <class S> |
| 852 | V8_INLINE Global& operator=(Global<S>&& rhs) { // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 853 | TYPE_CHECK(T, S); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 854 | if (this != &rhs) { |
| 855 | this->Reset(); |
| 856 | this->val_ = rhs.val_; |
| 857 | rhs.val_ = nullptr; |
| 858 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 859 | return *this; |
| 860 | } |
| 861 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 862 | * Pass allows returning uniques from functions, etc. |
| 863 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 864 | Global Pass() { return static_cast<Global&&>(*this); } // NOLINT |
| 865 | |
| 866 | /* |
| 867 | * For compatibility with Chromium's base::Bind (base::Passed). |
| 868 | */ |
| 869 | typedef void MoveOnlyTypeForCPP03; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 870 | |
| 871 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 872 | template <class F> |
| 873 | friend class ReturnValue; |
| 874 | Global(const Global&) = delete; |
| 875 | void operator=(const Global&) = delete; |
| 876 | V8_INLINE T* operator*() const { return this->val_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 877 | }; |
| 878 | |
| 879 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 880 | // UniquePersistent is an alias for Global for historical reason. |
| 881 | template <class T> |
| 882 | using UniquePersistent = Global<T>; |
| 883 | |
| 884 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 885 | /** |
| 886 | * A stack-allocated class that governs a number of local handles. |
| 887 | * After a handle scope has been created, all local handles will be |
| 888 | * allocated within that handle scope until either the handle scope is |
| 889 | * deleted or another handle scope is created. If there is already a |
| 890 | * handle scope and a new one is created, all allocations will take |
| 891 | * place in the new handle scope until it is deleted. After that, |
| 892 | * new handles will again be allocated in the original handle scope. |
| 893 | * |
| 894 | * After the handle scope of a local handle has been deleted the |
| 895 | * garbage collector will no longer track the object stored in the |
| 896 | * handle and may deallocate it. The behavior of accessing a handle |
| 897 | * for which the handle scope has been deleted is undefined. |
| 898 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 899 | class V8_EXPORT HandleScope { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 900 | public: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 901 | explicit HandleScope(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 902 | |
| 903 | ~HandleScope(); |
| 904 | |
| 905 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 906 | * Counts the number of allocated handles. |
| 907 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 908 | static int NumberOfHandles(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 909 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 910 | V8_INLINE Isolate* GetIsolate() const { |
| 911 | return reinterpret_cast<Isolate*>(isolate_); |
| 912 | } |
| 913 | |
| 914 | protected: |
| 915 | V8_INLINE HandleScope() {} |
| 916 | |
| 917 | void Initialize(Isolate* isolate); |
| 918 | |
| 919 | static internal::Object** CreateHandle(internal::Isolate* isolate, |
| 920 | internal::Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 921 | |
| 922 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 923 | // Uses heap_object to obtain the current Isolate. |
| 924 | static internal::Object** CreateHandle(internal::HeapObject* heap_object, |
| 925 | internal::Object* value); |
| 926 | |
| 927 | // Make it hard to create heap-allocated or illegal handle scopes by |
| 928 | // disallowing certain operations. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 929 | HandleScope(const HandleScope&); |
| 930 | void operator=(const HandleScope&); |
| 931 | void* operator new(size_t size); |
| 932 | void operator delete(void*, size_t); |
| 933 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 934 | internal::Isolate* isolate_; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 935 | internal::Object** prev_next_; |
| 936 | internal::Object** prev_limit_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 937 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 938 | // Local::New uses CreateHandle with an Isolate* parameter. |
| 939 | template<class F> friend class Local; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 940 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 941 | // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with |
| 942 | // a HeapObject* in their shortcuts. |
| 943 | friend class Object; |
| 944 | friend class Context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 945 | }; |
| 946 | |
| 947 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 948 | /** |
| 949 | * A HandleScope which first allocates a handle in the current scope |
| 950 | * which will be later filled with the escape value. |
| 951 | */ |
| 952 | class V8_EXPORT EscapableHandleScope : public HandleScope { |
| 953 | public: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 954 | explicit EscapableHandleScope(Isolate* isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 955 | V8_INLINE ~EscapableHandleScope() {} |
| 956 | |
| 957 | /** |
| 958 | * Pushes the value into the previous scope and returns a handle to it. |
| 959 | * Cannot be called twice. |
| 960 | */ |
| 961 | template <class T> |
| 962 | V8_INLINE Local<T> Escape(Local<T> value) { |
| 963 | internal::Object** slot = |
| 964 | Escape(reinterpret_cast<internal::Object**>(*value)); |
| 965 | return Local<T>(reinterpret_cast<T*>(slot)); |
| 966 | } |
| 967 | |
| 968 | private: |
| 969 | internal::Object** Escape(internal::Object** escape_value); |
| 970 | |
| 971 | // Make it hard to create heap-allocated or illegal handle scopes by |
| 972 | // disallowing certain operations. |
| 973 | EscapableHandleScope(const EscapableHandleScope&); |
| 974 | void operator=(const EscapableHandleScope&); |
| 975 | void* operator new(size_t size); |
| 976 | void operator delete(void*, size_t); |
| 977 | |
| 978 | internal::Object** escape_slot_; |
| 979 | }; |
| 980 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 981 | class V8_EXPORT SealHandleScope { |
| 982 | public: |
| 983 | SealHandleScope(Isolate* isolate); |
| 984 | ~SealHandleScope(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 985 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 986 | private: |
| 987 | // Make it hard to create heap-allocated or illegal handle scopes by |
| 988 | // disallowing certain operations. |
| 989 | SealHandleScope(const SealHandleScope&); |
| 990 | void operator=(const SealHandleScope&); |
| 991 | void* operator new(size_t size); |
| 992 | void operator delete(void*, size_t); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 993 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 994 | internal::Isolate* isolate_; |
| 995 | internal::Object** prev_limit_; |
| 996 | int prev_sealed_level_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 997 | }; |
| 998 | |
| 999 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1000 | // --- Special objects --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1001 | |
| 1002 | |
| 1003 | /** |
| 1004 | * The superclass of values and API object templates. |
| 1005 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1006 | class V8_EXPORT Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1007 | private: |
| 1008 | Data(); |
| 1009 | }; |
| 1010 | |
| 1011 | |
| 1012 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1013 | * The optional attributes of ScriptOrigin. |
| 1014 | */ |
| 1015 | class ScriptOriginOptions { |
| 1016 | public: |
| 1017 | V8_INLINE ScriptOriginOptions(bool is_embedder_debug_script = false, |
| 1018 | bool is_shared_cross_origin = false, |
| 1019 | bool is_opaque = false) |
| 1020 | : flags_((is_embedder_debug_script ? kIsEmbedderDebugScript : 0) | |
| 1021 | (is_shared_cross_origin ? kIsSharedCrossOrigin : 0) | |
| 1022 | (is_opaque ? kIsOpaque : 0)) {} |
| 1023 | V8_INLINE ScriptOriginOptions(int flags) |
| 1024 | : flags_(flags & |
| 1025 | (kIsEmbedderDebugScript | kIsSharedCrossOrigin | kIsOpaque)) {} |
| 1026 | bool IsEmbedderDebugScript() const { |
| 1027 | return (flags_ & kIsEmbedderDebugScript) != 0; |
| 1028 | } |
| 1029 | bool IsSharedCrossOrigin() const { |
| 1030 | return (flags_ & kIsSharedCrossOrigin) != 0; |
| 1031 | } |
| 1032 | bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; } |
| 1033 | int Flags() const { return flags_; } |
| 1034 | |
| 1035 | private: |
| 1036 | enum { |
| 1037 | kIsEmbedderDebugScript = 1, |
| 1038 | kIsSharedCrossOrigin = 1 << 1, |
| 1039 | kIsOpaque = 1 << 2 |
| 1040 | }; |
| 1041 | const int flags_; |
| 1042 | }; |
| 1043 | |
| 1044 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1045 | * The origin, within a file, of a script. |
| 1046 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1047 | class ScriptOrigin { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1048 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1049 | V8_INLINE ScriptOrigin( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1050 | Local<Value> resource_name, |
| 1051 | Local<Integer> resource_line_offset = Local<Integer>(), |
| 1052 | Local<Integer> resource_column_offset = Local<Integer>(), |
| 1053 | Local<Boolean> resource_is_shared_cross_origin = Local<Boolean>(), |
| 1054 | Local<Integer> script_id = Local<Integer>(), |
| 1055 | Local<Boolean> resource_is_embedder_debug_script = Local<Boolean>(), |
| 1056 | Local<Value> source_map_url = Local<Value>(), |
| 1057 | Local<Boolean> resource_is_opaque = Local<Boolean>()); |
| 1058 | V8_INLINE Local<Value> ResourceName() const; |
| 1059 | V8_INLINE Local<Integer> ResourceLineOffset() const; |
| 1060 | V8_INLINE Local<Integer> ResourceColumnOffset() const; |
| 1061 | /** |
| 1062 | * Returns true for embedder's debugger scripts |
| 1063 | */ |
| 1064 | V8_INLINE Local<Integer> ScriptID() const; |
| 1065 | V8_INLINE Local<Value> SourceMapUrl() const; |
| 1066 | V8_INLINE ScriptOriginOptions Options() const { return options_; } |
| 1067 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1068 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1069 | Local<Value> resource_name_; |
| 1070 | Local<Integer> resource_line_offset_; |
| 1071 | Local<Integer> resource_column_offset_; |
| 1072 | ScriptOriginOptions options_; |
| 1073 | Local<Integer> script_id_; |
| 1074 | Local<Value> source_map_url_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1075 | }; |
| 1076 | |
| 1077 | |
| 1078 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1079 | * A compiled JavaScript script, not yet tied to a Context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1080 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1081 | class V8_EXPORT UnboundScript { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1082 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1083 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1084 | * Binds the script to the currently entered context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1085 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1086 | Local<Script> BindToCurrentContext(); |
| 1087 | |
| 1088 | int GetId(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1089 | Local<Value> GetScriptName(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1090 | |
| 1091 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1092 | * Data read from magic sourceURL comments. |
| 1093 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1094 | Local<Value> GetSourceURL(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1095 | /** |
| 1096 | * Data read from magic sourceMappingURL comments. |
| 1097 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1098 | Local<Value> GetSourceMappingURL(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1099 | |
| 1100 | /** |
| 1101 | * Returns zero based line number of the code_pos location in the script. |
| 1102 | * -1 will be returned if no information available. |
| 1103 | */ |
| 1104 | int GetLineNumber(int code_pos); |
| 1105 | |
| 1106 | static const int kNoScriptId = 0; |
| 1107 | }; |
| 1108 | |
| 1109 | |
| 1110 | /** |
| 1111 | * A compiled JavaScript script, tied to a Context which was active when the |
| 1112 | * script was compiled. |
| 1113 | */ |
| 1114 | class V8_EXPORT Script { |
| 1115 | public: |
| 1116 | /** |
| 1117 | * A shorthand for ScriptCompiler::Compile(). |
| 1118 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1119 | static V8_DEPRECATE_SOON( |
| 1120 | "Use maybe version", |
| 1121 | Local<Script> Compile(Local<String> source, |
| 1122 | ScriptOrigin* origin = nullptr)); |
| 1123 | static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( |
| 1124 | Local<Context> context, Local<String> source, |
| 1125 | ScriptOrigin* origin = nullptr); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1126 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1127 | static Local<Script> V8_DEPRECATE_SOON("Use maybe version", |
| 1128 | Compile(Local<String> source, |
| 1129 | Local<String> file_name)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1130 | |
| 1131 | /** |
| 1132 | * Runs the script returning the resulting value. It will be run in the |
| 1133 | * context in which it was created (ScriptCompiler::CompileBound or |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1134 | * UnboundScript::BindToCurrentContext()). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1135 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1136 | V8_DEPRECATE_SOON("Use maybe version", Local<Value> Run()); |
| 1137 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1138 | |
| 1139 | /** |
| 1140 | * Returns the corresponding context-unbound script. |
| 1141 | */ |
| 1142 | Local<UnboundScript> GetUnboundScript(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1143 | }; |
| 1144 | |
| 1145 | |
| 1146 | /** |
| 1147 | * For compiling scripts. |
| 1148 | */ |
| 1149 | class V8_EXPORT ScriptCompiler { |
| 1150 | public: |
| 1151 | /** |
| 1152 | * Compilation data that the embedder can cache and pass back to speed up |
| 1153 | * future compilations. The data is produced if the CompilerOptions passed to |
| 1154 | * the compilation functions in ScriptCompiler contains produce_data_to_cache |
| 1155 | * = true. The data to cache can then can be retrieved from |
| 1156 | * UnboundScript. |
| 1157 | */ |
| 1158 | struct V8_EXPORT CachedData { |
| 1159 | enum BufferPolicy { |
| 1160 | BufferNotOwned, |
| 1161 | BufferOwned |
| 1162 | }; |
| 1163 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1164 | CachedData() |
| 1165 | : data(NULL), |
| 1166 | length(0), |
| 1167 | rejected(false), |
| 1168 | buffer_policy(BufferNotOwned) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1169 | |
| 1170 | // If buffer_policy is BufferNotOwned, the caller keeps the ownership of |
| 1171 | // data and guarantees that it stays alive until the CachedData object is |
| 1172 | // destroyed. If the policy is BufferOwned, the given data will be deleted |
| 1173 | // (with delete[]) when the CachedData object is destroyed. |
| 1174 | CachedData(const uint8_t* data, int length, |
| 1175 | BufferPolicy buffer_policy = BufferNotOwned); |
| 1176 | ~CachedData(); |
| 1177 | // TODO(marja): Async compilation; add constructors which take a callback |
| 1178 | // which will be called when V8 no longer needs the data. |
| 1179 | const uint8_t* data; |
| 1180 | int length; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1181 | bool rejected; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1182 | BufferPolicy buffer_policy; |
| 1183 | |
| 1184 | private: |
| 1185 | // Prevent copying. Not implemented. |
| 1186 | CachedData(const CachedData&); |
| 1187 | CachedData& operator=(const CachedData&); |
| 1188 | }; |
| 1189 | |
| 1190 | /** |
| 1191 | * Source code which can be then compiled to a UnboundScript or Script. |
| 1192 | */ |
| 1193 | class Source { |
| 1194 | public: |
| 1195 | // Source takes ownership of CachedData. |
| 1196 | V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin, |
| 1197 | CachedData* cached_data = NULL); |
| 1198 | V8_INLINE Source(Local<String> source_string, |
| 1199 | CachedData* cached_data = NULL); |
| 1200 | V8_INLINE ~Source(); |
| 1201 | |
| 1202 | // Ownership of the CachedData or its buffers is *not* transferred to the |
| 1203 | // caller. The CachedData object is alive as long as the Source object is |
| 1204 | // alive. |
| 1205 | V8_INLINE const CachedData* GetCachedData() const; |
| 1206 | |
| 1207 | private: |
| 1208 | friend class ScriptCompiler; |
| 1209 | // Prevent copying. Not implemented. |
| 1210 | Source(const Source&); |
| 1211 | Source& operator=(const Source&); |
| 1212 | |
| 1213 | Local<String> source_string; |
| 1214 | |
| 1215 | // Origin information |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1216 | Local<Value> resource_name; |
| 1217 | Local<Integer> resource_line_offset; |
| 1218 | Local<Integer> resource_column_offset; |
| 1219 | ScriptOriginOptions resource_options; |
| 1220 | Local<Value> source_map_url; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Cached data from previous compilation (if a kConsume*Cache flag is |
| 1223 | // set), or hold newly generated cache data (kProduce*Cache flags) are |
| 1224 | // set when calling a compile method. |
| 1225 | CachedData* cached_data; |
| 1226 | }; |
| 1227 | |
| 1228 | /** |
| 1229 | * For streaming incomplete script data to V8. The embedder should implement a |
| 1230 | * subclass of this class. |
| 1231 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1232 | class V8_EXPORT ExternalSourceStream { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1233 | public: |
| 1234 | virtual ~ExternalSourceStream() {} |
| 1235 | |
| 1236 | /** |
| 1237 | * V8 calls this to request the next chunk of data from the embedder. This |
| 1238 | * function will be called on a background thread, so it's OK to block and |
| 1239 | * wait for the data, if the embedder doesn't have data yet. Returns the |
| 1240 | * length of the data returned. When the data ends, GetMoreData should |
| 1241 | * return 0. Caller takes ownership of the data. |
| 1242 | * |
| 1243 | * When streaming UTF-8 data, V8 handles multi-byte characters split between |
| 1244 | * two data chunks, but doesn't handle multi-byte characters split between |
| 1245 | * more than two data chunks. The embedder can avoid this problem by always |
| 1246 | * returning at least 2 bytes of data. |
| 1247 | * |
| 1248 | * If the embedder wants to cancel the streaming, they should make the next |
| 1249 | * GetMoreData call return 0. V8 will interpret it as end of data (and most |
| 1250 | * probably, parsing will fail). The streaming task will return as soon as |
| 1251 | * V8 has parsed the data it received so far. |
| 1252 | */ |
| 1253 | virtual size_t GetMoreData(const uint8_t** src) = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1254 | |
| 1255 | /** |
| 1256 | * V8 calls this method to set a 'bookmark' at the current position in |
| 1257 | * the source stream, for the purpose of (maybe) later calling |
| 1258 | * ResetToBookmark. If ResetToBookmark is called later, then subsequent |
| 1259 | * calls to GetMoreData should return the same data as they did when |
| 1260 | * SetBookmark was called earlier. |
| 1261 | * |
| 1262 | * The embedder may return 'false' to indicate it cannot provide this |
| 1263 | * functionality. |
| 1264 | */ |
| 1265 | virtual bool SetBookmark(); |
| 1266 | |
| 1267 | /** |
| 1268 | * V8 calls this to return to a previously set bookmark. |
| 1269 | */ |
| 1270 | virtual void ResetToBookmark(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1271 | }; |
| 1272 | |
| 1273 | |
| 1274 | /** |
| 1275 | * Source code which can be streamed into V8 in pieces. It will be parsed |
| 1276 | * while streaming. It can be compiled after the streaming is complete. |
| 1277 | * StreamedSource must be kept alive while the streaming task is ran (see |
| 1278 | * ScriptStreamingTask below). |
| 1279 | */ |
| 1280 | class V8_EXPORT StreamedSource { |
| 1281 | public: |
| 1282 | enum Encoding { ONE_BYTE, TWO_BYTE, UTF8 }; |
| 1283 | |
| 1284 | StreamedSource(ExternalSourceStream* source_stream, Encoding encoding); |
| 1285 | ~StreamedSource(); |
| 1286 | |
| 1287 | // Ownership of the CachedData or its buffers is *not* transferred to the |
| 1288 | // caller. The CachedData object is alive as long as the StreamedSource |
| 1289 | // object is alive. |
| 1290 | const CachedData* GetCachedData() const; |
| 1291 | |
| 1292 | internal::StreamedSource* impl() const { return impl_; } |
| 1293 | |
| 1294 | private: |
| 1295 | // Prevent copying. Not implemented. |
| 1296 | StreamedSource(const StreamedSource&); |
| 1297 | StreamedSource& operator=(const StreamedSource&); |
| 1298 | |
| 1299 | internal::StreamedSource* impl_; |
| 1300 | }; |
| 1301 | |
| 1302 | /** |
| 1303 | * A streaming task which the embedder must run on a background thread to |
| 1304 | * stream scripts into V8. Returned by ScriptCompiler::StartStreamingScript. |
| 1305 | */ |
| 1306 | class ScriptStreamingTask { |
| 1307 | public: |
| 1308 | virtual ~ScriptStreamingTask() {} |
| 1309 | virtual void Run() = 0; |
| 1310 | }; |
| 1311 | |
| 1312 | enum CompileOptions { |
| 1313 | kNoCompileOptions = 0, |
| 1314 | kProduceParserCache, |
| 1315 | kConsumeParserCache, |
| 1316 | kProduceCodeCache, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1317 | kConsumeCodeCache |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1318 | }; |
| 1319 | |
| 1320 | /** |
| 1321 | * Compiles the specified script (context-independent). |
| 1322 | * Cached data as part of the source object can be optionally produced to be |
| 1323 | * consumed later to speed up compilation of identical source scripts. |
| 1324 | * |
| 1325 | * Note that when producing cached data, the source must point to NULL for |
| 1326 | * cached data. When consuming cached data, the cached data must have been |
| 1327 | * produced by the same version of V8. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1328 | * |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1329 | * \param source Script source code. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1330 | * \return Compiled script object (context independent; for running it must be |
| 1331 | * bound to a context). |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1332 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1333 | static V8_DEPRECATED("Use maybe version", |
| 1334 | Local<UnboundScript> CompileUnbound( |
| 1335 | Isolate* isolate, Source* source, |
| 1336 | CompileOptions options = kNoCompileOptions)); |
| 1337 | static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1338 | Isolate* isolate, Source* source, |
| 1339 | CompileOptions options = kNoCompileOptions); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1340 | |
| 1341 | /** |
| 1342 | * Compiles the specified script (bound to current context). |
| 1343 | * |
| 1344 | * \param source Script source code. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1345 | * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile() |
| 1346 | * using pre_data speeds compilation if it's done multiple times. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1347 | * Owned by caller, no references are kept when this function returns. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1348 | * \return Compiled script object, bound to the context that was active |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1349 | * when this function was called. When run it will always use this |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1350 | * context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1351 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1352 | static V8_DEPRECATED( |
| 1353 | "Use maybe version", |
| 1354 | Local<Script> Compile(Isolate* isolate, Source* source, |
| 1355 | CompileOptions options = kNoCompileOptions)); |
| 1356 | static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( |
| 1357 | Local<Context> context, Source* source, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1358 | CompileOptions options = kNoCompileOptions); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1359 | |
| 1360 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1361 | * Returns a task which streams script data into V8, or NULL if the script |
| 1362 | * cannot be streamed. The user is responsible for running the task on a |
| 1363 | * background thread and deleting it. When ran, the task starts parsing the |
| 1364 | * script, and it will request data from the StreamedSource as needed. When |
| 1365 | * ScriptStreamingTask::Run exits, all data has been streamed and the script |
| 1366 | * can be compiled (see Compile below). |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1367 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1368 | * This API allows to start the streaming with as little data as possible, and |
| 1369 | * the remaining data (for example, the ScriptOrigin) is passed to Compile. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1370 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1371 | static ScriptStreamingTask* StartStreamingScript( |
| 1372 | Isolate* isolate, StreamedSource* source, |
| 1373 | CompileOptions options = kNoCompileOptions); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1374 | |
| 1375 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1376 | * Compiles a streamed script (bound to current context). |
| 1377 | * |
| 1378 | * This can only be called after the streaming has finished |
| 1379 | * (ScriptStreamingTask has been run). V8 doesn't construct the source string |
| 1380 | * during streaming, so the embedder needs to pass the full source here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1381 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1382 | static V8_DEPRECATED("Use maybe version", |
| 1383 | Local<Script> Compile(Isolate* isolate, |
| 1384 | StreamedSource* source, |
| 1385 | Local<String> full_source_string, |
| 1386 | const ScriptOrigin& origin)); |
| 1387 | static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile( |
| 1388 | Local<Context> context, StreamedSource* source, |
| 1389 | Local<String> full_source_string, const ScriptOrigin& origin); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1390 | |
| 1391 | /** |
| 1392 | * Return a version tag for CachedData for the current V8 version & flags. |
| 1393 | * |
| 1394 | * This value is meant only for determining whether a previously generated |
| 1395 | * CachedData instance is still valid; the tag has no other meaing. |
| 1396 | * |
| 1397 | * Background: The data carried by CachedData may depend on the exact |
| 1398 | * V8 version number or currently compiler flags. This means when |
| 1399 | * persisting CachedData, the embedder must take care to not pass in |
| 1400 | * data from another V8 version, or the same version with different |
| 1401 | * features enabled. |
| 1402 | * |
| 1403 | * The easiest way to do so is to clear the embedder's cache on any |
| 1404 | * such change. |
| 1405 | * |
| 1406 | * Alternatively, this tag can be stored alongside the cached data and |
| 1407 | * compared when it is being used. |
| 1408 | */ |
| 1409 | static uint32_t CachedDataVersionTag(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1410 | |
| 1411 | /** |
| 1412 | * Compile an ES6 module. |
| 1413 | * |
| 1414 | * This is an unfinished experimental feature, and is only exposed |
| 1415 | * here for internal testing purposes. |
| 1416 | * Only parsing works at the moment. Do not use. |
| 1417 | * |
| 1418 | * TODO(adamk): Script is likely the wrong return value for this; |
| 1419 | * should return some new Module type. |
| 1420 | */ |
| 1421 | static V8_WARN_UNUSED_RESULT MaybeLocal<Script> CompileModule( |
| 1422 | Local<Context> context, Source* source, |
| 1423 | CompileOptions options = kNoCompileOptions); |
| 1424 | |
| 1425 | /** |
| 1426 | * Compile a function for a given context. This is equivalent to running |
| 1427 | * |
| 1428 | * with (obj) { |
| 1429 | * return function(args) { ... } |
| 1430 | * } |
| 1431 | * |
| 1432 | * It is possible to specify multiple context extensions (obj in the above |
| 1433 | * example). |
| 1434 | */ |
| 1435 | static V8_DEPRECATE_SOON("Use maybe version", |
| 1436 | Local<Function> CompileFunctionInContext( |
| 1437 | Isolate* isolate, Source* source, |
| 1438 | Local<Context> context, size_t arguments_count, |
| 1439 | Local<String> arguments[], |
| 1440 | size_t context_extension_count, |
| 1441 | Local<Object> context_extensions[])); |
| 1442 | static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext( |
| 1443 | Local<Context> context, Source* source, size_t arguments_count, |
| 1444 | Local<String> arguments[], size_t context_extension_count, |
| 1445 | Local<Object> context_extensions[]); |
| 1446 | |
| 1447 | private: |
| 1448 | static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal( |
| 1449 | Isolate* isolate, Source* source, CompileOptions options, bool is_module); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1450 | }; |
| 1451 | |
| 1452 | |
| 1453 | /** |
| 1454 | * An error message. |
| 1455 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1456 | class V8_EXPORT Message { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1457 | public: |
| 1458 | Local<String> Get() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1459 | |
| 1460 | V8_DEPRECATE_SOON("Use maybe version", Local<String> GetSourceLine() const); |
| 1461 | V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine( |
| 1462 | Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1463 | |
| 1464 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1465 | * Returns the origin for the script from where the function causing the |
| 1466 | * error originates. |
| 1467 | */ |
| 1468 | ScriptOrigin GetScriptOrigin() const; |
| 1469 | |
| 1470 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1471 | * Returns the resource name for the script from where the function causing |
| 1472 | * the error originates. |
| 1473 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1474 | Local<Value> GetScriptResourceName() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1475 | |
| 1476 | /** |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1477 | * Exception stack trace. By default stack traces are not captured for |
| 1478 | * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows |
| 1479 | * to change this option. |
| 1480 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1481 | Local<StackTrace> GetStackTrace() const; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1482 | |
| 1483 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1484 | * Returns the number, 1-based, of the line where the error occurred. |
| 1485 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1486 | V8_DEPRECATE_SOON("Use maybe version", int GetLineNumber() const); |
| 1487 | V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1488 | |
| 1489 | /** |
| 1490 | * Returns the index within the script of the first character where |
| 1491 | * the error occurred. |
| 1492 | */ |
| 1493 | int GetStartPosition() const; |
| 1494 | |
| 1495 | /** |
| 1496 | * Returns the index within the script of the last character where |
| 1497 | * the error occurred. |
| 1498 | */ |
| 1499 | int GetEndPosition() const; |
| 1500 | |
| 1501 | /** |
| 1502 | * Returns the index within the line of the first character where |
| 1503 | * the error occurred. |
| 1504 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1505 | V8_DEPRECATE_SOON("Use maybe version", int GetStartColumn() const); |
| 1506 | V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1507 | |
| 1508 | /** |
| 1509 | * Returns the index within the line of the last character where |
| 1510 | * the error occurred. |
| 1511 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1512 | V8_DEPRECATED("Use maybe version", int GetEndColumn() const); |
| 1513 | V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1514 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1515 | /** |
| 1516 | * Passes on the value set by the embedder when it fed the script from which |
| 1517 | * this Message was generated to V8. |
| 1518 | */ |
| 1519 | bool IsSharedCrossOrigin() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1520 | bool IsOpaque() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1521 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1522 | // TODO(1245381): Print to a string instead of on a FILE. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1523 | static void PrintCurrentStackTrace(Isolate* isolate, FILE* out); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1524 | |
| 1525 | static const int kNoLineNumberInfo = 0; |
| 1526 | static const int kNoColumnInfo = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1527 | static const int kNoScriptIdInfo = 0; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1528 | }; |
| 1529 | |
| 1530 | |
| 1531 | /** |
| 1532 | * Representation of a JavaScript stack trace. The information collected is a |
| 1533 | * snapshot of the execution stack and the information remains valid after |
| 1534 | * execution continues. |
| 1535 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1536 | class V8_EXPORT StackTrace { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1537 | public: |
| 1538 | /** |
| 1539 | * Flags that determine what information is placed captured for each |
| 1540 | * StackFrame when grabbing the current stack trace. |
| 1541 | */ |
| 1542 | enum StackTraceOptions { |
| 1543 | kLineNumber = 1, |
| 1544 | kColumnOffset = 1 << 1 | kLineNumber, |
| 1545 | kScriptName = 1 << 2, |
| 1546 | kFunctionName = 1 << 3, |
| 1547 | kIsEval = 1 << 4, |
| 1548 | kIsConstructor = 1 << 5, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1549 | kScriptNameOrSourceURL = 1 << 6, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1550 | kScriptId = 1 << 7, |
| 1551 | kExposeFramesAcrossSecurityOrigins = 1 << 8, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1552 | kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1553 | kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1554 | }; |
| 1555 | |
| 1556 | /** |
| 1557 | * Returns a StackFrame at a particular index. |
| 1558 | */ |
| 1559 | Local<StackFrame> GetFrame(uint32_t index) const; |
| 1560 | |
| 1561 | /** |
| 1562 | * Returns the number of StackFrames. |
| 1563 | */ |
| 1564 | int GetFrameCount() const; |
| 1565 | |
| 1566 | /** |
| 1567 | * Returns StackTrace as a v8::Array that contains StackFrame objects. |
| 1568 | */ |
| 1569 | Local<Array> AsArray(); |
| 1570 | |
| 1571 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1572 | * Grab a snapshot of the current JavaScript execution stack. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1573 | * |
| 1574 | * \param frame_limit The maximum number of stack frames we want to capture. |
| 1575 | * \param options Enumerates the set of things we will capture for each |
| 1576 | * StackFrame. |
| 1577 | */ |
| 1578 | static Local<StackTrace> CurrentStackTrace( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1579 | Isolate* isolate, |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1580 | int frame_limit, |
| 1581 | StackTraceOptions options = kOverview); |
| 1582 | }; |
| 1583 | |
| 1584 | |
| 1585 | /** |
| 1586 | * A single JavaScript stack frame. |
| 1587 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1588 | class V8_EXPORT StackFrame { |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1589 | public: |
| 1590 | /** |
| 1591 | * Returns the number, 1-based, of the line for the associate function call. |
| 1592 | * This method will return Message::kNoLineNumberInfo if it is unable to |
| 1593 | * retrieve the line number, or if kLineNumber was not passed as an option |
| 1594 | * when capturing the StackTrace. |
| 1595 | */ |
| 1596 | int GetLineNumber() const; |
| 1597 | |
| 1598 | /** |
| 1599 | * Returns the 1-based column offset on the line for the associated function |
| 1600 | * call. |
| 1601 | * This method will return Message::kNoColumnInfo if it is unable to retrieve |
| 1602 | * the column number, or if kColumnOffset was not passed as an option when |
| 1603 | * capturing the StackTrace. |
| 1604 | */ |
| 1605 | int GetColumn() const; |
| 1606 | |
| 1607 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1608 | * Returns the id of the script for the function for this StackFrame. |
| 1609 | * This method will return Message::kNoScriptIdInfo if it is unable to |
| 1610 | * retrieve the script id, or if kScriptId was not passed as an option when |
| 1611 | * capturing the StackTrace. |
| 1612 | */ |
| 1613 | int GetScriptId() const; |
| 1614 | |
| 1615 | /** |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1616 | * Returns the name of the resource that contains the script for the |
| 1617 | * function for this StackFrame. |
| 1618 | */ |
| 1619 | Local<String> GetScriptName() const; |
| 1620 | |
| 1621 | /** |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1622 | * Returns the name of the resource that contains the script for the |
| 1623 | * function for this StackFrame or sourceURL value if the script name |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1624 | * is undefined and its source ends with //# sourceURL=... string or |
| 1625 | * deprecated //@ sourceURL=... string. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1626 | */ |
| 1627 | Local<String> GetScriptNameOrSourceURL() const; |
| 1628 | |
| 1629 | /** |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1630 | * Returns the name of the function associated with this stack frame. |
| 1631 | */ |
| 1632 | Local<String> GetFunctionName() const; |
| 1633 | |
| 1634 | /** |
| 1635 | * Returns whether or not the associated function is compiled via a call to |
| 1636 | * eval(). |
| 1637 | */ |
| 1638 | bool IsEval() const; |
| 1639 | |
| 1640 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1641 | * Returns whether or not the associated function is called as a |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 1642 | * constructor via "new". |
| 1643 | */ |
| 1644 | bool IsConstructor() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1645 | }; |
| 1646 | |
| 1647 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1648 | // A StateTag represents a possible state of the VM. |
| 1649 | enum StateTag { JS, GC, COMPILER, OTHER, EXTERNAL, IDLE }; |
| 1650 | |
| 1651 | |
| 1652 | // A RegisterState represents the current state of registers used |
| 1653 | // by the sampling profiler API. |
| 1654 | struct RegisterState { |
| 1655 | RegisterState() : pc(NULL), sp(NULL), fp(NULL) {} |
| 1656 | void* pc; // Instruction pointer. |
| 1657 | void* sp; // Stack pointer. |
| 1658 | void* fp; // Frame pointer. |
| 1659 | }; |
| 1660 | |
| 1661 | |
| 1662 | // The output structure filled up by GetStackSample API function. |
| 1663 | struct SampleInfo { |
| 1664 | size_t frames_count; |
| 1665 | StateTag vm_state; |
| 1666 | }; |
| 1667 | |
| 1668 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1669 | /** |
| 1670 | * A JSON Parser. |
| 1671 | */ |
| 1672 | class V8_EXPORT JSON { |
| 1673 | public: |
| 1674 | /** |
| 1675 | * Tries to parse the string |json_string| and returns it as value if |
| 1676 | * successful. |
| 1677 | * |
| 1678 | * \param json_string The string to parse. |
| 1679 | * \return The corresponding value if successfully parsed. |
| 1680 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1681 | static V8_DEPRECATED("Use maybe version", |
| 1682 | Local<Value> Parse(Local<String> json_string)); |
| 1683 | static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse( |
| 1684 | Isolate* isolate, Local<String> json_string); |
| 1685 | }; |
| 1686 | |
| 1687 | |
| 1688 | /** |
| 1689 | * A map whose keys are referenced weakly. It is similar to JavaScript WeakMap |
| 1690 | * but can be created without entering a v8::Context and hence shouldn't |
| 1691 | * escape to JavaScript. |
| 1692 | */ |
| 1693 | class V8_EXPORT NativeWeakMap : public Data { |
| 1694 | public: |
| 1695 | static Local<NativeWeakMap> New(Isolate* isolate); |
| 1696 | void Set(Local<Value> key, Local<Value> value); |
| 1697 | Local<Value> Get(Local<Value> key); |
| 1698 | bool Has(Local<Value> key); |
| 1699 | bool Delete(Local<Value> key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1700 | }; |
| 1701 | |
| 1702 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1703 | // --- Value --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1704 | |
| 1705 | |
| 1706 | /** |
| 1707 | * The superclass of all JavaScript values and objects. |
| 1708 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1709 | class V8_EXPORT Value : public Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1710 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1711 | /** |
| 1712 | * Returns true if this value is the undefined value. See ECMA-262 |
| 1713 | * 4.3.10. |
| 1714 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1715 | V8_INLINE bool IsUndefined() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1716 | |
| 1717 | /** |
| 1718 | * Returns true if this value is the null value. See ECMA-262 |
| 1719 | * 4.3.11. |
| 1720 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1721 | V8_INLINE bool IsNull() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1722 | |
| 1723 | /** |
| 1724 | * Returns true if this value is true. |
| 1725 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1726 | bool IsTrue() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1727 | |
| 1728 | /** |
| 1729 | * Returns true if this value is false. |
| 1730 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1731 | bool IsFalse() const; |
| 1732 | |
| 1733 | /** |
| 1734 | * Returns true if this value is a symbol or a string. |
| 1735 | * This is an experimental feature. |
| 1736 | */ |
| 1737 | bool IsName() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1738 | |
| 1739 | /** |
| 1740 | * Returns true if this value is an instance of the String type. |
| 1741 | * See ECMA-262 8.4. |
| 1742 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1743 | V8_INLINE bool IsString() const; |
| 1744 | |
| 1745 | /** |
| 1746 | * Returns true if this value is a symbol. |
| 1747 | * This is an experimental feature. |
| 1748 | */ |
| 1749 | bool IsSymbol() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1750 | |
| 1751 | /** |
| 1752 | * Returns true if this value is a function. |
| 1753 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1754 | bool IsFunction() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1755 | |
| 1756 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1757 | * Returns true if this value is an array. Note that it will return false for |
| 1758 | * an Proxy for an array. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1759 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1760 | bool IsArray() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1761 | |
| 1762 | /** |
| 1763 | * Returns true if this value is an object. |
| 1764 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1765 | bool IsObject() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1766 | |
| 1767 | /** |
| 1768 | * Returns true if this value is boolean. |
| 1769 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1770 | bool IsBoolean() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1771 | |
| 1772 | /** |
| 1773 | * Returns true if this value is a number. |
| 1774 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1775 | bool IsNumber() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1776 | |
| 1777 | /** |
| 1778 | * Returns true if this value is external. |
| 1779 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1780 | bool IsExternal() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1781 | |
| 1782 | /** |
| 1783 | * Returns true if this value is a 32-bit signed integer. |
| 1784 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1785 | bool IsInt32() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1786 | |
| 1787 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1788 | * Returns true if this value is a 32-bit unsigned integer. |
| 1789 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1790 | bool IsUint32() const; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1791 | |
| 1792 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1793 | * Returns true if this value is a Date. |
| 1794 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1795 | bool IsDate() const; |
| 1796 | |
| 1797 | /** |
| 1798 | * Returns true if this value is an Arguments object. |
| 1799 | */ |
| 1800 | bool IsArgumentsObject() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1801 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1802 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1803 | * Returns true if this value is a Boolean object. |
| 1804 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1805 | bool IsBooleanObject() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1806 | |
| 1807 | /** |
| 1808 | * Returns true if this value is a Number object. |
| 1809 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1810 | bool IsNumberObject() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1811 | |
| 1812 | /** |
| 1813 | * Returns true if this value is a String object. |
| 1814 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1815 | bool IsStringObject() const; |
| 1816 | |
| 1817 | /** |
| 1818 | * Returns true if this value is a Symbol object. |
| 1819 | * This is an experimental feature. |
| 1820 | */ |
| 1821 | bool IsSymbolObject() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1822 | |
| 1823 | /** |
| 1824 | * Returns true if this value is a NativeError. |
| 1825 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1826 | bool IsNativeError() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1827 | |
| 1828 | /** |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1829 | * Returns true if this value is a RegExp. |
| 1830 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1831 | bool IsRegExp() const; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1832 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1833 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1834 | * Returns true if this value is a Generator function. |
| 1835 | * This is an experimental feature. |
| 1836 | */ |
| 1837 | bool IsGeneratorFunction() const; |
| 1838 | |
| 1839 | /** |
| 1840 | * Returns true if this value is a Generator object (iterator). |
| 1841 | * This is an experimental feature. |
| 1842 | */ |
| 1843 | bool IsGeneratorObject() const; |
| 1844 | |
| 1845 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1846 | * Returns true if this value is a Promise. |
| 1847 | * This is an experimental feature. |
| 1848 | */ |
| 1849 | bool IsPromise() const; |
| 1850 | |
| 1851 | /** |
| 1852 | * Returns true if this value is a Map. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1853 | */ |
| 1854 | bool IsMap() const; |
| 1855 | |
| 1856 | /** |
| 1857 | * Returns true if this value is a Set. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1858 | */ |
| 1859 | bool IsSet() const; |
| 1860 | |
| 1861 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1862 | * Returns true if this value is a Map Iterator. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1863 | */ |
| 1864 | bool IsMapIterator() const; |
| 1865 | |
| 1866 | /** |
| 1867 | * Returns true if this value is a Set Iterator. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1868 | */ |
| 1869 | bool IsSetIterator() const; |
| 1870 | |
| 1871 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1872 | * Returns true if this value is a WeakMap. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1873 | */ |
| 1874 | bool IsWeakMap() const; |
| 1875 | |
| 1876 | /** |
| 1877 | * Returns true if this value is a WeakSet. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1878 | */ |
| 1879 | bool IsWeakSet() const; |
| 1880 | |
| 1881 | /** |
| 1882 | * Returns true if this value is an ArrayBuffer. |
| 1883 | * This is an experimental feature. |
| 1884 | */ |
| 1885 | bool IsArrayBuffer() const; |
| 1886 | |
| 1887 | /** |
| 1888 | * Returns true if this value is an ArrayBufferView. |
| 1889 | * This is an experimental feature. |
| 1890 | */ |
| 1891 | bool IsArrayBufferView() const; |
| 1892 | |
| 1893 | /** |
| 1894 | * Returns true if this value is one of TypedArrays. |
| 1895 | * This is an experimental feature. |
| 1896 | */ |
| 1897 | bool IsTypedArray() const; |
| 1898 | |
| 1899 | /** |
| 1900 | * Returns true if this value is an Uint8Array. |
| 1901 | * This is an experimental feature. |
| 1902 | */ |
| 1903 | bool IsUint8Array() const; |
| 1904 | |
| 1905 | /** |
| 1906 | * Returns true if this value is an Uint8ClampedArray. |
| 1907 | * This is an experimental feature. |
| 1908 | */ |
| 1909 | bool IsUint8ClampedArray() const; |
| 1910 | |
| 1911 | /** |
| 1912 | * Returns true if this value is an Int8Array. |
| 1913 | * This is an experimental feature. |
| 1914 | */ |
| 1915 | bool IsInt8Array() const; |
| 1916 | |
| 1917 | /** |
| 1918 | * Returns true if this value is an Uint16Array. |
| 1919 | * This is an experimental feature. |
| 1920 | */ |
| 1921 | bool IsUint16Array() const; |
| 1922 | |
| 1923 | /** |
| 1924 | * Returns true if this value is an Int16Array. |
| 1925 | * This is an experimental feature. |
| 1926 | */ |
| 1927 | bool IsInt16Array() const; |
| 1928 | |
| 1929 | /** |
| 1930 | * Returns true if this value is an Uint32Array. |
| 1931 | * This is an experimental feature. |
| 1932 | */ |
| 1933 | bool IsUint32Array() const; |
| 1934 | |
| 1935 | /** |
| 1936 | * Returns true if this value is an Int32Array. |
| 1937 | * This is an experimental feature. |
| 1938 | */ |
| 1939 | bool IsInt32Array() const; |
| 1940 | |
| 1941 | /** |
| 1942 | * Returns true if this value is a Float32Array. |
| 1943 | * This is an experimental feature. |
| 1944 | */ |
| 1945 | bool IsFloat32Array() const; |
| 1946 | |
| 1947 | /** |
| 1948 | * Returns true if this value is a Float64Array. |
| 1949 | * This is an experimental feature. |
| 1950 | */ |
| 1951 | bool IsFloat64Array() const; |
| 1952 | |
| 1953 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1954 | * Returns true if this value is a SIMD Float32x4. |
| 1955 | * This is an experimental feature. |
| 1956 | */ |
| 1957 | bool IsFloat32x4() const; |
| 1958 | |
| 1959 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1960 | * Returns true if this value is a DataView. |
| 1961 | * This is an experimental feature. |
| 1962 | */ |
| 1963 | bool IsDataView() const; |
| 1964 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1965 | /** |
| 1966 | * Returns true if this value is a SharedArrayBuffer. |
| 1967 | * This is an experimental feature. |
| 1968 | */ |
| 1969 | bool IsSharedArrayBuffer() const; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1970 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1971 | /** |
| 1972 | * Returns true if this value is a JavaScript Proxy. |
| 1973 | */ |
| 1974 | bool IsProxy() const; |
| 1975 | |
| 1976 | |
| 1977 | V8_WARN_UNUSED_RESULT MaybeLocal<Boolean> ToBoolean( |
| 1978 | Local<Context> context) const; |
| 1979 | V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber( |
| 1980 | Local<Context> context) const; |
| 1981 | V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString( |
| 1982 | Local<Context> context) const; |
| 1983 | V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString( |
| 1984 | Local<Context> context) const; |
| 1985 | V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject( |
| 1986 | Local<Context> context) const; |
| 1987 | V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger( |
| 1988 | Local<Context> context) const; |
| 1989 | V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32( |
| 1990 | Local<Context> context) const; |
| 1991 | V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const; |
| 1992 | |
| 1993 | V8_DEPRECATE_SOON("Use maybe version", |
| 1994 | Local<Boolean> ToBoolean(Isolate* isolate) const); |
| 1995 | V8_DEPRECATE_SOON("Use maybe version", |
| 1996 | Local<Number> ToNumber(Isolate* isolate) const); |
| 1997 | V8_DEPRECATE_SOON("Use maybe version", |
| 1998 | Local<String> ToString(Isolate* isolate) const); |
| 1999 | V8_DEPRECATED("Use maybe version", |
| 2000 | Local<String> ToDetailString(Isolate* isolate) const); |
| 2001 | V8_DEPRECATE_SOON("Use maybe version", |
| 2002 | Local<Object> ToObject(Isolate* isolate) const); |
| 2003 | V8_DEPRECATE_SOON("Use maybe version", |
| 2004 | Local<Integer> ToInteger(Isolate* isolate) const); |
| 2005 | V8_DEPRECATED("Use maybe version", |
| 2006 | Local<Uint32> ToUint32(Isolate* isolate) const); |
| 2007 | V8_DEPRECATE_SOON("Use maybe version", |
| 2008 | Local<Int32> ToInt32(Isolate* isolate) const); |
| 2009 | |
| 2010 | inline V8_DEPRECATE_SOON("Use maybe version", |
| 2011 | Local<Boolean> ToBoolean() const); |
| 2012 | inline V8_DEPRECATED("Use maybe version", Local<Number> ToNumber() const); |
| 2013 | inline V8_DEPRECATE_SOON("Use maybe version", Local<String> ToString() const); |
| 2014 | inline V8_DEPRECATED("Use maybe version", |
| 2015 | Local<String> ToDetailString() const); |
| 2016 | inline V8_DEPRECATE_SOON("Use maybe version", Local<Object> ToObject() const); |
| 2017 | inline V8_DEPRECATE_SOON("Use maybe version", |
| 2018 | Local<Integer> ToInteger() const); |
| 2019 | inline V8_DEPRECATED("Use maybe version", Local<Uint32> ToUint32() const); |
| 2020 | inline V8_DEPRECATED("Use maybe version", Local<Int32> ToInt32() const); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2021 | |
| 2022 | /** |
| 2023 | * Attempts to convert a string to an array index. |
| 2024 | * Returns an empty handle if the conversion fails. |
| 2025 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2026 | V8_DEPRECATED("Use maybe version", Local<Uint32> ToArrayIndex() const); |
| 2027 | V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex( |
| 2028 | Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2029 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2030 | V8_WARN_UNUSED_RESULT Maybe<bool> BooleanValue(Local<Context> context) const; |
| 2031 | V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const; |
| 2032 | V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue( |
| 2033 | Local<Context> context) const; |
| 2034 | V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value( |
| 2035 | Local<Context> context) const; |
| 2036 | V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const; |
| 2037 | |
| 2038 | V8_DEPRECATE_SOON("Use maybe version", bool BooleanValue() const); |
| 2039 | V8_DEPRECATE_SOON("Use maybe version", double NumberValue() const); |
| 2040 | V8_DEPRECATE_SOON("Use maybe version", int64_t IntegerValue() const); |
| 2041 | V8_DEPRECATE_SOON("Use maybe version", uint32_t Uint32Value() const); |
| 2042 | V8_DEPRECATE_SOON("Use maybe version", int32_t Int32Value() const); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2043 | |
| 2044 | /** JS == */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2045 | V8_DEPRECATE_SOON("Use maybe version", bool Equals(Local<Value> that) const); |
| 2046 | V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context, |
| 2047 | Local<Value> that) const; |
| 2048 | bool StrictEquals(Local<Value> that) const; |
| 2049 | bool SameValue(Local<Value> that) const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2050 | |
| 2051 | template <class T> V8_INLINE static Value* Cast(T* value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2052 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2053 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2054 | V8_INLINE bool QuickIsUndefined() const; |
| 2055 | V8_INLINE bool QuickIsNull() const; |
| 2056 | V8_INLINE bool QuickIsString() const; |
| 2057 | bool FullIsUndefined() const; |
| 2058 | bool FullIsNull() const; |
| 2059 | bool FullIsString() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2060 | }; |
| 2061 | |
| 2062 | |
| 2063 | /** |
| 2064 | * The superclass of primitive values. See ECMA-262 4.3.2. |
| 2065 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2066 | class V8_EXPORT Primitive : public Value { }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2067 | |
| 2068 | |
| 2069 | /** |
| 2070 | * A primitive boolean value (ECMA-262, 4.3.14). Either the true |
| 2071 | * or false value. |
| 2072 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2073 | class V8_EXPORT Boolean : public Primitive { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2074 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2075 | bool Value() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2076 | V8_INLINE static Boolean* Cast(v8::Value* obj); |
| 2077 | V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value); |
| 2078 | |
| 2079 | private: |
| 2080 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2081 | }; |
| 2082 | |
| 2083 | |
| 2084 | /** |
| 2085 | * A superclass for symbols and strings. |
| 2086 | */ |
| 2087 | class V8_EXPORT Name : public Primitive { |
| 2088 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2089 | /** |
| 2090 | * Returns the identity hash for this object. The current implementation |
| 2091 | * uses an inline property on the object to store the identity hash. |
| 2092 | * |
| 2093 | * The return value will never be 0. Also, it is not guaranteed to be |
| 2094 | * unique. |
| 2095 | */ |
| 2096 | int GetIdentityHash(); |
| 2097 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2098 | V8_INLINE static Name* Cast(v8::Value* obj); |
| 2099 | private: |
| 2100 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2101 | }; |
| 2102 | |
| 2103 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2104 | enum class NewStringType { kNormal, kInternalized }; |
| 2105 | |
| 2106 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2107 | /** |
| 2108 | * A JavaScript string value (ECMA-262, 4.3.17). |
| 2109 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2110 | class V8_EXPORT String : public Name { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2111 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2112 | static const int kMaxLength = (1 << 28) - 16; |
| 2113 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2114 | enum Encoding { |
| 2115 | UNKNOWN_ENCODING = 0x1, |
| 2116 | TWO_BYTE_ENCODING = 0x0, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2117 | ONE_BYTE_ENCODING = 0x4 |
| 2118 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2119 | /** |
| 2120 | * Returns the number of characters in this string. |
| 2121 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2122 | int Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2123 | |
| 2124 | /** |
| 2125 | * Returns the number of bytes in the UTF-8 encoded |
| 2126 | * representation of this string. |
| 2127 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2128 | int Utf8Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2129 | |
| 2130 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2131 | * Returns whether this string is known to contain only one byte data. |
| 2132 | * Does not read the string. |
| 2133 | * False negatives are possible. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2134 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2135 | bool IsOneByte() const; |
| 2136 | |
| 2137 | /** |
| 2138 | * Returns whether this string contain only one byte data. |
| 2139 | * Will read the entire string in some cases. |
| 2140 | */ |
| 2141 | bool ContainsOnlyOneByte() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2142 | |
| 2143 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2144 | * Write the contents of the string to an external buffer. |
| 2145 | * If no arguments are given, expects the buffer to be large |
| 2146 | * enough to hold the entire string and NULL terminator. Copies |
| 2147 | * the contents of the string and the NULL terminator into the |
| 2148 | * buffer. |
| 2149 | * |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2150 | * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop |
| 2151 | * before the end of the buffer. |
| 2152 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2153 | * Copies up to length characters into the output buffer. |
| 2154 | * Only null-terminates if there is enough space in the buffer. |
| 2155 | * |
| 2156 | * \param buffer The buffer into which the string will be copied. |
| 2157 | * \param start The starting position within the string at which |
| 2158 | * copying begins. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2159 | * \param length The number of characters to copy from the string. For |
| 2160 | * WriteUtf8 the number of bytes in the buffer. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2161 | * \param nchars_ref The number of characters written, can be NULL. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2162 | * \param options Various options that might affect performance of this or |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2163 | * subsequent operations. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2164 | * \return The number of characters copied to the buffer excluding the null |
| 2165 | * terminator. For WriteUtf8: The number of bytes copied to the buffer |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2166 | * including the null terminator (if written). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2167 | */ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2168 | enum WriteOptions { |
| 2169 | NO_OPTIONS = 0, |
| 2170 | HINT_MANY_WRITES_EXPECTED = 1, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2171 | NO_NULL_TERMINATION = 2, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2172 | PRESERVE_ONE_BYTE_NULL = 4, |
| 2173 | // Used by WriteUtf8 to replace orphan surrogate code units with the |
| 2174 | // unicode replacement character. Needs to be set to guarantee valid UTF-8 |
| 2175 | // output. |
| 2176 | REPLACE_INVALID_UTF8 = 8 |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2177 | }; |
| 2178 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2179 | // 16-bit character codes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2180 | int Write(uint16_t* buffer, |
| 2181 | int start = 0, |
| 2182 | int length = -1, |
| 2183 | int options = NO_OPTIONS) const; |
| 2184 | // One byte characters. |
| 2185 | int WriteOneByte(uint8_t* buffer, |
| 2186 | int start = 0, |
| 2187 | int length = -1, |
| 2188 | int options = NO_OPTIONS) const; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2189 | // UTF-8 encoded characters. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2190 | int WriteUtf8(char* buffer, |
| 2191 | int length = -1, |
| 2192 | int* nchars_ref = NULL, |
| 2193 | int options = NO_OPTIONS) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2194 | |
| 2195 | /** |
| 2196 | * A zero length string. |
| 2197 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2198 | V8_INLINE static v8::Local<v8::String> Empty(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2199 | |
| 2200 | /** |
| 2201 | * Returns true if the string is external |
| 2202 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2203 | bool IsExternal() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2204 | |
| 2205 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2206 | * Returns true if the string is both external and one-byte. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2207 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2208 | bool IsExternalOneByte() const; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2209 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2210 | class V8_EXPORT ExternalStringResourceBase { // NOLINT |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2211 | public: |
| 2212 | virtual ~ExternalStringResourceBase() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2213 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2214 | virtual bool IsCompressible() const { return false; } |
| 2215 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2216 | protected: |
| 2217 | ExternalStringResourceBase() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2218 | |
| 2219 | /** |
| 2220 | * Internally V8 will call this Dispose method when the external string |
| 2221 | * resource is no longer needed. The default implementation will use the |
| 2222 | * delete operator. This method can be overridden in subclasses to |
| 2223 | * control how allocated external string resources are disposed. |
| 2224 | */ |
| 2225 | virtual void Dispose() { delete this; } |
| 2226 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2227 | private: |
| 2228 | // Disallow copying and assigning. |
| 2229 | ExternalStringResourceBase(const ExternalStringResourceBase&); |
| 2230 | void operator=(const ExternalStringResourceBase&); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2231 | |
| 2232 | friend class v8::internal::Heap; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2233 | }; |
| 2234 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2235 | /** |
| 2236 | * An ExternalStringResource is a wrapper around a two-byte string |
| 2237 | * buffer that resides outside V8's heap. Implement an |
| 2238 | * ExternalStringResource to manage the life cycle of the underlying |
| 2239 | * buffer. Note that the string data must be immutable. |
| 2240 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2241 | class V8_EXPORT ExternalStringResource |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2242 | : public ExternalStringResourceBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2243 | public: |
| 2244 | /** |
| 2245 | * Override the destructor to manage the life cycle of the underlying |
| 2246 | * buffer. |
| 2247 | */ |
| 2248 | virtual ~ExternalStringResource() {} |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2249 | |
| 2250 | /** |
| 2251 | * The string data from the underlying buffer. |
| 2252 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2253 | virtual const uint16_t* data() const = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2254 | |
| 2255 | /** |
| 2256 | * The length of the string. That is, the number of two-byte characters. |
| 2257 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2258 | virtual size_t length() const = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2259 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2260 | protected: |
| 2261 | ExternalStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2262 | }; |
| 2263 | |
| 2264 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2265 | * An ExternalOneByteStringResource is a wrapper around an one-byte |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2266 | * string buffer that resides outside V8's heap. Implement an |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2267 | * ExternalOneByteStringResource to manage the life cycle of the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2268 | * underlying buffer. Note that the string data must be immutable |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2269 | * and that the data must be Latin-1 and not UTF-8, which would require |
| 2270 | * special treatment internally in the engine and do not allow efficient |
| 2271 | * indexing. Use String::New or convert to 16 bit data for non-Latin1. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2272 | */ |
| 2273 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2274 | class V8_EXPORT ExternalOneByteStringResource |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2275 | : public ExternalStringResourceBase { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2276 | public: |
| 2277 | /** |
| 2278 | * Override the destructor to manage the life cycle of the underlying |
| 2279 | * buffer. |
| 2280 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2281 | virtual ~ExternalOneByteStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2282 | /** The string data from the underlying buffer.*/ |
| 2283 | virtual const char* data() const = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2284 | /** The number of Latin-1 characters in the string.*/ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2285 | virtual size_t length() const = 0; |
| 2286 | protected: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2287 | ExternalOneByteStringResource() {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2288 | }; |
| 2289 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2290 | /** |
| 2291 | * If the string is an external string, return the ExternalStringResourceBase |
| 2292 | * regardless of the encoding, otherwise return NULL. The encoding of the |
| 2293 | * string is returned in encoding_out. |
| 2294 | */ |
| 2295 | V8_INLINE ExternalStringResourceBase* GetExternalStringResourceBase( |
| 2296 | Encoding* encoding_out) const; |
| 2297 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2298 | /** |
| 2299 | * Get the ExternalStringResource for an external string. Returns |
| 2300 | * NULL if IsExternal() doesn't return true. |
| 2301 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2302 | V8_INLINE ExternalStringResource* GetExternalStringResource() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2303 | |
| 2304 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2305 | * Get the ExternalOneByteStringResource for an external one-byte string. |
| 2306 | * Returns NULL if IsExternalOneByte() doesn't return true. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2307 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2308 | const ExternalOneByteStringResource* GetExternalOneByteStringResource() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2309 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2310 | V8_INLINE static String* Cast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2311 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2312 | // TODO(dcarney): remove with deprecation of New functions. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2313 | enum NewStringType { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2314 | kNormalString = static_cast<int>(v8::NewStringType::kNormal), |
| 2315 | kInternalizedString = static_cast<int>(v8::NewStringType::kInternalized) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2316 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2317 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2318 | /** Allocates a new string from UTF-8 data.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2319 | static V8_DEPRECATE_SOON( |
| 2320 | "Use maybe version", |
| 2321 | Local<String> NewFromUtf8(Isolate* isolate, const char* data, |
| 2322 | NewStringType type = kNormalString, |
| 2323 | int length = -1)); |
| 2324 | |
| 2325 | /** Allocates a new string from UTF-8 data. Only returns an empty value when |
| 2326 | * length > kMaxLength. **/ |
| 2327 | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8( |
| 2328 | Isolate* isolate, const char* data, v8::NewStringType type, |
| 2329 | int length = -1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2330 | |
| 2331 | /** Allocates a new string from Latin-1 data.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2332 | static V8_DEPRECATED( |
| 2333 | "Use maybe version", |
| 2334 | Local<String> NewFromOneByte(Isolate* isolate, const uint8_t* data, |
| 2335 | NewStringType type = kNormalString, |
| 2336 | int length = -1)); |
| 2337 | |
| 2338 | /** Allocates a new string from Latin-1 data. Only returns an empty value |
| 2339 | * when length > kMaxLength. **/ |
| 2340 | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte( |
| 2341 | Isolate* isolate, const uint8_t* data, v8::NewStringType type, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2342 | int length = -1); |
| 2343 | |
| 2344 | /** Allocates a new string from UTF-16 data.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2345 | static V8_DEPRECATE_SOON( |
| 2346 | "Use maybe version", |
| 2347 | Local<String> NewFromTwoByte(Isolate* isolate, const uint16_t* data, |
| 2348 | NewStringType type = kNormalString, |
| 2349 | int length = -1)); |
| 2350 | |
| 2351 | /** Allocates a new string from UTF-16 data. Only returns an empty value when |
| 2352 | * length > kMaxLength. **/ |
| 2353 | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte( |
| 2354 | Isolate* isolate, const uint16_t* data, v8::NewStringType type, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2355 | int length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2356 | |
| 2357 | /** |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2358 | * Creates a new string by concatenating the left and the right strings |
| 2359 | * passed in as parameters. |
| 2360 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2361 | static Local<String> Concat(Local<String> left, Local<String> right); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2362 | |
| 2363 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2364 | * Creates a new external string using the data defined in the given |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2365 | * resource. When the external string is no longer live on V8's heap the |
| 2366 | * resource will be disposed by calling its Dispose method. The caller of |
| 2367 | * this function should not otherwise delete or modify the resource. Neither |
| 2368 | * should the underlying buffer be deallocated or modified except through the |
| 2369 | * destructor of the external string resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2370 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2371 | static V8_DEPRECATED("Use maybe version", |
| 2372 | Local<String> NewExternal( |
| 2373 | Isolate* isolate, ExternalStringResource* resource)); |
| 2374 | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte( |
| 2375 | Isolate* isolate, ExternalStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2376 | |
| 2377 | /** |
| 2378 | * Associate an external string resource with this string by transforming it |
| 2379 | * in place so that existing references to this string in the JavaScript heap |
| 2380 | * will use the external string resource. The external string resource's |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2381 | * character contents need to be equivalent to this string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2382 | * 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] | 2383 | * The string is not modified if the operation fails. See NewExternal for |
| 2384 | * information on the lifetime of the resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2385 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2386 | bool MakeExternal(ExternalStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2387 | |
| 2388 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2389 | * 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] | 2390 | * resource. When the external string is no longer live on V8's heap the |
| 2391 | * resource will be disposed by calling its Dispose method. The caller of |
| 2392 | * this function should not otherwise delete or modify the resource. Neither |
| 2393 | * should the underlying buffer be deallocated or modified except through the |
| 2394 | * destructor of the external string resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2395 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2396 | static V8_DEPRECATE_SOON( |
| 2397 | "Use maybe version", |
| 2398 | Local<String> NewExternal(Isolate* isolate, |
| 2399 | ExternalOneByteStringResource* resource)); |
| 2400 | static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte( |
| 2401 | Isolate* isolate, ExternalOneByteStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2402 | |
| 2403 | /** |
| 2404 | * Associate an external string resource with this string by transforming it |
| 2405 | * in place so that existing references to this string in the JavaScript heap |
| 2406 | * will use the external string resource. The external string resource's |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2407 | * character contents need to be equivalent to this string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2408 | * 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] | 2409 | * The string is not modified if the operation fails. See NewExternal for |
| 2410 | * information on the lifetime of the resource. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2411 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2412 | bool MakeExternal(ExternalOneByteStringResource* resource); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2413 | |
| 2414 | /** |
| 2415 | * Returns true if this string can be made external. |
| 2416 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2417 | bool CanMakeExternal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2418 | |
| 2419 | /** |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2420 | * Converts an object to a UTF-8-encoded character array. Useful if |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2421 | * you want to print the object. If conversion to a string fails |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2422 | * (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] | 2423 | * then the length() method returns 0 and the * operator returns |
| 2424 | * NULL. |
| 2425 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2426 | class V8_EXPORT Utf8Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2427 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2428 | explicit Utf8Value(Local<v8::Value> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2429 | ~Utf8Value(); |
| 2430 | char* operator*() { return str_; } |
| 2431 | const char* operator*() const { return str_; } |
| 2432 | int length() const { return length_; } |
| 2433 | private: |
| 2434 | char* str_; |
| 2435 | int length_; |
| 2436 | |
| 2437 | // Disallow copying and assigning. |
| 2438 | Utf8Value(const Utf8Value&); |
| 2439 | void operator=(const Utf8Value&); |
| 2440 | }; |
| 2441 | |
| 2442 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2443 | * Converts an object to a two-byte string. |
| 2444 | * If conversion to a string fails (eg. due to an exception in the toString() |
| 2445 | * method of the object) then the length() method returns 0 and the * operator |
| 2446 | * returns NULL. |
| 2447 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2448 | class V8_EXPORT Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2449 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2450 | explicit Value(Local<v8::Value> obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2451 | ~Value(); |
| 2452 | uint16_t* operator*() { return str_; } |
| 2453 | const uint16_t* operator*() const { return str_; } |
| 2454 | int length() const { return length_; } |
| 2455 | private: |
| 2456 | uint16_t* str_; |
| 2457 | int length_; |
| 2458 | |
| 2459 | // Disallow copying and assigning. |
| 2460 | Value(const Value&); |
| 2461 | void operator=(const Value&); |
| 2462 | }; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2463 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2464 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2465 | void VerifyExternalStringResourceBase(ExternalStringResourceBase* v, |
| 2466 | Encoding encoding) const; |
| 2467 | void VerifyExternalStringResource(ExternalStringResource* val) const; |
| 2468 | static void CheckCast(v8::Value* obj); |
| 2469 | }; |
| 2470 | |
| 2471 | |
| 2472 | /** |
| 2473 | * A JavaScript symbol (ECMA-262 edition 6) |
| 2474 | * |
| 2475 | * This is an experimental feature. Use at your own risk. |
| 2476 | */ |
| 2477 | class V8_EXPORT Symbol : public Name { |
| 2478 | public: |
| 2479 | // Returns the print name string of the symbol, or undefined if none. |
| 2480 | Local<Value> Name() const; |
| 2481 | |
| 2482 | // Create a symbol. If name is not empty, it will be used as the description. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2483 | static Local<Symbol> New(Isolate* isolate, |
| 2484 | Local<String> name = Local<String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2485 | |
| 2486 | // Access global symbol registry. |
| 2487 | // Note that symbols created this way are never collected, so |
| 2488 | // they should only be used for statically fixed properties. |
| 2489 | // Also, there is only one global name space for the names used as keys. |
| 2490 | // To minimize the potential for clashes, use qualified names as keys. |
| 2491 | static Local<Symbol> For(Isolate *isolate, Local<String> name); |
| 2492 | |
| 2493 | // Retrieve a global symbol. Similar to |For|, but using a separate |
| 2494 | // registry that is not accessible by (and cannot clash with) JavaScript code. |
| 2495 | static Local<Symbol> ForApi(Isolate *isolate, Local<String> name); |
| 2496 | |
| 2497 | // Well-known symbols |
| 2498 | static Local<Symbol> GetIterator(Isolate* isolate); |
| 2499 | static Local<Symbol> GetUnscopables(Isolate* isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 2500 | static Local<Symbol> GetToStringTag(Isolate* isolate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2501 | static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2502 | |
| 2503 | V8_INLINE static Symbol* Cast(v8::Value* obj); |
| 2504 | |
| 2505 | private: |
| 2506 | Symbol(); |
| 2507 | static void CheckCast(v8::Value* obj); |
| 2508 | }; |
| 2509 | |
| 2510 | |
| 2511 | /** |
| 2512 | * A private symbol |
| 2513 | * |
| 2514 | * This is an experimental feature. Use at your own risk. |
| 2515 | */ |
| 2516 | class V8_EXPORT Private : public Data { |
| 2517 | public: |
| 2518 | // Returns the print name string of the private symbol, or undefined if none. |
| 2519 | Local<Value> Name() const; |
| 2520 | |
| 2521 | // Create a private symbol. If name is not empty, it will be the description. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2522 | static Local<Private> New(Isolate* isolate, |
| 2523 | Local<String> name = Local<String>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Retrieve a global private symbol. If a symbol with this name has not |
| 2526 | // been retrieved in the same isolate before, it is created. |
| 2527 | // Note that private symbols created this way are never collected, so |
| 2528 | // they should only be used for statically fixed properties. |
| 2529 | // Also, there is only one global name space for the names used as keys. |
| 2530 | // To minimize the potential for clashes, use qualified names as keys, |
| 2531 | // e.g., "Class#property". |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2532 | static Local<Private> ForApi(Isolate* isolate, Local<String> name); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2533 | |
| 2534 | private: |
| 2535 | Private(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2536 | }; |
| 2537 | |
| 2538 | |
| 2539 | /** |
| 2540 | * A JavaScript number value (ECMA-262, 4.3.20) |
| 2541 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2542 | class V8_EXPORT Number : public Primitive { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2543 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2544 | double Value() const; |
| 2545 | static Local<Number> New(Isolate* isolate, double value); |
| 2546 | V8_INLINE static Number* Cast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2547 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2548 | Number(); |
| 2549 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2550 | }; |
| 2551 | |
| 2552 | |
| 2553 | /** |
| 2554 | * A JavaScript value representing a signed integer. |
| 2555 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2556 | class V8_EXPORT Integer : public Number { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2557 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2558 | static Local<Integer> New(Isolate* isolate, int32_t value); |
| 2559 | static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value); |
| 2560 | int64_t Value() const; |
| 2561 | V8_INLINE static Integer* Cast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2562 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2563 | Integer(); |
| 2564 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2565 | }; |
| 2566 | |
| 2567 | |
| 2568 | /** |
| 2569 | * A JavaScript value representing a 32-bit signed integer. |
| 2570 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2571 | class V8_EXPORT Int32 : public Integer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2572 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2573 | int32_t Value() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2574 | V8_INLINE static Int32* Cast(v8::Value* obj); |
| 2575 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2576 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2577 | Int32(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2578 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2579 | }; |
| 2580 | |
| 2581 | |
| 2582 | /** |
| 2583 | * A JavaScript value representing a 32-bit unsigned integer. |
| 2584 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2585 | class V8_EXPORT Uint32 : public Integer { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2586 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2587 | uint32_t Value() const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2588 | V8_INLINE static Uint32* Cast(v8::Value* obj); |
| 2589 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2590 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2591 | Uint32(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2592 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2593 | }; |
| 2594 | |
| 2595 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2596 | enum PropertyAttribute { |
| 2597 | None = 0, |
| 2598 | ReadOnly = 1 << 0, |
| 2599 | DontEnum = 1 << 1, |
| 2600 | DontDelete = 1 << 2 |
| 2601 | }; |
| 2602 | |
| 2603 | /** |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2604 | * Accessor[Getter|Setter] are used as callback functions when |
| 2605 | * setting|getting a particular property. See Object and ObjectTemplate's |
| 2606 | * method SetAccessor. |
| 2607 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2608 | typedef void (*AccessorGetterCallback)( |
| 2609 | Local<String> property, |
| 2610 | const PropertyCallbackInfo<Value>& info); |
| 2611 | typedef void (*AccessorNameGetterCallback)( |
| 2612 | Local<Name> property, |
| 2613 | const PropertyCallbackInfo<Value>& info); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2614 | |
| 2615 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2616 | typedef void (*AccessorSetterCallback)( |
| 2617 | Local<String> property, |
| 2618 | Local<Value> value, |
| 2619 | const PropertyCallbackInfo<void>& info); |
| 2620 | typedef void (*AccessorNameSetterCallback)( |
| 2621 | Local<Name> property, |
| 2622 | Local<Value> value, |
| 2623 | const PropertyCallbackInfo<void>& info); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2624 | |
| 2625 | |
| 2626 | /** |
| 2627 | * Access control specifications. |
| 2628 | * |
| 2629 | * Some accessors should be accessible across contexts. These |
| 2630 | * accessors have an explicit access control parameter which specifies |
| 2631 | * the kind of cross-context access that should be allowed. |
| 2632 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2633 | * TODO(dcarney): Remove PROHIBITS_OVERWRITING as it is now unused. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2634 | */ |
| 2635 | enum AccessControl { |
| 2636 | DEFAULT = 0, |
| 2637 | ALL_CAN_READ = 1, |
| 2638 | ALL_CAN_WRITE = 1 << 1, |
| 2639 | PROHIBITS_OVERWRITING = 1 << 2 |
| 2640 | }; |
| 2641 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2642 | /** |
| 2643 | * Integrity level for objects. |
| 2644 | */ |
| 2645 | enum class IntegrityLevel { kFrozen, kSealed }; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2646 | |
| 2647 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2648 | * A JavaScript object (ECMA-262, 4.3.3) |
| 2649 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2650 | class V8_EXPORT Object : public Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2651 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2652 | V8_DEPRECATE_SOON("Use maybe version", |
| 2653 | bool Set(Local<Value> key, Local<Value> value)); |
| 2654 | V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, |
| 2655 | Local<Value> key, Local<Value> value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2656 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2657 | V8_DEPRECATE_SOON("Use maybe version", |
| 2658 | bool Set(uint32_t index, Local<Value> value)); |
| 2659 | V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index, |
| 2660 | Local<Value> value); |
| 2661 | |
| 2662 | // Implements CreateDataProperty (ECMA-262, 7.3.4). |
| 2663 | // |
| 2664 | // Defines a configurable, writable, enumerable property with the given value |
| 2665 | // on the object unless the property already exists and is not configurable |
| 2666 | // or the object is not extensible. |
| 2667 | // |
| 2668 | // Returns true on success. |
| 2669 | V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context, |
| 2670 | Local<Name> key, |
| 2671 | Local<Value> value); |
| 2672 | V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context, |
| 2673 | uint32_t index, |
| 2674 | Local<Value> value); |
| 2675 | |
| 2676 | // Implements DefineOwnProperty. |
| 2677 | // |
| 2678 | // In general, CreateDataProperty will be faster, however, does not allow |
| 2679 | // for specifying attributes. |
| 2680 | // |
| 2681 | // Returns true on success. |
| 2682 | V8_WARN_UNUSED_RESULT Maybe<bool> DefineOwnProperty( |
| 2683 | Local<Context> context, Local<Name> key, Local<Value> value, |
| 2684 | PropertyAttribute attributes = None); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2685 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2686 | // Sets an own property on this object bypassing interceptors and |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2687 | // overriding accessors or read-only properties. |
| 2688 | // |
| 2689 | // Note that if the object has an interceptor the property will be set |
| 2690 | // locally, but since the interceptor takes precedence the local property |
| 2691 | // will only be returned if the interceptor doesn't return a value. |
| 2692 | // |
| 2693 | // Note also that this only works for named properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2694 | V8_DEPRECATED("Use CreateDataProperty / DefineOwnProperty", |
| 2695 | bool ForceSet(Local<Value> key, Local<Value> value, |
| 2696 | PropertyAttribute attribs = None)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 2697 | V8_DEPRECATE_SOON("Use CreateDataProperty / DefineOwnProperty", |
| 2698 | Maybe<bool> ForceSet(Local<Context> context, |
| 2699 | Local<Value> key, Local<Value> value, |
| 2700 | PropertyAttribute attribs = None)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2701 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2702 | V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key)); |
| 2703 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context, |
| 2704 | Local<Value> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2705 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2706 | V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(uint32_t index)); |
| 2707 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context, |
| 2708 | uint32_t index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2709 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2710 | /** |
| 2711 | * Gets the property attributes of a property which can be None or |
| 2712 | * any combination of ReadOnly, DontEnum and DontDelete. Returns |
| 2713 | * None when the property doesn't exist. |
| 2714 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2715 | V8_DEPRECATED("Use maybe version", |
| 2716 | PropertyAttribute GetPropertyAttributes(Local<Value> key)); |
| 2717 | V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetPropertyAttributes( |
| 2718 | Local<Context> context, Local<Value> key); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2719 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2720 | /** |
| 2721 | * Returns Object.getOwnPropertyDescriptor as per ES5 section 15.2.3.3. |
| 2722 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2723 | V8_DEPRECATED("Use maybe version", |
| 2724 | Local<Value> GetOwnPropertyDescriptor(Local<String> key)); |
| 2725 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetOwnPropertyDescriptor( |
| 2726 | Local<Context> context, Local<String> key); |
Steve 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_DEPRECATE_SOON("Use maybe version", bool Has(Local<Value> key)); |
| 2729 | V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, |
| 2730 | Local<Value> key); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2731 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2732 | V8_DEPRECATE_SOON("Use maybe version", bool Delete(Local<Value> key)); |
| 2733 | // TODO(dcarney): mark V8_WARN_UNUSED_RESULT |
| 2734 | Maybe<bool> Delete(Local<Context> context, Local<Value> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2735 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2736 | V8_DEPRECATED("Use maybe version", bool Has(uint32_t index)); |
| 2737 | V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2738 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2739 | V8_DEPRECATED("Use maybe version", bool Delete(uint32_t index)); |
| 2740 | // TODO(dcarney): mark V8_WARN_UNUSED_RESULT |
| 2741 | Maybe<bool> Delete(Local<Context> context, uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2742 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2743 | V8_DEPRECATED("Use maybe version", |
| 2744 | bool SetAccessor(Local<String> name, |
| 2745 | AccessorGetterCallback getter, |
| 2746 | AccessorSetterCallback setter = 0, |
| 2747 | Local<Value> data = Local<Value>(), |
| 2748 | AccessControl settings = DEFAULT, |
| 2749 | PropertyAttribute attribute = None)); |
| 2750 | V8_DEPRECATED("Use maybe version", |
| 2751 | bool SetAccessor(Local<Name> name, |
| 2752 | AccessorNameGetterCallback getter, |
| 2753 | AccessorNameSetterCallback setter = 0, |
| 2754 | Local<Value> data = Local<Value>(), |
| 2755 | AccessControl settings = DEFAULT, |
| 2756 | PropertyAttribute attribute = None)); |
| 2757 | // TODO(dcarney): mark V8_WARN_UNUSED_RESULT |
| 2758 | Maybe<bool> SetAccessor(Local<Context> context, Local<Name> name, |
| 2759 | AccessorNameGetterCallback getter, |
| 2760 | AccessorNameSetterCallback setter = 0, |
| 2761 | MaybeLocal<Value> data = MaybeLocal<Value>(), |
| 2762 | AccessControl settings = DEFAULT, |
| 2763 | PropertyAttribute attribute = None); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2764 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2765 | void SetAccessorProperty(Local<Name> name, Local<Function> getter, |
| 2766 | Local<Function> setter = Local<Function>(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2767 | PropertyAttribute attribute = None, |
| 2768 | AccessControl settings = DEFAULT); |
| 2769 | |
| 2770 | /** |
| 2771 | * Functionality for private properties. |
| 2772 | * This is an experimental feature, use at your own risk. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2773 | * Note: Private properties are not inherited. Do not rely on this, since it |
| 2774 | * may change. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2775 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2776 | Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key); |
| 2777 | Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key, |
| 2778 | Local<Value> value); |
| 2779 | Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key); |
| 2780 | MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2781 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2782 | /** |
| 2783 | * Returns an array containing the names of the enumerable properties |
| 2784 | * of this object, including properties from prototype objects. The |
| 2785 | * array returned by this method contains the same values as would |
| 2786 | * be enumerated by a for-in statement over this object. |
| 2787 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2788 | V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetPropertyNames()); |
| 2789 | V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames( |
| 2790 | Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2791 | |
| 2792 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2793 | * This function has the same functionality as GetPropertyNames but |
| 2794 | * the returned array doesn't contain the names of properties from |
| 2795 | * prototype objects. |
| 2796 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2797 | V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetOwnPropertyNames()); |
| 2798 | V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames( |
| 2799 | Local<Context> context); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2800 | |
| 2801 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2802 | * Get the prototype object. This does not skip objects marked to |
| 2803 | * be skipped by __proto__ and it does not consult the security |
| 2804 | * handler. |
| 2805 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2806 | Local<Value> GetPrototype(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2807 | |
| 2808 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2809 | * Set the prototype object. This does not skip objects marked to |
| 2810 | * be skipped by __proto__ and it does not consult the security |
| 2811 | * handler. |
| 2812 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2813 | V8_DEPRECATED("Use maybe version", bool SetPrototype(Local<Value> prototype)); |
| 2814 | V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context, |
| 2815 | Local<Value> prototype); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2816 | |
| 2817 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2818 | * Finds an instance of the given function template in the prototype |
| 2819 | * chain. |
| 2820 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2821 | Local<Object> FindInstanceInPrototypeChain(Local<FunctionTemplate> tmpl); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2822 | |
| 2823 | /** |
| 2824 | * Call builtin Object.prototype.toString on this object. |
| 2825 | * This is different from Value::ToString() that may call |
| 2826 | * user-defined toString function. This one does not. |
| 2827 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2828 | V8_DEPRECATED("Use maybe version", Local<String> ObjectProtoToString()); |
| 2829 | V8_WARN_UNUSED_RESULT MaybeLocal<String> ObjectProtoToString( |
| 2830 | Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2831 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2832 | /** |
| 2833 | * Returns the name of the function invoked as a constructor for this object. |
| 2834 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2835 | Local<String> GetConstructorName(); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 2836 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 2837 | /** |
| 2838 | * Sets the integrity level of the object. |
| 2839 | */ |
| 2840 | Maybe<bool> SetIntegrityLevel(Local<Context> context, IntegrityLevel level); |
| 2841 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2842 | /** Gets the number of internal fields for this Object. */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2843 | int InternalFieldCount(); |
| 2844 | |
| 2845 | /** Same as above, but works for Persistents */ |
| 2846 | V8_INLINE static int InternalFieldCount( |
| 2847 | const PersistentBase<Object>& object) { |
| 2848 | return object.val_->InternalFieldCount(); |
| 2849 | } |
| 2850 | |
| 2851 | /** Gets the value from an internal field. */ |
| 2852 | V8_INLINE Local<Value> GetInternalField(int index); |
| 2853 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2854 | /** Sets the value in an internal field. */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2855 | void SetInternalField(int index, Local<Value> value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2856 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2857 | /** |
| 2858 | * Gets a 2-byte-aligned native pointer from an internal field. This field |
| 2859 | * must have been set by SetAlignedPointerInInternalField, everything else |
| 2860 | * leads to undefined behavior. |
| 2861 | */ |
| 2862 | V8_INLINE void* GetAlignedPointerFromInternalField(int index); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2863 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2864 | /** Same as above, but works for Persistents */ |
| 2865 | V8_INLINE static void* GetAlignedPointerFromInternalField( |
| 2866 | const PersistentBase<Object>& object, int index) { |
| 2867 | return object.val_->GetAlignedPointerFromInternalField(index); |
| 2868 | } |
| 2869 | |
| 2870 | /** |
| 2871 | * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such |
| 2872 | * a field, GetAlignedPointerFromInternalField must be used, everything else |
| 2873 | * leads to undefined behavior. |
| 2874 | */ |
| 2875 | void SetAlignedPointerInInternalField(int index, void* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2876 | |
| 2877 | // Testers for local properties. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2878 | V8_DEPRECATED("Use maybe version", bool HasOwnProperty(Local<String> key)); |
| 2879 | V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context, |
| 2880 | Local<Name> key); |
| 2881 | V8_DEPRECATE_SOON("Use maybe version", |
| 2882 | bool HasRealNamedProperty(Local<String> key)); |
| 2883 | V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedProperty(Local<Context> context, |
| 2884 | Local<Name> key); |
| 2885 | V8_DEPRECATE_SOON("Use maybe version", |
| 2886 | bool HasRealIndexedProperty(uint32_t index)); |
| 2887 | V8_WARN_UNUSED_RESULT Maybe<bool> HasRealIndexedProperty( |
| 2888 | Local<Context> context, uint32_t index); |
| 2889 | V8_DEPRECATE_SOON("Use maybe version", |
| 2890 | bool HasRealNamedCallbackProperty(Local<String> key)); |
| 2891 | V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedCallbackProperty( |
| 2892 | Local<Context> context, Local<Name> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2893 | |
| 2894 | /** |
| 2895 | * If result.IsEmpty() no real property was located in the prototype chain. |
| 2896 | * This means interceptors in the prototype chain are not called. |
| 2897 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2898 | V8_DEPRECATED( |
| 2899 | "Use maybe version", |
| 2900 | Local<Value> GetRealNamedPropertyInPrototypeChain(Local<String> key)); |
| 2901 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedPropertyInPrototypeChain( |
| 2902 | Local<Context> context, Local<Name> key); |
| 2903 | |
| 2904 | /** |
| 2905 | * Gets the property attributes of a real property in the prototype chain, |
| 2906 | * which can be None or any combination of ReadOnly, DontEnum and DontDelete. |
| 2907 | * Interceptors in the prototype chain are not called. |
| 2908 | */ |
| 2909 | V8_DEPRECATED( |
| 2910 | "Use maybe version", |
| 2911 | Maybe<PropertyAttribute> GetRealNamedPropertyAttributesInPrototypeChain( |
| 2912 | Local<String> key)); |
| 2913 | V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> |
| 2914 | GetRealNamedPropertyAttributesInPrototypeChain(Local<Context> context, |
| 2915 | Local<Name> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2916 | |
| 2917 | /** |
| 2918 | * If result.IsEmpty() no real property was located on the object or |
| 2919 | * in the prototype chain. |
| 2920 | * This means interceptors in the prototype chain are not called. |
| 2921 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2922 | V8_DEPRECATED("Use maybe version", |
| 2923 | Local<Value> GetRealNamedProperty(Local<String> key)); |
| 2924 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedProperty( |
| 2925 | Local<Context> context, Local<Name> key); |
| 2926 | |
| 2927 | /** |
| 2928 | * Gets the property attributes of a real property which can be |
| 2929 | * None or any combination of ReadOnly, DontEnum and DontDelete. |
| 2930 | * Interceptors in the prototype chain are not called. |
| 2931 | */ |
| 2932 | V8_DEPRECATED("Use maybe version", |
| 2933 | Maybe<PropertyAttribute> GetRealNamedPropertyAttributes( |
| 2934 | Local<String> key)); |
| 2935 | V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetRealNamedPropertyAttributes( |
| 2936 | Local<Context> context, Local<Name> key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2937 | |
| 2938 | /** Tests for a named lookup interceptor.*/ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2939 | bool HasNamedLookupInterceptor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2940 | |
| 2941 | /** Tests for an index lookup interceptor.*/ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2942 | bool HasIndexedLookupInterceptor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2943 | |
| 2944 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2945 | * Returns the identity hash for this object. The current implementation |
| 2946 | * uses a hidden property on the object to store the identity hash. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2947 | * |
| 2948 | * The return value will never be 0. Also, it is not guaranteed to be |
| 2949 | * unique. |
| 2950 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2951 | int GetIdentityHash(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2952 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2953 | V8_DEPRECATED("Use v8::Object::SetPrivate instead.", |
| 2954 | bool SetHiddenValue(Local<String> key, Local<Value> value)); |
| 2955 | V8_DEPRECATED("Use v8::Object::GetPrivate instead.", |
| 2956 | Local<Value> GetHiddenValue(Local<String> key)); |
| 2957 | V8_DEPRECATED("Use v8::Object::DeletePrivate instead.", |
| 2958 | bool DeleteHiddenValue(Local<String> key)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2959 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2960 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2961 | * Clone this object with a fast but shallow copy. Values will point |
| 2962 | * to the same values as the original object. |
| 2963 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2964 | // TODO(dcarney): take an isolate and optionally bail out? |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2965 | Local<Object> Clone(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2966 | |
| 2967 | /** |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2968 | * Returns the context in which the object was created. |
| 2969 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2970 | Local<Context> CreationContext(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2971 | |
| 2972 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2973 | * Checks whether a callback is set by the |
| 2974 | * ObjectTemplate::SetCallAsFunctionHandler method. |
| 2975 | * When an Object is callable this method returns true. |
| 2976 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2977 | bool IsCallable(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2978 | |
| 2979 | /** |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2980 | * 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] | 2981 | * ObjectTemplate::SetCallAsFunctionHandler method. |
| 2982 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2983 | V8_DEPRECATED("Use maybe version", |
| 2984 | Local<Value> CallAsFunction(Local<Value> recv, int argc, |
| 2985 | Local<Value> argv[])); |
| 2986 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsFunction(Local<Context> context, |
| 2987 | Local<Value> recv, |
| 2988 | int argc, |
| 2989 | Local<Value> argv[]); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2990 | |
| 2991 | /** |
| 2992 | * Call an Object as a constructor if a callback is set by the |
| 2993 | * ObjectTemplate::SetCallAsFunctionHandler method. |
| 2994 | * Note: This method behaves like the Function::NewInstance method. |
| 2995 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 2996 | V8_DEPRECATED("Use maybe version", |
| 2997 | Local<Value> CallAsConstructor(int argc, Local<Value> argv[])); |
| 2998 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsConstructor( |
| 2999 | Local<Context> context, int argc, Local<Value> argv[]); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3000 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3001 | /** |
| 3002 | * Return the isolate to which the Object belongs to. |
| 3003 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3004 | V8_DEPRECATE_SOON("Keep track of isolate correctly", Isolate* GetIsolate()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3005 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3006 | static Local<Object> New(Isolate* isolate); |
| 3007 | |
| 3008 | V8_INLINE static Object* Cast(Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3009 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3010 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3011 | Object(); |
| 3012 | static void CheckCast(Value* obj); |
| 3013 | Local<Value> SlowGetInternalField(int index); |
| 3014 | void* SlowGetAlignedPointerFromInternalField(int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3015 | }; |
| 3016 | |
| 3017 | |
| 3018 | /** |
| 3019 | * An instance of the built-in array constructor (ECMA-262, 15.4.2). |
| 3020 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3021 | class V8_EXPORT Array : public Object { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3022 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3023 | uint32_t Length() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3024 | |
| 3025 | /** |
| 3026 | * Clones an element at index |index|. Returns an empty |
| 3027 | * handle if cloning fails (for any reason). |
| 3028 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3029 | V8_DEPRECATED("Cloning is not supported.", |
| 3030 | Local<Object> CloneElementAt(uint32_t index)); |
| 3031 | V8_DEPRECATED("Cloning is not supported.", |
| 3032 | MaybeLocal<Object> CloneElementAt(Local<Context> context, |
| 3033 | uint32_t index)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3034 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3035 | /** |
| 3036 | * Creates a JavaScript array with the given length. If the length |
| 3037 | * is negative the returned array will have length 0. |
| 3038 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3039 | static Local<Array> New(Isolate* isolate, int length = 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3040 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3041 | V8_INLINE static Array* Cast(Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3042 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3043 | Array(); |
| 3044 | static void CheckCast(Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3045 | }; |
| 3046 | |
| 3047 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3048 | /** |
| 3049 | * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1). |
| 3050 | */ |
| 3051 | class V8_EXPORT Map : public Object { |
| 3052 | public: |
| 3053 | size_t Size() const; |
| 3054 | void Clear(); |
| 3055 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context, |
| 3056 | Local<Value> key); |
| 3057 | V8_WARN_UNUSED_RESULT MaybeLocal<Map> Set(Local<Context> context, |
| 3058 | Local<Value> key, |
| 3059 | Local<Value> value); |
| 3060 | V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, |
| 3061 | Local<Value> key); |
| 3062 | V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context, |
| 3063 | Local<Value> key); |
| 3064 | |
| 3065 | /** |
| 3066 | * Returns an array of length Size() * 2, where index N is the Nth key and |
| 3067 | * index N + 1 is the Nth value. |
| 3068 | */ |
| 3069 | Local<Array> AsArray() const; |
| 3070 | |
| 3071 | /** |
| 3072 | * Creates a new empty Map. |
| 3073 | */ |
| 3074 | static Local<Map> New(Isolate* isolate); |
| 3075 | |
| 3076 | V8_INLINE static Map* Cast(Value* obj); |
| 3077 | |
| 3078 | private: |
| 3079 | Map(); |
| 3080 | static void CheckCast(Value* obj); |
| 3081 | }; |
| 3082 | |
| 3083 | |
| 3084 | /** |
| 3085 | * An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1). |
| 3086 | */ |
| 3087 | class V8_EXPORT Set : public Object { |
| 3088 | public: |
| 3089 | size_t Size() const; |
| 3090 | void Clear(); |
| 3091 | V8_WARN_UNUSED_RESULT MaybeLocal<Set> Add(Local<Context> context, |
| 3092 | Local<Value> key); |
| 3093 | V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, |
| 3094 | Local<Value> key); |
| 3095 | V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context, |
| 3096 | Local<Value> key); |
| 3097 | |
| 3098 | /** |
| 3099 | * Returns an array of the keys in this Set. |
| 3100 | */ |
| 3101 | Local<Array> AsArray() const; |
| 3102 | |
| 3103 | /** |
| 3104 | * Creates a new empty Set. |
| 3105 | */ |
| 3106 | static Local<Set> New(Isolate* isolate); |
| 3107 | |
| 3108 | V8_INLINE static Set* Cast(Value* obj); |
| 3109 | |
| 3110 | private: |
| 3111 | Set(); |
| 3112 | static void CheckCast(Value* obj); |
| 3113 | }; |
| 3114 | |
| 3115 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3116 | template<typename T> |
| 3117 | class ReturnValue { |
| 3118 | public: |
| 3119 | template <class S> V8_INLINE ReturnValue(const ReturnValue<S>& that) |
| 3120 | : value_(that.value_) { |
| 3121 | TYPE_CHECK(T, S); |
| 3122 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3123 | // Local setters |
| 3124 | template <typename S> |
| 3125 | V8_INLINE V8_DEPRECATE_SOON("Use Global<> instead", |
| 3126 | void Set(const Persistent<S>& handle)); |
| 3127 | template <typename S> |
| 3128 | V8_INLINE void Set(const Global<S>& handle); |
| 3129 | template <typename S> |
| 3130 | V8_INLINE void Set(const Local<S> handle); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3131 | // Fast primitive setters |
| 3132 | V8_INLINE void Set(bool value); |
| 3133 | V8_INLINE void Set(double i); |
| 3134 | V8_INLINE void Set(int32_t i); |
| 3135 | V8_INLINE void Set(uint32_t i); |
| 3136 | // Fast JS primitive setters |
| 3137 | V8_INLINE void SetNull(); |
| 3138 | V8_INLINE void SetUndefined(); |
| 3139 | V8_INLINE void SetEmptyString(); |
| 3140 | // Convenience getter for Isolate |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3141 | V8_INLINE Isolate* GetIsolate() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3142 | |
| 3143 | // Pointer setter: Uncompilable to prevent inadvertent misuse. |
| 3144 | template <typename S> |
| 3145 | V8_INLINE void Set(S* whatever); |
| 3146 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 3147 | // Getter. Creates a new Local<> so it comes with a certain performance |
| 3148 | // hit. If the ReturnValue was not yet set, this will return the undefined |
| 3149 | // value. |
| 3150 | V8_INLINE Local<Value> Get() const; |
| 3151 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3152 | private: |
| 3153 | template<class F> friend class ReturnValue; |
| 3154 | template<class F> friend class FunctionCallbackInfo; |
| 3155 | template<class F> friend class PropertyCallbackInfo; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3156 | template <class F, class G, class H> |
| 3157 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3158 | V8_INLINE void SetInternal(internal::Object* value) { *value_ = value; } |
| 3159 | V8_INLINE internal::Object* GetDefaultValue(); |
| 3160 | V8_INLINE explicit ReturnValue(internal::Object** slot); |
| 3161 | internal::Object** value_; |
| 3162 | }; |
| 3163 | |
| 3164 | |
| 3165 | /** |
| 3166 | * The argument information given to function call callbacks. This |
| 3167 | * class provides access to information about the context of the call, |
| 3168 | * including the receiver, the number and values of arguments, and |
| 3169 | * the holder of the function. |
| 3170 | */ |
| 3171 | template<typename T> |
| 3172 | class FunctionCallbackInfo { |
| 3173 | public: |
| 3174 | V8_INLINE int Length() const; |
| 3175 | V8_INLINE Local<Value> operator[](int i) const; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3176 | V8_INLINE V8_DEPRECATED("Use Data() to explicitly pass Callee instead", |
| 3177 | Local<Function> Callee() const); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3178 | V8_INLINE Local<Object> This() const; |
| 3179 | V8_INLINE Local<Object> Holder() const; |
| 3180 | V8_INLINE bool IsConstructCall() const; |
| 3181 | V8_INLINE Local<Value> Data() const; |
| 3182 | V8_INLINE Isolate* GetIsolate() const; |
| 3183 | V8_INLINE ReturnValue<T> GetReturnValue() const; |
| 3184 | // This shouldn't be public, but the arm compiler needs it. |
| 3185 | static const int kArgsLength = 7; |
| 3186 | |
| 3187 | protected: |
| 3188 | friend class internal::FunctionCallbackArguments; |
| 3189 | friend class internal::CustomArguments<FunctionCallbackInfo>; |
| 3190 | static const int kHolderIndex = 0; |
| 3191 | static const int kIsolateIndex = 1; |
| 3192 | static const int kReturnValueDefaultValueIndex = 2; |
| 3193 | static const int kReturnValueIndex = 3; |
| 3194 | static const int kDataIndex = 4; |
| 3195 | static const int kCalleeIndex = 5; |
| 3196 | static const int kContextSaveIndex = 6; |
| 3197 | |
| 3198 | V8_INLINE FunctionCallbackInfo(internal::Object** implicit_args, |
| 3199 | internal::Object** values, |
| 3200 | int length, |
| 3201 | bool is_construct_call); |
| 3202 | internal::Object** implicit_args_; |
| 3203 | internal::Object** values_; |
| 3204 | int length_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3205 | int is_construct_call_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3206 | }; |
| 3207 | |
| 3208 | |
| 3209 | /** |
| 3210 | * The information passed to a property callback about the context |
| 3211 | * of the property access. |
| 3212 | */ |
| 3213 | template<typename T> |
| 3214 | class PropertyCallbackInfo { |
| 3215 | public: |
| 3216 | V8_INLINE Isolate* GetIsolate() const; |
| 3217 | V8_INLINE Local<Value> Data() const; |
| 3218 | V8_INLINE Local<Object> This() const; |
| 3219 | V8_INLINE Local<Object> Holder() const; |
| 3220 | V8_INLINE ReturnValue<T> GetReturnValue() const; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3221 | V8_INLINE bool ShouldThrowOnError() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3222 | // This shouldn't be public, but the arm compiler needs it. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3223 | static const int kArgsLength = 7; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3224 | |
| 3225 | protected: |
| 3226 | friend class MacroAssembler; |
| 3227 | friend class internal::PropertyCallbackArguments; |
| 3228 | friend class internal::CustomArguments<PropertyCallbackInfo>; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 3229 | static const int kShouldThrowOnErrorIndex = 0; |
| 3230 | static const int kHolderIndex = 1; |
| 3231 | static const int kIsolateIndex = 2; |
| 3232 | static const int kReturnValueDefaultValueIndex = 3; |
| 3233 | static const int kReturnValueIndex = 4; |
| 3234 | static const int kDataIndex = 5; |
| 3235 | static const int kThisIndex = 6; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3236 | |
| 3237 | V8_INLINE PropertyCallbackInfo(internal::Object** args) : args_(args) {} |
| 3238 | internal::Object** args_; |
| 3239 | }; |
| 3240 | |
| 3241 | |
| 3242 | typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info); |
| 3243 | |
| 3244 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3245 | /** |
| 3246 | * A JavaScript function object (ECMA-262, 15.3). |
| 3247 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3248 | class V8_EXPORT Function : public Object { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3249 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3250 | /** |
| 3251 | * Create a function in the current execution context |
| 3252 | * for a given FunctionCallback. |
| 3253 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3254 | static MaybeLocal<Function> New(Local<Context> context, |
| 3255 | FunctionCallback callback, |
| 3256 | Local<Value> data = Local<Value>(), |
| 3257 | int length = 0); |
| 3258 | static V8_DEPRECATE_SOON( |
| 3259 | "Use maybe version", |
| 3260 | Local<Function> New(Isolate* isolate, FunctionCallback callback, |
| 3261 | Local<Value> data = Local<Value>(), int length = 0)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3262 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3263 | V8_DEPRECATED("Use maybe version", |
| 3264 | Local<Object> NewInstance(int argc, Local<Value> argv[]) const); |
| 3265 | V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance( |
| 3266 | Local<Context> context, int argc, Local<Value> argv[]) const; |
| 3267 | |
| 3268 | V8_DEPRECATED("Use maybe version", Local<Object> NewInstance() const); |
| 3269 | V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance( |
| 3270 | Local<Context> context) const { |
| 3271 | return NewInstance(context, 0, nullptr); |
| 3272 | } |
| 3273 | |
| 3274 | V8_DEPRECATE_SOON("Use maybe version", |
| 3275 | Local<Value> Call(Local<Value> recv, int argc, |
| 3276 | Local<Value> argv[])); |
| 3277 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context, |
| 3278 | Local<Value> recv, int argc, |
| 3279 | Local<Value> argv[]); |
| 3280 | |
| 3281 | void SetName(Local<String> name); |
| 3282 | Local<Value> GetName() const; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3283 | |
| 3284 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3285 | * Name inferred from variable or property assignment of this function. |
| 3286 | * Used to facilitate debugging and profiling of JavaScript code written |
| 3287 | * in an OO style, where many functions are anonymous but are assigned |
| 3288 | * to object properties. |
| 3289 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3290 | Local<Value> GetInferredName() const; |
| 3291 | |
| 3292 | /** |
| 3293 | * displayName if it is set, otherwise name if it is configured, otherwise |
| 3294 | * function name, otherwise inferred name. |
| 3295 | */ |
| 3296 | Local<Value> GetDebugName() const; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3297 | |
| 3298 | /** |
| 3299 | * User-defined name assigned to the "displayName" property of this function. |
| 3300 | * Used to facilitate debugging and profiling of JavaScript code. |
| 3301 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3302 | Local<Value> GetDisplayName() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3303 | |
| 3304 | /** |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3305 | * Returns zero based line number of function body and |
| 3306 | * kLineOffsetNotFound if no information available. |
| 3307 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3308 | int GetScriptLineNumber() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3309 | /** |
| 3310 | * Returns zero based column number of function body and |
| 3311 | * kLineOffsetNotFound if no information available. |
| 3312 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3313 | int GetScriptColumnNumber() const; |
| 3314 | |
| 3315 | /** |
| 3316 | * Tells whether this function is builtin. |
| 3317 | */ |
| 3318 | bool IsBuiltin() const; |
| 3319 | |
| 3320 | /** |
| 3321 | * Returns scriptId. |
| 3322 | */ |
| 3323 | int ScriptId() const; |
| 3324 | |
| 3325 | /** |
| 3326 | * Returns the original function if this function is bound, else returns |
| 3327 | * v8::Undefined. |
| 3328 | */ |
| 3329 | Local<Value> GetBoundFunction() const; |
| 3330 | |
| 3331 | ScriptOrigin GetScriptOrigin() const; |
| 3332 | V8_INLINE static Function* Cast(Value* obj); |
| 3333 | static const int kLineOffsetNotFound; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3334 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3335 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3336 | Function(); |
| 3337 | static void CheckCast(Value* obj); |
| 3338 | }; |
| 3339 | |
| 3340 | |
| 3341 | /** |
| 3342 | * An instance of the built-in Promise constructor (ES6 draft). |
| 3343 | * This API is experimental. Only works with --harmony flag. |
| 3344 | */ |
| 3345 | class V8_EXPORT Promise : public Object { |
| 3346 | public: |
| 3347 | class V8_EXPORT Resolver : public Object { |
| 3348 | public: |
| 3349 | /** |
| 3350 | * Create a new resolver, along with an associated promise in pending state. |
| 3351 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3352 | static V8_DEPRECATE_SOON("Use maybe version", |
| 3353 | Local<Resolver> New(Isolate* isolate)); |
| 3354 | static V8_WARN_UNUSED_RESULT MaybeLocal<Resolver> New( |
| 3355 | Local<Context> context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3356 | |
| 3357 | /** |
| 3358 | * Extract the associated promise. |
| 3359 | */ |
| 3360 | Local<Promise> GetPromise(); |
| 3361 | |
| 3362 | /** |
| 3363 | * Resolve/reject the associated promise with a given value. |
| 3364 | * Ignored if the promise is no longer pending. |
| 3365 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3366 | V8_DEPRECATE_SOON("Use maybe version", void Resolve(Local<Value> value)); |
| 3367 | // TODO(dcarney): mark V8_WARN_UNUSED_RESULT |
| 3368 | Maybe<bool> Resolve(Local<Context> context, Local<Value> value); |
| 3369 | |
| 3370 | V8_DEPRECATE_SOON("Use maybe version", void Reject(Local<Value> value)); |
| 3371 | // TODO(dcarney): mark V8_WARN_UNUSED_RESULT |
| 3372 | Maybe<bool> Reject(Local<Context> context, Local<Value> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3373 | |
| 3374 | V8_INLINE static Resolver* Cast(Value* obj); |
| 3375 | |
| 3376 | private: |
| 3377 | Resolver(); |
| 3378 | static void CheckCast(Value* obj); |
| 3379 | }; |
| 3380 | |
| 3381 | /** |
| 3382 | * Register a resolution/rejection handler with a promise. |
| 3383 | * The handler is given the respective resolution/rejection value as |
| 3384 | * an argument. If the promise is already resolved/rejected, the handler is |
| 3385 | * invoked at the end of turn. |
| 3386 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3387 | V8_DEPRECATED("Use maybe version of Then", |
| 3388 | Local<Promise> Chain(Local<Function> handler)); |
| 3389 | V8_DEPRECATED("Use Then", |
| 3390 | V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Chain( |
| 3391 | Local<Context> context, Local<Function> handler)); |
| 3392 | |
| 3393 | V8_DEPRECATED("Use maybe version", |
| 3394 | Local<Promise> Catch(Local<Function> handler)); |
| 3395 | V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Catch(Local<Context> context, |
| 3396 | Local<Function> handler); |
| 3397 | |
| 3398 | V8_DEPRECATED("Use maybe version", |
| 3399 | Local<Promise> Then(Local<Function> handler)); |
| 3400 | V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Then(Local<Context> context, |
| 3401 | Local<Function> handler); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3402 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3403 | /** |
| 3404 | * Returns true if the promise has at least one derived promise, and |
| 3405 | * therefore resolve/reject handlers (including default handler). |
| 3406 | */ |
| 3407 | bool HasHandler(); |
| 3408 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3409 | V8_INLINE static Promise* Cast(Value* obj); |
| 3410 | |
| 3411 | private: |
| 3412 | Promise(); |
| 3413 | static void CheckCast(Value* obj); |
| 3414 | }; |
| 3415 | |
| 3416 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3417 | /** |
| 3418 | * An instance of the built-in Proxy constructor (ECMA-262, 6th Edition, |
| 3419 | * 26.2.1). |
| 3420 | */ |
| 3421 | class V8_EXPORT Proxy : public Object { |
| 3422 | public: |
| 3423 | Local<Object> GetTarget(); |
| 3424 | Local<Value> GetHandler(); |
| 3425 | bool IsRevoked(); |
| 3426 | void Revoke(); |
| 3427 | |
| 3428 | /** |
| 3429 | * Creates a new empty Map. |
| 3430 | */ |
| 3431 | static MaybeLocal<Proxy> New(Local<Context> context, |
| 3432 | Local<Object> local_target, |
| 3433 | Local<Object> local_handler); |
| 3434 | |
| 3435 | V8_INLINE static Proxy* Cast(Value* obj); |
| 3436 | |
| 3437 | private: |
| 3438 | Proxy(); |
| 3439 | static void CheckCast(Value* obj); |
| 3440 | }; |
| 3441 | |
| 3442 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3443 | #ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT |
| 3444 | // The number of required internal fields can be defined by embedder. |
| 3445 | #define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2 |
| 3446 | #endif |
| 3447 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3448 | |
| 3449 | enum class ArrayBufferCreationMode { kInternalized, kExternalized }; |
| 3450 | |
| 3451 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3452 | /** |
| 3453 | * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5). |
| 3454 | * This API is experimental and may change significantly. |
| 3455 | */ |
| 3456 | class V8_EXPORT ArrayBuffer : public Object { |
| 3457 | public: |
| 3458 | /** |
| 3459 | * Allocator that V8 uses to allocate |ArrayBuffer|'s memory. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3460 | * The allocator is a global V8 setting. It has to be set via |
| 3461 | * Isolate::CreateParams. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3462 | * |
| 3463 | * This API is experimental and may change significantly. |
| 3464 | */ |
| 3465 | class V8_EXPORT Allocator { // NOLINT |
| 3466 | public: |
| 3467 | virtual ~Allocator() {} |
| 3468 | |
| 3469 | /** |
| 3470 | * Allocate |length| bytes. Return NULL if allocation is not successful. |
| 3471 | * Memory should be initialized to zeroes. |
| 3472 | */ |
| 3473 | virtual void* Allocate(size_t length) = 0; |
| 3474 | |
| 3475 | /** |
| 3476 | * Allocate |length| bytes. Return NULL if allocation is not successful. |
| 3477 | * Memory does not have to be initialized. |
| 3478 | */ |
| 3479 | virtual void* AllocateUninitialized(size_t length) = 0; |
| 3480 | /** |
| 3481 | * Free the memory block of size |length|, pointed to by |data|. |
| 3482 | * That memory is guaranteed to be previously allocated by |Allocate|. |
| 3483 | */ |
| 3484 | virtual void Free(void* data, size_t length) = 0; |
| 3485 | }; |
| 3486 | |
| 3487 | /** |
| 3488 | * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer| |
| 3489 | * returns an instance of this class, populated, with a pointer to data |
| 3490 | * and byte length. |
| 3491 | * |
| 3492 | * The Data pointer of ArrayBuffer::Contents is always allocated with |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3493 | * Allocator::Allocate that is set via Isolate::CreateParams. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3494 | * |
| 3495 | * This API is experimental and may change significantly. |
| 3496 | */ |
| 3497 | class V8_EXPORT Contents { // NOLINT |
| 3498 | public: |
| 3499 | Contents() : data_(NULL), byte_length_(0) {} |
| 3500 | |
| 3501 | void* Data() const { return data_; } |
| 3502 | size_t ByteLength() const { return byte_length_; } |
| 3503 | |
| 3504 | private: |
| 3505 | void* data_; |
| 3506 | size_t byte_length_; |
| 3507 | |
| 3508 | friend class ArrayBuffer; |
| 3509 | }; |
| 3510 | |
| 3511 | |
| 3512 | /** |
| 3513 | * Data length in bytes. |
| 3514 | */ |
| 3515 | size_t ByteLength() const; |
| 3516 | |
| 3517 | /** |
| 3518 | * Create a new ArrayBuffer. Allocate |byte_length| bytes. |
| 3519 | * Allocated memory will be owned by a created ArrayBuffer and |
| 3520 | * will be deallocated when it is garbage-collected, |
| 3521 | * unless the object is externalized. |
| 3522 | */ |
| 3523 | static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length); |
| 3524 | |
| 3525 | /** |
| 3526 | * Create a new ArrayBuffer over an existing memory block. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3527 | * The created array buffer is by default immediately in externalized state. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3528 | * The memory block will not be reclaimed when a created ArrayBuffer |
| 3529 | * is garbage-collected. |
| 3530 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3531 | static Local<ArrayBuffer> New( |
| 3532 | Isolate* isolate, void* data, size_t byte_length, |
| 3533 | ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3534 | |
| 3535 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3536 | * Returns true if ArrayBuffer is externalized, that is, does not |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3537 | * own its memory block. |
| 3538 | */ |
| 3539 | bool IsExternal() const; |
| 3540 | |
| 3541 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3542 | * Returns true if this ArrayBuffer may be neutered. |
| 3543 | */ |
| 3544 | bool IsNeuterable() const; |
| 3545 | |
| 3546 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3547 | * Neuters this ArrayBuffer and all its views (typed arrays). |
| 3548 | * Neutering sets the byte length of the buffer and all typed arrays to zero, |
| 3549 | * preventing JavaScript from ever accessing underlying backing store. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 3550 | * ArrayBuffer should have been externalized and must be neuterable. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3551 | */ |
| 3552 | void Neuter(); |
| 3553 | |
| 3554 | /** |
| 3555 | * Make this ArrayBuffer external. The pointer to underlying memory block |
| 3556 | * and byte length are returned as |Contents| structure. After ArrayBuffer |
| 3557 | * had been etxrenalized, it does no longer owns the memory block. The caller |
| 3558 | * should take steps to free memory when it is no longer needed. |
| 3559 | * |
| 3560 | * The memory block is guaranteed to be allocated with |Allocator::Allocate| |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3561 | * that has been set via Isolate::CreateParams. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3562 | */ |
| 3563 | Contents Externalize(); |
| 3564 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3565 | /** |
| 3566 | * Get a pointer to the ArrayBuffer's underlying memory block without |
| 3567 | * externalizing it. If the ArrayBuffer is not externalized, this pointer |
| 3568 | * will become invalid as soon as the ArrayBuffer became garbage collected. |
| 3569 | * |
| 3570 | * The embedder should make sure to hold a strong reference to the |
| 3571 | * ArrayBuffer while accessing this pointer. |
| 3572 | * |
| 3573 | * The memory block is guaranteed to be allocated with |Allocator::Allocate|. |
| 3574 | */ |
| 3575 | Contents GetContents(); |
| 3576 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3577 | V8_INLINE static ArrayBuffer* Cast(Value* obj); |
| 3578 | |
| 3579 | static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT; |
| 3580 | |
| 3581 | private: |
| 3582 | ArrayBuffer(); |
| 3583 | static void CheckCast(Value* obj); |
| 3584 | }; |
| 3585 | |
| 3586 | |
| 3587 | #ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT |
| 3588 | // The number of required internal fields can be defined by embedder. |
| 3589 | #define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2 |
| 3590 | #endif |
| 3591 | |
| 3592 | |
| 3593 | /** |
| 3594 | * A base class for an instance of one of "views" over ArrayBuffer, |
| 3595 | * including TypedArrays and DataView (ES6 draft 15.13). |
| 3596 | * |
| 3597 | * This API is experimental and may change significantly. |
| 3598 | */ |
| 3599 | class V8_EXPORT ArrayBufferView : public Object { |
| 3600 | public: |
| 3601 | /** |
| 3602 | * Returns underlying ArrayBuffer. |
| 3603 | */ |
| 3604 | Local<ArrayBuffer> Buffer(); |
| 3605 | /** |
| 3606 | * Byte offset in |Buffer|. |
| 3607 | */ |
| 3608 | size_t ByteOffset(); |
| 3609 | /** |
| 3610 | * Size of a view in bytes. |
| 3611 | */ |
| 3612 | size_t ByteLength(); |
| 3613 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3614 | /** |
| 3615 | * Copy the contents of the ArrayBufferView's buffer to an embedder defined |
| 3616 | * memory without additional overhead that calling ArrayBufferView::Buffer |
| 3617 | * might incur. |
| 3618 | * |
| 3619 | * Will write at most min(|byte_length|, ByteLength) bytes starting at |
| 3620 | * ByteOffset of the underling buffer to the memory starting at |dest|. |
| 3621 | * Returns the number of bytes actually written. |
| 3622 | */ |
| 3623 | size_t CopyContents(void* dest, size_t byte_length); |
| 3624 | |
| 3625 | /** |
| 3626 | * Returns true if ArrayBufferView's backing ArrayBuffer has already been |
| 3627 | * allocated. |
| 3628 | */ |
| 3629 | bool HasBuffer() const; |
| 3630 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3631 | V8_INLINE static ArrayBufferView* Cast(Value* obj); |
| 3632 | |
| 3633 | static const int kInternalFieldCount = |
| 3634 | V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT; |
| 3635 | |
| 3636 | private: |
| 3637 | ArrayBufferView(); |
| 3638 | static void CheckCast(Value* obj); |
| 3639 | }; |
| 3640 | |
| 3641 | |
| 3642 | /** |
| 3643 | * A base class for an instance of TypedArray series of constructors |
| 3644 | * (ES6 draft 15.13.6). |
| 3645 | * This API is experimental and may change significantly. |
| 3646 | */ |
| 3647 | class V8_EXPORT TypedArray : public ArrayBufferView { |
| 3648 | public: |
| 3649 | /** |
| 3650 | * Number of elements in this typed array |
| 3651 | * (e.g. for Int16Array, |ByteLength|/2). |
| 3652 | */ |
| 3653 | size_t Length(); |
| 3654 | |
| 3655 | V8_INLINE static TypedArray* Cast(Value* obj); |
| 3656 | |
| 3657 | private: |
| 3658 | TypedArray(); |
| 3659 | static void CheckCast(Value* obj); |
| 3660 | }; |
| 3661 | |
| 3662 | |
| 3663 | /** |
| 3664 | * An instance of Uint8Array constructor (ES6 draft 15.13.6). |
| 3665 | * This API is experimental and may change significantly. |
| 3666 | */ |
| 3667 | class V8_EXPORT Uint8Array : public TypedArray { |
| 3668 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3669 | static Local<Uint8Array> New(Local<ArrayBuffer> array_buffer, |
| 3670 | size_t byte_offset, size_t length); |
| 3671 | static Local<Uint8Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3672 | size_t byte_offset, size_t length); |
| 3673 | V8_INLINE static Uint8Array* Cast(Value* obj); |
| 3674 | |
| 3675 | private: |
| 3676 | Uint8Array(); |
| 3677 | static void CheckCast(Value* obj); |
| 3678 | }; |
| 3679 | |
| 3680 | |
| 3681 | /** |
| 3682 | * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6). |
| 3683 | * This API is experimental and may change significantly. |
| 3684 | */ |
| 3685 | class V8_EXPORT Uint8ClampedArray : public TypedArray { |
| 3686 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3687 | static Local<Uint8ClampedArray> New(Local<ArrayBuffer> array_buffer, |
| 3688 | size_t byte_offset, size_t length); |
| 3689 | static Local<Uint8ClampedArray> New( |
| 3690 | Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset, |
| 3691 | size_t length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3692 | V8_INLINE static Uint8ClampedArray* Cast(Value* obj); |
| 3693 | |
| 3694 | private: |
| 3695 | Uint8ClampedArray(); |
| 3696 | static void CheckCast(Value* obj); |
| 3697 | }; |
| 3698 | |
| 3699 | /** |
| 3700 | * An instance of Int8Array constructor (ES6 draft 15.13.6). |
| 3701 | * This API is experimental and may change significantly. |
| 3702 | */ |
| 3703 | class V8_EXPORT Int8Array : public TypedArray { |
| 3704 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3705 | static Local<Int8Array> New(Local<ArrayBuffer> array_buffer, |
| 3706 | size_t byte_offset, size_t length); |
| 3707 | static Local<Int8Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
| 3708 | size_t byte_offset, size_t length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3709 | V8_INLINE static Int8Array* Cast(Value* obj); |
| 3710 | |
| 3711 | private: |
| 3712 | Int8Array(); |
| 3713 | static void CheckCast(Value* obj); |
| 3714 | }; |
| 3715 | |
| 3716 | |
| 3717 | /** |
| 3718 | * An instance of Uint16Array constructor (ES6 draft 15.13.6). |
| 3719 | * This API is experimental and may change significantly. |
| 3720 | */ |
| 3721 | class V8_EXPORT Uint16Array : public TypedArray { |
| 3722 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3723 | static Local<Uint16Array> New(Local<ArrayBuffer> array_buffer, |
| 3724 | size_t byte_offset, size_t length); |
| 3725 | static Local<Uint16Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
| 3726 | size_t byte_offset, size_t length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3727 | V8_INLINE static Uint16Array* Cast(Value* obj); |
| 3728 | |
| 3729 | private: |
| 3730 | Uint16Array(); |
| 3731 | static void CheckCast(Value* obj); |
| 3732 | }; |
| 3733 | |
| 3734 | |
| 3735 | /** |
| 3736 | * An instance of Int16Array constructor (ES6 draft 15.13.6). |
| 3737 | * This API is experimental and may change significantly. |
| 3738 | */ |
| 3739 | class V8_EXPORT Int16Array : public TypedArray { |
| 3740 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3741 | static Local<Int16Array> New(Local<ArrayBuffer> array_buffer, |
| 3742 | size_t byte_offset, size_t length); |
| 3743 | static Local<Int16Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3744 | size_t byte_offset, size_t length); |
| 3745 | V8_INLINE static Int16Array* Cast(Value* obj); |
| 3746 | |
| 3747 | private: |
| 3748 | Int16Array(); |
| 3749 | static void CheckCast(Value* obj); |
| 3750 | }; |
| 3751 | |
| 3752 | |
| 3753 | /** |
| 3754 | * An instance of Uint32Array constructor (ES6 draft 15.13.6). |
| 3755 | * This API is experimental and may change significantly. |
| 3756 | */ |
| 3757 | class V8_EXPORT Uint32Array : public TypedArray { |
| 3758 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3759 | static Local<Uint32Array> New(Local<ArrayBuffer> array_buffer, |
| 3760 | size_t byte_offset, size_t length); |
| 3761 | static Local<Uint32Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
| 3762 | size_t byte_offset, size_t length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3763 | V8_INLINE static Uint32Array* Cast(Value* obj); |
| 3764 | |
| 3765 | private: |
| 3766 | Uint32Array(); |
| 3767 | static void CheckCast(Value* obj); |
| 3768 | }; |
| 3769 | |
| 3770 | |
| 3771 | /** |
| 3772 | * An instance of Int32Array constructor (ES6 draft 15.13.6). |
| 3773 | * This API is experimental and may change significantly. |
| 3774 | */ |
| 3775 | class V8_EXPORT Int32Array : public TypedArray { |
| 3776 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3777 | static Local<Int32Array> New(Local<ArrayBuffer> array_buffer, |
| 3778 | size_t byte_offset, size_t length); |
| 3779 | static Local<Int32Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3780 | size_t byte_offset, size_t length); |
| 3781 | V8_INLINE static Int32Array* Cast(Value* obj); |
| 3782 | |
| 3783 | private: |
| 3784 | Int32Array(); |
| 3785 | static void CheckCast(Value* obj); |
| 3786 | }; |
| 3787 | |
| 3788 | |
| 3789 | /** |
| 3790 | * An instance of Float32Array constructor (ES6 draft 15.13.6). |
| 3791 | * This API is experimental and may change significantly. |
| 3792 | */ |
| 3793 | class V8_EXPORT Float32Array : public TypedArray { |
| 3794 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3795 | static Local<Float32Array> New(Local<ArrayBuffer> array_buffer, |
| 3796 | size_t byte_offset, size_t length); |
| 3797 | static Local<Float32Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
| 3798 | size_t byte_offset, size_t length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3799 | V8_INLINE static Float32Array* Cast(Value* obj); |
| 3800 | |
| 3801 | private: |
| 3802 | Float32Array(); |
| 3803 | static void CheckCast(Value* obj); |
| 3804 | }; |
| 3805 | |
| 3806 | |
| 3807 | /** |
| 3808 | * An instance of Float64Array constructor (ES6 draft 15.13.6). |
| 3809 | * This API is experimental and may change significantly. |
| 3810 | */ |
| 3811 | class V8_EXPORT Float64Array : public TypedArray { |
| 3812 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3813 | static Local<Float64Array> New(Local<ArrayBuffer> array_buffer, |
| 3814 | size_t byte_offset, size_t length); |
| 3815 | static Local<Float64Array> New(Local<SharedArrayBuffer> shared_array_buffer, |
| 3816 | size_t byte_offset, size_t length); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3817 | V8_INLINE static Float64Array* Cast(Value* obj); |
| 3818 | |
| 3819 | private: |
| 3820 | Float64Array(); |
| 3821 | static void CheckCast(Value* obj); |
| 3822 | }; |
| 3823 | |
| 3824 | |
| 3825 | /** |
| 3826 | * An instance of DataView constructor (ES6 draft 15.13.7). |
| 3827 | * This API is experimental and may change significantly. |
| 3828 | */ |
| 3829 | class V8_EXPORT DataView : public ArrayBufferView { |
| 3830 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3831 | static Local<DataView> New(Local<ArrayBuffer> array_buffer, |
| 3832 | size_t byte_offset, size_t length); |
| 3833 | static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3834 | size_t byte_offset, size_t length); |
| 3835 | V8_INLINE static DataView* Cast(Value* obj); |
| 3836 | |
| 3837 | private: |
| 3838 | DataView(); |
| 3839 | static void CheckCast(Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3840 | }; |
| 3841 | |
| 3842 | |
| 3843 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3844 | * An instance of the built-in SharedArrayBuffer constructor. |
| 3845 | * This API is experimental and may change significantly. |
| 3846 | */ |
| 3847 | class V8_EXPORT SharedArrayBuffer : public Object { |
| 3848 | public: |
| 3849 | /** |
| 3850 | * The contents of an |SharedArrayBuffer|. Externalization of |
| 3851 | * |SharedArrayBuffer| returns an instance of this class, populated, with a |
| 3852 | * pointer to data and byte length. |
| 3853 | * |
| 3854 | * The Data pointer of SharedArrayBuffer::Contents is always allocated with |
| 3855 | * |ArrayBuffer::Allocator::Allocate| by the allocator specified in |
| 3856 | * v8::Isolate::CreateParams::array_buffer_allocator. |
| 3857 | * |
| 3858 | * This API is experimental and may change significantly. |
| 3859 | */ |
| 3860 | class V8_EXPORT Contents { // NOLINT |
| 3861 | public: |
| 3862 | Contents() : data_(NULL), byte_length_(0) {} |
| 3863 | |
| 3864 | void* Data() const { return data_; } |
| 3865 | size_t ByteLength() const { return byte_length_; } |
| 3866 | |
| 3867 | private: |
| 3868 | void* data_; |
| 3869 | size_t byte_length_; |
| 3870 | |
| 3871 | friend class SharedArrayBuffer; |
| 3872 | }; |
| 3873 | |
| 3874 | |
| 3875 | /** |
| 3876 | * Data length in bytes. |
| 3877 | */ |
| 3878 | size_t ByteLength() const; |
| 3879 | |
| 3880 | /** |
| 3881 | * Create a new SharedArrayBuffer. Allocate |byte_length| bytes. |
| 3882 | * Allocated memory will be owned by a created SharedArrayBuffer and |
| 3883 | * will be deallocated when it is garbage-collected, |
| 3884 | * unless the object is externalized. |
| 3885 | */ |
| 3886 | static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length); |
| 3887 | |
| 3888 | /** |
| 3889 | * Create a new SharedArrayBuffer over an existing memory block. The created |
| 3890 | * array buffer is immediately in externalized state unless otherwise |
| 3891 | * specified. The memory block will not be reclaimed when a created |
| 3892 | * SharedArrayBuffer is garbage-collected. |
| 3893 | */ |
| 3894 | static Local<SharedArrayBuffer> New( |
| 3895 | Isolate* isolate, void* data, size_t byte_length, |
| 3896 | ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized); |
| 3897 | |
| 3898 | /** |
| 3899 | * Returns true if SharedArrayBuffer is externalized, that is, does not |
| 3900 | * own its memory block. |
| 3901 | */ |
| 3902 | bool IsExternal() const; |
| 3903 | |
| 3904 | /** |
| 3905 | * Make this SharedArrayBuffer external. The pointer to underlying memory |
| 3906 | * block and byte length are returned as |Contents| structure. After |
| 3907 | * SharedArrayBuffer had been etxrenalized, it does no longer owns the memory |
| 3908 | * block. The caller should take steps to free memory when it is no longer |
| 3909 | * needed. |
| 3910 | * |
| 3911 | * The memory block is guaranteed to be allocated with |Allocator::Allocate| |
| 3912 | * by the allocator specified in |
| 3913 | * v8::Isolate::CreateParams::array_buffer_allocator. |
| 3914 | * |
| 3915 | */ |
| 3916 | Contents Externalize(); |
| 3917 | |
| 3918 | /** |
| 3919 | * Get a pointer to the ArrayBuffer's underlying memory block without |
| 3920 | * externalizing it. If the ArrayBuffer is not externalized, this pointer |
| 3921 | * will become invalid as soon as the ArrayBuffer became garbage collected. |
| 3922 | * |
| 3923 | * The embedder should make sure to hold a strong reference to the |
| 3924 | * ArrayBuffer while accessing this pointer. |
| 3925 | * |
| 3926 | * The memory block is guaranteed to be allocated with |Allocator::Allocate| |
| 3927 | * by the allocator specified in |
| 3928 | * v8::Isolate::CreateParams::array_buffer_allocator. |
| 3929 | */ |
| 3930 | Contents GetContents(); |
| 3931 | |
| 3932 | V8_INLINE static SharedArrayBuffer* Cast(Value* obj); |
| 3933 | |
| 3934 | static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT; |
| 3935 | |
| 3936 | private: |
| 3937 | SharedArrayBuffer(); |
| 3938 | static void CheckCast(Value* obj); |
| 3939 | }; |
| 3940 | |
| 3941 | |
| 3942 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3943 | * An instance of the built-in Date constructor (ECMA-262, 15.9). |
| 3944 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3945 | class V8_EXPORT Date : public Object { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3946 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 3947 | static V8_DEPRECATE_SOON("Use maybe version.", |
| 3948 | Local<Value> New(Isolate* isolate, double time)); |
| 3949 | static V8_WARN_UNUSED_RESULT MaybeLocal<Value> New(Local<Context> context, |
| 3950 | double time); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3951 | |
| 3952 | /** |
| 3953 | * A specialization of Value::NumberValue that is more efficient |
| 3954 | * because we know the structure of this object. |
| 3955 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3956 | double ValueOf() const; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3957 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3958 | V8_INLINE static Date* Cast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3959 | |
| 3960 | /** |
| 3961 | * Notification that the embedder has changed the time zone, |
| 3962 | * daylight savings time, or other date / time configuration |
| 3963 | * parameters. V8 keeps a cache of various values used for |
| 3964 | * date / time computation. This notification will reset |
| 3965 | * those cached values for the current context so that date / |
| 3966 | * time configuration changes would be reflected in the Date |
| 3967 | * object. |
| 3968 | * |
| 3969 | * This API should not be called more than needed as it will |
| 3970 | * negatively impact the performance of date operations. |
| 3971 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3972 | static void DateTimeConfigurationChangeNotification(Isolate* isolate); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3973 | |
| 3974 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3975 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3976 | }; |
| 3977 | |
| 3978 | |
| 3979 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3980 | * A Number object (ECMA-262, 4.3.21). |
| 3981 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3982 | class V8_EXPORT NumberObject : public Object { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3983 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3984 | static Local<Value> New(Isolate* isolate, double value); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3985 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3986 | double ValueOf() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3987 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3988 | V8_INLINE static NumberObject* Cast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3989 | |
| 3990 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3991 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3992 | }; |
| 3993 | |
| 3994 | |
| 3995 | /** |
| 3996 | * A Boolean object (ECMA-262, 4.3.15). |
| 3997 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 3998 | class V8_EXPORT BooleanObject : public Object { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3999 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4000 | static Local<Value> New(Isolate* isolate, bool value); |
| 4001 | V8_DEPRECATED("Pass an isolate", static Local<Value> New(bool value)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4002 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4003 | bool ValueOf() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4004 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4005 | V8_INLINE static BooleanObject* Cast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4006 | |
| 4007 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4008 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4009 | }; |
| 4010 | |
| 4011 | |
| 4012 | /** |
| 4013 | * A String object (ECMA-262, 4.3.18). |
| 4014 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4015 | class V8_EXPORT StringObject : public Object { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4016 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4017 | static Local<Value> New(Local<String> value); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4018 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4019 | Local<String> ValueOf() const; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4020 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4021 | V8_INLINE static StringObject* Cast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4022 | |
| 4023 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4024 | static void CheckCast(v8::Value* obj); |
| 4025 | }; |
| 4026 | |
| 4027 | |
| 4028 | /** |
| 4029 | * A Symbol object (ECMA-262 edition 6). |
| 4030 | * |
| 4031 | * This is an experimental feature. Use at your own risk. |
| 4032 | */ |
| 4033 | class V8_EXPORT SymbolObject : public Object { |
| 4034 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4035 | static Local<Value> New(Isolate* isolate, Local<Symbol> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4036 | |
| 4037 | Local<Symbol> ValueOf() const; |
| 4038 | |
| 4039 | V8_INLINE static SymbolObject* Cast(v8::Value* obj); |
| 4040 | |
| 4041 | private: |
| 4042 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4043 | }; |
| 4044 | |
| 4045 | |
| 4046 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4047 | * An instance of the built-in RegExp constructor (ECMA-262, 15.10). |
| 4048 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4049 | class V8_EXPORT RegExp : public Object { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4050 | public: |
| 4051 | /** |
| 4052 | * Regular expression flag bits. They can be or'ed to enable a set |
| 4053 | * of flags. |
| 4054 | */ |
| 4055 | enum Flags { |
| 4056 | kNone = 0, |
| 4057 | kGlobal = 1, |
| 4058 | kIgnoreCase = 2, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4059 | kMultiline = 4, |
| 4060 | kSticky = 8, |
| 4061 | kUnicode = 16 |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4062 | }; |
| 4063 | |
| 4064 | /** |
| 4065 | * Creates a regular expression from the given pattern string and |
| 4066 | * the flags bit field. May throw a JavaScript exception as |
| 4067 | * described in ECMA-262, 15.10.4.1. |
| 4068 | * |
| 4069 | * For example, |
| 4070 | * RegExp::New(v8::String::New("foo"), |
| 4071 | * static_cast<RegExp::Flags>(kGlobal | kMultiline)) |
| 4072 | * is equivalent to evaluating "/foo/gm". |
| 4073 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4074 | static V8_DEPRECATE_SOON("Use maybe version", |
| 4075 | Local<RegExp> New(Local<String> pattern, |
| 4076 | Flags flags)); |
| 4077 | static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context, |
| 4078 | Local<String> pattern, |
| 4079 | Flags flags); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4080 | |
| 4081 | /** |
| 4082 | * Returns the value of the source property: a string representing |
| 4083 | * the regular expression. |
| 4084 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4085 | Local<String> GetSource() const; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4086 | |
| 4087 | /** |
| 4088 | * Returns the flags bit field. |
| 4089 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4090 | Flags GetFlags() const; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4091 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4092 | V8_INLINE static RegExp* Cast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4093 | |
| 4094 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4095 | static void CheckCast(v8::Value* obj); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4096 | }; |
| 4097 | |
| 4098 | |
| 4099 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4100 | * A JavaScript value that wraps a C++ void*. This type of value is mainly used |
| 4101 | * to associate C++ data structures with JavaScript objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4102 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4103 | class V8_EXPORT External : public Value { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4104 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4105 | static Local<External> New(Isolate* isolate, void* value); |
| 4106 | V8_INLINE static External* Cast(Value* obj); |
| 4107 | void* Value() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4108 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4109 | static void CheckCast(v8::Value* obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4110 | }; |
| 4111 | |
| 4112 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4113 | #define V8_INTRINSICS_LIST(F) F(ArrayProto_values, array_values_iterator) |
| 4114 | |
| 4115 | enum Intrinsic { |
| 4116 | #define V8_DECL_INTRINSIC(name, iname) k##name, |
| 4117 | V8_INTRINSICS_LIST(V8_DECL_INTRINSIC) |
| 4118 | #undef V8_DECL_INTRINSIC |
| 4119 | }; |
| 4120 | |
| 4121 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4122 | // --- Templates --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4123 | |
| 4124 | |
| 4125 | /** |
| 4126 | * The superclass of object and function templates. |
| 4127 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4128 | class V8_EXPORT Template : public Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4129 | public: |
| 4130 | /** Adds a property to each instance created by this template.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4131 | void Set(Local<Name> name, Local<Data> value, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4132 | PropertyAttribute attributes = None); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4133 | V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4134 | |
| 4135 | void SetAccessorProperty( |
| 4136 | Local<Name> name, |
| 4137 | Local<FunctionTemplate> getter = Local<FunctionTemplate>(), |
| 4138 | Local<FunctionTemplate> setter = Local<FunctionTemplate>(), |
| 4139 | PropertyAttribute attribute = None, |
| 4140 | AccessControl settings = DEFAULT); |
| 4141 | |
| 4142 | /** |
| 4143 | * Whenever the property with the given name is accessed on objects |
| 4144 | * created from this Template the getter and setter callbacks |
| 4145 | * are called instead of getting and setting the property directly |
| 4146 | * on the JavaScript object. |
| 4147 | * |
| 4148 | * \param name The name of the property for which an accessor is added. |
| 4149 | * \param getter The callback to invoke when getting the property. |
| 4150 | * \param setter The callback to invoke when setting the property. |
| 4151 | * \param data A piece of data that will be passed to the getter and setter |
| 4152 | * callbacks whenever they are invoked. |
| 4153 | * \param settings Access control settings for the accessor. This is a bit |
| 4154 | * field consisting of one of more of |
| 4155 | * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2. |
| 4156 | * The default is to not allow cross-context access. |
| 4157 | * ALL_CAN_READ means that all cross-context reads are allowed. |
| 4158 | * ALL_CAN_WRITE means that all cross-context writes are allowed. |
| 4159 | * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all |
| 4160 | * cross-context access. |
| 4161 | * \param attribute The attributes of the property for which an accessor |
| 4162 | * is added. |
| 4163 | * \param signature The signature describes valid receivers for the accessor |
| 4164 | * and is used to perform implicit instance checks against them. If the |
| 4165 | * receiver is incompatible (i.e. is not an instance of the constructor as |
| 4166 | * defined by FunctionTemplate::HasInstance()), an implicit TypeError is |
| 4167 | * thrown and no callback is invoked. |
| 4168 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4169 | void SetNativeDataProperty( |
| 4170 | Local<String> name, AccessorGetterCallback getter, |
| 4171 | AccessorSetterCallback setter = 0, |
| 4172 | // TODO(dcarney): gcc can't handle Local below |
| 4173 | Local<Value> data = Local<Value>(), PropertyAttribute attribute = None, |
| 4174 | Local<AccessorSignature> signature = Local<AccessorSignature>(), |
| 4175 | AccessControl settings = DEFAULT); |
| 4176 | void SetNativeDataProperty( |
| 4177 | Local<Name> name, AccessorNameGetterCallback getter, |
| 4178 | AccessorNameSetterCallback setter = 0, |
| 4179 | // TODO(dcarney): gcc can't handle Local below |
| 4180 | Local<Value> data = Local<Value>(), PropertyAttribute attribute = None, |
| 4181 | Local<AccessorSignature> signature = Local<AccessorSignature>(), |
| 4182 | AccessControl settings = DEFAULT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4183 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4184 | /** |
| 4185 | * During template instantiation, sets the value with the intrinsic property |
| 4186 | * from the correct context. |
| 4187 | */ |
| 4188 | void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic, |
| 4189 | PropertyAttribute attribute = None); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4190 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4191 | private: |
| 4192 | Template(); |
| 4193 | |
| 4194 | friend class ObjectTemplate; |
| 4195 | friend class FunctionTemplate; |
| 4196 | }; |
| 4197 | |
| 4198 | |
| 4199 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4200 | * NamedProperty[Getter|Setter] are used as interceptors on object. |
| 4201 | * See ObjectTemplate::SetNamedPropertyHandler. |
| 4202 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4203 | typedef void (*NamedPropertyGetterCallback)( |
| 4204 | Local<String> property, |
| 4205 | const PropertyCallbackInfo<Value>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4206 | |
| 4207 | |
| 4208 | /** |
| 4209 | * Returns the value if the setter intercepts the request. |
| 4210 | * Otherwise, returns an empty handle. |
| 4211 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4212 | typedef void (*NamedPropertySetterCallback)( |
| 4213 | Local<String> property, |
| 4214 | Local<Value> value, |
| 4215 | const PropertyCallbackInfo<Value>& info); |
| 4216 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4217 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4218 | /** |
| 4219 | * Returns a non-empty handle if the interceptor intercepts the request. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4220 | * The result is an integer encoding property attributes (like v8::None, |
| 4221 | * v8::DontEnum, etc.) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4222 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4223 | typedef void (*NamedPropertyQueryCallback)( |
| 4224 | Local<String> property, |
| 4225 | const PropertyCallbackInfo<Integer>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4226 | |
| 4227 | |
| 4228 | /** |
| 4229 | * Returns a non-empty handle if the deleter intercepts the request. |
| 4230 | * The return value is true if the property could be deleted and false |
| 4231 | * otherwise. |
| 4232 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4233 | typedef void (*NamedPropertyDeleterCallback)( |
| 4234 | Local<String> property, |
| 4235 | const PropertyCallbackInfo<Boolean>& info); |
| 4236 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4237 | |
| 4238 | /** |
| 4239 | * Returns an array containing the names of the properties the named |
| 4240 | * property getter intercepts. |
| 4241 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4242 | typedef void (*NamedPropertyEnumeratorCallback)( |
| 4243 | const PropertyCallbackInfo<Array>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4244 | |
| 4245 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4246 | // TODO(dcarney): Deprecate and remove previous typedefs, and replace |
| 4247 | // GenericNamedPropertyFooCallback with just NamedPropertyFooCallback. |
| 4248 | /** |
| 4249 | * GenericNamedProperty[Getter|Setter] are used as interceptors on object. |
| 4250 | * See ObjectTemplate::SetNamedPropertyHandler. |
| 4251 | */ |
| 4252 | typedef void (*GenericNamedPropertyGetterCallback)( |
| 4253 | Local<Name> property, const PropertyCallbackInfo<Value>& info); |
| 4254 | |
| 4255 | |
| 4256 | /** |
| 4257 | * Returns the value if the setter intercepts the request. |
| 4258 | * Otherwise, returns an empty handle. |
| 4259 | */ |
| 4260 | typedef void (*GenericNamedPropertySetterCallback)( |
| 4261 | Local<Name> property, Local<Value> value, |
| 4262 | const PropertyCallbackInfo<Value>& info); |
| 4263 | |
| 4264 | |
| 4265 | /** |
| 4266 | * Returns a non-empty handle if the interceptor intercepts the request. |
| 4267 | * The result is an integer encoding property attributes (like v8::None, |
| 4268 | * v8::DontEnum, etc.) |
| 4269 | */ |
| 4270 | typedef void (*GenericNamedPropertyQueryCallback)( |
| 4271 | Local<Name> property, const PropertyCallbackInfo<Integer>& info); |
| 4272 | |
| 4273 | |
| 4274 | /** |
| 4275 | * Returns a non-empty handle if the deleter intercepts the request. |
| 4276 | * The return value is true if the property could be deleted and false |
| 4277 | * otherwise. |
| 4278 | */ |
| 4279 | typedef void (*GenericNamedPropertyDeleterCallback)( |
| 4280 | Local<Name> property, const PropertyCallbackInfo<Boolean>& info); |
| 4281 | |
| 4282 | |
| 4283 | /** |
| 4284 | * Returns an array containing the names of the properties the named |
| 4285 | * property getter intercepts. |
| 4286 | */ |
| 4287 | typedef void (*GenericNamedPropertyEnumeratorCallback)( |
| 4288 | const PropertyCallbackInfo<Array>& info); |
| 4289 | |
| 4290 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4291 | /** |
| 4292 | * Returns the value of the property if the getter intercepts the |
| 4293 | * request. Otherwise, returns an empty handle. |
| 4294 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4295 | typedef void (*IndexedPropertyGetterCallback)( |
| 4296 | uint32_t index, |
| 4297 | const PropertyCallbackInfo<Value>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4298 | |
| 4299 | |
| 4300 | /** |
| 4301 | * Returns the value if the setter intercepts the request. |
| 4302 | * Otherwise, returns an empty handle. |
| 4303 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4304 | typedef void (*IndexedPropertySetterCallback)( |
| 4305 | uint32_t index, |
| 4306 | Local<Value> value, |
| 4307 | const PropertyCallbackInfo<Value>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4308 | |
| 4309 | |
| 4310 | /** |
| 4311 | * Returns a non-empty handle if the interceptor intercepts the request. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4312 | * The result is an integer encoding property attributes. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4313 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4314 | typedef void (*IndexedPropertyQueryCallback)( |
| 4315 | uint32_t index, |
| 4316 | const PropertyCallbackInfo<Integer>& info); |
| 4317 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4318 | |
| 4319 | /** |
| 4320 | * Returns a non-empty handle if the deleter intercepts the request. |
| 4321 | * The return value is true if the property could be deleted and false |
| 4322 | * otherwise. |
| 4323 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4324 | typedef void (*IndexedPropertyDeleterCallback)( |
| 4325 | uint32_t index, |
| 4326 | const PropertyCallbackInfo<Boolean>& info); |
| 4327 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4328 | |
| 4329 | /** |
| 4330 | * Returns an array containing the indices of the properties the |
| 4331 | * indexed property getter intercepts. |
| 4332 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4333 | typedef void (*IndexedPropertyEnumeratorCallback)( |
| 4334 | const PropertyCallbackInfo<Array>& info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4335 | |
| 4336 | |
| 4337 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4338 | * Access type specification. |
| 4339 | */ |
| 4340 | enum AccessType { |
| 4341 | ACCESS_GET, |
| 4342 | ACCESS_SET, |
| 4343 | ACCESS_HAS, |
| 4344 | ACCESS_DELETE, |
| 4345 | ACCESS_KEYS |
| 4346 | }; |
| 4347 | |
| 4348 | |
| 4349 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4350 | * Returns true if the given context should be allowed to access the given |
| 4351 | * object. |
| 4352 | */ |
| 4353 | typedef bool (*AccessCheckCallback)(Local<Context> accessing_context, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4354 | Local<Object> accessed_object, |
| 4355 | Local<Value> data); |
| 4356 | typedef bool (*DeprecatedAccessCheckCallback)(Local<Context> accessing_context, |
| 4357 | Local<Object> accessed_object); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4358 | |
| 4359 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4360 | * Returns true if cross-context access should be allowed to the named |
| 4361 | * property with the given key on the host object. |
| 4362 | */ |
| 4363 | typedef bool (*NamedSecurityCallback)(Local<Object> host, |
| 4364 | Local<Value> key, |
| 4365 | AccessType type, |
| 4366 | Local<Value> data); |
| 4367 | |
| 4368 | |
| 4369 | /** |
| 4370 | * Returns true if cross-context access should be allowed to the indexed |
| 4371 | * property with the given index on the host object. |
| 4372 | */ |
| 4373 | typedef bool (*IndexedSecurityCallback)(Local<Object> host, |
| 4374 | uint32_t index, |
| 4375 | AccessType type, |
| 4376 | Local<Value> data); |
| 4377 | |
| 4378 | |
| 4379 | /** |
| 4380 | * A FunctionTemplate is used to create functions at runtime. There |
| 4381 | * can only be one function created from a FunctionTemplate in a |
| 4382 | * context. The lifetime of the created function is equal to the |
| 4383 | * lifetime of the context. So in case the embedder needs to create |
| 4384 | * temporary functions that can be collected using Scripts is |
| 4385 | * preferred. |
| 4386 | * |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4387 | * Any modification of a FunctionTemplate after first instantiation will trigger |
| 4388 | *a crash. |
| 4389 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4390 | * A FunctionTemplate can have properties, these properties are added to the |
| 4391 | * function object when it is created. |
| 4392 | * |
| 4393 | * A FunctionTemplate has a corresponding instance template which is |
| 4394 | * used to create object instances when the function is used as a |
| 4395 | * constructor. Properties added to the instance template are added to |
| 4396 | * each object instance. |
| 4397 | * |
| 4398 | * A FunctionTemplate can have a prototype template. The prototype template |
| 4399 | * is used to create the prototype object of the function. |
| 4400 | * |
| 4401 | * The following example shows how to use a FunctionTemplate: |
| 4402 | * |
| 4403 | * \code |
| 4404 | * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 4405 | * t->Set("func_property", v8::Number::New(1)); |
| 4406 | * |
| 4407 | * v8::Local<v8::Template> proto_t = t->PrototypeTemplate(); |
| 4408 | * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback)); |
| 4409 | * proto_t->Set("proto_const", v8::Number::New(2)); |
| 4410 | * |
| 4411 | * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate(); |
| 4412 | * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback); |
| 4413 | * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...); |
| 4414 | * instance_t->Set("instance_property", Number::New(3)); |
| 4415 | * |
| 4416 | * v8::Local<v8::Function> function = t->GetFunction(); |
| 4417 | * v8::Local<v8::Object> instance = function->NewInstance(); |
| 4418 | * \endcode |
| 4419 | * |
| 4420 | * Let's use "function" as the JS variable name of the function object |
| 4421 | * and "instance" for the instance object created above. The function |
| 4422 | * and the instance will have the following properties: |
| 4423 | * |
| 4424 | * \code |
| 4425 | * func_property in function == true; |
| 4426 | * function.func_property == 1; |
| 4427 | * |
| 4428 | * function.prototype.proto_method() invokes 'InvokeCallback' |
| 4429 | * function.prototype.proto_const == 2; |
| 4430 | * |
| 4431 | * instance instanceof function == true; |
| 4432 | * instance.instance_accessor calls 'InstanceAccessorCallback' |
| 4433 | * instance.instance_property == 3; |
| 4434 | * \endcode |
| 4435 | * |
| 4436 | * A FunctionTemplate can inherit from another one by calling the |
| 4437 | * FunctionTemplate::Inherit method. The following graph illustrates |
| 4438 | * the semantics of inheritance: |
| 4439 | * |
| 4440 | * \code |
| 4441 | * FunctionTemplate Parent -> Parent() . prototype -> { } |
| 4442 | * ^ ^ |
| 4443 | * | Inherit(Parent) | .__proto__ |
| 4444 | * | | |
| 4445 | * FunctionTemplate Child -> Child() . prototype -> { } |
| 4446 | * \endcode |
| 4447 | * |
| 4448 | * A FunctionTemplate 'Child' inherits from 'Parent', the prototype |
| 4449 | * object of the Child() function has __proto__ pointing to the |
| 4450 | * Parent() function's prototype object. An instance of the Child |
| 4451 | * function has all properties on Parent's instance templates. |
| 4452 | * |
| 4453 | * Let Parent be the FunctionTemplate initialized in the previous |
| 4454 | * section and create a Child FunctionTemplate by: |
| 4455 | * |
| 4456 | * \code |
| 4457 | * Local<FunctionTemplate> parent = t; |
| 4458 | * Local<FunctionTemplate> child = FunctionTemplate::New(); |
| 4459 | * child->Inherit(parent); |
| 4460 | * |
| 4461 | * Local<Function> child_function = child->GetFunction(); |
| 4462 | * Local<Object> child_instance = child_function->NewInstance(); |
| 4463 | * \endcode |
| 4464 | * |
| 4465 | * The Child function and Child instance will have the following |
| 4466 | * properties: |
| 4467 | * |
| 4468 | * \code |
| 4469 | * child_func.prototype.__proto__ == function.prototype; |
| 4470 | * child_instance.instance_accessor calls 'InstanceAccessorCallback' |
| 4471 | * child_instance.instance_property == 3; |
| 4472 | * \endcode |
| 4473 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4474 | class V8_EXPORT FunctionTemplate : public Template { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4475 | public: |
| 4476 | /** Creates a function template.*/ |
| 4477 | static Local<FunctionTemplate> New( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4478 | Isolate* isolate, FunctionCallback callback = 0, |
| 4479 | Local<Value> data = Local<Value>(), |
| 4480 | Local<Signature> signature = Local<Signature>(), int length = 0); |
| 4481 | |
| 4482 | /** |
| 4483 | * Creates a function template with a fast handler. If a fast handler is set, |
| 4484 | * the callback cannot be null. |
| 4485 | */ |
| 4486 | static Local<FunctionTemplate> NewWithFastHandler( |
| 4487 | Isolate* isolate, FunctionCallback callback, |
| 4488 | experimental::FastAccessorBuilder* fast_handler = nullptr, |
| 4489 | Local<Value> data = Local<Value>(), |
| 4490 | Local<Signature> signature = Local<Signature>(), int length = 0); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4491 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4492 | /** Returns the unique function instance in the current execution context.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4493 | V8_DEPRECATE_SOON("Use maybe version", Local<Function> GetFunction()); |
| 4494 | V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction( |
| 4495 | Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4496 | |
| 4497 | /** |
| 4498 | * Set the call-handler callback for a FunctionTemplate. This |
| 4499 | * callback is called whenever the function created from this |
| 4500 | * FunctionTemplate is called. |
| 4501 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4502 | void SetCallHandler( |
| 4503 | FunctionCallback callback, Local<Value> data = Local<Value>(), |
| 4504 | experimental::FastAccessorBuilder* fast_handler = nullptr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4505 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4506 | /** Set the predefined length property for the FunctionTemplate. */ |
| 4507 | void SetLength(int length); |
| 4508 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4509 | /** Get the InstanceTemplate. */ |
| 4510 | Local<ObjectTemplate> InstanceTemplate(); |
| 4511 | |
| 4512 | /** Causes the function template to inherit from a parent function template.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4513 | void Inherit(Local<FunctionTemplate> parent); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4514 | |
| 4515 | /** |
| 4516 | * A PrototypeTemplate is the template used to create the prototype object |
| 4517 | * of the function created by this template. |
| 4518 | */ |
| 4519 | Local<ObjectTemplate> PrototypeTemplate(); |
| 4520 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4521 | /** |
| 4522 | * Set the class name of the FunctionTemplate. This is used for |
| 4523 | * printing objects created with the function created from the |
| 4524 | * FunctionTemplate as its constructor. |
| 4525 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4526 | void SetClassName(Local<String> name); |
| 4527 | |
| 4528 | |
| 4529 | /** |
| 4530 | * When set to true, no access check will be performed on the receiver of a |
| 4531 | * function call. Currently defaults to true, but this is subject to change. |
| 4532 | */ |
| 4533 | void SetAcceptAnyReceiver(bool value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4534 | |
| 4535 | /** |
| 4536 | * Determines whether the __proto__ accessor ignores instances of |
| 4537 | * the function template. If instances of the function template are |
| 4538 | * ignored, __proto__ skips all instances and instead returns the |
| 4539 | * next object in the prototype chain. |
| 4540 | * |
| 4541 | * Call with a value of true to make the __proto__ accessor ignore |
| 4542 | * instances of the function template. Call with a value of false |
| 4543 | * to make the __proto__ accessor not ignore instances of the |
| 4544 | * function template. By default, instances of a function template |
| 4545 | * are not ignored. |
| 4546 | */ |
| 4547 | void SetHiddenPrototype(bool value); |
| 4548 | |
| 4549 | /** |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4550 | * Sets the ReadOnly flag in the attributes of the 'prototype' property |
| 4551 | * of functions created from this FunctionTemplate to true. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4552 | */ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4553 | void ReadOnlyPrototype(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4554 | |
| 4555 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4556 | * Removes the prototype property from functions created from this |
| 4557 | * FunctionTemplate. |
| 4558 | */ |
| 4559 | void RemovePrototype(); |
| 4560 | |
| 4561 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4562 | * Returns true if the given object is an instance of this function |
| 4563 | * template. |
| 4564 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4565 | bool HasInstance(Local<Value> object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4566 | |
| 4567 | private: |
| 4568 | FunctionTemplate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4569 | friend class Context; |
| 4570 | friend class ObjectTemplate; |
| 4571 | }; |
| 4572 | |
| 4573 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4574 | enum class PropertyHandlerFlags { |
| 4575 | kNone = 0, |
| 4576 | // See ALL_CAN_READ above. |
| 4577 | kAllCanRead = 1, |
| 4578 | // Will not call into interceptor for properties on the receiver or prototype |
| 4579 | // chain. Currently only valid for named interceptors. |
| 4580 | kNonMasking = 1 << 1, |
| 4581 | // Will not call into interceptor for symbol lookup. Only meaningful for |
| 4582 | // named interceptors. |
| 4583 | kOnlyInterceptStrings = 1 << 2, |
| 4584 | }; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4585 | |
| 4586 | |
| 4587 | struct NamedPropertyHandlerConfiguration { |
| 4588 | NamedPropertyHandlerConfiguration( |
| 4589 | /** Note: getter is required **/ |
| 4590 | GenericNamedPropertyGetterCallback getter = 0, |
| 4591 | GenericNamedPropertySetterCallback setter = 0, |
| 4592 | GenericNamedPropertyQueryCallback query = 0, |
| 4593 | GenericNamedPropertyDeleterCallback deleter = 0, |
| 4594 | GenericNamedPropertyEnumeratorCallback enumerator = 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4595 | Local<Value> data = Local<Value>(), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4596 | PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) |
| 4597 | : getter(getter), |
| 4598 | setter(setter), |
| 4599 | query(query), |
| 4600 | deleter(deleter), |
| 4601 | enumerator(enumerator), |
| 4602 | data(data), |
| 4603 | flags(flags) {} |
| 4604 | |
| 4605 | GenericNamedPropertyGetterCallback getter; |
| 4606 | GenericNamedPropertySetterCallback setter; |
| 4607 | GenericNamedPropertyQueryCallback query; |
| 4608 | GenericNamedPropertyDeleterCallback deleter; |
| 4609 | GenericNamedPropertyEnumeratorCallback enumerator; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4610 | Local<Value> data; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4611 | PropertyHandlerFlags flags; |
| 4612 | }; |
| 4613 | |
| 4614 | |
| 4615 | struct IndexedPropertyHandlerConfiguration { |
| 4616 | IndexedPropertyHandlerConfiguration( |
| 4617 | /** Note: getter is required **/ |
| 4618 | IndexedPropertyGetterCallback getter = 0, |
| 4619 | IndexedPropertySetterCallback setter = 0, |
| 4620 | IndexedPropertyQueryCallback query = 0, |
| 4621 | IndexedPropertyDeleterCallback deleter = 0, |
| 4622 | IndexedPropertyEnumeratorCallback enumerator = 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4623 | Local<Value> data = Local<Value>(), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4624 | PropertyHandlerFlags flags = PropertyHandlerFlags::kNone) |
| 4625 | : getter(getter), |
| 4626 | setter(setter), |
| 4627 | query(query), |
| 4628 | deleter(deleter), |
| 4629 | enumerator(enumerator), |
| 4630 | data(data), |
| 4631 | flags(flags) {} |
| 4632 | |
| 4633 | IndexedPropertyGetterCallback getter; |
| 4634 | IndexedPropertySetterCallback setter; |
| 4635 | IndexedPropertyQueryCallback query; |
| 4636 | IndexedPropertyDeleterCallback deleter; |
| 4637 | IndexedPropertyEnumeratorCallback enumerator; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4638 | Local<Value> data; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4639 | PropertyHandlerFlags flags; |
| 4640 | }; |
| 4641 | |
| 4642 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4643 | /** |
| 4644 | * An ObjectTemplate is used to create objects at runtime. |
| 4645 | * |
| 4646 | * Properties added to an ObjectTemplate are added to each object |
| 4647 | * created from the ObjectTemplate. |
| 4648 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4649 | class V8_EXPORT ObjectTemplate : public Template { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4650 | public: |
| 4651 | /** Creates an ObjectTemplate. */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4652 | static Local<ObjectTemplate> New( |
| 4653 | Isolate* isolate, |
| 4654 | Local<FunctionTemplate> constructor = Local<FunctionTemplate>()); |
| 4655 | static V8_DEPRECATED("Use isolate version", Local<ObjectTemplate> New()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4656 | |
| 4657 | /** Creates a new instance of this template.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4658 | V8_DEPRECATE_SOON("Use maybe version", Local<Object> NewInstance()); |
| 4659 | V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4660 | |
| 4661 | /** |
| 4662 | * Sets an accessor on the object template. |
| 4663 | * |
| 4664 | * Whenever the property with the given name is accessed on objects |
| 4665 | * created from this ObjectTemplate the getter and setter callbacks |
| 4666 | * are called instead of getting and setting the property directly |
| 4667 | * on the JavaScript object. |
| 4668 | * |
| 4669 | * \param name The name of the property for which an accessor is added. |
| 4670 | * \param getter The callback to invoke when getting the property. |
| 4671 | * \param setter The callback to invoke when setting the property. |
| 4672 | * \param data A piece of data that will be passed to the getter and setter |
| 4673 | * callbacks whenever they are invoked. |
| 4674 | * \param settings Access control settings for the accessor. This is a bit |
| 4675 | * field consisting of one of more of |
| 4676 | * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2. |
| 4677 | * The default is to not allow cross-context access. |
| 4678 | * ALL_CAN_READ means that all cross-context reads are allowed. |
| 4679 | * ALL_CAN_WRITE means that all cross-context writes are allowed. |
| 4680 | * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all |
| 4681 | * cross-context access. |
| 4682 | * \param attribute The attributes of the property for which an accessor |
| 4683 | * is added. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4684 | * \param signature The signature describes valid receivers for the accessor |
| 4685 | * and is used to perform implicit instance checks against them. If the |
| 4686 | * receiver is incompatible (i.e. is not an instance of the constructor as |
| 4687 | * defined by FunctionTemplate::HasInstance()), an implicit TypeError is |
| 4688 | * thrown and no callback is invoked. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4689 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4690 | void SetAccessor( |
| 4691 | Local<String> name, AccessorGetterCallback getter, |
| 4692 | AccessorSetterCallback setter = 0, Local<Value> data = Local<Value>(), |
| 4693 | AccessControl settings = DEFAULT, PropertyAttribute attribute = None, |
| 4694 | Local<AccessorSignature> signature = Local<AccessorSignature>()); |
| 4695 | void SetAccessor( |
| 4696 | Local<Name> name, AccessorNameGetterCallback getter, |
| 4697 | AccessorNameSetterCallback setter = 0, Local<Value> data = Local<Value>(), |
| 4698 | AccessControl settings = DEFAULT, PropertyAttribute attribute = None, |
| 4699 | Local<AccessorSignature> signature = Local<AccessorSignature>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4700 | |
| 4701 | /** |
| 4702 | * Sets a named property handler on the object template. |
| 4703 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4704 | * Whenever a property whose name is a string is accessed on objects created |
| 4705 | * from this object template, the provided callback is invoked instead of |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4706 | * accessing the property directly on the JavaScript object. |
| 4707 | * |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4708 | * Note that new code should use the second version that can intercept |
| 4709 | * symbol-named properties as well as string-named properties. |
| 4710 | * |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4711 | * \param getter The callback to invoke when getting a property. |
| 4712 | * \param setter The callback to invoke when setting a property. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4713 | * \param query The callback to invoke to check if a property is present, |
| 4714 | * and if present, get its attributes. |
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 named |
| 4717 | * properties of an object. |
| 4718 | * \param data A piece of data that will be passed to the callbacks |
| 4719 | * whenever they are invoked. |
| 4720 | */ |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4721 | // TODO(dcarney): deprecate |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4722 | void SetNamedPropertyHandler(NamedPropertyGetterCallback getter, |
| 4723 | NamedPropertySetterCallback setter = 0, |
| 4724 | NamedPropertyQueryCallback query = 0, |
| 4725 | NamedPropertyDeleterCallback deleter = 0, |
| 4726 | NamedPropertyEnumeratorCallback enumerator = 0, |
| 4727 | Local<Value> data = Local<Value>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4728 | void SetHandler(const NamedPropertyHandlerConfiguration& configuration); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4729 | |
| 4730 | /** |
| 4731 | * Sets an indexed property handler on the object template. |
| 4732 | * |
| 4733 | * Whenever an indexed property is accessed on objects created from |
| 4734 | * this object template, the provided callback is invoked instead of |
| 4735 | * accessing the property directly on the JavaScript object. |
| 4736 | * |
| 4737 | * \param getter The callback to invoke when getting a property. |
| 4738 | * \param setter The callback to invoke when setting a property. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4739 | * \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] | 4740 | * \param deleter The callback to invoke when deleting a property. |
| 4741 | * \param enumerator The callback to invoke to enumerate all the indexed |
| 4742 | * properties of an object. |
| 4743 | * \param data A piece of data that will be passed to the callbacks |
| 4744 | * whenever they are invoked. |
| 4745 | */ |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4746 | void SetHandler(const IndexedPropertyHandlerConfiguration& configuration); |
| 4747 | // TODO(dcarney): deprecate |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4748 | void SetIndexedPropertyHandler( |
| 4749 | IndexedPropertyGetterCallback getter, |
| 4750 | IndexedPropertySetterCallback setter = 0, |
| 4751 | IndexedPropertyQueryCallback query = 0, |
| 4752 | IndexedPropertyDeleterCallback deleter = 0, |
| 4753 | IndexedPropertyEnumeratorCallback enumerator = 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4754 | Local<Value> data = Local<Value>()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4755 | SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query, |
| 4756 | deleter, enumerator, data)); |
| 4757 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4758 | /** |
| 4759 | * Sets the callback to be used when calling instances created from |
| 4760 | * this template as a function. If no callback is set, instances |
| 4761 | * behave like normal JavaScript objects that cannot be called as a |
| 4762 | * function. |
| 4763 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4764 | void SetCallAsFunctionHandler(FunctionCallback callback, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4765 | Local<Value> data = Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4766 | |
| 4767 | /** |
| 4768 | * Mark object instances of the template as undetectable. |
| 4769 | * |
| 4770 | * In many ways, undetectable objects behave as though they are not |
| 4771 | * there. They behave like 'undefined' in conditionals and when |
| 4772 | * printed. However, properties can be accessed and called as on |
| 4773 | * normal objects. |
| 4774 | */ |
| 4775 | void MarkAsUndetectable(); |
| 4776 | |
| 4777 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4778 | * Sets access check callback on the object template and enables access |
| 4779 | * checks. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4780 | * |
| 4781 | * When accessing properties on instances of this object template, |
| 4782 | * the access check callback will be called to determine whether or |
| 4783 | * not to allow cross-context access to the properties. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4784 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4785 | void SetAccessCheckCallback(AccessCheckCallback callback, |
| 4786 | Local<Value> data = Local<Value>()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 4787 | V8_DEPRECATED( |
| 4788 | "Use SetAccessCheckCallback with new AccessCheckCallback signature.", |
| 4789 | void SetAccessCheckCallback(DeprecatedAccessCheckCallback callback, |
| 4790 | Local<Value> data = Local<Value>())); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4791 | |
| 4792 | V8_DEPRECATED( |
| 4793 | "Use SetAccessCheckCallback instead", |
| 4794 | void SetAccessCheckCallbacks(NamedSecurityCallback named_handler, |
| 4795 | IndexedSecurityCallback indexed_handler, |
| 4796 | Local<Value> data = Local<Value>())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4797 | |
| 4798 | /** |
| 4799 | * Gets the number of internal fields for objects generated from |
| 4800 | * this template. |
| 4801 | */ |
| 4802 | int InternalFieldCount(); |
| 4803 | |
| 4804 | /** |
| 4805 | * Sets the number of internal fields for objects generated from |
| 4806 | * this template. |
| 4807 | */ |
| 4808 | void SetInternalFieldCount(int value); |
| 4809 | |
| 4810 | private: |
| 4811 | ObjectTemplate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4812 | static Local<ObjectTemplate> New(internal::Isolate* isolate, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4813 | Local<FunctionTemplate> constructor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4814 | friend class FunctionTemplate; |
| 4815 | }; |
| 4816 | |
| 4817 | |
| 4818 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4819 | * A Signature specifies which receiver is valid for a function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4820 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4821 | class V8_EXPORT Signature : public Data { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4822 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4823 | static Local<Signature> New( |
| 4824 | Isolate* isolate, |
| 4825 | Local<FunctionTemplate> receiver = Local<FunctionTemplate>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4826 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4827 | private: |
| 4828 | Signature(); |
| 4829 | }; |
| 4830 | |
| 4831 | |
| 4832 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4833 | * An AccessorSignature specifies which receivers are valid parameters |
| 4834 | * to an accessor callback. |
| 4835 | */ |
| 4836 | class V8_EXPORT AccessorSignature : public Data { |
| 4837 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4838 | static Local<AccessorSignature> New( |
| 4839 | Isolate* isolate, |
| 4840 | Local<FunctionTemplate> receiver = Local<FunctionTemplate>()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4841 | |
| 4842 | private: |
| 4843 | AccessorSignature(); |
| 4844 | }; |
| 4845 | |
| 4846 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4847 | // --- Extensions --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4848 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4849 | class V8_EXPORT ExternalOneByteStringResourceImpl |
| 4850 | : public String::ExternalOneByteStringResource { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4851 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4852 | ExternalOneByteStringResourceImpl() : data_(0), length_(0) {} |
| 4853 | ExternalOneByteStringResourceImpl(const char* data, size_t length) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4854 | : data_(data), length_(length) {} |
| 4855 | const char* data() const { return data_; } |
| 4856 | size_t length() const { return length_; } |
| 4857 | |
| 4858 | private: |
| 4859 | const char* data_; |
| 4860 | size_t length_; |
| 4861 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4862 | |
| 4863 | /** |
| 4864 | * Ignore |
| 4865 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4866 | class V8_EXPORT Extension { // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4867 | public: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4868 | // Note that the strings passed into this constructor must live as long |
| 4869 | // as the Extension itself. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4870 | Extension(const char* name, |
| 4871 | const char* source = 0, |
| 4872 | int dep_count = 0, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4873 | const char** deps = 0, |
| 4874 | int source_length = -1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4875 | virtual ~Extension() { } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4876 | virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( |
| 4877 | v8::Isolate* isolate, v8::Local<v8::String> name) { |
| 4878 | return v8::Local<v8::FunctionTemplate>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4881 | const char* name() const { return name_; } |
| 4882 | size_t source_length() const { return source_length_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4883 | const String::ExternalOneByteStringResource* source() const { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4884 | return &source_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4885 | int dependency_count() { return dep_count_; } |
| 4886 | const char** dependencies() { return deps_; } |
| 4887 | void set_auto_enable(bool value) { auto_enable_ = value; } |
| 4888 | bool auto_enable() { return auto_enable_; } |
| 4889 | |
| 4890 | private: |
| 4891 | const char* name_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4892 | size_t source_length_; // expected to initialize before source_ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4893 | ExternalOneByteStringResourceImpl source_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4894 | int dep_count_; |
| 4895 | const char** deps_; |
| 4896 | bool auto_enable_; |
| 4897 | |
| 4898 | // Disallow copying and assigning. |
| 4899 | Extension(const Extension&); |
| 4900 | void operator=(const Extension&); |
| 4901 | }; |
| 4902 | |
| 4903 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4904 | void V8_EXPORT RegisterExtension(Extension* extension); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4905 | |
| 4906 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4907 | // --- Statics --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4908 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4909 | V8_INLINE Local<Primitive> Undefined(Isolate* isolate); |
| 4910 | V8_INLINE Local<Primitive> Null(Isolate* isolate); |
| 4911 | V8_INLINE Local<Boolean> True(Isolate* isolate); |
| 4912 | V8_INLINE Local<Boolean> False(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4913 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4914 | /** |
| 4915 | * A set of constraints that specifies the limits of the runtime's memory use. |
| 4916 | * You must set the heap size before initializing the VM - the size cannot be |
| 4917 | * adjusted after the VM is initialized. |
| 4918 | * |
| 4919 | * If you are using threads then you should hold the V8::Locker lock while |
| 4920 | * setting the stack limit and you must set a non-default stack limit separately |
| 4921 | * for each thread. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4922 | * |
| 4923 | * The arguments for set_max_semi_space_size, set_max_old_space_size, |
| 4924 | * set_max_executable_size, set_code_range_size specify limits in MB. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4925 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4926 | class V8_EXPORT ResourceConstraints { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4927 | public: |
| 4928 | ResourceConstraints(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4929 | |
| 4930 | /** |
| 4931 | * Configures the constraints with reasonable default values based on the |
| 4932 | * capabilities of the current device the VM is running on. |
| 4933 | * |
| 4934 | * \param physical_memory The total amount of physical memory on the current |
| 4935 | * device, in bytes. |
| 4936 | * \param virtual_memory_limit The amount of virtual memory on the current |
| 4937 | * device, in bytes, or zero, if there is no limit. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4938 | */ |
| 4939 | void ConfigureDefaults(uint64_t physical_memory, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4940 | uint64_t virtual_memory_limit); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4941 | |
| 4942 | int max_semi_space_size() const { return max_semi_space_size_; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4943 | void set_max_semi_space_size(int limit_in_mb) { |
| 4944 | max_semi_space_size_ = limit_in_mb; |
| 4945 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4946 | int max_old_space_size() const { return max_old_space_size_; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4947 | void set_max_old_space_size(int limit_in_mb) { |
| 4948 | max_old_space_size_ = limit_in_mb; |
| 4949 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4950 | int max_executable_size() const { return max_executable_size_; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4951 | void set_max_executable_size(int limit_in_mb) { |
| 4952 | max_executable_size_ = limit_in_mb; |
| 4953 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4954 | uint32_t* stack_limit() const { return stack_limit_; } |
| 4955 | // Sets an address beyond which the VM's stack may not grow. |
| 4956 | void set_stack_limit(uint32_t* value) { stack_limit_ = value; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4957 | size_t code_range_size() const { return code_range_size_; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 4958 | void set_code_range_size(size_t limit_in_mb) { |
| 4959 | code_range_size_ = limit_in_mb; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4962 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4963 | int max_semi_space_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4964 | int max_old_space_size_; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 4965 | int max_executable_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4966 | uint32_t* stack_limit_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4967 | size_t code_range_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4968 | }; |
| 4969 | |
| 4970 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4971 | // --- Exceptions --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4972 | |
| 4973 | |
| 4974 | typedef void (*FatalErrorCallback)(const char* location, const char* message); |
| 4975 | |
| 4976 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4977 | typedef void (*MessageCallback)(Local<Message> message, Local<Value> error); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4978 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4979 | // --- Tracing --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4980 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4981 | typedef void (*LogEventCallback)(const char* name, int event); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4982 | |
| 4983 | /** |
| 4984 | * Create new error objects by calling the corresponding error object |
| 4985 | * constructor with the message. |
| 4986 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 4987 | class V8_EXPORT Exception { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4988 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4989 | static Local<Value> RangeError(Local<String> message); |
| 4990 | static Local<Value> ReferenceError(Local<String> message); |
| 4991 | static Local<Value> SyntaxError(Local<String> message); |
| 4992 | static Local<Value> TypeError(Local<String> message); |
| 4993 | static Local<Value> Error(Local<String> message); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 4994 | |
| 4995 | /** |
| 4996 | * Creates an error message for the given exception. |
| 4997 | * Will try to reconstruct the original stack trace from the exception value, |
| 4998 | * or capture the current stack trace if not available. |
| 4999 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5000 | static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception); |
| 5001 | V8_DEPRECATED("Use version with an Isolate*", |
| 5002 | static Local<Message> CreateMessage(Local<Value> exception)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5003 | |
| 5004 | /** |
| 5005 | * Returns the original stack trace that was captured at the creation time |
| 5006 | * of a given exception, or an empty handle if not available. |
| 5007 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5008 | static Local<StackTrace> GetStackTrace(Local<Value> exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5009 | }; |
| 5010 | |
| 5011 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5012 | // --- Counters Callbacks --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5013 | |
| 5014 | typedef int* (*CounterLookupCallback)(const char* name); |
| 5015 | |
| 5016 | typedef void* (*CreateHistogramCallback)(const char* name, |
| 5017 | int min, |
| 5018 | int max, |
| 5019 | size_t buckets); |
| 5020 | |
| 5021 | typedef void (*AddHistogramSampleCallback)(void* histogram, int sample); |
| 5022 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5023 | // --- Memory Allocation Callback --- |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5024 | enum ObjectSpace { |
| 5025 | kObjectSpaceNewSpace = 1 << 0, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5026 | kObjectSpaceOldSpace = 1 << 1, |
| 5027 | kObjectSpaceCodeSpace = 1 << 2, |
| 5028 | kObjectSpaceMapSpace = 1 << 3, |
| 5029 | kObjectSpaceLoSpace = 1 << 4, |
| 5030 | kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldSpace | |
| 5031 | kObjectSpaceCodeSpace | kObjectSpaceMapSpace | |
| 5032 | kObjectSpaceLoSpace |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5033 | }; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5034 | |
| 5035 | enum AllocationAction { |
| 5036 | kAllocationActionAllocate = 1 << 0, |
| 5037 | kAllocationActionFree = 1 << 1, |
| 5038 | kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree |
| 5039 | }; |
| 5040 | |
| 5041 | typedef void (*MemoryAllocationCallback)(ObjectSpace space, |
| 5042 | AllocationAction action, |
| 5043 | int size); |
| 5044 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5045 | // --- Enter/Leave Script Callback --- |
| 5046 | typedef void (*BeforeCallEnteredCallback)(Isolate*); |
| 5047 | typedef void (*CallCompletedCallback)(Isolate*); |
| 5048 | typedef void (*DeprecatedCallCompletedCallback)(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5049 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5050 | // --- Promise Reject Callback --- |
| 5051 | enum PromiseRejectEvent { |
| 5052 | kPromiseRejectWithNoHandler = 0, |
| 5053 | kPromiseHandlerAddedAfterReject = 1 |
| 5054 | }; |
| 5055 | |
| 5056 | class PromiseRejectMessage { |
| 5057 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5058 | PromiseRejectMessage(Local<Promise> promise, PromiseRejectEvent event, |
| 5059 | Local<Value> value, Local<StackTrace> stack_trace) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5060 | : promise_(promise), |
| 5061 | event_(event), |
| 5062 | value_(value), |
| 5063 | stack_trace_(stack_trace) {} |
| 5064 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5065 | V8_INLINE Local<Promise> GetPromise() const { return promise_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5066 | V8_INLINE PromiseRejectEvent GetEvent() const { return event_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5067 | V8_INLINE Local<Value> GetValue() const { return value_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5068 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5069 | V8_DEPRECATED("Use v8::Exception::CreateMessage(GetValue())->GetStackTrace()", |
| 5070 | V8_INLINE Local<StackTrace> GetStackTrace() const) { |
| 5071 | return stack_trace_; |
| 5072 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5073 | |
| 5074 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5075 | Local<Promise> promise_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5076 | PromiseRejectEvent event_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5077 | Local<Value> value_; |
| 5078 | Local<StackTrace> stack_trace_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5079 | }; |
| 5080 | |
| 5081 | typedef void (*PromiseRejectCallback)(PromiseRejectMessage message); |
| 5082 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5083 | // --- Microtasks Callbacks --- |
| 5084 | typedef void (*MicrotasksCompletedCallback)(Isolate*); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5085 | typedef void (*MicrotaskCallback)(void* data); |
| 5086 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5087 | |
| 5088 | /** |
| 5089 | * Policy for running microtasks: |
| 5090 | * - explicit: microtasks are invoked with Isolate::RunMicrotasks() method; |
| 5091 | * - scoped: microtasks invocation is controlled by MicrotasksScope objects; |
| 5092 | * - auto: microtasks are invoked when the script call depth decrements |
| 5093 | * to zero. |
| 5094 | */ |
| 5095 | enum class MicrotasksPolicy { kExplicit, kScoped, kAuto }; |
| 5096 | |
| 5097 | |
| 5098 | /** |
| 5099 | * This scope is used to control microtasks when kScopeMicrotasksInvocation |
| 5100 | * is used on Isolate. In this mode every non-primitive call to V8 should be |
| 5101 | * done inside some MicrotasksScope. |
| 5102 | * Microtasks are executed when topmost MicrotasksScope marked as kRunMicrotasks |
| 5103 | * exits. |
| 5104 | * kDoNotRunMicrotasks should be used to annotate calls not intended to trigger |
| 5105 | * microtasks. |
| 5106 | */ |
| 5107 | class V8_EXPORT MicrotasksScope { |
| 5108 | public: |
| 5109 | enum Type { kRunMicrotasks, kDoNotRunMicrotasks }; |
| 5110 | |
| 5111 | MicrotasksScope(Isolate* isolate, Type type); |
| 5112 | ~MicrotasksScope(); |
| 5113 | |
| 5114 | /** |
| 5115 | * Runs microtasks if no kRunMicrotasks scope is currently active. |
| 5116 | */ |
| 5117 | static void PerformCheckpoint(Isolate* isolate); |
| 5118 | |
| 5119 | /** |
| 5120 | * Returns current depth of nested kRunMicrotasks scopes. |
| 5121 | */ |
| 5122 | static int GetCurrentDepth(Isolate* isolate); |
| 5123 | |
| 5124 | private: |
| 5125 | internal::Isolate* const isolate_; |
| 5126 | bool run_; |
| 5127 | |
| 5128 | // Prevent copying. |
| 5129 | MicrotasksScope(const MicrotasksScope&); |
| 5130 | MicrotasksScope& operator=(const MicrotasksScope&); |
| 5131 | }; |
| 5132 | |
| 5133 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5134 | // --- Failed Access Check Callback --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5135 | typedef void (*FailedAccessCheckCallback)(Local<Object> target, |
| 5136 | AccessType type, |
| 5137 | Local<Value> data); |
| 5138 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5139 | // --- AllowCodeGenerationFromStrings callbacks --- |
| 5140 | |
| 5141 | /** |
| 5142 | * Callback to check if code generation from strings is allowed. See |
| 5143 | * Context::AllowCodeGenerationFromStrings. |
| 5144 | */ |
| 5145 | typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context); |
| 5146 | |
| 5147 | // --- Garbage Collection Callbacks --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5148 | |
| 5149 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5150 | * Applications can register callback functions which will be called before and |
| 5151 | * after certain garbage collection operations. Allocations are not allowed in |
| 5152 | * the callback functions, you therefore cannot manipulate objects (set or |
| 5153 | * delete properties for example) since it is possible such operations will |
| 5154 | * result in the allocation of objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5155 | */ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5156 | enum GCType { |
| 5157 | kGCTypeScavenge = 1 << 0, |
| 5158 | kGCTypeMarkSweepCompact = 1 << 1, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5159 | kGCTypeIncrementalMarking = 1 << 2, |
| 5160 | kGCTypeProcessWeakCallbacks = 1 << 3, |
| 5161 | kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact | |
| 5162 | kGCTypeIncrementalMarking | kGCTypeProcessWeakCallbacks |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5163 | }; |
| 5164 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5165 | /** |
| 5166 | * GCCallbackFlags is used to notify additional information about the GC |
| 5167 | * callback. |
| 5168 | * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for |
| 5169 | * constructing retained object infos. |
| 5170 | * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing. |
| 5171 | * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback |
| 5172 | * is called synchronously without getting posted to an idle task. |
| 5173 | * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called |
| 5174 | * in a phase where V8 is trying to collect all available garbage |
| 5175 | * (e.g., handling a low memory notification). |
| 5176 | */ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5177 | enum GCCallbackFlags { |
| 5178 | kNoGCCallbackFlags = 0, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5179 | kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5180 | kGCCallbackFlagForced = 1 << 2, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5181 | kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3, |
| 5182 | kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5183 | }; |
| 5184 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5185 | typedef void (*GCCallback)(GCType type, GCCallbackFlags flags); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5186 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5187 | typedef void (*InterruptCallback)(Isolate* isolate, void* data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5188 | |
| 5189 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5190 | /** |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5191 | * Collection of V8 heap information. |
| 5192 | * |
| 5193 | * Instances of this class can be passed to v8::V8::HeapStatistics to |
| 5194 | * get heap statistics from V8. |
| 5195 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5196 | class V8_EXPORT HeapStatistics { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5197 | public: |
| 5198 | HeapStatistics(); |
| 5199 | size_t total_heap_size() { return total_heap_size_; } |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 5200 | size_t total_heap_size_executable() { return total_heap_size_executable_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5201 | size_t total_physical_size() { return total_physical_size_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5202 | size_t total_available_size() { return total_available_size_; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5203 | size_t used_heap_size() { return used_heap_size_; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5204 | size_t heap_size_limit() { return heap_size_limit_; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5205 | size_t malloced_memory() { return malloced_memory_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5206 | size_t does_zap_garbage() { return does_zap_garbage_; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5207 | |
| 5208 | private: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5209 | size_t total_heap_size_; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 5210 | size_t total_heap_size_executable_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5211 | size_t total_physical_size_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5212 | size_t total_available_size_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5213 | size_t used_heap_size_; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5214 | size_t heap_size_limit_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5215 | size_t malloced_memory_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5216 | bool does_zap_garbage_; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5217 | |
| 5218 | friend class V8; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5219 | friend class Isolate; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5220 | }; |
| 5221 | |
| 5222 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5223 | class V8_EXPORT HeapSpaceStatistics { |
| 5224 | public: |
| 5225 | HeapSpaceStatistics(); |
| 5226 | const char* space_name() { return space_name_; } |
| 5227 | size_t space_size() { return space_size_; } |
| 5228 | size_t space_used_size() { return space_used_size_; } |
| 5229 | size_t space_available_size() { return space_available_size_; } |
| 5230 | size_t physical_space_size() { return physical_space_size_; } |
| 5231 | |
| 5232 | private: |
| 5233 | const char* space_name_; |
| 5234 | size_t space_size_; |
| 5235 | size_t space_used_size_; |
| 5236 | size_t space_available_size_; |
| 5237 | size_t physical_space_size_; |
| 5238 | |
| 5239 | friend class Isolate; |
| 5240 | }; |
| 5241 | |
| 5242 | |
| 5243 | class V8_EXPORT HeapObjectStatistics { |
| 5244 | public: |
| 5245 | HeapObjectStatistics(); |
| 5246 | const char* object_type() { return object_type_; } |
| 5247 | const char* object_sub_type() { return object_sub_type_; } |
| 5248 | size_t object_count() { return object_count_; } |
| 5249 | size_t object_size() { return object_size_; } |
| 5250 | |
| 5251 | private: |
| 5252 | const char* object_type_; |
| 5253 | const char* object_sub_type_; |
| 5254 | size_t object_count_; |
| 5255 | size_t object_size_; |
| 5256 | |
| 5257 | friend class Isolate; |
| 5258 | }; |
| 5259 | |
| 5260 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5261 | class RetainedObjectInfo; |
| 5262 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5263 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5264 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5265 | * FunctionEntryHook is the type of the profile entry hook called at entry to |
| 5266 | * any generated function when function-level profiling is enabled. |
| 5267 | * |
| 5268 | * \param function the address of the function that's being entered. |
| 5269 | * \param return_addr_location points to a location on stack where the machine |
| 5270 | * return address resides. This can be used to identify the caller of |
| 5271 | * \p function, and/or modified to divert execution when \p function exits. |
| 5272 | * |
| 5273 | * \note the entry hook must not cause garbage collection. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5274 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5275 | typedef void (*FunctionEntryHook)(uintptr_t function, |
| 5276 | uintptr_t return_addr_location); |
| 5277 | |
| 5278 | /** |
| 5279 | * A JIT code event is issued each time code is added, moved or removed. |
| 5280 | * |
| 5281 | * \note removal events are not currently issued. |
| 5282 | */ |
| 5283 | struct JitCodeEvent { |
| 5284 | enum EventType { |
| 5285 | CODE_ADDED, |
| 5286 | CODE_MOVED, |
| 5287 | CODE_REMOVED, |
| 5288 | CODE_ADD_LINE_POS_INFO, |
| 5289 | CODE_START_LINE_INFO_RECORDING, |
| 5290 | CODE_END_LINE_INFO_RECORDING |
| 5291 | }; |
| 5292 | // Definition of the code position type. The "POSITION" type means the place |
| 5293 | // in the source code which are of interest when making stack traces to |
| 5294 | // pin-point the source location of a stack frame as close as possible. |
| 5295 | // The "STATEMENT_POSITION" means the place at the beginning of each |
| 5296 | // statement, and is used to indicate possible break locations. |
| 5297 | enum PositionType { POSITION, STATEMENT_POSITION }; |
| 5298 | |
| 5299 | // Type of event. |
| 5300 | EventType type; |
| 5301 | // Start of the instructions. |
| 5302 | void* code_start; |
| 5303 | // Size of the instructions. |
| 5304 | size_t code_len; |
| 5305 | // Script info for CODE_ADDED event. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5306 | Local<UnboundScript> script; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5307 | // User-defined data for *_LINE_INFO_* event. It's used to hold the source |
| 5308 | // code line information which is returned from the |
| 5309 | // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent |
| 5310 | // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events. |
| 5311 | void* user_data; |
| 5312 | |
| 5313 | struct name_t { |
| 5314 | // Name of the object associated with the code, note that the string is not |
| 5315 | // zero-terminated. |
| 5316 | const char* str; |
| 5317 | // Number of chars in str. |
| 5318 | size_t len; |
| 5319 | }; |
| 5320 | |
| 5321 | struct line_info_t { |
| 5322 | // PC offset |
| 5323 | size_t offset; |
| 5324 | // Code postion |
| 5325 | size_t pos; |
| 5326 | // The position type. |
| 5327 | PositionType position_type; |
| 5328 | }; |
| 5329 | |
| 5330 | union { |
| 5331 | // Only valid for CODE_ADDED. |
| 5332 | struct name_t name; |
| 5333 | |
| 5334 | // Only valid for CODE_ADD_LINE_POS_INFO |
| 5335 | struct line_info_t line_info; |
| 5336 | |
| 5337 | // New location of instructions. Only valid for CODE_MOVED. |
| 5338 | void* new_code_start; |
| 5339 | }; |
| 5340 | }; |
| 5341 | |
| 5342 | /** |
| 5343 | * Option flags passed to the SetJitCodeEventHandler function. |
| 5344 | */ |
| 5345 | enum JitCodeEventOptions { |
| 5346 | kJitCodeEventDefault = 0, |
| 5347 | // Generate callbacks for already existent code. |
| 5348 | kJitCodeEventEnumExisting = 1 |
| 5349 | }; |
| 5350 | |
| 5351 | |
| 5352 | /** |
| 5353 | * Callback function passed to SetJitCodeEventHandler. |
| 5354 | * |
| 5355 | * \param event code add, move or removal event. |
| 5356 | */ |
| 5357 | typedef void (*JitCodeEventHandler)(const JitCodeEvent* event); |
| 5358 | |
| 5359 | |
| 5360 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5361 | * Interface for iterating through all external resources in the heap. |
| 5362 | */ |
| 5363 | class V8_EXPORT ExternalResourceVisitor { // NOLINT |
| 5364 | public: |
| 5365 | virtual ~ExternalResourceVisitor() {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5366 | virtual void VisitExternalString(Local<String> string) {} |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5367 | }; |
| 5368 | |
| 5369 | |
| 5370 | /** |
| 5371 | * Interface for iterating through all the persistent handles in the heap. |
| 5372 | */ |
| 5373 | class V8_EXPORT PersistentHandleVisitor { // NOLINT |
| 5374 | public: |
| 5375 | virtual ~PersistentHandleVisitor() {} |
| 5376 | virtual void VisitPersistentHandle(Persistent<Value>* value, |
| 5377 | uint16_t class_id) {} |
| 5378 | }; |
| 5379 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5380 | /** |
| 5381 | * Memory pressure level for the MemoryPressureNotification. |
| 5382 | * kNone hints V8 that there is no memory pressure. |
| 5383 | * kModerate hints V8 to speed up incremental garbage collection at the cost of |
| 5384 | * of higher latency due to garbage collection pauses. |
| 5385 | * kCritical hints V8 to free memory as soon as possible. Garbage collection |
| 5386 | * pauses at this level will be large. |
| 5387 | */ |
| 5388 | enum class MemoryPressureLevel { kNone, kModerate, kCritical }; |
| 5389 | |
| 5390 | /** |
| 5391 | * Interface for tracing through the embedder heap. During the v8 garbage |
| 5392 | * collection, v8 collects hidden fields of all potential wrappers, and at the |
| 5393 | * end of its marking phase iterates the collection and asks the embedder to |
| 5394 | * trace through its heap and call PersistentBase::RegisterExternalReference on |
| 5395 | * each js object reachable from any of the given wrappers. |
| 5396 | * |
| 5397 | * Before the first call to the TraceWrappableFrom function v8 will call |
| 5398 | * TraceRoots. When the v8 garbage collection is finished, v8 will call |
| 5399 | * ClearTracingMarks. |
| 5400 | */ |
| 5401 | class EmbedderHeapTracer { |
| 5402 | public: |
| 5403 | /** |
| 5404 | * V8 will call this method at the beginning of the gc cycle. |
| 5405 | */ |
| 5406 | virtual void TraceRoots(Isolate* isolate) = 0; |
| 5407 | |
| 5408 | /** |
| 5409 | * V8 will call this method with internal fields of a potential wrappers. |
| 5410 | * Embedder is expected to trace its heap (synchronously) and call |
| 5411 | * PersistentBase::RegisterExternalReference() on all wrappers reachable from |
| 5412 | * any of the given wrappers. |
| 5413 | */ |
| 5414 | virtual void TraceWrappableFrom( |
| 5415 | Isolate* isolate, |
| 5416 | const std::vector<std::pair<void*, void*> >& internal_fields) = 0; |
| 5417 | /** |
| 5418 | * V8 will call this method at the end of the gc cycle. Allocation is *not* |
| 5419 | * allowed in the ClearTracingMarks. |
| 5420 | */ |
| 5421 | virtual void ClearTracingMarks(Isolate* isolate) = 0; |
| 5422 | |
| 5423 | protected: |
| 5424 | virtual ~EmbedderHeapTracer() = default; |
| 5425 | }; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5426 | |
| 5427 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5428 | * Isolate represents an isolated instance of the V8 engine. V8 isolates have |
| 5429 | * completely separate states. Objects from one isolate must not be used in |
| 5430 | * other isolates. The embedder can create multiple isolates and use them in |
| 5431 | * parallel in multiple threads. An isolate can be entered by at most one |
| 5432 | * thread at any given time. The Locker/Unlocker API must be used to |
| 5433 | * synchronize. |
| 5434 | */ |
| 5435 | class V8_EXPORT Isolate { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5436 | public: |
| 5437 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5438 | * Initial configuration parameters for a new Isolate. |
| 5439 | */ |
| 5440 | struct CreateParams { |
| 5441 | CreateParams() |
| 5442 | : entry_hook(NULL), |
| 5443 | code_event_handler(NULL), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5444 | snapshot_blob(NULL), |
| 5445 | counter_lookup_callback(NULL), |
| 5446 | create_histogram_callback(NULL), |
| 5447 | add_histogram_sample_callback(NULL), |
| 5448 | array_buffer_allocator(NULL) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5449 | |
| 5450 | /** |
| 5451 | * The optional entry_hook allows the host application to provide the |
| 5452 | * address of a function that's invoked on entry to every V8-generated |
| 5453 | * function. Note that entry_hook is invoked at the very start of each |
| 5454 | * generated function. Furthermore, if an entry_hook is given, V8 will |
| 5455 | * always run without a context snapshot. |
| 5456 | */ |
| 5457 | FunctionEntryHook entry_hook; |
| 5458 | |
| 5459 | /** |
| 5460 | * Allows the host application to provide the address of a function that is |
| 5461 | * notified each time code is added, moved or removed. |
| 5462 | */ |
| 5463 | JitCodeEventHandler code_event_handler; |
| 5464 | |
| 5465 | /** |
| 5466 | * ResourceConstraints to use for the new Isolate. |
| 5467 | */ |
| 5468 | ResourceConstraints constraints; |
| 5469 | |
| 5470 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5471 | * Explicitly specify a startup snapshot blob. The embedder owns the blob. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5472 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5473 | StartupData* snapshot_blob; |
| 5474 | |
| 5475 | |
| 5476 | /** |
| 5477 | * Enables the host application to provide a mechanism for recording |
| 5478 | * statistics counters. |
| 5479 | */ |
| 5480 | CounterLookupCallback counter_lookup_callback; |
| 5481 | |
| 5482 | /** |
| 5483 | * Enables the host application to provide a mechanism for recording |
| 5484 | * histograms. The CreateHistogram function returns a |
| 5485 | * histogram which will later be passed to the AddHistogramSample |
| 5486 | * function. |
| 5487 | */ |
| 5488 | CreateHistogramCallback create_histogram_callback; |
| 5489 | AddHistogramSampleCallback add_histogram_sample_callback; |
| 5490 | |
| 5491 | /** |
| 5492 | * The ArrayBuffer::Allocator to use for allocating and freeing the backing |
| 5493 | * store of ArrayBuffers. |
| 5494 | */ |
| 5495 | ArrayBuffer::Allocator* array_buffer_allocator; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5496 | }; |
| 5497 | |
| 5498 | |
| 5499 | /** |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5500 | * Stack-allocated class which sets the isolate for all operations |
| 5501 | * executed within a local scope. |
| 5502 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5503 | class V8_EXPORT Scope { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5504 | public: |
| 5505 | explicit Scope(Isolate* isolate) : isolate_(isolate) { |
| 5506 | isolate->Enter(); |
| 5507 | } |
| 5508 | |
| 5509 | ~Scope() { isolate_->Exit(); } |
| 5510 | |
| 5511 | private: |
| 5512 | Isolate* const isolate_; |
| 5513 | |
| 5514 | // Prevent copying of Scope objects. |
| 5515 | Scope(const Scope&); |
| 5516 | Scope& operator=(const Scope&); |
| 5517 | }; |
| 5518 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5519 | |
| 5520 | /** |
| 5521 | * Assert that no Javascript code is invoked. |
| 5522 | */ |
| 5523 | class V8_EXPORT DisallowJavascriptExecutionScope { |
| 5524 | public: |
| 5525 | enum OnFailure { CRASH_ON_FAILURE, THROW_ON_FAILURE }; |
| 5526 | |
| 5527 | DisallowJavascriptExecutionScope(Isolate* isolate, OnFailure on_failure); |
| 5528 | ~DisallowJavascriptExecutionScope(); |
| 5529 | |
| 5530 | private: |
| 5531 | bool on_failure_; |
| 5532 | void* internal_; |
| 5533 | |
| 5534 | // Prevent copying of Scope objects. |
| 5535 | DisallowJavascriptExecutionScope(const DisallowJavascriptExecutionScope&); |
| 5536 | DisallowJavascriptExecutionScope& operator=( |
| 5537 | const DisallowJavascriptExecutionScope&); |
| 5538 | }; |
| 5539 | |
| 5540 | |
| 5541 | /** |
| 5542 | * Introduce exception to DisallowJavascriptExecutionScope. |
| 5543 | */ |
| 5544 | class V8_EXPORT AllowJavascriptExecutionScope { |
| 5545 | public: |
| 5546 | explicit AllowJavascriptExecutionScope(Isolate* isolate); |
| 5547 | ~AllowJavascriptExecutionScope(); |
| 5548 | |
| 5549 | private: |
| 5550 | void* internal_throws_; |
| 5551 | void* internal_assert_; |
| 5552 | |
| 5553 | // Prevent copying of Scope objects. |
| 5554 | AllowJavascriptExecutionScope(const AllowJavascriptExecutionScope&); |
| 5555 | AllowJavascriptExecutionScope& operator=( |
| 5556 | const AllowJavascriptExecutionScope&); |
| 5557 | }; |
| 5558 | |
| 5559 | /** |
| 5560 | * Do not run microtasks while this scope is active, even if microtasks are |
| 5561 | * automatically executed otherwise. |
| 5562 | */ |
| 5563 | class V8_EXPORT SuppressMicrotaskExecutionScope { |
| 5564 | public: |
| 5565 | explicit SuppressMicrotaskExecutionScope(Isolate* isolate); |
| 5566 | ~SuppressMicrotaskExecutionScope(); |
| 5567 | |
| 5568 | private: |
| 5569 | internal::Isolate* isolate_; |
| 5570 | |
| 5571 | // Prevent copying of Scope objects. |
| 5572 | SuppressMicrotaskExecutionScope(const SuppressMicrotaskExecutionScope&); |
| 5573 | SuppressMicrotaskExecutionScope& operator=( |
| 5574 | const SuppressMicrotaskExecutionScope&); |
| 5575 | }; |
| 5576 | |
| 5577 | /** |
| 5578 | * Types of garbage collections that can be requested via |
| 5579 | * RequestGarbageCollectionForTesting. |
| 5580 | */ |
| 5581 | enum GarbageCollectionType { |
| 5582 | kFullGarbageCollection, |
| 5583 | kMinorGarbageCollection |
| 5584 | }; |
| 5585 | |
| 5586 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5587 | * Features reported via the SetUseCounterCallback callback. Do not change |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5588 | * assigned numbers of existing items; add new features to the end of this |
| 5589 | * list. |
| 5590 | */ |
| 5591 | enum UseCounterFeature { |
| 5592 | kUseAsm = 0, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5593 | kBreakIterator = 1, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5594 | kLegacyConst = 2, |
| 5595 | kMarkDequeOverflow = 3, |
| 5596 | kStoreBufferOverflow = 4, |
| 5597 | kSlotsBufferOverflow = 5, |
| 5598 | kObjectObserve = 6, |
| 5599 | kForcedGC = 7, |
| 5600 | kSloppyMode = 8, |
| 5601 | kStrictMode = 9, |
| 5602 | kStrongMode = 10, |
| 5603 | kRegExpPrototypeStickyGetter = 11, |
| 5604 | kRegExpPrototypeToString = 12, |
| 5605 | kRegExpPrototypeUnicodeGetter = 13, |
| 5606 | kIntlV8Parse = 14, |
| 5607 | kIntlPattern = 15, |
| 5608 | kIntlResolved = 16, |
| 5609 | kPromiseChain = 17, |
| 5610 | kPromiseAccept = 18, |
| 5611 | kPromiseDefer = 19, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5612 | kHtmlCommentInExternalScript = 20, |
| 5613 | kHtmlComment = 21, |
| 5614 | kSloppyModeBlockScopedFunctionRedefinition = 22, |
| 5615 | kForInInitializer = 23, |
| 5616 | kArrayProtectorDirtied = 24, |
| 5617 | kArraySpeciesModified = 25, |
| 5618 | kArrayPrototypeConstructorModified = 26, |
| 5619 | kArrayInstanceProtoModified = 27, |
| 5620 | kArrayInstanceConstructorModified = 28, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5621 | kLegacyFunctionDeclaration = 29, |
| 5622 | kRegExpPrototypeSourceGetter = 30, |
| 5623 | kRegExpPrototypeOldFlagGetter = 31, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5624 | |
| 5625 | // If you add new values here, you'll also need to update V8Initializer.cpp |
| 5626 | // in Chromium. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5627 | kUseCounterFeatureCount // This enum value must be last. |
| 5628 | }; |
| 5629 | |
| 5630 | typedef void (*UseCounterCallback)(Isolate* isolate, |
| 5631 | UseCounterFeature feature); |
| 5632 | |
| 5633 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5634 | /** |
| 5635 | * Creates a new isolate. Does not change the currently entered |
| 5636 | * isolate. |
| 5637 | * |
| 5638 | * When an isolate is no longer used its resources should be freed |
| 5639 | * by calling Dispose(). Using the delete operator is not allowed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5640 | * |
| 5641 | * V8::Initialize() must have run prior to this. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5642 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5643 | static Isolate* New(const CreateParams& params); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5644 | |
| 5645 | /** |
| 5646 | * Returns the entered isolate for the current thread or NULL in |
| 5647 | * case there is no current isolate. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5648 | * |
| 5649 | * This method must not be invoked before V8::Initialize() was invoked. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5650 | */ |
| 5651 | static Isolate* GetCurrent(); |
| 5652 | |
| 5653 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5654 | * Custom callback used by embedders to help V8 determine if it should abort |
| 5655 | * when it throws and no internal handler is predicted to catch the |
| 5656 | * exception. If --abort-on-uncaught-exception is used on the command line, |
| 5657 | * then V8 will abort if either: |
| 5658 | * - no custom callback is set. |
| 5659 | * - the custom callback set returns true. |
| 5660 | * Otherwise, the custom callback will not be called and V8 will not abort. |
| 5661 | */ |
| 5662 | typedef bool (*AbortOnUncaughtExceptionCallback)(Isolate*); |
| 5663 | void SetAbortOnUncaughtExceptionCallback( |
| 5664 | AbortOnUncaughtExceptionCallback callback); |
| 5665 | |
| 5666 | /** |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5667 | * Optional notification that the system is running low on memory. |
| 5668 | * V8 uses these notifications to guide heuristics. |
| 5669 | * It is allowed to call this function from another thread while |
| 5670 | * the isolate is executing long running JavaScript code. |
| 5671 | */ |
| 5672 | void MemoryPressureNotification(MemoryPressureLevel level); |
| 5673 | |
| 5674 | /** |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5675 | * Methods below this point require holding a lock (using Locker) in |
| 5676 | * a multi-threaded environment. |
| 5677 | */ |
| 5678 | |
| 5679 | /** |
| 5680 | * Sets this isolate as the entered one for the current thread. |
| 5681 | * Saves the previously entered one (if any), so that it can be |
| 5682 | * restored when exiting. Re-entering an isolate is allowed. |
| 5683 | */ |
| 5684 | void Enter(); |
| 5685 | |
| 5686 | /** |
| 5687 | * Exits this isolate by restoring the previously entered one in the |
| 5688 | * current thread. The isolate may still stay the same, if it was |
| 5689 | * entered more than once. |
| 5690 | * |
| 5691 | * Requires: this == Isolate::GetCurrent(). |
| 5692 | */ |
| 5693 | void Exit(); |
| 5694 | |
| 5695 | /** |
| 5696 | * Disposes the isolate. The isolate must not be entered by any |
| 5697 | * thread to be disposable. |
| 5698 | */ |
| 5699 | void Dispose(); |
| 5700 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5701 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5702 | * Discards all V8 thread-specific data for the Isolate. Should be used |
| 5703 | * if a thread is terminating and it has used an Isolate that will outlive |
| 5704 | * the thread -- all thread-specific data for an Isolate is discarded when |
| 5705 | * an Isolate is disposed so this call is pointless if an Isolate is about |
| 5706 | * to be Disposed. |
| 5707 | */ |
| 5708 | void DiscardThreadSpecificMetadata(); |
| 5709 | |
| 5710 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5711 | * Associate embedder-specific data with the isolate. |slot| has to be |
| 5712 | * between 0 and GetNumberOfDataSlots() - 1. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5713 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5714 | V8_INLINE void SetData(uint32_t slot, void* data); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5715 | |
| 5716 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5717 | * Retrieve embedder-specific data from the isolate. |
| 5718 | * Returns NULL if SetData has never been called for the given |slot|. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5719 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5720 | V8_INLINE void* GetData(uint32_t slot); |
| 5721 | |
| 5722 | /** |
| 5723 | * Returns the maximum number of available embedder data slots. Valid slots |
| 5724 | * are in the range of 0 - GetNumberOfDataSlots() - 1. |
| 5725 | */ |
| 5726 | V8_INLINE static uint32_t GetNumberOfDataSlots(); |
| 5727 | |
| 5728 | /** |
| 5729 | * Get statistics about the heap memory usage. |
| 5730 | */ |
| 5731 | void GetHeapStatistics(HeapStatistics* heap_statistics); |
| 5732 | |
| 5733 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5734 | * Returns the number of spaces in the heap. |
| 5735 | */ |
| 5736 | size_t NumberOfHeapSpaces(); |
| 5737 | |
| 5738 | /** |
| 5739 | * Get the memory usage of a space in the heap. |
| 5740 | * |
| 5741 | * \param space_statistics The HeapSpaceStatistics object to fill in |
| 5742 | * statistics. |
| 5743 | * \param index The index of the space to get statistics from, which ranges |
| 5744 | * from 0 to NumberOfHeapSpaces() - 1. |
| 5745 | * \returns true on success. |
| 5746 | */ |
| 5747 | bool GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics, |
| 5748 | size_t index); |
| 5749 | |
| 5750 | /** |
| 5751 | * Returns the number of types of objects tracked in the heap at GC. |
| 5752 | */ |
| 5753 | size_t NumberOfTrackedHeapObjectTypes(); |
| 5754 | |
| 5755 | /** |
| 5756 | * Get statistics about objects in the heap. |
| 5757 | * |
| 5758 | * \param object_statistics The HeapObjectStatistics object to fill in |
| 5759 | * statistics of objects of given type, which were live in the previous GC. |
| 5760 | * \param type_index The index of the type of object to fill details about, |
| 5761 | * which ranges from 0 to NumberOfTrackedHeapObjectTypes() - 1. |
| 5762 | * \returns true on success. |
| 5763 | */ |
| 5764 | bool GetHeapObjectStatisticsAtLastGC(HeapObjectStatistics* object_statistics, |
| 5765 | size_t type_index); |
| 5766 | |
| 5767 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5768 | * Get a call stack sample from the isolate. |
| 5769 | * \param state Execution state. |
| 5770 | * \param frames Caller allocated buffer to store stack frames. |
| 5771 | * \param frames_limit Maximum number of frames to capture. The buffer must |
| 5772 | * be large enough to hold the number of frames. |
| 5773 | * \param sample_info The sample info is filled up by the function |
| 5774 | * provides number of actual captured stack frames and |
| 5775 | * the current VM state. |
| 5776 | * \note GetStackSample should only be called when the JS thread is paused or |
| 5777 | * interrupted. Otherwise the behavior is undefined. |
| 5778 | */ |
| 5779 | void GetStackSample(const RegisterState& state, void** frames, |
| 5780 | size_t frames_limit, SampleInfo* sample_info); |
| 5781 | |
| 5782 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5783 | * Adjusts the amount of registered external memory. Used to give V8 an |
| 5784 | * indication of the amount of externally allocated memory that is kept alive |
| 5785 | * by JavaScript objects. V8 uses this to decide when to perform global |
| 5786 | * garbage collections. Registering externally allocated memory will trigger |
| 5787 | * global garbage collections more often than it would otherwise in an attempt |
| 5788 | * to garbage collect the JavaScript objects that keep the externally |
| 5789 | * allocated memory alive. |
| 5790 | * |
| 5791 | * \param change_in_bytes the change in externally allocated memory that is |
| 5792 | * kept alive by JavaScript objects. |
| 5793 | * \returns the adjusted value. |
| 5794 | */ |
| 5795 | V8_INLINE int64_t |
| 5796 | AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes); |
| 5797 | |
| 5798 | /** |
| 5799 | * Returns heap profiler for this isolate. Will return NULL until the isolate |
| 5800 | * is initialized. |
| 5801 | */ |
| 5802 | HeapProfiler* GetHeapProfiler(); |
| 5803 | |
| 5804 | /** |
| 5805 | * Returns CPU profiler for this isolate. Will return NULL unless the isolate |
| 5806 | * is initialized. It is the embedder's responsibility to stop all CPU |
| 5807 | * profiling activities if it has started any. |
| 5808 | */ |
| 5809 | CpuProfiler* GetCpuProfiler(); |
| 5810 | |
| 5811 | /** Returns true if this isolate has a current context. */ |
| 5812 | bool InContext(); |
| 5813 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5814 | /** |
| 5815 | * Returns the context of the currently running JavaScript, or the context |
| 5816 | * on the top of the stack if no JavaScript is running. |
| 5817 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5818 | Local<Context> GetCurrentContext(); |
| 5819 | |
| 5820 | /** |
| 5821 | * Returns the context of the calling JavaScript code. That is the |
| 5822 | * context of the top-most JavaScript frame. If there are no |
| 5823 | * JavaScript frames an empty handle is returned. |
| 5824 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5825 | V8_DEPRECATE_SOON( |
| 5826 | "Calling context concept is not compatible with tail calls, and will be " |
| 5827 | "removed.", |
| 5828 | Local<Context> GetCallingContext()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5829 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5830 | /** Returns the last context entered through V8's C++ API. */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5831 | Local<Context> GetEnteredContext(); |
| 5832 | |
| 5833 | /** |
| 5834 | * Schedules an exception to be thrown when returning to JavaScript. When an |
| 5835 | * exception has been scheduled it is illegal to invoke any JavaScript |
| 5836 | * operation; the caller must return immediately and only after the exception |
| 5837 | * has been handled does it become legal to invoke JavaScript operations. |
| 5838 | */ |
| 5839 | Local<Value> ThrowException(Local<Value> exception); |
| 5840 | |
| 5841 | /** |
| 5842 | * Allows the host application to group objects together. If one |
| 5843 | * object in the group is alive, all objects in the group are alive. |
| 5844 | * After each garbage collection, object groups are removed. It is |
| 5845 | * intended to be used in the before-garbage-collection callback |
| 5846 | * function, for instance to simulate DOM tree connections among JS |
| 5847 | * wrapper objects. Object groups for all dependent handles need to |
| 5848 | * be provided for kGCTypeMarkSweepCompact collections, for all other |
| 5849 | * garbage collection types it is sufficient to provide object groups |
| 5850 | * for partially dependent handles only. |
| 5851 | */ |
| 5852 | template<typename T> void SetObjectGroupId(const Persistent<T>& object, |
| 5853 | UniqueId id); |
| 5854 | |
| 5855 | /** |
| 5856 | * Allows the host application to declare implicit references from an object |
| 5857 | * group to an object. If the objects of the object group are alive, the child |
| 5858 | * object is alive too. After each garbage collection, all implicit references |
| 5859 | * are removed. It is intended to be used in the before-garbage-collection |
| 5860 | * callback function. |
| 5861 | */ |
| 5862 | template<typename T> void SetReferenceFromGroup(UniqueId id, |
| 5863 | const Persistent<T>& child); |
| 5864 | |
| 5865 | /** |
| 5866 | * Allows the host application to declare implicit references from an object |
| 5867 | * to another object. If the parent object is alive, the child object is alive |
| 5868 | * too. After each garbage collection, all implicit references are removed. It |
| 5869 | * is intended to be used in the before-garbage-collection callback function. |
| 5870 | */ |
| 5871 | template<typename T, typename S> |
| 5872 | void SetReference(const Persistent<T>& parent, const Persistent<S>& child); |
| 5873 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5874 | typedef void (*GCCallback)(Isolate* isolate, GCType type, |
| 5875 | GCCallbackFlags flags); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5876 | |
| 5877 | /** |
| 5878 | * Enables the host application to receive a notification before a |
| 5879 | * garbage collection. Allocations are allowed in the callback function, |
| 5880 | * but the callback is not re-entrant: if the allocation inside it will |
| 5881 | * trigger the garbage collection, the callback won't be called again. |
| 5882 | * It is possible to specify the GCType filter for your callback. But it is |
| 5883 | * not possible to register the same callback function two times with |
| 5884 | * different GCType filters. |
| 5885 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5886 | void AddGCPrologueCallback(GCCallback callback, |
| 5887 | GCType gc_type_filter = kGCTypeAll); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5888 | |
| 5889 | /** |
| 5890 | * This function removes callback which was installed by |
| 5891 | * AddGCPrologueCallback function. |
| 5892 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5893 | void RemoveGCPrologueCallback(GCCallback callback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5894 | |
| 5895 | /** |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 5896 | * Sets the embedder heap tracer for the isolate. |
| 5897 | */ |
| 5898 | void SetEmbedderHeapTracer(EmbedderHeapTracer* tracer); |
| 5899 | |
| 5900 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5901 | * Enables the host application to receive a notification after a |
| 5902 | * garbage collection. Allocations are allowed in the callback function, |
| 5903 | * but the callback is not re-entrant: if the allocation inside it will |
| 5904 | * trigger the garbage collection, the callback won't be called again. |
| 5905 | * It is possible to specify the GCType filter for your callback. But it is |
| 5906 | * not possible to register the same callback function two times with |
| 5907 | * different GCType filters. |
| 5908 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5909 | void AddGCEpilogueCallback(GCCallback callback, |
| 5910 | GCType gc_type_filter = kGCTypeAll); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5911 | |
| 5912 | /** |
| 5913 | * This function removes callback which was installed by |
| 5914 | * AddGCEpilogueCallback function. |
| 5915 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5916 | void RemoveGCEpilogueCallback(GCCallback callback); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5917 | |
| 5918 | /** |
| 5919 | * Forcefully terminate the current thread of JavaScript execution |
| 5920 | * in the given isolate. |
| 5921 | * |
| 5922 | * This method can be used by any thread even if that thread has not |
| 5923 | * acquired the V8 lock with a Locker object. |
| 5924 | */ |
| 5925 | void TerminateExecution(); |
| 5926 | |
| 5927 | /** |
| 5928 | * Is V8 terminating JavaScript execution. |
| 5929 | * |
| 5930 | * Returns true if JavaScript execution is currently terminating |
| 5931 | * because of a call to TerminateExecution. In that case there are |
| 5932 | * still JavaScript frames on the stack and the termination |
| 5933 | * exception is still active. |
| 5934 | */ |
| 5935 | bool IsExecutionTerminating(); |
| 5936 | |
| 5937 | /** |
| 5938 | * Resume execution capability in the given isolate, whose execution |
| 5939 | * was previously forcefully terminated using TerminateExecution(). |
| 5940 | * |
| 5941 | * When execution is forcefully terminated using TerminateExecution(), |
| 5942 | * the isolate can not resume execution until all JavaScript frames |
| 5943 | * have propagated the uncatchable exception which is generated. This |
| 5944 | * method allows the program embedding the engine to handle the |
| 5945 | * termination event and resume execution capability, even if |
| 5946 | * JavaScript frames remain on the stack. |
| 5947 | * |
| 5948 | * This method can be used by any thread even if that thread has not |
| 5949 | * acquired the V8 lock with a Locker object. |
| 5950 | */ |
| 5951 | void CancelTerminateExecution(); |
| 5952 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5953 | /** |
| 5954 | * Request V8 to interrupt long running JavaScript code and invoke |
| 5955 | * the given |callback| passing the given |data| to it. After |callback| |
| 5956 | * returns control will be returned to the JavaScript code. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 5957 | * There may be a number of interrupt requests in flight. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5958 | * Can be called from another thread without acquiring a |Locker|. |
| 5959 | * Registered |callback| must not reenter interrupted Isolate. |
| 5960 | */ |
| 5961 | void RequestInterrupt(InterruptCallback callback, void* data); |
| 5962 | |
| 5963 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5964 | * Request garbage collection in this Isolate. It is only valid to call this |
| 5965 | * function if --expose_gc was specified. |
| 5966 | * |
| 5967 | * This should only be used for testing purposes and not to enforce a garbage |
| 5968 | * collection schedule. It has strong negative impact on the garbage |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5969 | * collection performance. Use IdleNotificationDeadline() or |
| 5970 | * LowMemoryNotification() instead to influence the garbage collection |
| 5971 | * schedule. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5972 | */ |
| 5973 | void RequestGarbageCollectionForTesting(GarbageCollectionType type); |
| 5974 | |
| 5975 | /** |
| 5976 | * Set the callback to invoke for logging event. |
| 5977 | */ |
| 5978 | void SetEventLogger(LogEventCallback that); |
| 5979 | |
| 5980 | /** |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 5981 | * Adds a callback to notify the host application right before a script |
| 5982 | * is about to run. If a script re-enters the runtime during executing, the |
| 5983 | * BeforeCallEnteredCallback is invoked for each re-entrance. |
| 5984 | * Executing scripts inside the callback will re-trigger the callback. |
| 5985 | */ |
| 5986 | void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback); |
| 5987 | |
| 5988 | /** |
| 5989 | * Removes callback that was installed by AddBeforeCallEnteredCallback. |
| 5990 | */ |
| 5991 | void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback); |
| 5992 | |
| 5993 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5994 | * Adds a callback to notify the host application when a script finished |
| 5995 | * running. If a script re-enters the runtime during executing, the |
| 5996 | * CallCompletedCallback is only invoked when the outer-most script |
| 5997 | * execution ends. Executing scripts inside the callback do not trigger |
| 5998 | * further callbacks. |
| 5999 | */ |
| 6000 | void AddCallCompletedCallback(CallCompletedCallback callback); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6001 | V8_DEPRECATE_SOON( |
| 6002 | "Use callback with parameter", |
| 6003 | void AddCallCompletedCallback(DeprecatedCallCompletedCallback callback)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6004 | |
| 6005 | /** |
| 6006 | * Removes callback that was installed by AddCallCompletedCallback. |
| 6007 | */ |
| 6008 | void RemoveCallCompletedCallback(CallCompletedCallback callback); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6009 | V8_DEPRECATE_SOON( |
| 6010 | "Use callback with parameter", |
| 6011 | void RemoveCallCompletedCallback( |
| 6012 | DeprecatedCallCompletedCallback callback)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6013 | |
| 6014 | /** |
| 6015 | * Set callback to notify about promise reject with no handler, or |
| 6016 | * revocation of such a previous notification once the handler is added. |
| 6017 | */ |
| 6018 | void SetPromiseRejectCallback(PromiseRejectCallback callback); |
| 6019 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6020 | /** |
| 6021 | * Experimental: Runs the Microtask Work Queue until empty |
| 6022 | * Any exceptions thrown by microtask callbacks are swallowed. |
| 6023 | */ |
| 6024 | void RunMicrotasks(); |
| 6025 | |
| 6026 | /** |
| 6027 | * Experimental: Enqueues the callback to the Microtask Work Queue |
| 6028 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6029 | void EnqueueMicrotask(Local<Function> microtask); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6030 | |
| 6031 | /** |
| 6032 | * Experimental: Enqueues the callback to the Microtask Work Queue |
| 6033 | */ |
| 6034 | void EnqueueMicrotask(MicrotaskCallback microtask, void* data = NULL); |
| 6035 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6036 | /** |
| 6037 | * Experimental: Controls how Microtasks are invoked. See MicrotasksPolicy |
| 6038 | * for details. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6039 | */ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6040 | void SetMicrotasksPolicy(MicrotasksPolicy policy); |
| 6041 | V8_DEPRECATE_SOON("Use SetMicrotasksPolicy", |
| 6042 | void SetAutorunMicrotasks(bool autorun)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6043 | |
| 6044 | /** |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6045 | * Experimental: Returns the policy controlling how Microtasks are invoked. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6046 | */ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6047 | MicrotasksPolicy GetMicrotasksPolicy() const; |
| 6048 | V8_DEPRECATE_SOON("Use GetMicrotasksPolicy", |
| 6049 | bool WillAutorunMicrotasks() const); |
| 6050 | |
| 6051 | /** |
| 6052 | * Experimental: adds a callback to notify the host application after |
| 6053 | * microtasks were run. The callback is triggered by explicit RunMicrotasks |
| 6054 | * call or automatic microtasks execution (see SetAutorunMicrotasks). |
| 6055 | * |
| 6056 | * Callback will trigger even if microtasks were attempted to run, |
| 6057 | * but the microtasks queue was empty and no single microtask was actually |
| 6058 | * executed. |
| 6059 | * |
| 6060 | * Executing scriptsinside the callback will not re-trigger microtasks and |
| 6061 | * the callback. |
| 6062 | */ |
| 6063 | void AddMicrotasksCompletedCallback(MicrotasksCompletedCallback callback); |
| 6064 | |
| 6065 | /** |
| 6066 | * Removes callback that was installed by AddMicrotasksCompletedCallback. |
| 6067 | */ |
| 6068 | void RemoveMicrotasksCompletedCallback(MicrotasksCompletedCallback callback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6069 | |
| 6070 | /** |
| 6071 | * Sets a callback for counting the number of times a feature of V8 is used. |
| 6072 | */ |
| 6073 | void SetUseCounterCallback(UseCounterCallback callback); |
| 6074 | |
| 6075 | /** |
| 6076 | * Enables the host application to provide a mechanism for recording |
| 6077 | * statistics counters. |
| 6078 | */ |
| 6079 | void SetCounterFunction(CounterLookupCallback); |
| 6080 | |
| 6081 | /** |
| 6082 | * Enables the host application to provide a mechanism for recording |
| 6083 | * histograms. The CreateHistogram function returns a |
| 6084 | * histogram which will later be passed to the AddHistogramSample |
| 6085 | * function. |
| 6086 | */ |
| 6087 | void SetCreateHistogramFunction(CreateHistogramCallback); |
| 6088 | void SetAddHistogramSampleFunction(AddHistogramSampleCallback); |
| 6089 | |
| 6090 | /** |
| 6091 | * Optional notification that the embedder is idle. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6092 | * V8 uses the notification to perform garbage collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6093 | * This call can be used repeatedly if the embedder remains idle. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6094 | * Returns true if the embedder should stop calling IdleNotificationDeadline |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6095 | * until real work has been done. This indicates that V8 has done |
| 6096 | * as much cleanup as it will be able to do. |
| 6097 | * |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6098 | * The deadline_in_seconds argument specifies the deadline V8 has to finish |
| 6099 | * garbage collection work. deadline_in_seconds is compared with |
| 6100 | * MonotonicallyIncreasingTime() and should be based on the same timebase as |
| 6101 | * that function. There is no guarantee that the actual work will be done |
| 6102 | * within the time limit. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6103 | */ |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6104 | bool IdleNotificationDeadline(double deadline_in_seconds); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6105 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6106 | V8_DEPRECATED("use IdleNotificationDeadline()", |
| 6107 | bool IdleNotification(int idle_time_in_ms)); |
| 6108 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6109 | /** |
| 6110 | * Optional notification that the system is running low on memory. |
| 6111 | * V8 uses these notifications to attempt to free memory. |
| 6112 | */ |
| 6113 | void LowMemoryNotification(); |
| 6114 | |
| 6115 | /** |
| 6116 | * Optional notification that a context has been disposed. V8 uses |
| 6117 | * these notifications to guide the GC heuristic. Returns the number |
| 6118 | * of context disposals - including this one - since the last time |
| 6119 | * V8 had a chance to clean up. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6120 | * |
| 6121 | * The optional parameter |dependant_context| specifies whether the disposed |
| 6122 | * context was depending on state from other contexts or not. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6123 | */ |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6124 | int ContextDisposedNotification(bool dependant_context = true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6125 | |
| 6126 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6127 | * Optional notification that the isolate switched to the foreground. |
| 6128 | * V8 uses these notifications to guide heuristics. |
| 6129 | */ |
| 6130 | void IsolateInForegroundNotification(); |
| 6131 | |
| 6132 | /** |
| 6133 | * Optional notification that the isolate switched to the background. |
| 6134 | * V8 uses these notifications to guide heuristics. |
| 6135 | */ |
| 6136 | void IsolateInBackgroundNotification(); |
| 6137 | |
| 6138 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6139 | * Allows the host application to provide the address of a function that is |
| 6140 | * notified each time code is added, moved or removed. |
| 6141 | * |
| 6142 | * \param options options for the JIT code event handler. |
| 6143 | * \param event_handler the JIT code event handler, which will be invoked |
| 6144 | * each time code is added, moved or removed. |
| 6145 | * \note \p event_handler won't get notified of existent code. |
| 6146 | * \note since code removal notifications are not currently issued, the |
| 6147 | * \p event_handler may get notifications of code that overlaps earlier |
| 6148 | * code notifications. This happens when code areas are reused, and the |
| 6149 | * earlier overlapping code areas should therefore be discarded. |
| 6150 | * \note the events passed to \p event_handler and the strings they point to |
| 6151 | * are not guaranteed to live past each call. The \p event_handler must |
| 6152 | * copy strings and other parameters it needs to keep around. |
| 6153 | * \note the set of events declared in JitCodeEvent::EventType is expected to |
| 6154 | * grow over time, and the JitCodeEvent structure is expected to accrue |
| 6155 | * new members. The \p event_handler function must ignore event codes |
| 6156 | * it does not recognize to maintain future compatibility. |
| 6157 | * \note Use Isolate::CreateParams to get events for code executed during |
| 6158 | * Isolate setup. |
| 6159 | */ |
| 6160 | void SetJitCodeEventHandler(JitCodeEventOptions options, |
| 6161 | JitCodeEventHandler event_handler); |
| 6162 | |
| 6163 | /** |
| 6164 | * Modifies the stack limit for this Isolate. |
| 6165 | * |
| 6166 | * \param stack_limit An address beyond which the Vm's stack may not grow. |
| 6167 | * |
| 6168 | * \note If you are using threads then you should hold the V8::Locker lock |
| 6169 | * while setting the stack limit and you must set a non-default stack |
| 6170 | * limit separately for each thread. |
| 6171 | */ |
| 6172 | void SetStackLimit(uintptr_t stack_limit); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6173 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6174 | /** |
| 6175 | * Returns a memory range that can potentially contain jitted code. |
| 6176 | * |
| 6177 | * On Win64, embedders are advised to install function table callbacks for |
| 6178 | * these ranges, as default SEH won't be able to unwind through jitted code. |
| 6179 | * |
| 6180 | * The first page of the code range is reserved for the embedder and is |
| 6181 | * committed, writable, and executable. |
| 6182 | * |
| 6183 | * Might be empty on other platforms. |
| 6184 | * |
| 6185 | * https://code.google.com/p/v8/issues/detail?id=3598 |
| 6186 | */ |
| 6187 | void GetCodeRange(void** start, size_t* length_in_bytes); |
| 6188 | |
| 6189 | /** Set the callback to invoke in case of fatal errors. */ |
| 6190 | void SetFatalErrorHandler(FatalErrorCallback that); |
| 6191 | |
| 6192 | /** |
| 6193 | * Set the callback to invoke to check if code generation from |
| 6194 | * strings should be allowed. |
| 6195 | */ |
| 6196 | void SetAllowCodeGenerationFromStringsCallback( |
| 6197 | AllowCodeGenerationFromStringsCallback callback); |
| 6198 | |
| 6199 | /** |
| 6200 | * Check if V8 is dead and therefore unusable. This is the case after |
| 6201 | * fatal errors such as out-of-memory situations. |
| 6202 | */ |
| 6203 | bool IsDead(); |
| 6204 | |
| 6205 | /** |
| 6206 | * Adds a message listener. |
| 6207 | * |
| 6208 | * The same message listener can be added more than once and in that |
| 6209 | * case it will be called more than once for each message. |
| 6210 | * |
| 6211 | * If data is specified, it will be passed to the callback when it is called. |
| 6212 | * Otherwise, the exception object will be passed to the callback instead. |
| 6213 | */ |
| 6214 | bool AddMessageListener(MessageCallback that, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6215 | Local<Value> data = Local<Value>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6216 | |
| 6217 | /** |
| 6218 | * Remove all message listeners from the specified callback function. |
| 6219 | */ |
| 6220 | void RemoveMessageListeners(MessageCallback that); |
| 6221 | |
| 6222 | /** Callback function for reporting failed access checks.*/ |
| 6223 | void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback); |
| 6224 | |
| 6225 | /** |
| 6226 | * Tells V8 to capture current stack trace when uncaught exception occurs |
| 6227 | * and report it to the message listeners. The option is off by default. |
| 6228 | */ |
| 6229 | void SetCaptureStackTraceForUncaughtExceptions( |
| 6230 | bool capture, int frame_limit = 10, |
| 6231 | StackTrace::StackTraceOptions options = StackTrace::kOverview); |
| 6232 | |
| 6233 | /** |
| 6234 | * Enables the host application to provide a mechanism to be notified |
| 6235 | * and perform custom logging when V8 Allocates Executable Memory. |
| 6236 | */ |
| 6237 | void AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 6238 | ObjectSpace space, AllocationAction action); |
| 6239 | |
| 6240 | /** |
| 6241 | * Removes callback that was installed by AddMemoryAllocationCallback. |
| 6242 | */ |
| 6243 | void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback); |
| 6244 | |
| 6245 | /** |
| 6246 | * Iterates through all external resources referenced from current isolate |
| 6247 | * heap. GC is not invoked prior to iterating, therefore there is no |
| 6248 | * guarantee that visited objects are still alive. |
| 6249 | */ |
| 6250 | void VisitExternalResources(ExternalResourceVisitor* visitor); |
| 6251 | |
| 6252 | /** |
| 6253 | * Iterates through all the persistent handles in the current isolate's heap |
| 6254 | * that have class_ids. |
| 6255 | */ |
| 6256 | void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor); |
| 6257 | |
| 6258 | /** |
| 6259 | * Iterates through all the persistent handles in the current isolate's heap |
| 6260 | * that have class_ids and are candidates to be marked as partially dependent |
| 6261 | * handles. This will visit handles to young objects created since the last |
| 6262 | * garbage collection but is free to visit an arbitrary superset of these |
| 6263 | * objects. |
| 6264 | */ |
| 6265 | void VisitHandlesForPartialDependence(PersistentHandleVisitor* visitor); |
| 6266 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6267 | /** |
| 6268 | * Iterates through all the persistent handles in the current isolate's heap |
| 6269 | * that have class_ids and are weak to be marked as inactive if there is no |
| 6270 | * pending activity for the handle. |
| 6271 | */ |
| 6272 | void VisitWeakHandles(PersistentHandleVisitor* visitor); |
| 6273 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6274 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6275 | template <class K, class V, class Traits> |
| 6276 | friend class PersistentValueMapBase; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6277 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6278 | Isolate(); |
| 6279 | Isolate(const Isolate&); |
| 6280 | ~Isolate(); |
| 6281 | Isolate& operator=(const Isolate&); |
| 6282 | void* operator new(size_t size); |
| 6283 | void operator delete(void*, size_t); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6284 | |
| 6285 | void SetObjectGroupId(internal::Object** object, UniqueId id); |
| 6286 | void SetReferenceFromGroup(UniqueId id, internal::Object** object); |
| 6287 | void SetReference(internal::Object** parent, internal::Object** child); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6288 | void ReportExternalAllocationLimitReached(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6289 | }; |
| 6290 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6291 | class V8_EXPORT StartupData { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6292 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6293 | const char* data; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6294 | int raw_size; |
| 6295 | }; |
| 6296 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6297 | |
| 6298 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6299 | * EntropySource is used as a callback function when v8 needs a source |
| 6300 | * of entropy. |
| 6301 | */ |
| 6302 | typedef bool (*EntropySource)(unsigned char* buffer, size_t length); |
| 6303 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6304 | |
| 6305 | /** |
| 6306 | * ReturnAddressLocationResolver is used as a callback function when v8 is |
| 6307 | * resolving the location of a return address on the stack. Profilers that |
| 6308 | * change the return address on the stack can use this to resolve the stack |
| 6309 | * location to whereever the profiler stashed the original return address. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6310 | * |
| 6311 | * \param return_addr_location points to a location on stack where a machine |
| 6312 | * return address resides. |
| 6313 | * \returns either return_addr_location, or else a pointer to the profiler's |
| 6314 | * copy of the original return address. |
| 6315 | * |
| 6316 | * \note the resolver function must not cause garbage collection. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6317 | */ |
| 6318 | typedef uintptr_t (*ReturnAddressLocationResolver)( |
| 6319 | uintptr_t return_addr_location); |
| 6320 | |
| 6321 | |
| 6322 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6323 | * Container class for static utility functions. |
| 6324 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6325 | class V8_EXPORT V8 { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6326 | public: |
| 6327 | /** Set the callback to invoke in case of fatal errors. */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6328 | V8_INLINE static V8_DEPRECATED( |
| 6329 | "Use isolate version", |
| 6330 | void SetFatalErrorHandler(FatalErrorCallback that)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6331 | |
| 6332 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6333 | * Set the callback to invoke to check if code generation from |
| 6334 | * strings should be allowed. |
| 6335 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6336 | V8_INLINE static V8_DEPRECATED( |
| 6337 | "Use isolate version", void SetAllowCodeGenerationFromStringsCallback( |
| 6338 | AllowCodeGenerationFromStringsCallback that)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6339 | |
| 6340 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6341 | * Check if V8 is dead and therefore unusable. This is the case after |
| 6342 | * fatal errors such as out-of-memory situations. |
| 6343 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6344 | V8_INLINE static V8_DEPRECATED("Use isolate version", bool IsDead()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6345 | |
| 6346 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6347 | * Hand startup data to V8, in case the embedder has chosen to build |
| 6348 | * V8 with external startup data. |
| 6349 | * |
| 6350 | * Note: |
| 6351 | * - By default the startup data is linked into the V8 library, in which |
| 6352 | * case this function is not meaningful. |
| 6353 | * - If this needs to be called, it needs to be called before V8 |
| 6354 | * tries to make use of its built-ins. |
| 6355 | * - To avoid unnecessary copies of data, V8 will point directly into the |
| 6356 | * given data blob, so pretty please keep it around until V8 exit. |
| 6357 | * - Compression of the startup blob might be useful, but needs to |
| 6358 | * handled entirely on the embedders' side. |
| 6359 | * - The call will abort if the data is invalid. |
| 6360 | */ |
| 6361 | static void SetNativesDataBlob(StartupData* startup_blob); |
| 6362 | static void SetSnapshotDataBlob(StartupData* startup_blob); |
| 6363 | |
| 6364 | /** |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6365 | * Bootstrap an isolate and a context from scratch to create a startup |
| 6366 | * snapshot. Include the side-effects of running the optional script. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6367 | * Returns { NULL, 0 } on failure. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6368 | * The caller acquires ownership of the data array in the return value. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6369 | */ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6370 | static StartupData CreateSnapshotDataBlob(const char* embedded_source = NULL); |
| 6371 | |
| 6372 | /** |
| 6373 | * Bootstrap an isolate and a context from the cold startup blob, run the |
| 6374 | * warm-up script to trigger code compilation. The side effects are then |
| 6375 | * discarded. The resulting startup snapshot will include compiled code. |
| 6376 | * Returns { NULL, 0 } on failure. |
| 6377 | * The caller acquires ownership of the data array in the return value. |
| 6378 | * The argument startup blob is untouched. |
| 6379 | */ |
| 6380 | static StartupData WarmUpSnapshotDataBlob(StartupData cold_startup_blob, |
| 6381 | const char* warmup_source); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6382 | |
| 6383 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6384 | * Adds a message listener. |
| 6385 | * |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6386 | * 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] | 6387 | * case it will be called more than once for each message. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6388 | * |
| 6389 | * If data is specified, it will be passed to the callback when it is called. |
| 6390 | * Otherwise, the exception object will be passed to the callback instead. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6391 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6392 | V8_INLINE static V8_DEPRECATED( |
| 6393 | "Use isolate version", |
| 6394 | bool AddMessageListener(MessageCallback that, |
| 6395 | Local<Value> data = Local<Value>())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6396 | |
| 6397 | /** |
| 6398 | * Remove all message listeners from the specified callback function. |
| 6399 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6400 | V8_INLINE static V8_DEPRECATED( |
| 6401 | "Use isolate version", void RemoveMessageListeners(MessageCallback that)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6402 | |
| 6403 | /** |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6404 | * Tells V8 to capture current stack trace when uncaught exception occurs |
| 6405 | * and report it to the message listeners. The option is off by default. |
| 6406 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6407 | V8_INLINE static V8_DEPRECATED( |
| 6408 | "Use isolate version", |
| 6409 | void SetCaptureStackTraceForUncaughtExceptions( |
| 6410 | bool capture, int frame_limit = 10, |
| 6411 | StackTrace::StackTraceOptions options = StackTrace::kOverview)); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 6412 | |
| 6413 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6414 | * Sets V8 flags from a string. |
| 6415 | */ |
| 6416 | static void SetFlagsFromString(const char* str, int length); |
| 6417 | |
| 6418 | /** |
| 6419 | * Sets V8 flags from the command line. |
| 6420 | */ |
| 6421 | static void SetFlagsFromCommandLine(int* argc, |
| 6422 | char** argv, |
| 6423 | bool remove_flags); |
| 6424 | |
| 6425 | /** Get the version string. */ |
| 6426 | static const char* GetVersion(); |
| 6427 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6428 | /** Callback function for reporting failed access checks.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6429 | V8_INLINE static V8_DEPRECATED( |
| 6430 | "Use isolate version", |
| 6431 | void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6432 | |
| 6433 | /** |
| 6434 | * Enables the host application to receive a notification before a |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6435 | * garbage collection. Allocations are not allowed in the |
| 6436 | * callback function, you therefore cannot manipulate objects (set |
| 6437 | * or delete properties for example) since it is possible such |
| 6438 | * operations will result in the allocation of objects. It is possible |
| 6439 | * to specify the GCType filter for your callback. But it is not possible to |
| 6440 | * register the same callback function two times with different |
| 6441 | * GCType filters. |
| 6442 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6443 | static V8_DEPRECATED( |
| 6444 | "Use isolate version", |
| 6445 | void AddGCPrologueCallback(GCCallback callback, |
| 6446 | GCType gc_type_filter = kGCTypeAll)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6447 | |
| 6448 | /** |
| 6449 | * This function removes callback which was installed by |
| 6450 | * AddGCPrologueCallback function. |
| 6451 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6452 | V8_INLINE static V8_DEPRECATED( |
| 6453 | "Use isolate version", |
| 6454 | void RemoveGCPrologueCallback(GCCallback callback)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6455 | |
| 6456 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6457 | * Enables the host application to receive a notification after a |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6458 | * garbage collection. Allocations are not allowed in the |
| 6459 | * callback function, you therefore cannot manipulate objects (set |
| 6460 | * or delete properties for example) since it is possible such |
| 6461 | * operations will result in the allocation of objects. It is possible |
| 6462 | * to specify the GCType filter for your callback. But it is not possible to |
| 6463 | * register the same callback function two times with different |
| 6464 | * GCType filters. |
| 6465 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6466 | static V8_DEPRECATED( |
| 6467 | "Use isolate version", |
| 6468 | void AddGCEpilogueCallback(GCCallback callback, |
| 6469 | GCType gc_type_filter = kGCTypeAll)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6470 | |
| 6471 | /** |
| 6472 | * This function removes callback which was installed by |
| 6473 | * AddGCEpilogueCallback function. |
| 6474 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6475 | V8_INLINE static V8_DEPRECATED( |
| 6476 | "Use isolate version", |
| 6477 | void RemoveGCEpilogueCallback(GCCallback callback)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6478 | |
| 6479 | /** |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6480 | * Enables the host application to provide a mechanism to be notified |
| 6481 | * and perform custom logging when V8 Allocates Executable Memory. |
| 6482 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6483 | V8_INLINE static V8_DEPRECATED( |
| 6484 | "Use isolate version", |
| 6485 | void AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 6486 | ObjectSpace space, |
| 6487 | AllocationAction action)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6488 | |
| 6489 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6490 | * Removes callback that was installed by AddMemoryAllocationCallback. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6491 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6492 | V8_INLINE static V8_DEPRECATED( |
| 6493 | "Use isolate version", |
| 6494 | void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 6495 | |
| 6496 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6497 | * Initializes V8. This function needs to be called before the first Isolate |
| 6498 | * is created. It always returns true. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6499 | */ |
| 6500 | static bool Initialize(); |
| 6501 | |
| 6502 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6503 | * Allows the host application to provide a callback which can be used |
| 6504 | * as a source of entropy for random number generators. |
| 6505 | */ |
| 6506 | static void SetEntropySource(EntropySource source); |
| 6507 | |
| 6508 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6509 | * Allows the host application to provide a callback that allows v8 to |
| 6510 | * cooperate with a profiler that rewrites return addresses on stack. |
| 6511 | */ |
| 6512 | static void SetReturnAddressLocationResolver( |
| 6513 | ReturnAddressLocationResolver return_address_resolver); |
| 6514 | |
| 6515 | /** |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6516 | * Forcefully terminate the current thread of JavaScript execution |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6517 | * in the given isolate. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6518 | * |
| 6519 | * This method can be used by any thread even if that thread has not |
| 6520 | * acquired the V8 lock with a Locker object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6521 | * |
| 6522 | * \param isolate The isolate in which to terminate the current JS execution. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6523 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6524 | V8_INLINE static V8_DEPRECATED("Use isolate version", |
| 6525 | void TerminateExecution(Isolate* isolate)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6526 | |
| 6527 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6528 | * Is V8 terminating JavaScript execution. |
| 6529 | * |
| 6530 | * Returns true if JavaScript execution is currently terminating |
| 6531 | * because of a call to TerminateExecution. In that case there are |
| 6532 | * still JavaScript frames on the stack and the termination |
| 6533 | * exception is still active. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6534 | * |
| 6535 | * \param isolate The isolate in which to check. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6536 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6537 | V8_INLINE static V8_DEPRECATED( |
| 6538 | "Use isolate version", |
| 6539 | bool IsExecutionTerminating(Isolate* isolate = NULL)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6540 | |
| 6541 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6542 | * Resume execution capability in the given isolate, whose execution |
| 6543 | * was previously forcefully terminated using TerminateExecution(). |
| 6544 | * |
| 6545 | * When execution is forcefully terminated using TerminateExecution(), |
| 6546 | * the isolate can not resume execution until all JavaScript frames |
| 6547 | * have propagated the uncatchable exception which is generated. This |
| 6548 | * method allows the program embedding the engine to handle the |
| 6549 | * termination event and resume execution capability, even if |
| 6550 | * JavaScript frames remain on the stack. |
| 6551 | * |
| 6552 | * This method can be used by any thread even if that thread has not |
| 6553 | * acquired the V8 lock with a Locker object. |
| 6554 | * |
| 6555 | * \param isolate The isolate in which to resume execution capability. |
| 6556 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6557 | V8_INLINE static V8_DEPRECATED( |
| 6558 | "Use isolate version", void CancelTerminateExecution(Isolate* isolate)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6559 | |
| 6560 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6561 | * Releases any resources used by v8 and stops any utility threads |
| 6562 | * that may be running. Note that disposing v8 is permanent, it |
| 6563 | * cannot be reinitialized. |
| 6564 | * |
| 6565 | * It should generally not be necessary to dispose v8 before exiting |
| 6566 | * a process, this should happen automatically. It is only necessary |
| 6567 | * to use if the process needs the resources taken up by v8. |
| 6568 | */ |
| 6569 | static bool Dispose(); |
| 6570 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6571 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6572 | * Iterates through all external resources referenced from current isolate |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6573 | * heap. GC is not invoked prior to iterating, therefore there is no |
| 6574 | * guarantee that visited objects are still alive. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6575 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6576 | V8_INLINE static V8_DEPRECATED( |
| 6577 | "Use isolate version", |
| 6578 | void VisitExternalResources(ExternalResourceVisitor* visitor)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6579 | |
| 6580 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6581 | * Iterates through all the persistent handles in the current isolate's heap |
| 6582 | * that have class_ids. |
| 6583 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6584 | V8_INLINE static V8_DEPRECATED( |
| 6585 | "Use isolate version", |
| 6586 | void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6587 | |
| 6588 | /** |
| 6589 | * Iterates through all the persistent handles in isolate's heap that have |
| 6590 | * class_ids. |
| 6591 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6592 | V8_INLINE static V8_DEPRECATED( |
| 6593 | "Use isolate version", |
| 6594 | void VisitHandlesWithClassIds(Isolate* isolate, |
| 6595 | PersistentHandleVisitor* visitor)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6596 | |
| 6597 | /** |
| 6598 | * Iterates through all the persistent handles in the current isolate's heap |
| 6599 | * that have class_ids and are candidates to be marked as partially dependent |
| 6600 | * handles. This will visit handles to young objects created since the last |
| 6601 | * garbage collection but is free to visit an arbitrary superset of these |
| 6602 | * objects. |
| 6603 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6604 | V8_INLINE static V8_DEPRECATED( |
| 6605 | "Use isolate version", |
| 6606 | void VisitHandlesForPartialDependence(Isolate* isolate, |
| 6607 | PersistentHandleVisitor* visitor)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6608 | |
| 6609 | /** |
| 6610 | * Initialize the ICU library bundled with V8. The embedder should only |
| 6611 | * invoke this method when using the bundled ICU. Returns true on success. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6612 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6613 | * If V8 was compiled with the ICU data in an external file, the location |
| 6614 | * of the data file has to be provided. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6615 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6616 | static bool InitializeICU(const char* icu_data_file = NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6617 | |
| 6618 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6619 | * Initialize the external startup data. The embedder only needs to |
| 6620 | * invoke this method when external startup data was enabled in a build. |
| 6621 | * |
| 6622 | * If V8 was compiled with the startup data in an external file, then |
| 6623 | * V8 needs to be given those external files during startup. There are |
| 6624 | * three ways to do this: |
| 6625 | * - InitializeExternalStartupData(const char*) |
| 6626 | * This will look in the given directory for files "natives_blob.bin" |
| 6627 | * and "snapshot_blob.bin" - which is what the default build calls them. |
| 6628 | * - InitializeExternalStartupData(const char*, const char*) |
| 6629 | * As above, but will directly use the two given file names. |
| 6630 | * - Call SetNativesDataBlob, SetNativesDataBlob. |
| 6631 | * This will read the blobs from the given data structures and will |
| 6632 | * not perform any file IO. |
| 6633 | */ |
| 6634 | static void InitializeExternalStartupData(const char* directory_path); |
| 6635 | static void InitializeExternalStartupData(const char* natives_blob, |
| 6636 | const char* snapshot_blob); |
| 6637 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6638 | * Sets the v8::Platform to use. This should be invoked before V8 is |
| 6639 | * initialized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6640 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6641 | static void InitializePlatform(Platform* platform); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6642 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6643 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6644 | * Clears all references to the v8::Platform. This should be invoked after |
| 6645 | * V8 was disposed. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6646 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6647 | static void ShutdownPlatform(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6648 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6649 | private: |
| 6650 | V8(); |
| 6651 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6652 | static internal::Object** GlobalizeReference(internal::Isolate* isolate, |
| 6653 | internal::Object** handle); |
| 6654 | static internal::Object** CopyPersistent(internal::Object** handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6655 | static void DisposeGlobal(internal::Object** global_handle); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6656 | typedef WeakCallbackData<Value, void>::Callback WeakCallback; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 6657 | static void RegisterExternallyReferencedObject(internal::Object** object, |
| 6658 | internal::Isolate* isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6659 | static void MakeWeak(internal::Object** global_handle, void* data, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6660 | WeakCallback weak_callback); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6661 | static void MakeWeak(internal::Object** global_handle, void* data, |
| 6662 | WeakCallbackInfo<void>::Callback weak_callback, |
| 6663 | WeakCallbackType type); |
| 6664 | static void MakeWeak(internal::Object** global_handle, void* data, |
| 6665 | // Must be 0 or -1. |
| 6666 | int internal_field_index1, |
| 6667 | // Must be 1 or -1. |
| 6668 | int internal_field_index2, |
| 6669 | WeakCallbackInfo<void>::Callback weak_callback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6670 | static void* ClearWeak(internal::Object** global_handle); |
| 6671 | static void Eternalize(Isolate* isolate, |
| 6672 | Value* handle, |
| 6673 | int* index); |
| 6674 | static Local<Value> GetEternal(Isolate* isolate, int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6675 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6676 | static void FromJustIsNothing(); |
| 6677 | static void ToLocalEmpty(); |
| 6678 | static void InternalFieldOutOfBounds(int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6679 | template <class T> friend class Local; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6680 | template <class T> |
| 6681 | friend class MaybeLocal; |
| 6682 | template <class T> |
| 6683 | friend class Maybe; |
| 6684 | template <class T> |
| 6685 | friend class WeakCallbackInfo; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6686 | template <class T> friend class Eternal; |
| 6687 | template <class T> friend class PersistentBase; |
| 6688 | template <class T, class M> friend class Persistent; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6689 | friend class Context; |
| 6690 | }; |
| 6691 | |
| 6692 | |
| 6693 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6694 | * A simple Maybe type, representing an object which may or may not have a |
| 6695 | * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html. |
| 6696 | * |
| 6697 | * If an API method returns a Maybe<>, the API method can potentially fail |
| 6698 | * either because an exception is thrown, or because an exception is pending, |
| 6699 | * e.g. because a previous API call threw an exception that hasn't been caught |
| 6700 | * yet, or because a TerminateExecution exception was thrown. In that case, a |
| 6701 | * "Nothing" value is returned. |
| 6702 | */ |
| 6703 | template <class T> |
| 6704 | class Maybe { |
| 6705 | public: |
| 6706 | V8_INLINE bool IsNothing() const { return !has_value; } |
| 6707 | V8_INLINE bool IsJust() const { return has_value; } |
| 6708 | |
| 6709 | // Will crash if the Maybe<> is nothing. |
| 6710 | V8_INLINE T FromJust() const { |
| 6711 | if (V8_UNLIKELY(!IsJust())) V8::FromJustIsNothing(); |
| 6712 | return value; |
| 6713 | } |
| 6714 | |
| 6715 | V8_INLINE T FromMaybe(const T& default_value) const { |
| 6716 | return has_value ? value : default_value; |
| 6717 | } |
| 6718 | |
| 6719 | V8_INLINE bool operator==(const Maybe& other) const { |
| 6720 | return (IsJust() == other.IsJust()) && |
| 6721 | (!IsJust() || FromJust() == other.FromJust()); |
| 6722 | } |
| 6723 | |
| 6724 | V8_INLINE bool operator!=(const Maybe& other) const { |
| 6725 | return !operator==(other); |
| 6726 | } |
| 6727 | |
| 6728 | private: |
| 6729 | Maybe() : has_value(false) {} |
| 6730 | explicit Maybe(const T& t) : has_value(true), value(t) {} |
| 6731 | |
| 6732 | bool has_value; |
| 6733 | T value; |
| 6734 | |
| 6735 | template <class U> |
| 6736 | friend Maybe<U> Nothing(); |
| 6737 | template <class U> |
| 6738 | friend Maybe<U> Just(const U& u); |
| 6739 | }; |
| 6740 | |
| 6741 | |
| 6742 | template <class T> |
| 6743 | inline Maybe<T> Nothing() { |
| 6744 | return Maybe<T>(); |
| 6745 | } |
| 6746 | |
| 6747 | |
| 6748 | template <class T> |
| 6749 | inline Maybe<T> Just(const T& t) { |
| 6750 | return Maybe<T>(t); |
| 6751 | } |
| 6752 | |
| 6753 | |
| 6754 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6755 | * An external exception handler. |
| 6756 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6757 | class V8_EXPORT TryCatch { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6758 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6759 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6760 | * Creates a new try/catch block and registers it with v8. Note that |
| 6761 | * all TryCatch blocks should be stack allocated because the memory |
| 6762 | * location itself is compared against JavaScript try/catch blocks. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6763 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6764 | V8_DEPRECATED("Use isolate version", TryCatch()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6765 | |
| 6766 | /** |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 6767 | * Creates a new try/catch block and registers it with v8. Note that |
| 6768 | * all TryCatch blocks should be stack allocated because the memory |
| 6769 | * location itself is compared against JavaScript try/catch blocks. |
| 6770 | */ |
| 6771 | TryCatch(Isolate* isolate); |
| 6772 | |
| 6773 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6774 | * Unregisters and deletes this try/catch block. |
| 6775 | */ |
| 6776 | ~TryCatch(); |
| 6777 | |
| 6778 | /** |
| 6779 | * Returns true if an exception has been caught by this try/catch block. |
| 6780 | */ |
| 6781 | bool HasCaught() const; |
| 6782 | |
| 6783 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6784 | * For certain types of exceptions, it makes no sense to continue execution. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6785 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6786 | * If CanContinue returns false, the correct action is to perform any C++ |
| 6787 | * cleanup needed and then return. If CanContinue returns false and |
| 6788 | * HasTerminated returns true, it is possible to call |
| 6789 | * CancelTerminateExecution in order to continue calling into the engine. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6790 | */ |
| 6791 | bool CanContinue() const; |
| 6792 | |
| 6793 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6794 | * Returns true if an exception has been caught due to script execution |
| 6795 | * being terminated. |
| 6796 | * |
| 6797 | * There is no JavaScript representation of an execution termination |
| 6798 | * exception. Such exceptions are thrown when the TerminateExecution |
| 6799 | * methods are called to terminate a long-running script. |
| 6800 | * |
| 6801 | * If such an exception has been thrown, HasTerminated will return true, |
| 6802 | * indicating that it is possible to call CancelTerminateExecution in order |
| 6803 | * to continue calling into the engine. |
| 6804 | */ |
| 6805 | bool HasTerminated() const; |
| 6806 | |
| 6807 | /** |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6808 | * Throws the exception caught by this TryCatch in a way that avoids |
| 6809 | * it being caught again by this same TryCatch. As with ThrowException |
| 6810 | * it is illegal to execute any JavaScript operations after calling |
| 6811 | * ReThrow; the caller must return immediately to where the exception |
| 6812 | * is caught. |
| 6813 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6814 | Local<Value> ReThrow(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6815 | |
| 6816 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6817 | * Returns the exception caught by this try/catch block. If no exception has |
| 6818 | * been caught an empty handle is returned. |
| 6819 | * |
| 6820 | * The returned handle is valid until this TryCatch block has been destroyed. |
| 6821 | */ |
| 6822 | Local<Value> Exception() const; |
| 6823 | |
| 6824 | /** |
| 6825 | * Returns the .stack property of the thrown object. If no .stack |
| 6826 | * property is present an empty handle is returned. |
| 6827 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6828 | V8_DEPRECATE_SOON("Use maybe version.", Local<Value> StackTrace() const); |
| 6829 | V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace( |
| 6830 | Local<Context> context) const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6831 | |
| 6832 | /** |
| 6833 | * Returns the message associated with this exception. If there is |
| 6834 | * no message associated an empty handle is returned. |
| 6835 | * |
| 6836 | * The returned handle is valid until this TryCatch block has been |
| 6837 | * destroyed. |
| 6838 | */ |
| 6839 | Local<v8::Message> Message() const; |
| 6840 | |
| 6841 | /** |
| 6842 | * Clears any exceptions that may have been caught by this try/catch block. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6843 | * After this method has been called, HasCaught() will return false. Cancels |
| 6844 | * the scheduled exception if it is caught and ReThrow() is not called before. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6845 | * |
| 6846 | * It is not necessary to clear a try/catch block before using it again; if |
| 6847 | * another exception is thrown the previously caught exception will just be |
| 6848 | * overwritten. However, it is often a good idea since it makes it easier |
| 6849 | * to determine which operation threw a given exception. |
| 6850 | */ |
| 6851 | void Reset(); |
| 6852 | |
| 6853 | /** |
| 6854 | * Set verbosity of the external exception handler. |
| 6855 | * |
| 6856 | * By default, exceptions that are caught by an external exception |
| 6857 | * handler are not reported. Call SetVerbose with true on an |
| 6858 | * external exception handler to have exceptions caught by the |
| 6859 | * handler reported as if they were not caught. |
| 6860 | */ |
| 6861 | void SetVerbose(bool value); |
| 6862 | |
| 6863 | /** |
| 6864 | * Set whether or not this TryCatch should capture a Message object |
| 6865 | * which holds source information about where the exception |
| 6866 | * occurred. True by default. |
| 6867 | */ |
| 6868 | void SetCaptureMessage(bool value); |
| 6869 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6870 | /** |
| 6871 | * There are cases when the raw address of C++ TryCatch object cannot be |
| 6872 | * used for comparisons with addresses into the JS stack. The cases are: |
| 6873 | * 1) ARM, ARM64 and MIPS simulators which have separate JS stack. |
| 6874 | * 2) Address sanitizer allocates local C++ object in the heap when |
| 6875 | * UseAfterReturn mode is enabled. |
| 6876 | * This method returns address that can be used for comparisons with |
| 6877 | * addresses into the JS stack. When neither simulator nor ASAN's |
| 6878 | * UseAfterReturn is enabled, then the address returned will be the address |
| 6879 | * of the C++ try catch handler itself. |
| 6880 | */ |
| 6881 | static void* JSStackComparableAddress(v8::TryCatch* handler) { |
| 6882 | if (handler == NULL) return NULL; |
| 6883 | return handler->js_stack_comparable_address_; |
| 6884 | } |
| 6885 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6886 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6887 | void ResetInternal(); |
| 6888 | |
| 6889 | // Make it hard to create heap-allocated TryCatch blocks. |
| 6890 | TryCatch(const TryCatch&); |
| 6891 | void operator=(const TryCatch&); |
| 6892 | void* operator new(size_t size); |
| 6893 | void operator delete(void*, size_t); |
| 6894 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 6895 | v8::internal::Isolate* isolate_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6896 | v8::TryCatch* next_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6897 | void* exception_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6898 | void* message_obj_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6899 | void* js_stack_comparable_address_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6900 | bool is_verbose_ : 1; |
| 6901 | bool can_continue_ : 1; |
| 6902 | bool capture_message_ : 1; |
| 6903 | bool rethrow_ : 1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6904 | bool has_terminated_ : 1; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6905 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6906 | friend class v8::internal::Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6907 | }; |
| 6908 | |
| 6909 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6910 | // --- Context --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6911 | |
| 6912 | |
| 6913 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6914 | * A container for extension names. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6915 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6916 | class V8_EXPORT ExtensionConfiguration { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6917 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6918 | ExtensionConfiguration() : name_count_(0), names_(NULL) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6919 | ExtensionConfiguration(int name_count, const char* names[]) |
| 6920 | : name_count_(name_count), names_(names) { } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6921 | |
| 6922 | const char** begin() const { return &names_[0]; } |
| 6923 | const char** end() const { return &names_[name_count_]; } |
| 6924 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6925 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6926 | const int name_count_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6927 | const char** names_; |
| 6928 | }; |
| 6929 | |
| 6930 | |
| 6931 | /** |
| 6932 | * A sandboxed execution context with its own set of built-in objects |
| 6933 | * and functions. |
| 6934 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6935 | class V8_EXPORT Context { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6936 | public: |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6937 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6938 | * Returns the global proxy object. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6939 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6940 | * Global proxy object is a thin wrapper whose prototype points to actual |
| 6941 | * context's global object with the properties like Object, etc. This is done |
| 6942 | * that way for security reasons (for more details see |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6943 | * https://wiki.mozilla.org/Gecko:SplitWindow). |
| 6944 | * |
| 6945 | * Please note that changes to global proxy object prototype most probably |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6946 | * would break VM---v8 expects only global object as a prototype of global |
| 6947 | * proxy object. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6948 | */ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6949 | Local<Object> Global(); |
| 6950 | |
| 6951 | /** |
| 6952 | * Detaches the global object from its context before |
| 6953 | * the global object can be reused to create a new context. |
| 6954 | */ |
| 6955 | void DetachGlobal(); |
| 6956 | |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 6957 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6958 | * Creates a new context and returns a handle to the newly allocated |
| 6959 | * context. |
Andrei Popescu | 74b3c14 | 2010-03-29 12:03:09 +0100 | [diff] [blame] | 6960 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6961 | * \param isolate The isolate in which to create the context. |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 6962 | * |
| 6963 | * \param extensions An optional extension configuration containing |
| 6964 | * the extensions to be installed in the newly created context. |
| 6965 | * |
| 6966 | * \param global_template An optional object template from which the |
| 6967 | * global object for the newly created context will be created. |
| 6968 | * |
| 6969 | * \param global_object An optional global object to be reused for |
| 6970 | * the newly created context. This global object must have been |
| 6971 | * created by a previous call to Context::New with the same global |
| 6972 | * template. The state of the global object will be completely reset |
| 6973 | * and only object identify will remain. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6974 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6975 | static Local<Context> New( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6976 | Isolate* isolate, ExtensionConfiguration* extensions = NULL, |
| 6977 | Local<ObjectTemplate> global_template = Local<ObjectTemplate>(), |
| 6978 | Local<Value> global_object = Local<Value>()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6979 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6980 | /** |
| 6981 | * Sets the security token for the context. To access an object in |
| 6982 | * another context, the security tokens must match. |
| 6983 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6984 | void SetSecurityToken(Local<Value> token); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6985 | |
| 6986 | /** Restores the security token to the default value. */ |
| 6987 | void UseDefaultSecurityToken(); |
| 6988 | |
| 6989 | /** Returns the security token of this context.*/ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6990 | Local<Value> GetSecurityToken(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6991 | |
| 6992 | /** |
| 6993 | * Enter this context. After entering a context, all code compiled |
| 6994 | * and run is compiled and run in this context. If another context |
| 6995 | * is already entered, this old context is saved so it can be |
| 6996 | * restored when the new context is exited. |
| 6997 | */ |
| 6998 | void Enter(); |
| 6999 | |
| 7000 | /** |
| 7001 | * Exit this context. Exiting the current context restores the |
| 7002 | * context that was in place when entering the current context. |
| 7003 | */ |
| 7004 | void Exit(); |
| 7005 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7006 | /** Returns an isolate associated with a current context. */ |
| 7007 | v8::Isolate* GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7008 | |
| 7009 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7010 | * The field at kDebugIdIndex is reserved for V8 debugger implementation. |
| 7011 | * The value is propagated to the scripts compiled in given Context and |
| 7012 | * can be used for filtering scripts. |
| 7013 | */ |
| 7014 | enum EmbedderDataFields { kDebugIdIndex = 0 }; |
| 7015 | |
| 7016 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7017 | * Gets the embedder data with the given index, which must have been set by a |
| 7018 | * previous call to SetEmbedderData with the same index. Note that index 0 |
| 7019 | * currently has a special meaning for Chrome's debugger. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7020 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7021 | V8_INLINE Local<Value> GetEmbedderData(int index); |
| 7022 | |
| 7023 | /** |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7024 | * Gets the binding object used by V8 extras. Extra natives get a reference |
| 7025 | * to this object and can use it to "export" functionality by adding |
| 7026 | * properties. Extra natives can also "import" functionality by accessing |
| 7027 | * properties added by the embedder using the V8 API. |
| 7028 | */ |
| 7029 | Local<Object> GetExtrasBindingObject(); |
| 7030 | |
| 7031 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7032 | * Sets the embedder data with the given index, growing the data as |
| 7033 | * needed. Note that index 0 currently has a special meaning for Chrome's |
| 7034 | * debugger. |
| 7035 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7036 | void SetEmbedderData(int index, Local<Value> value); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7037 | |
| 7038 | /** |
| 7039 | * Gets a 2-byte-aligned native pointer from the embedder data with the given |
| 7040 | * index, which must have bees set by a previous call to |
| 7041 | * SetAlignedPointerInEmbedderData with the same index. Note that index 0 |
| 7042 | * currently has a special meaning for Chrome's debugger. |
| 7043 | */ |
| 7044 | V8_INLINE void* GetAlignedPointerFromEmbedderData(int index); |
| 7045 | |
| 7046 | /** |
| 7047 | * Sets a 2-byte-aligned native pointer in the embedder data with the given |
| 7048 | * index, growing the data as needed. Note that index 0 currently has a |
| 7049 | * special meaning for Chrome's debugger. |
| 7050 | */ |
| 7051 | void SetAlignedPointerInEmbedderData(int index, void* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7052 | |
| 7053 | /** |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7054 | * Control whether code generation from strings is allowed. Calling |
| 7055 | * this method with false will disable 'eval' and the 'Function' |
| 7056 | * constructor for code running in this context. If 'eval' or the |
| 7057 | * 'Function' constructor are used an exception will be thrown. |
| 7058 | * |
| 7059 | * If code generation from strings is not allowed the |
| 7060 | * V8::AllowCodeGenerationFromStrings callback will be invoked if |
| 7061 | * set before blocking the call to 'eval' or the 'Function' |
| 7062 | * constructor. If that callback returns true, the call will be |
| 7063 | * allowed, otherwise an exception will be thrown. If no callback is |
| 7064 | * set an exception will be thrown. |
| 7065 | */ |
| 7066 | void AllowCodeGenerationFromStrings(bool allow); |
| 7067 | |
| 7068 | /** |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7069 | * Returns true if code generation from strings is allowed for the context. |
| 7070 | * For more details see AllowCodeGenerationFromStrings(bool) documentation. |
| 7071 | */ |
| 7072 | bool IsCodeGenerationFromStringsAllowed(); |
| 7073 | |
| 7074 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7075 | * Sets the error description for the exception that is thrown when |
| 7076 | * code generation from strings is not allowed and 'eval' or the 'Function' |
| 7077 | * constructor are called. |
| 7078 | */ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7079 | void SetErrorMessageForCodeGenerationFromStrings(Local<String> message); |
| 7080 | |
| 7081 | /** |
| 7082 | * Estimate the memory in bytes retained by this context. |
| 7083 | */ |
| 7084 | size_t EstimatedSize(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7085 | |
| 7086 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7087 | * Stack-allocated class which sets the execution context for all |
| 7088 | * operations executed within a local scope. |
| 7089 | */ |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7090 | class Scope { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7091 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7092 | explicit V8_INLINE Scope(Local<Context> context) : context_(context) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7093 | context_->Enter(); |
| 7094 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7095 | V8_INLINE ~Scope() { context_->Exit(); } |
| 7096 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7097 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7098 | Local<Context> context_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7099 | }; |
| 7100 | |
| 7101 | private: |
| 7102 | friend class Value; |
| 7103 | friend class Script; |
| 7104 | friend class Object; |
| 7105 | friend class Function; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7106 | |
| 7107 | Local<Value> SlowGetEmbedderData(int index); |
| 7108 | void* SlowGetAlignedPointerFromEmbedderData(int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7109 | }; |
| 7110 | |
| 7111 | |
| 7112 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7113 | * Multiple threads in V8 are allowed, but only one thread at a time is allowed |
| 7114 | * to use any given V8 isolate, see the comments in the Isolate class. The |
| 7115 | * definition of 'using a V8 isolate' includes accessing handles or holding onto |
| 7116 | * object pointers obtained from V8 handles while in the particular V8 isolate. |
| 7117 | * It is up to the user of V8 to ensure, perhaps with locking, that this |
| 7118 | * constraint is not violated. In addition to any other synchronization |
| 7119 | * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be |
| 7120 | * used to signal thead switches to V8. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7121 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7122 | * v8::Locker is a scoped lock object. While it's active, i.e. between its |
| 7123 | * construction and destruction, the current thread is allowed to use the locked |
| 7124 | * isolate. V8 guarantees that an isolate can be locked by at most one thread at |
| 7125 | * any time. In other words, the scope of a v8::Locker is a critical section. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7126 | * |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7127 | * Sample usage: |
| 7128 | * \code |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7129 | * ... |
| 7130 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7131 | * v8::Locker locker(isolate); |
| 7132 | * v8::Isolate::Scope isolate_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7133 | * ... |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7134 | * // Code using V8 and isolate goes here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7135 | * ... |
| 7136 | * } // Destructor called here |
| 7137 | * \endcode |
| 7138 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7139 | * If you wish to stop using V8 in a thread A you can do this either by |
| 7140 | * destroying the v8::Locker object as above or by constructing a v8::Unlocker |
| 7141 | * object: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7142 | * |
| 7143 | * \code |
| 7144 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7145 | * isolate->Exit(); |
| 7146 | * v8::Unlocker unlocker(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7147 | * ... |
| 7148 | * // Code not using V8 goes here while V8 can run in another thread. |
| 7149 | * ... |
| 7150 | * } // Destructor called here. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7151 | * isolate->Enter(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7152 | * \endcode |
| 7153 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7154 | * The Unlocker object is intended for use in a long-running callback from V8, |
| 7155 | * where you want to release the V8 lock for other threads to use. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7156 | * |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7157 | * The v8::Locker is a recursive lock, i.e. you can lock more than once in a |
| 7158 | * given thread. This can be useful if you have code that can be called either |
| 7159 | * from code that holds the lock or from code that does not. The Unlocker is |
| 7160 | * not recursive so you can not have several Unlockers on the stack at once, and |
| 7161 | * you can not use an Unlocker in a thread that is not inside a Locker's scope. |
Steve 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 | * An unlocker will unlock several lockers if it has to and reinstate the |
| 7164 | * correct depth of locking on its destruction, e.g.: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7165 | * |
| 7166 | * \code |
| 7167 | * // V8 not locked. |
| 7168 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7169 | * v8::Locker locker(isolate); |
| 7170 | * Isolate::Scope isolate_scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7171 | * // V8 locked. |
| 7172 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7173 | * v8::Locker another_locker(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7174 | * // V8 still locked (2 levels). |
| 7175 | * { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7176 | * isolate->Exit(); |
| 7177 | * v8::Unlocker unlocker(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7178 | * // V8 not locked. |
| 7179 | * } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7180 | * isolate->Enter(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7181 | * // V8 locked again (2 levels). |
| 7182 | * } |
| 7183 | * // V8 still locked (1 level). |
| 7184 | * } |
| 7185 | * // V8 Now no longer locked. |
| 7186 | * \endcode |
| 7187 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7188 | class V8_EXPORT Unlocker { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7189 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7190 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7191 | * Initialize Unlocker for a given Isolate. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7192 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7193 | V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); } |
| 7194 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7195 | ~Unlocker(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7196 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7197 | void Initialize(Isolate* isolate); |
| 7198 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7199 | internal::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7200 | }; |
| 7201 | |
| 7202 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7203 | class V8_EXPORT Locker { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7204 | public: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7205 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7206 | * Initialize Locker for a given Isolate. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7207 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7208 | V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); } |
| 7209 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7210 | ~Locker(); |
| 7211 | |
| 7212 | /** |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7213 | * Returns whether or not the locker for a given isolate, is locked by the |
| 7214 | * current thread. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7215 | */ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7216 | static bool IsLocked(Isolate* isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7217 | |
| 7218 | /** |
| 7219 | * Returns whether v8::Locker is being used by this V8 instance. |
| 7220 | */ |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 7221 | static bool IsActive(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7222 | |
| 7223 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7224 | void Initialize(Isolate* isolate); |
| 7225 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7226 | bool has_lock_; |
| 7227 | bool top_level_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7228 | internal::Isolate* isolate_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7229 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7230 | // Disallow copying and assigning. |
| 7231 | Locker(const Locker&); |
| 7232 | void operator=(const Locker&); |
| 7233 | }; |
| 7234 | |
| 7235 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7236 | // --- Implementation --- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7237 | |
| 7238 | |
| 7239 | namespace internal { |
| 7240 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 7241 | const int kApiPointerSize = sizeof(void*); // NOLINT |
| 7242 | const int kApiIntSize = sizeof(int); // NOLINT |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7243 | const int kApiInt64Size = sizeof(int64_t); // NOLINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7244 | |
| 7245 | // Tag information for HeapObject. |
| 7246 | const int kHeapObjectTag = 1; |
| 7247 | const int kHeapObjectTagSize = 2; |
| 7248 | const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; |
| 7249 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7250 | // Tag information for Smi. |
| 7251 | const int kSmiTag = 0; |
| 7252 | const int kSmiTagSize = 1; |
| 7253 | const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1; |
| 7254 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7255 | template <size_t ptr_size> struct SmiTagging; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7256 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7257 | template<int kSmiShiftSize> |
| 7258 | V8_INLINE internal::Object* IntToSmi(int value) { |
| 7259 | int smi_shift_bits = kSmiTagSize + kSmiShiftSize; |
| 7260 | uintptr_t tagged_value = |
| 7261 | (static_cast<uintptr_t>(value) << smi_shift_bits) | kSmiTag; |
| 7262 | return reinterpret_cast<internal::Object*>(tagged_value); |
| 7263 | } |
| 7264 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7265 | // Smi constants for 32-bit systems. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7266 | template <> struct SmiTagging<4> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7267 | enum { kSmiShiftSize = 0, kSmiValueSize = 31 }; |
| 7268 | static int SmiShiftSize() { return kSmiShiftSize; } |
| 7269 | static int SmiValueSize() { return kSmiValueSize; } |
| 7270 | V8_INLINE static int SmiToInt(const internal::Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7271 | int shift_bits = kSmiTagSize + kSmiShiftSize; |
| 7272 | // Throw away top 32 bits and shift down (requires >> to be sign extending). |
| 7273 | return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits; |
| 7274 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7275 | V8_INLINE static internal::Object* IntToSmi(int value) { |
| 7276 | return internal::IntToSmi<kSmiShiftSize>(value); |
| 7277 | } |
| 7278 | V8_INLINE static bool IsValidSmi(intptr_t value) { |
| 7279 | // To be representable as an tagged small integer, the two |
| 7280 | // most-significant bits of 'value' must be either 00 or 11 due to |
| 7281 | // sign-extension. To check this we add 01 to the two |
| 7282 | // most-significant bits, and check if the most-significant bit is 0 |
| 7283 | // |
| 7284 | // CAUTION: The original code below: |
| 7285 | // bool result = ((value + 0x40000000) & 0x80000000) == 0; |
| 7286 | // may lead to incorrect results according to the C language spec, and |
| 7287 | // in fact doesn't work correctly with gcc4.1.1 in some cases: The |
| 7288 | // compiler may produce undefined results in case of signed integer |
| 7289 | // overflow. The computation must be done w/ unsigned ints. |
| 7290 | return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U; |
| 7291 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7292 | }; |
| 7293 | |
| 7294 | // Smi constants for 64-bit systems. |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7295 | template <> struct SmiTagging<8> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7296 | enum { kSmiShiftSize = 31, kSmiValueSize = 32 }; |
| 7297 | static int SmiShiftSize() { return kSmiShiftSize; } |
| 7298 | static int SmiValueSize() { return kSmiValueSize; } |
| 7299 | V8_INLINE static int SmiToInt(const internal::Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7300 | int shift_bits = kSmiTagSize + kSmiShiftSize; |
| 7301 | // Shift down and throw away top 32 bits. |
| 7302 | return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits); |
| 7303 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7304 | V8_INLINE static internal::Object* IntToSmi(int value) { |
| 7305 | return internal::IntToSmi<kSmiShiftSize>(value); |
| 7306 | } |
| 7307 | V8_INLINE static bool IsValidSmi(intptr_t value) { |
| 7308 | // To be representable as a long smi, the value must be a 32-bit integer. |
| 7309 | return (value == static_cast<int32_t>(value)); |
| 7310 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7311 | }; |
| 7312 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7313 | typedef SmiTagging<kApiPointerSize> PlatformSmiTagging; |
| 7314 | const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize; |
| 7315 | const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7316 | V8_INLINE static bool SmiValuesAre31Bits() { return kSmiValueSize == 31; } |
| 7317 | V8_INLINE static bool SmiValuesAre32Bits() { return kSmiValueSize == 32; } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7318 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7319 | /** |
| 7320 | * This class exports constants and functionality from within v8 that |
| 7321 | * is necessary to implement inline functions in the v8 api. Don't |
| 7322 | * depend on functions and constants defined here. |
| 7323 | */ |
| 7324 | class Internals { |
| 7325 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7326 | // These values match non-compiler-dependent values defined within |
| 7327 | // the implementation of v8. |
| 7328 | static const int kHeapObjectMapOffset = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7329 | static const int kMapInstanceTypeAndBitFieldOffset = |
| 7330 | 1 * kApiPointerSize + kApiIntSize; |
| 7331 | static const int kStringResourceOffset = 3 * kApiPointerSize; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7332 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7333 | static const int kOddballKindOffset = 5 * kApiPointerSize; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7334 | static const int kForeignAddressOffset = kApiPointerSize; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 7335 | static const int kJSObjectHeaderSize = 3 * kApiPointerSize; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7336 | static const int kFixedArrayHeaderSize = 2 * kApiPointerSize; |
| 7337 | static const int kContextHeaderSize = 2 * kApiPointerSize; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7338 | static const int kContextEmbedderDataIndex = 5; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7339 | static const int kFullStringRepresentationMask = 0x07; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7340 | static const int kStringEncodingMask = 0x4; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7341 | static const int kExternalTwoByteRepresentationTag = 0x02; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7342 | static const int kExternalOneByteRepresentationTag = 0x06; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7343 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7344 | static const int kIsolateEmbedderDataOffset = 0 * kApiPointerSize; |
| 7345 | static const int kAmountOfExternalAllocatedMemoryOffset = |
| 7346 | 4 * kApiPointerSize; |
| 7347 | static const int kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset = |
| 7348 | kAmountOfExternalAllocatedMemoryOffset + kApiInt64Size; |
| 7349 | static const int kIsolateRootsOffset = |
| 7350 | kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset + kApiInt64Size + |
| 7351 | kApiPointerSize; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7352 | static const int kUndefinedValueRootIndex = 4; |
| 7353 | static const int kTheHoleValueRootIndex = 5; |
| 7354 | static const int kNullValueRootIndex = 6; |
| 7355 | static const int kTrueValueRootIndex = 7; |
| 7356 | static const int kFalseValueRootIndex = 8; |
| 7357 | static const int kEmptyStringRootIndex = 9; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7358 | |
| 7359 | // The external allocation limit should be below 256 MB on all architectures |
| 7360 | // to avoid that resource-constrained embedders run low on memory. |
| 7361 | static const int kExternalAllocationLimit = 192 * 1024 * 1024; |
| 7362 | |
| 7363 | static const int kNodeClassIdOffset = 1 * kApiPointerSize; |
| 7364 | static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7365 | static const int kNodeStateMask = 0x7; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7366 | static const int kNodeStateIsWeakValue = 2; |
| 7367 | static const int kNodeStateIsPendingValue = 3; |
| 7368 | static const int kNodeStateIsNearDeathValue = 4; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7369 | static const int kNodeIsIndependentShift = 3; |
| 7370 | static const int kNodeIsPartiallyDependentShift = 4; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7371 | static const int kNodeIsActiveShift = 4; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7372 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7373 | static const int kJSObjectType = 0xb8; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 7374 | static const int kFirstNonstringType = 0x80; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7375 | static const int kOddballType = 0x83; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7376 | static const int kForeignType = 0x87; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7377 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7378 | static const int kUndefinedOddballKind = 5; |
| 7379 | static const int kNullOddballKind = 3; |
| 7380 | |
| 7381 | static const uint32_t kNumIsolateDataSlots = 4; |
| 7382 | |
| 7383 | V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate); |
| 7384 | V8_INLINE static void CheckInitialized(v8::Isolate* isolate) { |
| 7385 | #ifdef V8_ENABLE_CHECKS |
| 7386 | CheckInitializedImpl(isolate); |
| 7387 | #endif |
| 7388 | } |
| 7389 | |
| 7390 | V8_INLINE static bool HasHeapObjectTag(const internal::Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7391 | return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) == |
| 7392 | kHeapObjectTag); |
| 7393 | } |
| 7394 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7395 | V8_INLINE static int SmiValue(const internal::Object* value) { |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7396 | return PlatformSmiTagging::SmiToInt(value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7397 | } |
| 7398 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7399 | V8_INLINE static internal::Object* IntToSmi(int value) { |
| 7400 | return PlatformSmiTagging::IntToSmi(value); |
| 7401 | } |
| 7402 | |
| 7403 | V8_INLINE static bool IsValidSmi(intptr_t value) { |
| 7404 | return PlatformSmiTagging::IsValidSmi(value); |
| 7405 | } |
| 7406 | |
| 7407 | V8_INLINE static int GetInstanceType(const internal::Object* obj) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7408 | typedef internal::Object O; |
| 7409 | O* map = ReadField<O*>(obj, kHeapObjectMapOffset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7410 | // Map::InstanceType is defined so that it will always be loaded into |
| 7411 | // the LS 8 bits of one 16-bit word, regardless of endianess. |
| 7412 | return ReadField<uint16_t>(map, kMapInstanceTypeAndBitFieldOffset) & 0xff; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7413 | } |
| 7414 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7415 | V8_INLINE static int GetOddballKind(const internal::Object* obj) { |
| 7416 | typedef internal::Object O; |
| 7417 | return SmiValue(ReadField<O*>(obj, kOddballKindOffset)); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 7418 | } |
| 7419 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7420 | V8_INLINE static bool IsExternalTwoByteString(int instance_type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7421 | int representation = (instance_type & kFullStringRepresentationMask); |
| 7422 | return representation == kExternalTwoByteRepresentationTag; |
| 7423 | } |
| 7424 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7425 | V8_INLINE static uint8_t GetNodeFlag(internal::Object** obj, int shift) { |
| 7426 | uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset; |
| 7427 | return *addr & static_cast<uint8_t>(1U << shift); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7428 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7429 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7430 | V8_INLINE static void UpdateNodeFlag(internal::Object** obj, |
| 7431 | bool value, int shift) { |
| 7432 | uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset; |
| 7433 | uint8_t mask = static_cast<uint8_t>(1U << shift); |
| 7434 | *addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift)); |
| 7435 | } |
| 7436 | |
| 7437 | V8_INLINE static uint8_t GetNodeState(internal::Object** obj) { |
| 7438 | uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset; |
| 7439 | return *addr & kNodeStateMask; |
| 7440 | } |
| 7441 | |
| 7442 | V8_INLINE static void UpdateNodeState(internal::Object** obj, |
| 7443 | uint8_t value) { |
| 7444 | uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset; |
| 7445 | *addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value); |
| 7446 | } |
| 7447 | |
| 7448 | V8_INLINE static void SetEmbedderData(v8::Isolate* isolate, |
| 7449 | uint32_t slot, |
| 7450 | void* data) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7451 | uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7452 | kIsolateEmbedderDataOffset + slot * kApiPointerSize; |
| 7453 | *reinterpret_cast<void**>(addr) = data; |
| 7454 | } |
| 7455 | |
| 7456 | V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate, |
| 7457 | uint32_t slot) { |
| 7458 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(isolate) + |
| 7459 | kIsolateEmbedderDataOffset + slot * kApiPointerSize; |
| 7460 | return *reinterpret_cast<void* const*>(addr); |
| 7461 | } |
| 7462 | |
| 7463 | V8_INLINE static internal::Object** GetRoot(v8::Isolate* isolate, |
| 7464 | int index) { |
| 7465 | uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset; |
| 7466 | return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize); |
| 7467 | } |
| 7468 | |
| 7469 | template <typename T> |
| 7470 | V8_INLINE static T ReadField(const internal::Object* ptr, int offset) { |
| 7471 | const uint8_t* addr = |
| 7472 | reinterpret_cast<const uint8_t*>(ptr) + offset - kHeapObjectTag; |
| 7473 | return *reinterpret_cast<const T*>(addr); |
| 7474 | } |
| 7475 | |
| 7476 | template <typename T> |
| 7477 | V8_INLINE static T ReadEmbedderData(const v8::Context* context, int index) { |
| 7478 | typedef internal::Object O; |
| 7479 | typedef internal::Internals I; |
| 7480 | O* ctx = *reinterpret_cast<O* const*>(context); |
| 7481 | int embedder_data_offset = I::kContextHeaderSize + |
| 7482 | (internal::kApiPointerSize * I::kContextEmbedderDataIndex); |
| 7483 | O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset); |
| 7484 | int value_offset = |
| 7485 | I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index); |
| 7486 | return I::ReadField<T>(embedder_data, value_offset); |
| 7487 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7488 | }; |
| 7489 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7490 | } // namespace internal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7491 | |
| 7492 | |
| 7493 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7494 | Local<T> Local<T>::New(Isolate* isolate, Local<T> that) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7495 | return New(isolate, that.val_); |
| 7496 | } |
| 7497 | |
| 7498 | template <class T> |
| 7499 | Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) { |
| 7500 | return New(isolate, that.val_); |
| 7501 | } |
| 7502 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7503 | |
| 7504 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7505 | Local<T> Local<T>::New(Isolate* isolate, T* that) { |
| 7506 | if (that == NULL) return Local<T>(); |
| 7507 | T* that_ptr = that; |
| 7508 | internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr); |
| 7509 | return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle( |
| 7510 | reinterpret_cast<internal::Isolate*>(isolate), *p))); |
| 7511 | } |
| 7512 | |
| 7513 | |
| 7514 | template<class T> |
| 7515 | template<class S> |
| 7516 | void Eternal<T>::Set(Isolate* isolate, Local<S> handle) { |
| 7517 | TYPE_CHECK(T, S); |
| 7518 | V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle), &this->index_); |
| 7519 | } |
| 7520 | |
| 7521 | |
| 7522 | template<class T> |
| 7523 | Local<T> Eternal<T>::Get(Isolate* isolate) { |
| 7524 | return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7525 | } |
| 7526 | |
| 7527 | |
| 7528 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7529 | Local<T> MaybeLocal<T>::ToLocalChecked() { |
| 7530 | if (V8_UNLIKELY(val_ == nullptr)) V8::ToLocalEmpty(); |
| 7531 | return Local<T>(val_); |
| 7532 | } |
| 7533 | |
| 7534 | |
| 7535 | template <class T> |
| 7536 | void* WeakCallbackInfo<T>::GetInternalField(int index) const { |
| 7537 | #ifdef V8_ENABLE_CHECKS |
| 7538 | if (index < 0 || index >= kInternalFieldsInWeakCallback) { |
| 7539 | V8::InternalFieldOutOfBounds(index); |
| 7540 | } |
| 7541 | #endif |
| 7542 | return internal_fields_[index]; |
| 7543 | } |
| 7544 | |
| 7545 | |
| 7546 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7547 | T* PersistentBase<T>::New(Isolate* isolate, T* that) { |
| 7548 | if (that == NULL) return NULL; |
| 7549 | internal::Object** p = reinterpret_cast<internal::Object**>(that); |
| 7550 | return reinterpret_cast<T*>( |
| 7551 | V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), |
| 7552 | p)); |
| 7553 | } |
| 7554 | |
| 7555 | |
| 7556 | template <class T, class M> |
| 7557 | template <class S, class M2> |
| 7558 | void Persistent<T, M>::Copy(const Persistent<S, M2>& that) { |
| 7559 | TYPE_CHECK(T, S); |
| 7560 | this->Reset(); |
| 7561 | if (that.IsEmpty()) return; |
| 7562 | internal::Object** p = reinterpret_cast<internal::Object**>(that.val_); |
| 7563 | this->val_ = reinterpret_cast<T*>(V8::CopyPersistent(p)); |
| 7564 | M::Copy(that, this); |
| 7565 | } |
| 7566 | |
| 7567 | |
| 7568 | template <class T> |
| 7569 | bool PersistentBase<T>::IsIndependent() const { |
| 7570 | typedef internal::Internals I; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7571 | if (this->IsEmpty()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7572 | return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_), |
| 7573 | I::kNodeIsIndependentShift); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7574 | } |
| 7575 | |
| 7576 | |
| 7577 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7578 | bool PersistentBase<T>::IsNearDeath() const { |
| 7579 | typedef internal::Internals I; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7580 | if (this->IsEmpty()) return false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7581 | uint8_t node_state = |
| 7582 | I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)); |
| 7583 | return node_state == I::kNodeStateIsNearDeathValue || |
| 7584 | node_state == I::kNodeStateIsPendingValue; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | |
| 7588 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7589 | bool PersistentBase<T>::IsWeak() const { |
| 7590 | typedef internal::Internals I; |
| 7591 | if (this->IsEmpty()) return false; |
| 7592 | return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) == |
| 7593 | I::kNodeStateIsWeakValue; |
| 7594 | } |
| 7595 | |
| 7596 | |
| 7597 | template <class T> |
| 7598 | void PersistentBase<T>::Reset() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7599 | if (this->IsEmpty()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7600 | V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_)); |
| 7601 | val_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7602 | } |
| 7603 | |
| 7604 | |
| 7605 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7606 | template <class S> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7607 | void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7608 | TYPE_CHECK(T, S); |
| 7609 | Reset(); |
| 7610 | if (other.IsEmpty()) return; |
| 7611 | this->val_ = New(isolate, other.val_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7612 | } |
| 7613 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7614 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7615 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7616 | template <class S> |
| 7617 | void PersistentBase<T>::Reset(Isolate* isolate, |
| 7618 | const PersistentBase<S>& other) { |
| 7619 | TYPE_CHECK(T, S); |
| 7620 | Reset(); |
| 7621 | if (other.IsEmpty()) return; |
| 7622 | this->val_ = New(isolate, other.val_); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7623 | } |
| 7624 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7625 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 7626 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7627 | template <typename S, typename P> |
| 7628 | void PersistentBase<T>::SetWeak( |
| 7629 | P* parameter, |
| 7630 | typename WeakCallbackData<S, P>::Callback callback) { |
| 7631 | TYPE_CHECK(S, T); |
| 7632 | typedef typename WeakCallbackData<Value, void>::Callback Callback; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7633 | V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7634 | reinterpret_cast<Callback>(callback)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7635 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7636 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7637 | |
| 7638 | template <class T> |
| 7639 | template <typename P> |
| 7640 | void PersistentBase<T>::SetWeak( |
| 7641 | P* parameter, |
| 7642 | typename WeakCallbackData<T, P>::Callback callback) { |
| 7643 | SetWeak<T, P>(parameter, callback); |
| 7644 | } |
| 7645 | |
| 7646 | |
| 7647 | template <class T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7648 | template <typename P> |
| 7649 | void PersistentBase<T>::SetPhantom( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7650 | P* parameter, typename WeakCallbackInfo<P>::Callback callback, |
| 7651 | int internal_field_index1, int internal_field_index2) { |
| 7652 | typedef typename WeakCallbackInfo<void>::Callback Callback; |
| 7653 | V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, |
| 7654 | internal_field_index1, internal_field_index2, |
| 7655 | reinterpret_cast<Callback>(callback)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7656 | } |
| 7657 | |
| 7658 | |
| 7659 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7660 | template <typename P> |
| 7661 | V8_INLINE void PersistentBase<T>::SetWeak( |
| 7662 | P* parameter, typename WeakCallbackInfo<P>::Callback callback, |
| 7663 | WeakCallbackType type) { |
| 7664 | typedef typename WeakCallbackInfo<void>::Callback Callback; |
| 7665 | V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter, |
| 7666 | reinterpret_cast<Callback>(callback), type); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7667 | } |
| 7668 | |
| 7669 | |
| 7670 | template <class T> |
| 7671 | template <typename P> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7672 | P* PersistentBase<T>::ClearWeak() { |
| 7673 | return reinterpret_cast<P*>( |
| 7674 | V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_))); |
| 7675 | } |
| 7676 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7677 | template <class T> |
| 7678 | void PersistentBase<T>::RegisterExternalReference(Isolate* isolate) { |
| 7679 | if (IsEmpty()) return; |
| 7680 | V8::RegisterExternallyReferencedObject( |
| 7681 | reinterpret_cast<internal::Object**>(this->val_), |
| 7682 | reinterpret_cast<internal::Isolate*>(isolate)); |
| 7683 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7684 | |
| 7685 | template <class T> |
| 7686 | void PersistentBase<T>::MarkIndependent() { |
| 7687 | typedef internal::Internals I; |
| 7688 | if (this->IsEmpty()) return; |
| 7689 | I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), |
| 7690 | true, |
| 7691 | I::kNodeIsIndependentShift); |
| 7692 | } |
| 7693 | |
| 7694 | |
| 7695 | template <class T> |
| 7696 | void PersistentBase<T>::MarkPartiallyDependent() { |
| 7697 | typedef internal::Internals I; |
| 7698 | if (this->IsEmpty()) return; |
| 7699 | I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), |
| 7700 | true, |
| 7701 | I::kNodeIsPartiallyDependentShift); |
| 7702 | } |
| 7703 | |
| 7704 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7705 | template <class T> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7706 | void PersistentBase<T>::MarkActive() { |
| 7707 | typedef internal::Internals I; |
| 7708 | if (this->IsEmpty()) return; |
| 7709 | I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), true, |
| 7710 | I::kNodeIsActiveShift); |
| 7711 | } |
| 7712 | |
| 7713 | |
| 7714 | template <class T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7715 | void PersistentBase<T>::SetWrapperClassId(uint16_t class_id) { |
| 7716 | typedef internal::Internals I; |
| 7717 | if (this->IsEmpty()) return; |
| 7718 | internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_); |
| 7719 | uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset; |
| 7720 | *reinterpret_cast<uint16_t*>(addr) = class_id; |
| 7721 | } |
| 7722 | |
| 7723 | |
| 7724 | template <class T> |
| 7725 | uint16_t PersistentBase<T>::WrapperClassId() const { |
| 7726 | typedef internal::Internals I; |
| 7727 | if (this->IsEmpty()) return 0; |
| 7728 | internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_); |
| 7729 | uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset; |
| 7730 | return *reinterpret_cast<uint16_t*>(addr); |
| 7731 | } |
| 7732 | |
| 7733 | |
| 7734 | template<typename T> |
| 7735 | ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {} |
| 7736 | |
| 7737 | template<typename T> |
| 7738 | template<typename S> |
| 7739 | void ReturnValue<T>::Set(const Persistent<S>& handle) { |
| 7740 | TYPE_CHECK(T, S); |
| 7741 | if (V8_UNLIKELY(handle.IsEmpty())) { |
| 7742 | *value_ = GetDefaultValue(); |
| 7743 | } else { |
| 7744 | *value_ = *reinterpret_cast<internal::Object**>(*handle); |
| 7745 | } |
| 7746 | } |
| 7747 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7748 | template <typename T> |
| 7749 | template <typename S> |
| 7750 | void ReturnValue<T>::Set(const Global<S>& handle) { |
| 7751 | TYPE_CHECK(T, S); |
| 7752 | if (V8_UNLIKELY(handle.IsEmpty())) { |
| 7753 | *value_ = GetDefaultValue(); |
| 7754 | } else { |
| 7755 | *value_ = *reinterpret_cast<internal::Object**>(*handle); |
| 7756 | } |
| 7757 | } |
| 7758 | |
| 7759 | template <typename T> |
| 7760 | template <typename S> |
| 7761 | void ReturnValue<T>::Set(const Local<S> handle) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7762 | TYPE_CHECK(T, S); |
| 7763 | if (V8_UNLIKELY(handle.IsEmpty())) { |
| 7764 | *value_ = GetDefaultValue(); |
| 7765 | } else { |
| 7766 | *value_ = *reinterpret_cast<internal::Object**>(*handle); |
| 7767 | } |
| 7768 | } |
| 7769 | |
| 7770 | template<typename T> |
| 7771 | void ReturnValue<T>::Set(double i) { |
| 7772 | TYPE_CHECK(T, Number); |
| 7773 | Set(Number::New(GetIsolate(), i)); |
| 7774 | } |
| 7775 | |
| 7776 | template<typename T> |
| 7777 | void ReturnValue<T>::Set(int32_t i) { |
| 7778 | TYPE_CHECK(T, Integer); |
| 7779 | typedef internal::Internals I; |
| 7780 | if (V8_LIKELY(I::IsValidSmi(i))) { |
| 7781 | *value_ = I::IntToSmi(i); |
| 7782 | return; |
| 7783 | } |
| 7784 | Set(Integer::New(GetIsolate(), i)); |
| 7785 | } |
| 7786 | |
| 7787 | template<typename T> |
| 7788 | void ReturnValue<T>::Set(uint32_t i) { |
| 7789 | TYPE_CHECK(T, Integer); |
| 7790 | // Can't simply use INT32_MAX here for whatever reason. |
| 7791 | bool fits_into_int32_t = (i & (1U << 31)) == 0; |
| 7792 | if (V8_LIKELY(fits_into_int32_t)) { |
| 7793 | Set(static_cast<int32_t>(i)); |
| 7794 | return; |
| 7795 | } |
| 7796 | Set(Integer::NewFromUnsigned(GetIsolate(), i)); |
| 7797 | } |
| 7798 | |
| 7799 | template<typename T> |
| 7800 | void ReturnValue<T>::Set(bool value) { |
| 7801 | TYPE_CHECK(T, Boolean); |
| 7802 | typedef internal::Internals I; |
| 7803 | int root_index; |
| 7804 | if (value) { |
| 7805 | root_index = I::kTrueValueRootIndex; |
| 7806 | } else { |
| 7807 | root_index = I::kFalseValueRootIndex; |
| 7808 | } |
| 7809 | *value_ = *I::GetRoot(GetIsolate(), root_index); |
| 7810 | } |
| 7811 | |
| 7812 | template<typename T> |
| 7813 | void ReturnValue<T>::SetNull() { |
| 7814 | TYPE_CHECK(T, Primitive); |
| 7815 | typedef internal::Internals I; |
| 7816 | *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex); |
| 7817 | } |
| 7818 | |
| 7819 | template<typename T> |
| 7820 | void ReturnValue<T>::SetUndefined() { |
| 7821 | TYPE_CHECK(T, Primitive); |
| 7822 | typedef internal::Internals I; |
| 7823 | *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex); |
| 7824 | } |
| 7825 | |
| 7826 | template<typename T> |
| 7827 | void ReturnValue<T>::SetEmptyString() { |
| 7828 | TYPE_CHECK(T, String); |
| 7829 | typedef internal::Internals I; |
| 7830 | *value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex); |
| 7831 | } |
| 7832 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7833 | template <typename T> |
| 7834 | Isolate* ReturnValue<T>::GetIsolate() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7835 | // Isolate is always the pointer below the default value on the stack. |
| 7836 | return *reinterpret_cast<Isolate**>(&value_[-2]); |
| 7837 | } |
| 7838 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 7839 | template <typename T> |
| 7840 | Local<Value> ReturnValue<T>::Get() const { |
| 7841 | typedef internal::Internals I; |
| 7842 | if (*value_ == *I::GetRoot(GetIsolate(), I::kTheHoleValueRootIndex)) |
| 7843 | return Local<Value>(*Undefined(GetIsolate())); |
| 7844 | return Local<Value>::New(GetIsolate(), reinterpret_cast<Value*>(value_)); |
| 7845 | } |
| 7846 | |
| 7847 | template <typename T> |
| 7848 | template <typename S> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7849 | void ReturnValue<T>::Set(S* whatever) { |
| 7850 | // Uncompilable to prevent inadvertent misuse. |
| 7851 | TYPE_CHECK(S*, Primitive); |
| 7852 | } |
| 7853 | |
| 7854 | template<typename T> |
| 7855 | internal::Object* ReturnValue<T>::GetDefaultValue() { |
| 7856 | // Default value is always the pointer below value_ on the stack. |
| 7857 | return value_[-1]; |
| 7858 | } |
| 7859 | |
| 7860 | |
| 7861 | template<typename T> |
| 7862 | FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args, |
| 7863 | internal::Object** values, |
| 7864 | int length, |
| 7865 | bool is_construct_call) |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7866 | : implicit_args_(implicit_args), |
| 7867 | values_(values), |
| 7868 | length_(length), |
| 7869 | is_construct_call_(is_construct_call) { } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7870 | |
| 7871 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7872 | template<typename T> |
| 7873 | Local<Value> FunctionCallbackInfo<T>::operator[](int i) const { |
| 7874 | if (i < 0 || length_ <= i) return Local<Value>(*Undefined(GetIsolate())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7875 | return Local<Value>(reinterpret_cast<Value*>(values_ - i)); |
| 7876 | } |
| 7877 | |
| 7878 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7879 | template<typename T> |
| 7880 | Local<Function> FunctionCallbackInfo<T>::Callee() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7881 | return Local<Function>(reinterpret_cast<Function*>( |
| 7882 | &implicit_args_[kCalleeIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7883 | } |
| 7884 | |
| 7885 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7886 | template<typename T> |
| 7887 | Local<Object> FunctionCallbackInfo<T>::This() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7888 | return Local<Object>(reinterpret_cast<Object*>(values_ + 1)); |
| 7889 | } |
| 7890 | |
| 7891 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7892 | template<typename T> |
| 7893 | Local<Object> FunctionCallbackInfo<T>::Holder() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7894 | return Local<Object>(reinterpret_cast<Object*>( |
| 7895 | &implicit_args_[kHolderIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7896 | } |
| 7897 | |
| 7898 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7899 | template<typename T> |
| 7900 | Local<Value> FunctionCallbackInfo<T>::Data() const { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 7901 | return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7902 | } |
| 7903 | |
| 7904 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7905 | template<typename T> |
| 7906 | Isolate* FunctionCallbackInfo<T>::GetIsolate() const { |
| 7907 | return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]); |
| 7908 | } |
| 7909 | |
| 7910 | |
| 7911 | template<typename T> |
| 7912 | ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const { |
| 7913 | return ReturnValue<T>(&implicit_args_[kReturnValueIndex]); |
| 7914 | } |
| 7915 | |
| 7916 | |
| 7917 | template<typename T> |
| 7918 | bool FunctionCallbackInfo<T>::IsConstructCall() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7919 | return is_construct_call_ & 0x1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7920 | } |
| 7921 | |
| 7922 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7923 | template<typename T> |
| 7924 | int FunctionCallbackInfo<T>::Length() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7925 | return length_; |
| 7926 | } |
| 7927 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7928 | ScriptOrigin::ScriptOrigin(Local<Value> resource_name, |
| 7929 | Local<Integer> resource_line_offset, |
| 7930 | Local<Integer> resource_column_offset, |
| 7931 | Local<Boolean> resource_is_shared_cross_origin, |
| 7932 | Local<Integer> script_id, |
| 7933 | Local<Boolean> resource_is_embedder_debug_script, |
| 7934 | Local<Value> source_map_url, |
| 7935 | Local<Boolean> resource_is_opaque) |
| 7936 | : resource_name_(resource_name), |
| 7937 | resource_line_offset_(resource_line_offset), |
| 7938 | resource_column_offset_(resource_column_offset), |
| 7939 | options_(!resource_is_embedder_debug_script.IsEmpty() && |
| 7940 | resource_is_embedder_debug_script->IsTrue(), |
| 7941 | !resource_is_shared_cross_origin.IsEmpty() && |
| 7942 | resource_is_shared_cross_origin->IsTrue(), |
| 7943 | !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue()), |
| 7944 | script_id_(script_id), |
| 7945 | source_map_url_(source_map_url) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7946 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7947 | Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7948 | |
| 7949 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7950 | Local<Integer> ScriptOrigin::ResourceLineOffset() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7951 | return resource_line_offset_; |
| 7952 | } |
| 7953 | |
| 7954 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7955 | Local<Integer> ScriptOrigin::ResourceColumnOffset() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7956 | return resource_column_offset_; |
| 7957 | } |
| 7958 | |
| 7959 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7960 | Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7961 | |
| 7962 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7963 | Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7964 | |
| 7965 | |
| 7966 | ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin, |
| 7967 | CachedData* data) |
| 7968 | : source_string(string), |
| 7969 | resource_name(origin.ResourceName()), |
| 7970 | resource_line_offset(origin.ResourceLineOffset()), |
| 7971 | resource_column_offset(origin.ResourceColumnOffset()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7972 | resource_options(origin.Options()), |
| 7973 | source_map_url(origin.SourceMapUrl()), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7974 | cached_data(data) {} |
| 7975 | |
| 7976 | |
| 7977 | ScriptCompiler::Source::Source(Local<String> string, |
| 7978 | CachedData* data) |
| 7979 | : source_string(string), cached_data(data) {} |
| 7980 | |
| 7981 | |
| 7982 | ScriptCompiler::Source::~Source() { |
| 7983 | delete cached_data; |
| 7984 | } |
| 7985 | |
| 7986 | |
| 7987 | const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData() |
| 7988 | const { |
| 7989 | return cached_data; |
| 7990 | } |
| 7991 | |
| 7992 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7993 | Local<Boolean> Boolean::New(Isolate* isolate, bool value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7994 | return value ? True(isolate) : False(isolate); |
| 7995 | } |
| 7996 | |
| 7997 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7998 | void Template::Set(Isolate* isolate, const char* name, v8::Local<Data> value) { |
| 7999 | Set(v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal) |
| 8000 | .ToLocalChecked(), |
| 8001 | value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8002 | } |
| 8003 | |
| 8004 | |
| 8005 | Local<Value> Object::GetInternalField(int index) { |
| 8006 | #ifndef V8_ENABLE_CHECKS |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8007 | typedef internal::Object O; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8008 | typedef internal::HeapObject HO; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8009 | typedef internal::Internals I; |
| 8010 | O* obj = *reinterpret_cast<O**>(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8011 | // Fast path: If the object is a plain JSObject, which is the common case, we |
| 8012 | // know where to find the internal fields and can return the value directly. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8013 | if (I::GetInstanceType(obj) == I::kJSObjectType) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 8014 | int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8015 | O* value = I::ReadField<O*>(obj, offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8016 | O** result = HandleScope::CreateHandle(reinterpret_cast<HO*>(obj), value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8017 | return Local<Value>(reinterpret_cast<Value*>(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8018 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8019 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8020 | return SlowGetInternalField(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8021 | } |
| 8022 | |
| 8023 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8024 | void* Object::GetAlignedPointerFromInternalField(int index) { |
| 8025 | #ifndef V8_ENABLE_CHECKS |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8026 | typedef internal::Object O; |
| 8027 | typedef internal::Internals I; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8028 | O* obj = *reinterpret_cast<O**>(this); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8029 | // Fast path: If the object is a plain JSObject, which is the common case, we |
| 8030 | // know where to find the internal fields and can return the value directly. |
| 8031 | if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 8032 | int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8033 | return I::ReadField<void*>(obj, offset); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8034 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8035 | #endif |
| 8036 | return SlowGetAlignedPointerFromInternalField(index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8037 | } |
| 8038 | |
| 8039 | |
| 8040 | String* String::Cast(v8::Value* value) { |
| 8041 | #ifdef V8_ENABLE_CHECKS |
| 8042 | CheckCast(value); |
| 8043 | #endif |
| 8044 | return static_cast<String*>(value); |
| 8045 | } |
| 8046 | |
| 8047 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8048 | Local<String> String::Empty(Isolate* isolate) { |
| 8049 | typedef internal::Object* S; |
| 8050 | typedef internal::Internals I; |
| 8051 | I::CheckInitialized(isolate); |
| 8052 | S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex); |
| 8053 | return Local<String>(reinterpret_cast<String*>(slot)); |
| 8054 | } |
| 8055 | |
| 8056 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8057 | String::ExternalStringResource* String::GetExternalStringResource() const { |
| 8058 | typedef internal::Object O; |
| 8059 | typedef internal::Internals I; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8060 | O* obj = *reinterpret_cast<O* const*>(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8061 | String::ExternalStringResource* result; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8062 | if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8063 | void* value = I::ReadField<void*>(obj, I::kStringResourceOffset); |
| 8064 | result = reinterpret_cast<String::ExternalStringResource*>(value); |
| 8065 | } else { |
| 8066 | result = NULL; |
| 8067 | } |
| 8068 | #ifdef V8_ENABLE_CHECKS |
| 8069 | VerifyExternalStringResource(result); |
| 8070 | #endif |
| 8071 | return result; |
| 8072 | } |
| 8073 | |
| 8074 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8075 | String::ExternalStringResourceBase* String::GetExternalStringResourceBase( |
| 8076 | String::Encoding* encoding_out) const { |
| 8077 | typedef internal::Object O; |
| 8078 | typedef internal::Internals I; |
| 8079 | O* obj = *reinterpret_cast<O* const*>(this); |
| 8080 | int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask; |
| 8081 | *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask); |
| 8082 | ExternalStringResourceBase* resource = NULL; |
| 8083 | if (type == I::kExternalOneByteRepresentationTag || |
| 8084 | type == I::kExternalTwoByteRepresentationTag) { |
| 8085 | void* value = I::ReadField<void*>(obj, I::kStringResourceOffset); |
| 8086 | resource = static_cast<ExternalStringResourceBase*>(value); |
| 8087 | } |
| 8088 | #ifdef V8_ENABLE_CHECKS |
| 8089 | VerifyExternalStringResourceBase(resource, *encoding_out); |
| 8090 | #endif |
| 8091 | return resource; |
| 8092 | } |
| 8093 | |
| 8094 | |
| 8095 | bool Value::IsUndefined() const { |
| 8096 | #ifdef V8_ENABLE_CHECKS |
| 8097 | return FullIsUndefined(); |
| 8098 | #else |
| 8099 | return QuickIsUndefined(); |
| 8100 | #endif |
| 8101 | } |
| 8102 | |
| 8103 | bool Value::QuickIsUndefined() const { |
| 8104 | typedef internal::Object O; |
| 8105 | typedef internal::Internals I; |
| 8106 | O* obj = *reinterpret_cast<O* const*>(this); |
| 8107 | if (!I::HasHeapObjectTag(obj)) return false; |
| 8108 | if (I::GetInstanceType(obj) != I::kOddballType) return false; |
| 8109 | return (I::GetOddballKind(obj) == I::kUndefinedOddballKind); |
| 8110 | } |
| 8111 | |
| 8112 | |
| 8113 | bool Value::IsNull() const { |
| 8114 | #ifdef V8_ENABLE_CHECKS |
| 8115 | return FullIsNull(); |
| 8116 | #else |
| 8117 | return QuickIsNull(); |
| 8118 | #endif |
| 8119 | } |
| 8120 | |
| 8121 | bool Value::QuickIsNull() const { |
| 8122 | typedef internal::Object O; |
| 8123 | typedef internal::Internals I; |
| 8124 | O* obj = *reinterpret_cast<O* const*>(this); |
| 8125 | if (!I::HasHeapObjectTag(obj)) return false; |
| 8126 | if (I::GetInstanceType(obj) != I::kOddballType) return false; |
| 8127 | return (I::GetOddballKind(obj) == I::kNullOddballKind); |
| 8128 | } |
| 8129 | |
| 8130 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8131 | bool Value::IsString() const { |
| 8132 | #ifdef V8_ENABLE_CHECKS |
| 8133 | return FullIsString(); |
| 8134 | #else |
| 8135 | return QuickIsString(); |
| 8136 | #endif |
| 8137 | } |
| 8138 | |
| 8139 | bool Value::QuickIsString() const { |
| 8140 | typedef internal::Object O; |
| 8141 | typedef internal::Internals I; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8142 | O* obj = *reinterpret_cast<O* const*>(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8143 | if (!I::HasHeapObjectTag(obj)) return false; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8144 | return (I::GetInstanceType(obj) < I::kFirstNonstringType); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8145 | } |
| 8146 | |
| 8147 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8148 | template <class T> Value* Value::Cast(T* value) { |
| 8149 | return static_cast<Value*>(value); |
| 8150 | } |
| 8151 | |
| 8152 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8153 | Local<Boolean> Value::ToBoolean() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8154 | return ToBoolean(Isolate::GetCurrent()->GetCurrentContext()) |
| 8155 | .FromMaybe(Local<Boolean>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8156 | } |
| 8157 | |
| 8158 | |
| 8159 | Local<Number> Value::ToNumber() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8160 | return ToNumber(Isolate::GetCurrent()->GetCurrentContext()) |
| 8161 | .FromMaybe(Local<Number>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8162 | } |
| 8163 | |
| 8164 | |
| 8165 | Local<String> Value::ToString() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8166 | return ToString(Isolate::GetCurrent()->GetCurrentContext()) |
| 8167 | .FromMaybe(Local<String>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8168 | } |
| 8169 | |
| 8170 | |
| 8171 | Local<String> Value::ToDetailString() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8172 | return ToDetailString(Isolate::GetCurrent()->GetCurrentContext()) |
| 8173 | .FromMaybe(Local<String>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8174 | } |
| 8175 | |
| 8176 | |
| 8177 | Local<Object> Value::ToObject() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8178 | return ToObject(Isolate::GetCurrent()->GetCurrentContext()) |
| 8179 | .FromMaybe(Local<Object>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8180 | } |
| 8181 | |
| 8182 | |
| 8183 | Local<Integer> Value::ToInteger() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8184 | return ToInteger(Isolate::GetCurrent()->GetCurrentContext()) |
| 8185 | .FromMaybe(Local<Integer>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8186 | } |
| 8187 | |
| 8188 | |
| 8189 | Local<Uint32> Value::ToUint32() const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8190 | return ToUint32(Isolate::GetCurrent()->GetCurrentContext()) |
| 8191 | .FromMaybe(Local<Uint32>()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8195 | Local<Int32> Value::ToInt32() const { |
| 8196 | return ToInt32(Isolate::GetCurrent()->GetCurrentContext()) |
| 8197 | .FromMaybe(Local<Int32>()); |
| 8198 | } |
| 8199 | |
| 8200 | |
| 8201 | Boolean* Boolean::Cast(v8::Value* value) { |
| 8202 | #ifdef V8_ENABLE_CHECKS |
| 8203 | CheckCast(value); |
| 8204 | #endif |
| 8205 | return static_cast<Boolean*>(value); |
| 8206 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8207 | |
| 8208 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8209 | Name* Name::Cast(v8::Value* value) { |
| 8210 | #ifdef V8_ENABLE_CHECKS |
| 8211 | CheckCast(value); |
| 8212 | #endif |
| 8213 | return static_cast<Name*>(value); |
| 8214 | } |
| 8215 | |
| 8216 | |
| 8217 | Symbol* Symbol::Cast(v8::Value* value) { |
| 8218 | #ifdef V8_ENABLE_CHECKS |
| 8219 | CheckCast(value); |
| 8220 | #endif |
| 8221 | return static_cast<Symbol*>(value); |
| 8222 | } |
| 8223 | |
| 8224 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8225 | Number* Number::Cast(v8::Value* value) { |
| 8226 | #ifdef V8_ENABLE_CHECKS |
| 8227 | CheckCast(value); |
| 8228 | #endif |
| 8229 | return static_cast<Number*>(value); |
| 8230 | } |
| 8231 | |
| 8232 | |
| 8233 | Integer* Integer::Cast(v8::Value* value) { |
| 8234 | #ifdef V8_ENABLE_CHECKS |
| 8235 | CheckCast(value); |
| 8236 | #endif |
| 8237 | return static_cast<Integer*>(value); |
| 8238 | } |
| 8239 | |
| 8240 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8241 | Int32* Int32::Cast(v8::Value* value) { |
| 8242 | #ifdef V8_ENABLE_CHECKS |
| 8243 | CheckCast(value); |
| 8244 | #endif |
| 8245 | return static_cast<Int32*>(value); |
| 8246 | } |
| 8247 | |
| 8248 | |
| 8249 | Uint32* Uint32::Cast(v8::Value* value) { |
| 8250 | #ifdef V8_ENABLE_CHECKS |
| 8251 | CheckCast(value); |
| 8252 | #endif |
| 8253 | return static_cast<Uint32*>(value); |
| 8254 | } |
| 8255 | |
| 8256 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8257 | Date* Date::Cast(v8::Value* value) { |
| 8258 | #ifdef V8_ENABLE_CHECKS |
| 8259 | CheckCast(value); |
| 8260 | #endif |
| 8261 | return static_cast<Date*>(value); |
| 8262 | } |
| 8263 | |
| 8264 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8265 | StringObject* StringObject::Cast(v8::Value* value) { |
| 8266 | #ifdef V8_ENABLE_CHECKS |
| 8267 | CheckCast(value); |
| 8268 | #endif |
| 8269 | return static_cast<StringObject*>(value); |
| 8270 | } |
| 8271 | |
| 8272 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8273 | SymbolObject* SymbolObject::Cast(v8::Value* value) { |
| 8274 | #ifdef V8_ENABLE_CHECKS |
| 8275 | CheckCast(value); |
| 8276 | #endif |
| 8277 | return static_cast<SymbolObject*>(value); |
| 8278 | } |
| 8279 | |
| 8280 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 8281 | NumberObject* NumberObject::Cast(v8::Value* value) { |
| 8282 | #ifdef V8_ENABLE_CHECKS |
| 8283 | CheckCast(value); |
| 8284 | #endif |
| 8285 | return static_cast<NumberObject*>(value); |
| 8286 | } |
| 8287 | |
| 8288 | |
| 8289 | BooleanObject* BooleanObject::Cast(v8::Value* value) { |
| 8290 | #ifdef V8_ENABLE_CHECKS |
| 8291 | CheckCast(value); |
| 8292 | #endif |
| 8293 | return static_cast<BooleanObject*>(value); |
| 8294 | } |
| 8295 | |
| 8296 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 8297 | RegExp* RegExp::Cast(v8::Value* value) { |
| 8298 | #ifdef V8_ENABLE_CHECKS |
| 8299 | CheckCast(value); |
| 8300 | #endif |
| 8301 | return static_cast<RegExp*>(value); |
| 8302 | } |
| 8303 | |
| 8304 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8305 | Object* Object::Cast(v8::Value* value) { |
| 8306 | #ifdef V8_ENABLE_CHECKS |
| 8307 | CheckCast(value); |
| 8308 | #endif |
| 8309 | return static_cast<Object*>(value); |
| 8310 | } |
| 8311 | |
| 8312 | |
| 8313 | Array* Array::Cast(v8::Value* value) { |
| 8314 | #ifdef V8_ENABLE_CHECKS |
| 8315 | CheckCast(value); |
| 8316 | #endif |
| 8317 | return static_cast<Array*>(value); |
| 8318 | } |
| 8319 | |
| 8320 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8321 | Map* Map::Cast(v8::Value* value) { |
| 8322 | #ifdef V8_ENABLE_CHECKS |
| 8323 | CheckCast(value); |
| 8324 | #endif |
| 8325 | return static_cast<Map*>(value); |
| 8326 | } |
| 8327 | |
| 8328 | |
| 8329 | Set* Set::Cast(v8::Value* value) { |
| 8330 | #ifdef V8_ENABLE_CHECKS |
| 8331 | CheckCast(value); |
| 8332 | #endif |
| 8333 | return static_cast<Set*>(value); |
| 8334 | } |
| 8335 | |
| 8336 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8337 | Promise* Promise::Cast(v8::Value* value) { |
| 8338 | #ifdef V8_ENABLE_CHECKS |
| 8339 | CheckCast(value); |
| 8340 | #endif |
| 8341 | return static_cast<Promise*>(value); |
| 8342 | } |
| 8343 | |
| 8344 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8345 | Proxy* Proxy::Cast(v8::Value* value) { |
| 8346 | #ifdef V8_ENABLE_CHECKS |
| 8347 | CheckCast(value); |
| 8348 | #endif |
| 8349 | return static_cast<Proxy*>(value); |
| 8350 | } |
| 8351 | |
| 8352 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8353 | Promise::Resolver* Promise::Resolver::Cast(v8::Value* value) { |
| 8354 | #ifdef V8_ENABLE_CHECKS |
| 8355 | CheckCast(value); |
| 8356 | #endif |
| 8357 | return static_cast<Promise::Resolver*>(value); |
| 8358 | } |
| 8359 | |
| 8360 | |
| 8361 | ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) { |
| 8362 | #ifdef V8_ENABLE_CHECKS |
| 8363 | CheckCast(value); |
| 8364 | #endif |
| 8365 | return static_cast<ArrayBuffer*>(value); |
| 8366 | } |
| 8367 | |
| 8368 | |
| 8369 | ArrayBufferView* ArrayBufferView::Cast(v8::Value* value) { |
| 8370 | #ifdef V8_ENABLE_CHECKS |
| 8371 | CheckCast(value); |
| 8372 | #endif |
| 8373 | return static_cast<ArrayBufferView*>(value); |
| 8374 | } |
| 8375 | |
| 8376 | |
| 8377 | TypedArray* TypedArray::Cast(v8::Value* value) { |
| 8378 | #ifdef V8_ENABLE_CHECKS |
| 8379 | CheckCast(value); |
| 8380 | #endif |
| 8381 | return static_cast<TypedArray*>(value); |
| 8382 | } |
| 8383 | |
| 8384 | |
| 8385 | Uint8Array* Uint8Array::Cast(v8::Value* value) { |
| 8386 | #ifdef V8_ENABLE_CHECKS |
| 8387 | CheckCast(value); |
| 8388 | #endif |
| 8389 | return static_cast<Uint8Array*>(value); |
| 8390 | } |
| 8391 | |
| 8392 | |
| 8393 | Int8Array* Int8Array::Cast(v8::Value* value) { |
| 8394 | #ifdef V8_ENABLE_CHECKS |
| 8395 | CheckCast(value); |
| 8396 | #endif |
| 8397 | return static_cast<Int8Array*>(value); |
| 8398 | } |
| 8399 | |
| 8400 | |
| 8401 | Uint16Array* Uint16Array::Cast(v8::Value* value) { |
| 8402 | #ifdef V8_ENABLE_CHECKS |
| 8403 | CheckCast(value); |
| 8404 | #endif |
| 8405 | return static_cast<Uint16Array*>(value); |
| 8406 | } |
| 8407 | |
| 8408 | |
| 8409 | Int16Array* Int16Array::Cast(v8::Value* value) { |
| 8410 | #ifdef V8_ENABLE_CHECKS |
| 8411 | CheckCast(value); |
| 8412 | #endif |
| 8413 | return static_cast<Int16Array*>(value); |
| 8414 | } |
| 8415 | |
| 8416 | |
| 8417 | Uint32Array* Uint32Array::Cast(v8::Value* value) { |
| 8418 | #ifdef V8_ENABLE_CHECKS |
| 8419 | CheckCast(value); |
| 8420 | #endif |
| 8421 | return static_cast<Uint32Array*>(value); |
| 8422 | } |
| 8423 | |
| 8424 | |
| 8425 | Int32Array* Int32Array::Cast(v8::Value* value) { |
| 8426 | #ifdef V8_ENABLE_CHECKS |
| 8427 | CheckCast(value); |
| 8428 | #endif |
| 8429 | return static_cast<Int32Array*>(value); |
| 8430 | } |
| 8431 | |
| 8432 | |
| 8433 | Float32Array* Float32Array::Cast(v8::Value* value) { |
| 8434 | #ifdef V8_ENABLE_CHECKS |
| 8435 | CheckCast(value); |
| 8436 | #endif |
| 8437 | return static_cast<Float32Array*>(value); |
| 8438 | } |
| 8439 | |
| 8440 | |
| 8441 | Float64Array* Float64Array::Cast(v8::Value* value) { |
| 8442 | #ifdef V8_ENABLE_CHECKS |
| 8443 | CheckCast(value); |
| 8444 | #endif |
| 8445 | return static_cast<Float64Array*>(value); |
| 8446 | } |
| 8447 | |
| 8448 | |
| 8449 | Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) { |
| 8450 | #ifdef V8_ENABLE_CHECKS |
| 8451 | CheckCast(value); |
| 8452 | #endif |
| 8453 | return static_cast<Uint8ClampedArray*>(value); |
| 8454 | } |
| 8455 | |
| 8456 | |
| 8457 | DataView* DataView::Cast(v8::Value* value) { |
| 8458 | #ifdef V8_ENABLE_CHECKS |
| 8459 | CheckCast(value); |
| 8460 | #endif |
| 8461 | return static_cast<DataView*>(value); |
| 8462 | } |
| 8463 | |
| 8464 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8465 | SharedArrayBuffer* SharedArrayBuffer::Cast(v8::Value* value) { |
| 8466 | #ifdef V8_ENABLE_CHECKS |
| 8467 | CheckCast(value); |
| 8468 | #endif |
| 8469 | return static_cast<SharedArrayBuffer*>(value); |
| 8470 | } |
| 8471 | |
| 8472 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8473 | Function* Function::Cast(v8::Value* value) { |
| 8474 | #ifdef V8_ENABLE_CHECKS |
| 8475 | CheckCast(value); |
| 8476 | #endif |
| 8477 | return static_cast<Function*>(value); |
| 8478 | } |
| 8479 | |
| 8480 | |
| 8481 | External* External::Cast(v8::Value* value) { |
| 8482 | #ifdef V8_ENABLE_CHECKS |
| 8483 | CheckCast(value); |
| 8484 | #endif |
| 8485 | return static_cast<External*>(value); |
| 8486 | } |
| 8487 | |
| 8488 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8489 | template<typename T> |
| 8490 | Isolate* PropertyCallbackInfo<T>::GetIsolate() const { |
| 8491 | return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8492 | } |
| 8493 | |
| 8494 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8495 | template<typename T> |
| 8496 | Local<Value> PropertyCallbackInfo<T>::Data() const { |
| 8497 | return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex])); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8498 | } |
| 8499 | |
| 8500 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8501 | template<typename T> |
| 8502 | Local<Object> PropertyCallbackInfo<T>::This() const { |
| 8503 | return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex])); |
| 8504 | } |
| 8505 | |
| 8506 | |
| 8507 | template<typename T> |
| 8508 | Local<Object> PropertyCallbackInfo<T>::Holder() const { |
| 8509 | return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex])); |
| 8510 | } |
| 8511 | |
| 8512 | |
| 8513 | template<typename T> |
| 8514 | ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const { |
| 8515 | return ReturnValue<T>(&args_[kReturnValueIndex]); |
| 8516 | } |
| 8517 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 8518 | template <typename T> |
| 8519 | bool PropertyCallbackInfo<T>::ShouldThrowOnError() const { |
| 8520 | typedef internal::Internals I; |
| 8521 | return args_[kShouldThrowOnErrorIndex] != I::IntToSmi(0); |
| 8522 | } |
| 8523 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8524 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8525 | Local<Primitive> Undefined(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8526 | typedef internal::Object* S; |
| 8527 | typedef internal::Internals I; |
| 8528 | I::CheckInitialized(isolate); |
| 8529 | S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8530 | return Local<Primitive>(reinterpret_cast<Primitive*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8531 | } |
| 8532 | |
| 8533 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8534 | Local<Primitive> Null(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8535 | typedef internal::Object* S; |
| 8536 | typedef internal::Internals I; |
| 8537 | I::CheckInitialized(isolate); |
| 8538 | S* slot = I::GetRoot(isolate, I::kNullValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8539 | return Local<Primitive>(reinterpret_cast<Primitive*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8540 | } |
| 8541 | |
| 8542 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8543 | Local<Boolean> True(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8544 | typedef internal::Object* S; |
| 8545 | typedef internal::Internals I; |
| 8546 | I::CheckInitialized(isolate); |
| 8547 | S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8548 | return Local<Boolean>(reinterpret_cast<Boolean*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8549 | } |
| 8550 | |
| 8551 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8552 | Local<Boolean> False(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8553 | typedef internal::Object* S; |
| 8554 | typedef internal::Internals I; |
| 8555 | I::CheckInitialized(isolate); |
| 8556 | S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8557 | return Local<Boolean>(reinterpret_cast<Boolean*>(slot)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8558 | } |
| 8559 | |
| 8560 | |
| 8561 | void Isolate::SetData(uint32_t slot, void* data) { |
| 8562 | typedef internal::Internals I; |
| 8563 | I::SetEmbedderData(this, slot, data); |
| 8564 | } |
| 8565 | |
| 8566 | |
| 8567 | void* Isolate::GetData(uint32_t slot) { |
| 8568 | typedef internal::Internals I; |
| 8569 | return I::GetEmbedderData(this, slot); |
| 8570 | } |
| 8571 | |
| 8572 | |
| 8573 | uint32_t Isolate::GetNumberOfDataSlots() { |
| 8574 | typedef internal::Internals I; |
| 8575 | return I::kNumIsolateDataSlots; |
| 8576 | } |
| 8577 | |
| 8578 | |
| 8579 | int64_t Isolate::AdjustAmountOfExternalAllocatedMemory( |
| 8580 | int64_t change_in_bytes) { |
| 8581 | typedef internal::Internals I; |
| 8582 | int64_t* amount_of_external_allocated_memory = |
| 8583 | reinterpret_cast<int64_t*>(reinterpret_cast<uint8_t*>(this) + |
| 8584 | I::kAmountOfExternalAllocatedMemoryOffset); |
| 8585 | int64_t* amount_of_external_allocated_memory_at_last_global_gc = |
| 8586 | reinterpret_cast<int64_t*>( |
| 8587 | reinterpret_cast<uint8_t*>(this) + |
| 8588 | I::kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset); |
| 8589 | int64_t amount = *amount_of_external_allocated_memory + change_in_bytes; |
| 8590 | if (change_in_bytes > 0 && |
| 8591 | amount - *amount_of_external_allocated_memory_at_last_global_gc > |
| 8592 | I::kExternalAllocationLimit) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8593 | ReportExternalAllocationLimitReached(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8594 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8595 | *amount_of_external_allocated_memory = amount; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8596 | return *amount_of_external_allocated_memory; |
| 8597 | } |
| 8598 | |
| 8599 | |
| 8600 | template<typename T> |
| 8601 | void Isolate::SetObjectGroupId(const Persistent<T>& object, |
| 8602 | UniqueId id) { |
| 8603 | TYPE_CHECK(Value, T); |
| 8604 | SetObjectGroupId(reinterpret_cast<v8::internal::Object**>(object.val_), id); |
| 8605 | } |
| 8606 | |
| 8607 | |
| 8608 | template<typename T> |
| 8609 | void Isolate::SetReferenceFromGroup(UniqueId id, |
| 8610 | const Persistent<T>& object) { |
| 8611 | TYPE_CHECK(Value, T); |
| 8612 | SetReferenceFromGroup(id, |
| 8613 | reinterpret_cast<v8::internal::Object**>(object.val_)); |
| 8614 | } |
| 8615 | |
| 8616 | |
| 8617 | template<typename T, typename S> |
| 8618 | void Isolate::SetReference(const Persistent<T>& parent, |
| 8619 | const Persistent<S>& child) { |
| 8620 | TYPE_CHECK(Object, T); |
| 8621 | TYPE_CHECK(Value, S); |
| 8622 | SetReference(reinterpret_cast<v8::internal::Object**>(parent.val_), |
| 8623 | reinterpret_cast<v8::internal::Object**>(child.val_)); |
| 8624 | } |
| 8625 | |
| 8626 | |
| 8627 | Local<Value> Context::GetEmbedderData(int index) { |
| 8628 | #ifndef V8_ENABLE_CHECKS |
| 8629 | typedef internal::Object O; |
| 8630 | typedef internal::HeapObject HO; |
| 8631 | typedef internal::Internals I; |
| 8632 | HO* context = *reinterpret_cast<HO**>(this); |
| 8633 | O** result = |
| 8634 | HandleScope::CreateHandle(context, I::ReadEmbedderData<O*>(this, index)); |
| 8635 | return Local<Value>(reinterpret_cast<Value*>(result)); |
| 8636 | #else |
| 8637 | return SlowGetEmbedderData(index); |
| 8638 | #endif |
| 8639 | } |
| 8640 | |
| 8641 | |
| 8642 | void* Context::GetAlignedPointerFromEmbedderData(int index) { |
| 8643 | #ifndef V8_ENABLE_CHECKS |
| 8644 | typedef internal::Internals I; |
| 8645 | return I::ReadEmbedderData<void*>(this, index); |
| 8646 | #else |
| 8647 | return SlowGetAlignedPointerFromEmbedderData(index); |
| 8648 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8649 | } |
| 8650 | |
| 8651 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8652 | void V8::SetAllowCodeGenerationFromStringsCallback( |
| 8653 | AllowCodeGenerationFromStringsCallback callback) { |
| 8654 | Isolate* isolate = Isolate::GetCurrent(); |
| 8655 | isolate->SetAllowCodeGenerationFromStringsCallback(callback); |
| 8656 | } |
| 8657 | |
| 8658 | |
| 8659 | bool V8::IsDead() { |
| 8660 | Isolate* isolate = Isolate::GetCurrent(); |
| 8661 | return isolate->IsDead(); |
| 8662 | } |
| 8663 | |
| 8664 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8665 | bool V8::AddMessageListener(MessageCallback that, Local<Value> data) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8666 | Isolate* isolate = Isolate::GetCurrent(); |
| 8667 | return isolate->AddMessageListener(that, data); |
| 8668 | } |
| 8669 | |
| 8670 | |
| 8671 | void V8::RemoveMessageListeners(MessageCallback that) { |
| 8672 | Isolate* isolate = Isolate::GetCurrent(); |
| 8673 | isolate->RemoveMessageListeners(that); |
| 8674 | } |
| 8675 | |
| 8676 | |
| 8677 | void V8::SetFailedAccessCheckCallbackFunction( |
| 8678 | FailedAccessCheckCallback callback) { |
| 8679 | Isolate* isolate = Isolate::GetCurrent(); |
| 8680 | isolate->SetFailedAccessCheckCallbackFunction(callback); |
| 8681 | } |
| 8682 | |
| 8683 | |
| 8684 | void V8::SetCaptureStackTraceForUncaughtExceptions( |
| 8685 | bool capture, int frame_limit, StackTrace::StackTraceOptions options) { |
| 8686 | Isolate* isolate = Isolate::GetCurrent(); |
| 8687 | isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit, |
| 8688 | options); |
| 8689 | } |
| 8690 | |
| 8691 | |
| 8692 | void V8::SetFatalErrorHandler(FatalErrorCallback callback) { |
| 8693 | Isolate* isolate = Isolate::GetCurrent(); |
| 8694 | isolate->SetFatalErrorHandler(callback); |
| 8695 | } |
| 8696 | |
| 8697 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8698 | void V8::RemoveGCPrologueCallback(GCCallback callback) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8699 | Isolate* isolate = Isolate::GetCurrent(); |
| 8700 | isolate->RemoveGCPrologueCallback( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8701 | reinterpret_cast<v8::Isolate::GCCallback>(callback)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8702 | } |
| 8703 | |
| 8704 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8705 | void V8::RemoveGCEpilogueCallback(GCCallback callback) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8706 | Isolate* isolate = Isolate::GetCurrent(); |
| 8707 | isolate->RemoveGCEpilogueCallback( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8708 | reinterpret_cast<v8::Isolate::GCCallback>(callback)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 8709 | } |
| 8710 | |
| 8711 | |
| 8712 | void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback, |
| 8713 | ObjectSpace space, |
| 8714 | AllocationAction action) { |
| 8715 | Isolate* isolate = Isolate::GetCurrent(); |
| 8716 | isolate->AddMemoryAllocationCallback(callback, space, action); |
| 8717 | } |
| 8718 | |
| 8719 | |
| 8720 | void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) { |
| 8721 | Isolate* isolate = Isolate::GetCurrent(); |
| 8722 | isolate->RemoveMemoryAllocationCallback(callback); |
| 8723 | } |
| 8724 | |
| 8725 | |
| 8726 | void V8::TerminateExecution(Isolate* isolate) { isolate->TerminateExecution(); } |
| 8727 | |
| 8728 | |
| 8729 | bool V8::IsExecutionTerminating(Isolate* isolate) { |
| 8730 | if (isolate == NULL) { |
| 8731 | isolate = Isolate::GetCurrent(); |
| 8732 | } |
| 8733 | return isolate->IsExecutionTerminating(); |
| 8734 | } |
| 8735 | |
| 8736 | |
| 8737 | void V8::CancelTerminateExecution(Isolate* isolate) { |
| 8738 | isolate->CancelTerminateExecution(); |
| 8739 | } |
| 8740 | |
| 8741 | |
| 8742 | void V8::VisitExternalResources(ExternalResourceVisitor* visitor) { |
| 8743 | Isolate* isolate = Isolate::GetCurrent(); |
| 8744 | isolate->VisitExternalResources(visitor); |
| 8745 | } |
| 8746 | |
| 8747 | |
| 8748 | void V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) { |
| 8749 | Isolate* isolate = Isolate::GetCurrent(); |
| 8750 | isolate->VisitHandlesWithClassIds(visitor); |
| 8751 | } |
| 8752 | |
| 8753 | |
| 8754 | void V8::VisitHandlesWithClassIds(Isolate* isolate, |
| 8755 | PersistentHandleVisitor* visitor) { |
| 8756 | isolate->VisitHandlesWithClassIds(visitor); |
| 8757 | } |
| 8758 | |
| 8759 | |
| 8760 | void V8::VisitHandlesForPartialDependence(Isolate* isolate, |
| 8761 | PersistentHandleVisitor* visitor) { |
| 8762 | isolate->VisitHandlesForPartialDependence(visitor); |
| 8763 | } |
| 8764 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8765 | /** |
| 8766 | * \example shell.cc |
| 8767 | * A simple shell that takes a list of expressions on the |
| 8768 | * command-line and executes them. |
| 8769 | */ |
| 8770 | |
| 8771 | |
| 8772 | /** |
| 8773 | * \example process.cc |
| 8774 | */ |
| 8775 | |
| 8776 | |
| 8777 | } // namespace v8 |
| 8778 | |
| 8779 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8780 | #undef TYPE_CHECK |
| 8781 | |
| 8782 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8783 | #endif // INCLUDE_V8_H_ |