Imported v8 version 3.7.3 from https://github.com/v8/v8.git
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / execution.h
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.
38 enum InterruptFlag {
39   INTERRUPT = 1 << 0,
40   DEBUGBREAK = 1 << 1,
41   DEBUGCOMMAND = 1 << 2,
42   PREEMPT = 1 << 3,
43   TERMINATE = 1 << 4,
44   RUNTIME_PROFILER_TICK = 1 << 5,
45   GC_REQUEST = 1 << 6
46 };
47
48 class Execution : public AllStatic {
49  public:
50   // Call a function, the caller supplies a receiver and an array
51   // of arguments. Arguments are Object* type. After function returns,
52   // pointers in 'args' might be invalid.
53   //
54   // *pending_exception tells whether the invoke resulted in
55   // a pending exception.
56   //
57   // When convert_receiver is set, and the receiver is not an object,
58   // and the function called is not in strict mode, receiver is converted to
59   // an object.
60   //
61   static Handle<Object> Call(Handle<Object> callable,
62                              Handle<Object> receiver,
63                              int argc,
64                              Handle<Object> argv[],
65                              bool* pending_exception,
66                              bool convert_receiver = false);
67
68   // Construct object from function, the caller supplies an array of
69   // arguments. Arguments are Object* type. After function returns,
70   // pointers in 'args' might be invalid.
71   //
72   // *pending_exception tells whether the invoke resulted in
73   // a pending exception.
74   //
75   static Handle<Object> New(Handle<JSFunction> func,
76                             int argc,
77                             Handle<Object> argv[],
78                             bool* pending_exception);
79
80   // Call a function, just like Call(), but make sure to silently catch
81   // any thrown exceptions. The return value is either the result of
82   // calling the function (if caught exception is false) or the exception
83   // that occurred (if caught exception is true).
84   static Handle<Object> TryCall(Handle<JSFunction> func,
85                                 Handle<Object> receiver,
86                                 int argc,
87                                 Handle<Object> argv[],
88                                 bool* caught_exception);
89
90   // ECMA-262 9.2
91   static Handle<Object> ToBoolean(Handle<Object> obj);
92
93   // ECMA-262 9.3
94   static Handle<Object> ToNumber(Handle<Object> obj, bool* exc);
95
96   // ECMA-262 9.4
97   static Handle<Object> ToInteger(Handle<Object> obj, bool* exc);
98
99   // ECMA-262 9.5
100   static Handle<Object> ToInt32(Handle<Object> obj, bool* exc);
101
102   // ECMA-262 9.6
103   static Handle<Object> ToUint32(Handle<Object> obj, bool* exc);
104
105   // ECMA-262 9.8
106   static Handle<Object> ToString(Handle<Object> obj, bool* exc);
107
108   // ECMA-262 9.8
109   static Handle<Object> ToDetailString(Handle<Object> obj, bool* exc);
110
111   // ECMA-262 9.9
112   static Handle<Object> ToObject(Handle<Object> obj, bool* exc);
113
114   // Create a new date object from 'time'.
115   static Handle<Object> NewDate(double time, bool* exc);
116
117   // Create a new regular expression object from 'pattern' and 'flags'.
118   static Handle<JSRegExp> NewJSRegExp(Handle<String> pattern,
119                                       Handle<String> flags,
120                                       bool* exc);
121
122   // Used to implement [] notation on strings (calls JS code)
123   static Handle<Object> CharAt(Handle<String> str, uint32_t index);
124
125   static Handle<Object> GetFunctionFor();
126   static Handle<JSFunction> InstantiateFunction(
127       Handle<FunctionTemplateInfo> data, bool* exc);
128   static Handle<JSObject> InstantiateObject(Handle<ObjectTemplateInfo> data,
129                                             bool* exc);
130   static void ConfigureInstance(Handle<Object> instance,
131                                 Handle<Object> data,
132                                 bool* exc);
133   static Handle<String> GetStackTraceLine(Handle<Object> recv,
134                                           Handle<JSFunction> fun,
135                                           Handle<Object> pos,
136                                           Handle<Object> is_global);
137 #ifdef ENABLE_DEBUGGER_SUPPORT
138   static Object* DebugBreakHelper();
139   static void ProcessDebugMesssages(bool debug_command_only);
140 #endif
141
142   // If the stack guard is triggered, but it is not an actual
143   // stack overflow, then handle the interruption accordingly.
144   MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt();
145
146   // Get a function delegate (or undefined) for the given non-function
147   // object. Used for support calling objects as functions.
148   static Handle<Object> GetFunctionDelegate(Handle<Object> object);
149   static Handle<Object> TryGetFunctionDelegate(Handle<Object> object,
150                                                bool* has_pending_exception);
151
152   // Get a function delegate (or undefined) for the given non-function
153   // object. Used for support calling objects as constructors.
154   static Handle<Object> GetConstructorDelegate(Handle<Object> object);
155   static Handle<Object> TryGetConstructorDelegate(Handle<Object> object,
156                                                   bool* has_pending_exception);
157 };
158
159
160 class ExecutionAccess;
161 class Isolate;
162
163
164 // StackGuard contains the handling of the limits that are used to limit the
165 // number of nested invocations of JavaScript and the stack size used in each
166 // invocation.
167 class StackGuard {
168  public:
169   // Pass the address beyond which the stack should not grow.  The stack
170   // is assumed to grow downwards.
171   void SetStackLimit(uintptr_t limit);
172
173   // Threading support.
174   char* ArchiveStackGuard(char* to);
175   char* RestoreStackGuard(char* from);
176   static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
177   void FreeThreadResources();
178   // Sets up the default stack guard for this thread if it has not
179   // already been set up.
180   void InitThread(const ExecutionAccess& lock);
181   // Clears the stack guard for this thread so it does not look as if
182   // it has been set up.
183   void ClearThread(const ExecutionAccess& lock);
184
185   bool IsStackOverflow();
186   bool IsPreempted();
187   void Preempt();
188   bool IsInterrupted();
189   void Interrupt();
190   bool IsTerminateExecution();
191   void TerminateExecution();
192   bool IsRuntimeProfilerTick();
193   void RequestRuntimeProfilerTick();
194 #ifdef ENABLE_DEBUGGER_SUPPORT
195   bool IsDebugBreak();
196   void DebugBreak();
197   bool IsDebugCommand();
198   void DebugCommand();
199 #endif
200   bool IsGCRequest();
201   void RequestGC();
202   void Continue(InterruptFlag after_what);
203
204   // This provides an asynchronous read of the stack limits for the current
205   // thread.  There are no locks protecting this, but it is assumed that you
206   // have the global V8 lock if you are using multiple V8 threads.
207   uintptr_t climit() {
208     return thread_local_.climit_;
209   }
210   uintptr_t real_climit() {
211     return thread_local_.real_climit_;
212   }
213   uintptr_t jslimit() {
214     return thread_local_.jslimit_;
215   }
216   uintptr_t real_jslimit() {
217     return thread_local_.real_jslimit_;
218   }
219   Address address_of_jslimit() {
220     return reinterpret_cast<Address>(&thread_local_.jslimit_);
221   }
222   Address address_of_real_jslimit() {
223     return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
224   }
225
226  private:
227   StackGuard();
228
229   // You should hold the ExecutionAccess lock when calling this method.
230   bool has_pending_interrupts(const ExecutionAccess& lock) {
231     // Sanity check: We shouldn't be asking about pending interrupts
232     // unless we're not postponing them anymore.
233     ASSERT(!should_postpone_interrupts(lock));
234     return thread_local_.interrupt_flags_ != 0;
235   }
236
237   // You should hold the ExecutionAccess lock when calling this method.
238   bool should_postpone_interrupts(const ExecutionAccess& lock) {
239     return thread_local_.postpone_interrupts_nesting_ > 0;
240   }
241
242   // You should hold the ExecutionAccess lock when calling this method.
243   inline void set_interrupt_limits(const ExecutionAccess& lock);
244
245   // Reset limits to actual values. For example after handling interrupt.
246   // You should hold the ExecutionAccess lock when calling this method.
247   inline void reset_limits(const ExecutionAccess& lock);
248
249   // Enable or disable interrupts.
250   void EnableInterrupts();
251   void DisableInterrupts();
252
253 #ifdef V8_TARGET_ARCH_X64
254   static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
255   static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
256 #else
257   static const uintptr_t kInterruptLimit = 0xfffffffe;
258   static const uintptr_t kIllegalLimit = 0xfffffff8;
259 #endif
260
261   class ThreadLocal {
262    public:
263     ThreadLocal() { Clear(); }
264     // You should hold the ExecutionAccess lock when you call Initialize or
265     // Clear.
266     void Clear();
267
268     // Returns true if the heap's stack limits should be set, false if not.
269     bool Initialize(Isolate* isolate);
270
271     // The stack limit is split into a JavaScript and a C++ stack limit. These
272     // two are the same except when running on a simulator where the C++ and
273     // JavaScript stacks are separate. Each of the two stack limits have two
274     // values. The one eith the real_ prefix is the actual stack limit
275     // set for the VM. The one without the real_ prefix has the same value as
276     // the actual stack limit except when there is an interruption (e.g. debug
277     // break or preemption) in which case it is lowered to make stack checks
278     // fail. Both the generated code and the runtime system check against the
279     // one without the real_ prefix.
280     uintptr_t real_jslimit_;  // Actual JavaScript stack limit set for the VM.
281     uintptr_t jslimit_;
282     uintptr_t real_climit_;  // Actual C++ stack limit set for the VM.
283     uintptr_t climit_;
284
285     int nesting_;
286     int postpone_interrupts_nesting_;
287     int interrupt_flags_;
288   };
289
290   // TODO(isolates): Technically this could be calculated directly from a
291   //                 pointer to StackGuard.
292   Isolate* isolate_;
293   ThreadLocal thread_local_;
294
295   friend class Isolate;
296   friend class StackLimitCheck;
297   friend class PostponeInterruptsScope;
298
299   DISALLOW_COPY_AND_ASSIGN(StackGuard);
300 };
301
302
303 } }  // namespace v8::internal
304
305 #endif  // V8_EXECUTION_H_