v8  3.14.5(node0.10.28)
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,
45  GC_REQUEST = 1 << 6,
46  CODE_READY = 1 << 7
47 };
48 
49 
50 class Isolate;
51 
52 
53 class Execution : public AllStatic {
54  public:
55  // Call a function, the caller supplies a receiver and an array
56  // of arguments. Arguments are Object* type. After function returns,
57  // pointers in 'args' might be invalid.
58  //
59  // *pending_exception tells whether the invoke resulted in
60  // a pending exception.
61  //
62  // When convert_receiver is set, and the receiver is not an object,
63  // and the function called is not in strict mode, receiver is converted to
64  // an object.
65  //
66  static Handle<Object> Call(Handle<Object> callable,
67  Handle<Object> receiver,
68  int argc,
69  Handle<Object> argv[],
70  bool* pending_exception,
71  bool convert_receiver = false);
72 
73  // Construct object from function, the caller supplies an array of
74  // arguments. Arguments are Object* type. After function returns,
75  // pointers in 'args' might be invalid.
76  //
77  // *pending_exception tells whether the invoke resulted in
78  // a pending exception.
79  //
81  int argc,
82  Handle<Object> argv[],
83  bool* pending_exception);
84 
85  // Call a function, just like Call(), but make sure to silently catch
86  // any thrown exceptions. The return value is either the result of
87  // calling the function (if caught exception is false) or the exception
88  // that occurred (if caught exception is true).
90  Handle<Object> receiver,
91  int argc,
92  Handle<Object> argv[],
93  bool* caught_exception);
94 
95  // ECMA-262 9.2
97 
98  // ECMA-262 9.3
99  static Handle<Object> ToNumber(Handle<Object> obj, bool* exc);
100 
101  // ECMA-262 9.4
102  static Handle<Object> ToInteger(Handle<Object> obj, bool* exc);
103 
104  // ECMA-262 9.5
105  static Handle<Object> ToInt32(Handle<Object> obj, bool* exc);
106 
107  // ECMA-262 9.6
108  static Handle<Object> ToUint32(Handle<Object> obj, bool* exc);
109 
110  // ECMA-262 9.8
111  static Handle<Object> ToString(Handle<Object> obj, bool* exc);
112 
113  // ECMA-262 9.8
114  static Handle<Object> ToDetailString(Handle<Object> obj, bool* exc);
115 
116  // ECMA-262 9.9
117  static Handle<Object> ToObject(Handle<Object> obj, bool* exc);
118 
119  // Create a new date object from 'time'.
120  static Handle<Object> NewDate(double time, bool* exc);
121 
122  // Create a new regular expression object from 'pattern' and 'flags'.
125  bool* exc);
126 
127  // Used to implement [] notation on strings (calls JS code)
128  static Handle<Object> CharAt(Handle<String> str, uint32_t index);
129 
132  Handle<FunctionTemplateInfo> data, bool* exc);
134  bool* exc);
135  static void ConfigureInstance(Handle<Object> instance,
136  Handle<Object> data,
137  bool* exc);
139  Handle<JSFunction> fun,
140  Handle<Object> pos,
141  Handle<Object> is_global);
142 #ifdef ENABLE_DEBUGGER_SUPPORT
143  static Object* DebugBreakHelper();
144  static void ProcessDebugMessages(bool debug_command_only);
145 #endif
146 
147  // If the stack guard is triggered, but it is not an actual
148  // stack overflow, then handle the interruption accordingly.
149  MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt(
150  Isolate* isolate);
151 
152  // Get a function delegate (or undefined) for the given non-function
153  // object. Used for support calling objects as functions.
156  bool* has_pending_exception);
157 
158  // Get a function delegate (or undefined) for the given non-function
159  // object. Used for support calling objects as constructors.
162  bool* has_pending_exception);
163 };
164 
165 
166 class ExecutionAccess;
167 
168 
169 // StackGuard contains the handling of the limits that are used to limit the
170 // number of nested invocations of JavaScript and the stack size used in each
171 // invocation.
172 class StackGuard {
173  public:
174  // Pass the address beyond which the stack should not grow. The stack
175  // is assumed to grow downwards.
176  void SetStackLimit(uintptr_t limit);
177 
178  // Threading support.
179  char* ArchiveStackGuard(char* to);
180  char* RestoreStackGuard(char* from);
181  static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
182  void FreeThreadResources();
183  // Sets up the default stack guard for this thread if it has not
184  // already been set up.
185  void InitThread(const ExecutionAccess& lock);
186  // Clears the stack guard for this thread so it does not look as if
187  // it has been set up.
188  void ClearThread(const ExecutionAccess& lock);
189 
190  bool IsStackOverflow();
191  bool IsPreempted();
192  void Preempt();
193  bool IsInterrupted();
194  void Interrupt();
195  bool IsTerminateExecution();
196  void TerminateExecution();
197  bool IsRuntimeProfilerTick();
199  bool IsCodeReadyEvent();
200  void RequestCodeReadyEvent();
201 #ifdef ENABLE_DEBUGGER_SUPPORT
202  bool IsDebugBreak();
203  void DebugBreak();
204  bool IsDebugCommand();
205  void DebugCommand();
206 #endif
207  bool IsGCRequest();
208  void RequestGC();
209  void Continue(InterruptFlag after_what);
210 
211  // This provides an asynchronous read of the stack limits for the current
212  // thread. There are no locks protecting this, but it is assumed that you
213  // have the global V8 lock if you are using multiple V8 threads.
214  uintptr_t climit() {
215  return thread_local_.climit_;
216  }
217  uintptr_t real_climit() {
218  return thread_local_.real_climit_;
219  }
220  uintptr_t jslimit() {
221  return thread_local_.jslimit_;
222  }
223  uintptr_t real_jslimit() {
224  return thread_local_.real_jslimit_;
225  }
227  return reinterpret_cast<Address>(&thread_local_.jslimit_);
228  }
230  return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
231  }
233 
234  private:
235  StackGuard();
236 
237  // You should hold the ExecutionAccess lock when calling this method.
238  bool has_pending_interrupts(const ExecutionAccess& lock) {
239  // Sanity check: We shouldn't be asking about pending interrupts
240  // unless we're not postponing them anymore.
241  ASSERT(!should_postpone_interrupts(lock));
242  return thread_local_.interrupt_flags_ != 0;
243  }
244 
245  // You should hold the ExecutionAccess lock when calling this method.
246  bool should_postpone_interrupts(const ExecutionAccess& lock) {
247  return thread_local_.postpone_interrupts_nesting_ > 0;
248  }
249 
250  // You should hold the ExecutionAccess lock when calling this method.
251  inline void set_interrupt_limits(const ExecutionAccess& lock);
252 
253  // Reset limits to actual values. For example after handling interrupt.
254  // You should hold the ExecutionAccess lock when calling this method.
255  inline void reset_limits(const ExecutionAccess& lock);
256 
257  // Enable or disable interrupts.
258  void EnableInterrupts();
259  void DisableInterrupts();
260 
261 #ifdef V8_TARGET_ARCH_X64
262  static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
263  static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
264 #else
265  static const uintptr_t kInterruptLimit = 0xfffffffe;
266  static const uintptr_t kIllegalLimit = 0xfffffff8;
267 #endif
268 
269  class ThreadLocal {
270  public:
271  ThreadLocal() { Clear(); }
272  // You should hold the ExecutionAccess lock when you call Initialize or
273  // Clear.
274  void Clear();
275 
276  // Returns true if the heap's stack limits should be set, false if not.
277  bool Initialize(Isolate* isolate);
278 
279  // The stack limit is split into a JavaScript and a C++ stack limit. These
280  // two are the same except when running on a simulator where the C++ and
281  // JavaScript stacks are separate. Each of the two stack limits have two
282  // values. The one eith the real_ prefix is the actual stack limit
283  // set for the VM. The one without the real_ prefix has the same value as
284  // the actual stack limit except when there is an interruption (e.g. debug
285  // break or preemption) in which case it is lowered to make stack checks
286  // fail. Both the generated code and the runtime system check against the
287  // one without the real_ prefix.
288  uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
289  uintptr_t jslimit_;
290  uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
291  uintptr_t climit_;
292 
293  int nesting_;
294  int postpone_interrupts_nesting_;
295  int interrupt_flags_;
296  };
297 
298  // TODO(isolates): Technically this could be calculated directly from a
299  // pointer to StackGuard.
300  Isolate* isolate_;
301  ThreadLocal thread_local_;
302 
303  friend class Isolate;
304  friend class StackLimitCheck;
306 
308 };
309 
310 
311 } } // namespace v8::internal
312 
313 #endif // V8_EXECUTION_H_
byte * Address
Definition: globals.h:157
static Handle< Object > New(Handle< JSFunction > func, int argc, Handle< Object > argv[], bool *pending_exception)
Definition: execution.cc:183
static Handle< Object > TryCall(Handle< JSFunction > func, Handle< Object > receiver, int argc, Handle< Object > argv[], bool *caught_exception)
Definition: execution.cc:192
static Handle< Object > ToUint32(Handle< Object > obj, bool *exc)
Definition: execution.cc:660
friend class PostponeInterruptsScope
Definition: execution.h:305
static Handle< Object > ToInteger(Handle< Object > obj, bool *exc)
Definition: execution.cc:655
static Handle< Object > GetConstructorDelegate(Handle< Object > object)
Definition: execution.cc:287
static Handle< Object > ToObject(Handle< Object > obj, bool *exc)
Definition: execution.cc:649
uintptr_t real_climit()
Definition: execution.h:217
static Handle< Object > ToInt32(Handle< Object > obj, bool *exc)
Definition: execution.cc:665
#define ASSERT(condition)
Definition: checks.h:270
static void ConfigureInstance(Handle< Object > instance, Handle< Object > data, bool *exc)
Definition: execution.cc:777
static Handle< Object > ToDetailString(Handle< Object > obj, bool *exc)
Definition: execution.cc:644
friend class StackLimitCheck
Definition: execution.h:304
static Handle< Object > GetFunctionFor()
uintptr_t real_jslimit()
Definition: execution.h:223
static Handle< Object > CharAt(Handle< String > str, uint32_t index)
Definition: execution.cc:691
static Handle< Object > ToBoolean(Handle< Object > obj)
Definition: execution.cc:618
char * RestoreStackGuard(char *from)
Definition: execution.cc:539
void ClearThread(const ExecutionAccess &lock)
Definition: execution.cc:586
#define MUST_USE_RESULT
Definition: globals.h:346
char * ArchiveStackGuard(char *to)
Definition: execution.cc:522
static MUST_USE_RESULT MaybeObject * HandleStackGuardInterrupt(Isolate *isolate)
Definition: execution.cc:921
Address address_of_jslimit()
Definition: execution.h:226
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:307
static Handle< JSRegExp > NewJSRegExp(Handle< String > pattern, Handle< String > flags, bool *exc)
Definition: execution.cc:679
static Handle< Object > NewDate(double time, bool *exc)
Definition: execution.cc:670
static Handle< Object > Call(Handle< Object > callable, Handle< Object > receiver, int argc, Handle< Object > argv[], bool *pending_exception, bool convert_receiver=false)
Definition: execution.cc:150
Address address_of_real_jslimit()
Definition: execution.h:229
static int ArchiveSpacePerThread()
Definition: execution.h:181
static Handle< JSFunction > InstantiateFunction(Handle< FunctionTemplateInfo > data, bool *exc)
Definition: execution.cc:722
void Continue(InterruptFlag after_what)
Definition: execution.cc:513
static Handle< Object > GetFunctionDelegate(Handle< Object > object)
Definition: execution.cc:229
static Handle< Object > ToNumber(Handle< Object > obj, bool *exc)
Definition: execution.cc:634
static Handle< JSObject > InstantiateObject(Handle< ObjectTemplateInfo > data, bool *exc)
Definition: execution.cc:744
static Handle< String > GetStackTraceLine(Handle< Object > recv, Handle< JSFunction > fun, Handle< Object > pos, Handle< Object > is_global)
Definition: execution.cc:790
static Handle< Object > ToString(Handle< Object > obj, bool *exc)
Definition: execution.cc:639
static Handle< Object > TryGetConstructorDelegate(Handle< Object > object, bool *has_pending_exception)
Definition: execution.cc:313
void SetStackLimit(uintptr_t limit)
Definition: execution.cc:363
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit 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 SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available 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 MIPS FPU instructions if expose natives in global object expose gc extension number of stack frames to capture disable builtin natives files print a stack trace if an assertion failure occurs use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations prepare for turning on always opt minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions automatically set the debug break flag when debugger commands are in the queue always cause a debug break before aborting 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 more details following each garbage collection print amount of external allocated memory after each time it is adjusted flush code that we expect not to use again before full gc do incremental marking steps track object counts and memory usage use caching Perform compaction on every full GC Never perform compaction on full GC testing only Compact code space on full incremental collections Default seed for initializing random allows verbose printing trace parsing and preparsing Check icache flushes in ARM and MIPS simulator Stack alingment in bytes in print stack trace when throwing exceptions randomize hashes to avoid predictable hash Fixed seed to use to hash property activate a timer that switches between V8 threads testing_bool_flag float flag Seed used for threading test randomness A filename with extra code to be included in the Print usage including flags
Definition: flags.cc:495
static Handle< Object > TryGetFunctionDelegate(Handle< Object > object, bool *has_pending_exception)
Definition: execution.cc:256
void InitThread(const ExecutionAccess &lock)
Definition: execution.cc:592
friend class Isolate
Definition: execution.h:303