v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
execution.h
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_EXECUTION_H_
29 #define V8_EXECUTION_H_
30 
31 #include "allocation.h"
32 
33 namespace v8 {
34 namespace internal {
35 
36 
37 // Flag used to set the interrupt causes.
39  INTERRUPT = 1 << 0,
40  DEBUGBREAK = 1 << 1,
41  DEBUGCOMMAND = 1 << 2,
42  PREEMPT = 1 << 3,
43  TERMINATE = 1 << 4,
44  GC_REQUEST = 1 << 5,
45  FULL_DEOPT = 1 << 6,
46  INSTALL_CODE = 1 << 7,
47  API_INTERRUPT = 1 << 8,
49 };
50 
51 
52 class Isolate;
53 
54 
55 class Execution : public AllStatic {
56  public:
57  // Call a function, the caller supplies a receiver and an array
58  // of arguments. Arguments are Object* type. After function returns,
59  // pointers in 'args' might be invalid.
60  //
61  // *pending_exception tells whether the invoke resulted in
62  // a pending exception.
63  //
64  // When convert_receiver is set, and the receiver is not an object,
65  // and the function called is not in strict mode, receiver is converted to
66  // an object.
67  //
68  static Handle<Object> Call(Isolate* isolate,
69  Handle<Object> callable,
70  Handle<Object> receiver,
71  int argc,
72  Handle<Object> argv[],
73  bool* pending_exception,
74  bool convert_receiver = false);
75 
76  // Construct object from function, the caller supplies an array of
77  // arguments. Arguments are Object* type. After function returns,
78  // pointers in 'args' might be invalid.
79  //
80  // *pending_exception tells whether the invoke resulted in
81  // a pending exception.
82  //
84  int argc,
85  Handle<Object> argv[],
86  bool* pending_exception);
87 
88  // Call a function, just like Call(), but make sure to silently catch
89  // any thrown exceptions. The return value is either the result of
90  // calling the function (if caught exception is false) or the exception
91  // that occurred (if caught exception is true).
93  Handle<Object> receiver,
94  int argc,
95  Handle<Object> argv[],
96  bool* caught_exception);
97 
98  // ECMA-262 9.3
99  static Handle<Object> ToNumber(
100  Isolate* isolate, Handle<Object> obj, bool* exc);
101 
102  // ECMA-262 9.4
103  static Handle<Object> ToInteger(
104  Isolate* isolate, Handle<Object> obj, bool* exc);
105 
106  // ECMA-262 9.5
107  static Handle<Object> ToInt32(
108  Isolate* isolate, Handle<Object> obj, bool* exc);
109 
110  // ECMA-262 9.6
111  static Handle<Object> ToUint32(
112  Isolate* isolate, Handle<Object> obj, bool* exc);
113 
114  // ECMA-262 9.8
115  static Handle<Object> ToString(
116  Isolate* isolate, Handle<Object> obj, bool* exc);
117 
118  // ECMA-262 9.8
120  Isolate* isolate, Handle<Object> obj, bool* exc);
121 
122  // ECMA-262 9.9
123  static Handle<Object> ToObject(
124  Isolate* isolate, Handle<Object> obj, bool* exc);
125 
126  // Create a new date object from 'time'.
127  static Handle<Object> NewDate(
128  Isolate* isolate, double time, bool* exc);
129 
130  // Create a new regular expression object from 'pattern' and 'flags'.
133  bool* exc);
134 
135  // Used to implement [] notation on strings (calls JS code)
136  static Handle<Object> CharAt(Handle<String> str, uint32_t index);
137 
140  Handle<FunctionTemplateInfo> data, bool* exc);
142  bool* exc);
143  static void ConfigureInstance(Isolate* isolate,
144  Handle<Object> instance,
145  Handle<Object> data,
146  bool* exc);
148  Handle<JSFunction> fun,
149  Handle<Object> pos,
150  Handle<Object> is_global);
151 #ifdef ENABLE_DEBUGGER_SUPPORT
152  static Object* DebugBreakHelper(Isolate* isolate);
153  static void ProcessDebugMessages(Isolate* isolate, bool debug_command_only);
154 #endif
155 
156  // If the stack guard is triggered, but it is not an actual
157  // stack overflow, then handle the interruption accordingly.
158  MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt(
159  Isolate* isolate);
160 
161  // Get a function delegate (or undefined) for the given non-function
162  // object. Used for support calling objects as functions.
164  Handle<Object> object);
166  Handle<Object> object,
167  bool* has_pending_exception);
168 
169  // Get a function delegate (or undefined) for the given non-function
170  // object. Used for support calling objects as constructors.
172  Handle<Object> object);
174  Handle<Object> object,
175  bool* has_pending_exception);
176 
177  static void RunMicrotasks(Isolate* isolate);
178  static void EnqueueMicrotask(Isolate* isolate, Handle<Object> microtask);
179 };
180 
181 
182 class ExecutionAccess;
183 
184 
185 // StackGuard contains the handling of the limits that are used to limit the
186 // number of nested invocations of JavaScript and the stack size used in each
187 // invocation.
188 class StackGuard {
189  public:
190  // Pass the address beyond which the stack should not grow. The stack
191  // is assumed to grow downwards.
192  void SetStackLimit(uintptr_t limit);
193 
194  // Threading support.
195  char* ArchiveStackGuard(char* to);
196  char* RestoreStackGuard(char* from);
197  static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
198  void FreeThreadResources();
199  // Sets up the default stack guard for this thread if it has not
200  // already been set up.
201  void InitThread(const ExecutionAccess& lock);
202  // Clears the stack guard for this thread so it does not look as if
203  // it has been set up.
204  void ClearThread(const ExecutionAccess& lock);
205 
206  bool IsStackOverflow();
207  bool IsPreempted();
208  void Preempt();
209  bool IsInterrupted();
210  void Interrupt();
211  bool IsTerminateExecution();
212  void TerminateExecution();
214 #ifdef ENABLE_DEBUGGER_SUPPORT
215  bool IsDebugBreak();
216  void DebugBreak();
217  bool IsDebugCommand();
218  void DebugCommand();
219 #endif
220  bool IsGCRequest();
221  void RequestGC();
222  bool IsInstallCodeRequest();
223  void RequestInstallCode();
224  bool IsFullDeopt();
225  void FullDeopt();
228  void Continue(InterruptFlag after_what);
229 
230  void RequestInterrupt(InterruptCallback callback, void* data);
231  void ClearInterrupt();
232  bool IsAPIInterrupt();
234 
235  // This provides an asynchronous read of the stack limits for the current
236  // thread. There are no locks protecting this, but it is assumed that you
237  // have the global V8 lock if you are using multiple V8 threads.
238  uintptr_t climit() {
239  return thread_local_.climit_;
240  }
241  uintptr_t real_climit() {
242  return thread_local_.real_climit_;
243  }
244  uintptr_t jslimit() {
245  return thread_local_.jslimit_;
246  }
247  uintptr_t real_jslimit() {
248  return thread_local_.real_jslimit_;
249  }
251  return reinterpret_cast<Address>(&thread_local_.jslimit_);
252  }
254  return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
255  }
257 
258  private:
259  StackGuard();
260 
261  // You should hold the ExecutionAccess lock when calling this method.
262  bool has_pending_interrupts(const ExecutionAccess& lock) {
263  // Sanity check: We shouldn't be asking about pending interrupts
264  // unless we're not postponing them anymore.
265  ASSERT(!should_postpone_interrupts(lock));
266  return thread_local_.interrupt_flags_ != 0;
267  }
268 
269  // You should hold the ExecutionAccess lock when calling this method.
270  bool should_postpone_interrupts(const ExecutionAccess& lock) {
271  return thread_local_.postpone_interrupts_nesting_ > 0;
272  }
273 
274  // You should hold the ExecutionAccess lock when calling this method.
275  inline void set_interrupt_limits(const ExecutionAccess& lock);
276 
277  // Reset limits to actual values. For example after handling interrupt.
278  // You should hold the ExecutionAccess lock when calling this method.
279  inline void reset_limits(const ExecutionAccess& lock);
280 
281  // Enable or disable interrupts.
282  void EnableInterrupts();
283  void DisableInterrupts();
284 
285 #if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_ARM64
286  static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
287  static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
288 #else
289  static const uintptr_t kInterruptLimit = 0xfffffffe;
290  static const uintptr_t kIllegalLimit = 0xfffffff8;
291 #endif
292 
293  class ThreadLocal {
294  public:
295  ThreadLocal() { Clear(); }
296  // You should hold the ExecutionAccess lock when you call Initialize or
297  // Clear.
298  void Clear();
299 
300  // Returns true if the heap's stack limits should be set, false if not.
301  bool Initialize(Isolate* isolate);
302 
303  // The stack limit is split into a JavaScript and a C++ stack limit. These
304  // two are the same except when running on a simulator where the C++ and
305  // JavaScript stacks are separate. Each of the two stack limits have two
306  // values. The one eith the real_ prefix is the actual stack limit
307  // set for the VM. The one without the real_ prefix has the same value as
308  // the actual stack limit except when there is an interruption (e.g. debug
309  // break or preemption) in which case it is lowered to make stack checks
310  // fail. Both the generated code and the runtime system check against the
311  // one without the real_ prefix.
312  uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
313  uintptr_t jslimit_;
314  uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
315  uintptr_t climit_;
316 
317  int nesting_;
318  int postpone_interrupts_nesting_;
319  int interrupt_flags_;
320 
321  InterruptCallback interrupt_callback_;
322  void* interrupt_callback_data_;
323  };
324 
325  // TODO(isolates): Technically this could be calculated directly from a
326  // pointer to StackGuard.
327  Isolate* isolate_;
328  ThreadLocal thread_local_;
329 
330  friend class Isolate;
331  friend class StackLimitCheck;
333 
335 };
336 
337 
338 } } // namespace v8::internal
339 
340 #endif // V8_EXECUTION_H_
byte * Address
Definition: globals.h:186
static Handle< Object > New(Handle< JSFunction > func, int argc, Handle< Object > argv[], bool *pending_exception)
Definition: execution.cc:190
static Handle< Object > TryCall(Handle< JSFunction > func, Handle< Object > receiver, int argc, Handle< Object > argv[], bool *caught_exception)
Definition: execution.cc:199
static Handle< Object > ToNumber(Isolate *isolate, Handle< Object > obj, bool *exc)
Definition: execution.cc:713
static Handle< Object > ToObject(Isolate *isolate, Handle< Object > obj, bool *exc)
Definition: execution.cc:731
friend class PostponeInterruptsScope
Definition: execution.h:332
#define V8_UINT64_C(x)
Definition: globals.h:217
static Handle< Object > GetFunctionDelegate(Isolate *isolate, Handle< Object > object)
Definition: execution.cc:238
uintptr_t real_climit()
Definition: execution.h:241
static Handle< Object > NewDate(Isolate *isolate, double time, bool *exc)
Definition: execution.cc:756
#define ASSERT(condition)
Definition: checks.h:329
static Handle< Object > TryGetFunctionDelegate(Isolate *isolate, Handle< Object > object, bool *has_pending_exception)
Definition: execution.cc:265
static Handle< Object > ToDetailString(Isolate *isolate, Handle< Object > obj, bool *exc)
Definition: execution.cc:725
bool IsDeoptMarkedAllocationSites()
Definition: execution.cc:519
friend class StackLimitCheck
Definition: execution.h:331
static Handle< Object > GetFunctionFor()
uintptr_t real_jslimit()
Definition: execution.h:247
static Handle< Object > CharAt(Handle< String > str, uint32_t index)
Definition: execution.cc:777
char * RestoreStackGuard(char *from)
Definition: execution.cc:629
void ClearThread(const ExecutionAccess &lock)
Definition: execution.cc:681
void RequestInterrupt(InterruptCallback callback, void *data)
Definition: execution.cc:570
#define MUST_USE_RESULT
Definition: globals.h:381
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long expose natives in global object expose freeBuffer extension expose gc extension under the specified name expose externalize string extension number of stack frames to capture disable builtin natives files print name of functions for which code is generated use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations always try to OSR functions trace optimize function deoptimization minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions trace debugging JSON request response trace out of bounds accesses to external arrays trace_js_array_abuse automatically set the debug break flag when debugger commands are in the queue abort by crashing maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print statistics of the maximum memory committed for the heap in only print modified registers Don t break for ASM_UNIMPLEMENTED_BREAK macros print stack trace when an illegal exception is thrown randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot testing_bool_flag testing_int_flag string flag tmp file in which to serialize heap Print the time it takes to lazily compile hydrogen code stubs concurrent_recompilation concurrent_sweeping Print usage including flags
Definition: flags.cc:665
char * ArchiveStackGuard(char *to)
Definition: execution.cc:612
static MUST_USE_RESULT MaybeObject * HandleStackGuardInterrupt(Isolate *isolate)
Definition: execution.cc:1006
static void ConfigureInstance(Isolate *isolate, Handle< Object > instance, Handle< Object > data, bool *exc)
Definition: execution.cc:866
Address address_of_jslimit()
Definition: execution.h:250
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:359
static Handle< Object > ToInteger(Isolate *isolate, Handle< Object > obj, bool *exc)
Definition: execution.cc:738
static Handle< Object > TryGetConstructorDelegate(Isolate *isolate, Handle< Object > object, bool *has_pending_exception)
Definition: execution.cc:322
static Handle< JSRegExp > NewJSRegExp(Handle< String > pattern, Handle< String > flags, bool *exc)
Definition: execution.cc:765
static void EnqueueMicrotask(Isolate *isolate, Handle< Object > microtask)
Definition: execution.cc:371
Address address_of_real_jslimit()
Definition: execution.h:253
static int ArchiveSpacePerThread()
Definition: execution.h:197
static Handle< JSFunction > InstantiateFunction(Handle< FunctionTemplateInfo > data, bool *exc)
Definition: execution.cc:807
static Handle< Object > Call(Isolate *isolate, Handle< Object > callable, Handle< Object > receiver, int argc, Handle< Object > argv[], bool *pending_exception, bool convert_receiver=false)
Definition: execution.cc:153
void Continue(InterruptFlag after_what)
Definition: execution.cc:561
static Handle< Object > ToInt32(Isolate *isolate, Handle< Object > obj, bool *exc)
Definition: execution.cc:750
static Handle< Object > GetConstructorDelegate(Isolate *isolate, Handle< Object > object)
Definition: execution.cc:296
void(* InterruptCallback)(Isolate *isolate, void *data)
Definition: v8.h:4083
static Handle< Object > ToUint32(Isolate *isolate, Handle< Object > obj, bool *exc)
Definition: execution.cc:744
static Handle< JSObject > InstantiateObject(Handle< ObjectTemplateInfo > data, bool *exc)
Definition: execution.cc:832
static Handle< String > GetStackTraceLine(Handle< Object > recv, Handle< JSFunction > fun, Handle< Object > pos, Handle< Object > is_global)
Definition: execution.cc:880
HeapObject * obj
static void RunMicrotasks(Isolate *isolate)
Definition: execution.cc:357
void SetStackLimit(uintptr_t limit)
Definition: execution.cc:400
static Handle< Object > ToString(Isolate *isolate, Handle< Object > obj, bool *exc)
Definition: execution.cc:719
void InitThread(const ExecutionAccess &lock)
Definition: execution.cc:687
friend class Isolate
Definition: execution.h:330