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