[go: nahoru, domu]

blob: 5cd7141fc260a338c0aee0430fc2d1ac7ff37809 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 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
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
36
37// Flag used to set the interrupt causes.
38enum InterruptFlag {
39 INTERRUPT = 1 << 0,
40 DEBUGBREAK = 1 << 1,
41 DEBUGCOMMAND = 1 << 2,
42 PREEMPT = 1 << 3,
Ben Murdochb0fe1622011-05-05 13:52:32 +010043 TERMINATE = 1 << 4,
44 RUNTIME_PROFILER_TICK = 1 << 5
Steve Blocka7e24c12009-10-30 11:49:00 +000045};
46
47class Execution : public AllStatic {
48 public:
49 // Call a function, the caller supplies a receiver and an array
50 // of arguments. Arguments are Object* type. After function returns,
51 // pointers in 'args' might be invalid.
52 //
53 // *pending_exception tells whether the invoke resulted in
54 // a pending exception.
55 //
Ben Murdoch589d6972011-11-30 16:04:58 +000056 // When convert_receiver is set, and the receiver is not an object,
57 // and the function called is not in strict mode, receiver is converted to
58 // an object.
59 //
Ben Murdoch257744e2011-11-30 15:57:28 +000060 static Handle<Object> Call(Handle<Object> callable,
Steve Blocka7e24c12009-10-30 11:49:00 +000061 Handle<Object> receiver,
62 int argc,
63 Object*** args,
Ben Murdoch589d6972011-11-30 16:04:58 +000064 bool* pending_exception,
65 bool convert_receiver = false);
Steve Blocka7e24c12009-10-30 11:49:00 +000066
67 // Construct object from function, the caller supplies an array of
68 // arguments. Arguments are Object* type. After function returns,
69 // pointers in 'args' might be invalid.
70 //
71 // *pending_exception tells whether the invoke resulted in
72 // a pending exception.
73 //
74 static Handle<Object> New(Handle<JSFunction> func,
75 int argc,
76 Object*** args,
77 bool* pending_exception);
78
79 // Call a function, just like Call(), but make sure to silently catch
80 // any thrown exceptions. The return value is either the result of
81 // calling the function (if caught exception is false) or the exception
82 // that occurred (if caught exception is true).
83 static Handle<Object> TryCall(Handle<JSFunction> func,
84 Handle<Object> receiver,
85 int argc,
86 Object*** args,
87 bool* caught_exception);
88
89 // ECMA-262 9.2
90 static Handle<Object> ToBoolean(Handle<Object> obj);
91
92 // ECMA-262 9.3
93 static Handle<Object> ToNumber(Handle<Object> obj, bool* exc);
94
95 // ECMA-262 9.4
96 static Handle<Object> ToInteger(Handle<Object> obj, bool* exc);
97
98 // ECMA-262 9.5
99 static Handle<Object> ToInt32(Handle<Object> obj, bool* exc);
100
101 // ECMA-262 9.6
102 static Handle<Object> ToUint32(Handle<Object> obj, bool* exc);
103
104 // ECMA-262 9.8
105 static Handle<Object> ToString(Handle<Object> obj, bool* exc);
106
107 // ECMA-262 9.8
108 static Handle<Object> ToDetailString(Handle<Object> obj, bool* exc);
109
110 // ECMA-262 9.9
111 static Handle<Object> ToObject(Handle<Object> obj, bool* exc);
112
113 // Create a new date object from 'time'.
114 static Handle<Object> NewDate(double time, bool* exc);
115
Ben Murdochf87a2032010-10-22 12:50:53 +0100116 // Create a new regular expression object from 'pattern' and 'flags'.
117 static Handle<JSRegExp> NewJSRegExp(Handle<String> pattern,
118 Handle<String> flags,
119 bool* exc);
120
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 // Used to implement [] notation on strings (calls JS code)
122 static Handle<Object> CharAt(Handle<String> str, uint32_t index);
123
124 static Handle<Object> GetFunctionFor();
125 static Handle<JSFunction> InstantiateFunction(
126 Handle<FunctionTemplateInfo> data, bool* exc);
127 static Handle<JSObject> InstantiateObject(Handle<ObjectTemplateInfo> data,
128 bool* exc);
129 static void ConfigureInstance(Handle<Object> instance,
130 Handle<Object> data,
131 bool* exc);
132 static Handle<String> GetStackTraceLine(Handle<Object> recv,
133 Handle<JSFunction> fun,
134 Handle<Object> pos,
135 Handle<Object> is_global);
136#ifdef ENABLE_DEBUGGER_SUPPORT
137 static Object* DebugBreakHelper();
Leon Clarkee46be812010-01-19 14:06:41 +0000138 static void ProcessDebugMesssages(bool debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139#endif
140
141 // If the stack guard is triggered, but it is not an actual
142 // stack overflow, then handle the interruption accordingly.
John Reck59135872010-11-02 12:39:01 -0700143 MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt();
Steve Blocka7e24c12009-10-30 11:49:00 +0000144
145 // Get a function delegate (or undefined) for the given non-function
146 // object. Used for support calling objects as functions.
147 static Handle<Object> GetFunctionDelegate(Handle<Object> object);
Ben Murdoch257744e2011-11-30 15:57:28 +0000148 static Handle<Object> TryGetFunctionDelegate(Handle<Object> object,
149 bool* has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000150
151 // Get a function delegate (or undefined) for the given non-function
152 // object. Used for support calling objects as constructors.
153 static Handle<Object> GetConstructorDelegate(Handle<Object> object);
Ben Murdoch257744e2011-11-30 15:57:28 +0000154 static Handle<Object> TryGetConstructorDelegate(Handle<Object> object,
155 bool* has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000156};
157
158
159class ExecutionAccess;
Steve Block44f0eee2011-05-26 01:26:41 +0100160class Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162
163// StackGuard contains the handling of the limits that are used to limit the
164// number of nested invocations of JavaScript and the stack size used in each
165// invocation.
Steve Block44f0eee2011-05-26 01:26:41 +0100166class StackGuard {
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 public:
168 // Pass the address beyond which the stack should not grow. The stack
169 // is assumed to grow downwards.
Steve Block44f0eee2011-05-26 01:26:41 +0100170 void SetStackLimit(uintptr_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000171
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 // Threading support.
Steve Block44f0eee2011-05-26 01:26:41 +0100173 char* ArchiveStackGuard(char* to);
174 char* RestoreStackGuard(char* from);
175 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
176 void FreeThreadResources();
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 // Sets up the default stack guard for this thread if it has not
178 // already been set up.
Steve Block44f0eee2011-05-26 01:26:41 +0100179 void InitThread(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 // Clears the stack guard for this thread so it does not look as if
181 // it has been set up.
Steve Block44f0eee2011-05-26 01:26:41 +0100182 void ClearThread(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
Steve Block44f0eee2011-05-26 01:26:41 +0100184 bool IsStackOverflow();
185 bool IsPreempted();
186 void Preempt();
187 bool IsInterrupted();
188 void Interrupt();
189 bool IsTerminateExecution();
190 void TerminateExecution();
191 bool IsRuntimeProfilerTick();
192 void RequestRuntimeProfilerTick();
Steve Blocka7e24c12009-10-30 11:49:00 +0000193#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100194 bool IsDebugBreak();
195 void DebugBreak();
196 bool IsDebugCommand();
197 void DebugCommand();
Steve Blocka7e24c12009-10-30 11:49:00 +0000198#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100199 void Continue(InterruptFlag after_what);
Steve Blocka7e24c12009-10-30 11:49:00 +0000200
Steve Blockd0582a62009-12-15 09:54:21 +0000201 // This provides an asynchronous read of the stack limits for the current
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 // thread. There are no locks protecting this, but it is assumed that you
203 // have the global V8 lock if you are using multiple V8 threads.
Steve Block44f0eee2011-05-26 01:26:41 +0100204 uintptr_t climit() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 return thread_local_.climit_;
206 }
Steve Block44f0eee2011-05-26 01:26:41 +0100207 uintptr_t real_climit() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800208 return thread_local_.real_climit_;
209 }
Steve Block44f0eee2011-05-26 01:26:41 +0100210 uintptr_t jslimit() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 return thread_local_.jslimit_;
212 }
Steve Block44f0eee2011-05-26 01:26:41 +0100213 uintptr_t real_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000214 return thread_local_.real_jslimit_;
215 }
Steve Block44f0eee2011-05-26 01:26:41 +0100216 Address address_of_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000217 return reinterpret_cast<Address>(&thread_local_.jslimit_);
218 }
Steve Block44f0eee2011-05-26 01:26:41 +0100219 Address address_of_real_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000220 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
221 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000222
223 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100224 StackGuard();
225
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100227 bool has_pending_interrupts(const ExecutionAccess& lock) {
Steve Block6ded16b2010-05-10 14:33:55 +0100228 // Sanity check: We shouldn't be asking about pending interrupts
229 // unless we're not postponing them anymore.
230 ASSERT(!should_postpone_interrupts(lock));
231 return thread_local_.interrupt_flags_ != 0;
232 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000233
234 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100235 bool should_postpone_interrupts(const ExecutionAccess& lock) {
Steve Block6ded16b2010-05-10 14:33:55 +0100236 return thread_local_.postpone_interrupts_nesting_ > 0;
237 }
238
239 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100240 inline void set_interrupt_limits(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000241
Steve Blockd0582a62009-12-15 09:54:21 +0000242 // Reset limits to actual values. For example after handling interrupt.
Steve Blocka7e24c12009-10-30 11:49:00 +0000243 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100244 inline void reset_limits(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000245
246 // Enable or disable interrupts.
Steve Block44f0eee2011-05-26 01:26:41 +0100247 void EnableInterrupts();
248 void DisableInterrupts();
Steve Blocka7e24c12009-10-30 11:49:00 +0000249
Steve Blocka7e24c12009-10-30 11:49:00 +0000250#ifdef V8_TARGET_ARCH_X64
251 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
252 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
253#else
254 static const uintptr_t kInterruptLimit = 0xfffffffe;
255 static const uintptr_t kIllegalLimit = 0xfffffff8;
256#endif
257
258 class ThreadLocal {
259 public:
260 ThreadLocal() { Clear(); }
261 // You should hold the ExecutionAccess lock when you call Initialize or
262 // Clear.
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 void Clear();
Steve Blockd0582a62009-12-15 09:54:21 +0000264
Steve Block44f0eee2011-05-26 01:26:41 +0100265 // Returns true if the heap's stack limits should be set, false if not.
Ben Murdoch257744e2011-11-30 15:57:28 +0000266 bool Initialize(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100267
Steve Blockd0582a62009-12-15 09:54:21 +0000268 // The stack limit is split into a JavaScript and a C++ stack limit. These
269 // two are the same except when running on a simulator where the C++ and
270 // JavaScript stacks are separate. Each of the two stack limits have two
271 // values. The one eith the real_ prefix is the actual stack limit
272 // set for the VM. The one without the real_ prefix has the same value as
273 // the actual stack limit except when there is an interruption (e.g. debug
274 // break or preemption) in which case it is lowered to make stack checks
275 // fail. Both the generated code and the runtime system check against the
276 // one without the real_ prefix.
277 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 uintptr_t jslimit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000279 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 uintptr_t climit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000281
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 int nesting_;
283 int postpone_interrupts_nesting_;
284 int interrupt_flags_;
285 };
286
Steve Block44f0eee2011-05-26 01:26:41 +0100287 // TODO(isolates): Technically this could be calculated directly from a
288 // pointer to StackGuard.
289 Isolate* isolate_;
290 ThreadLocal thread_local_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000291
Steve Block44f0eee2011-05-26 01:26:41 +0100292 friend class Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 friend class StackLimitCheck;
294 friend class PostponeInterruptsScope;
Steve Block44f0eee2011-05-26 01:26:41 +0100295
296 DISALLOW_COPY_AND_ASSIGN(StackGuard);
Steve Blocka7e24c12009-10-30 11:49:00 +0000297};
298
299
Steve Blocka7e24c12009-10-30 11:49:00 +0000300} } // namespace v8::internal
301
302#endif // V8_EXECUTION_H_