1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_EXECUTION_H_
6 #define V8_EXECUTION_H_
8 #include "src/allocation.h"
9 #include "src/base/atomicops.h"
10 #include "src/handles.h"
11 #include "src/utils.h"
16 // Forward declarations.
19 class Execution final : public AllStatic {
21 // Call a function, the caller supplies a receiver and an array
22 // of arguments. Arguments are Object* type. After function returns,
23 // pointers in 'args' might be invalid.
25 // *pending_exception tells whether the invoke resulted in
26 // a pending exception.
28 // When convert_receiver is set, and the receiver is not an object,
29 // and the function called is not in strict mode, receiver is converted to
32 MUST_USE_RESULT static MaybeHandle<Object> Call(
34 Handle<Object> callable,
35 Handle<Object> receiver,
37 Handle<Object> argv[],
38 bool convert_receiver = false);
40 // Construct object from function, the caller supplies an array of
41 // arguments. Arguments are Object* type. After function returns,
42 // pointers in 'args' might be invalid.
44 // *pending_exception tells whether the invoke resulted in
45 // a pending exception.
47 MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> func,
49 Handle<Object> argv[]);
51 // Call a function, just like Call(), but make sure to silently catch
52 // any thrown exceptions. The return value is either the result of
53 // calling the function (if caught exception is false) or the exception
54 // that occurred (if caught exception is true).
55 // In the exception case, exception_out holds the caught exceptions, unless
56 // it is a termination exception.
57 static MaybeHandle<Object> TryCall(Handle<JSFunction> func,
58 Handle<Object> receiver, int argc,
59 Handle<Object> argv[],
60 MaybeHandle<Object>* exception_out = NULL);
63 MUST_USE_RESULT static MaybeHandle<Object> ToNumber(
64 Isolate* isolate, Handle<Object> obj);
67 MUST_USE_RESULT static MaybeHandle<Object> ToInteger(
68 Isolate* isolate, Handle<Object> obj);
71 MUST_USE_RESULT static MaybeHandle<Object> ToInt32(
72 Isolate* isolate, Handle<Object> obj);
75 MUST_USE_RESULT static MaybeHandle<Object> ToUint32(
76 Isolate* isolate, Handle<Object> obj);
79 // ES6, draft 10-14-14, section 7.1.15
80 MUST_USE_RESULT static MaybeHandle<Object> ToLength(
81 Isolate* isolate, Handle<Object> obj);
84 MUST_USE_RESULT static MaybeHandle<Object> ToString(
85 Isolate* isolate, Handle<Object> obj);
88 MUST_USE_RESULT static MaybeHandle<Object> ToDetailString(
89 Isolate* isolate, Handle<Object> obj);
92 MUST_USE_RESULT static MaybeHandle<Object> ToObject(
93 Isolate* isolate, Handle<Object> obj);
95 // Create a new date object from 'time'.
96 MUST_USE_RESULT static MaybeHandle<Object> NewDate(
97 Isolate* isolate, double time);
99 // Create a new regular expression object from 'pattern' and 'flags'.
100 MUST_USE_RESULT static MaybeHandle<JSRegExp> NewJSRegExp(
101 Handle<String> pattern, Handle<String> flags);
103 static Handle<Object> GetFunctionFor();
104 static Handle<String> GetStackTraceLine(Handle<Object> recv,
105 Handle<JSFunction> fun,
107 Handle<Object> is_global);
109 // Get a function delegate (or undefined) for the given non-function
110 // object. Used for support calling objects as functions.
111 static Handle<Object> GetFunctionDelegate(Isolate* isolate,
112 Handle<Object> object);
113 MUST_USE_RESULT static MaybeHandle<Object> TryGetFunctionDelegate(
115 Handle<Object> object);
117 // Get a function delegate (or undefined) for the given non-function
118 // object. Used for support calling objects as constructors.
119 static Handle<Object> GetConstructorDelegate(Isolate* isolate,
120 Handle<Object> object);
121 static MaybeHandle<Object> TryGetConstructorDelegate(Isolate* isolate,
122 Handle<Object> object);
126 class ExecutionAccess;
127 class PostponeInterruptsScope;
130 // StackGuard contains the handling of the limits that are used to limit the
131 // number of nested invocations of JavaScript and the stack size used in each
133 class StackGuard final {
135 // Pass the address beyond which the stack should not grow. The stack
136 // is assumed to grow downwards.
137 void SetStackLimit(uintptr_t limit);
139 // The simulator uses a separate JS stack. Limits on the JS stack might have
140 // to be adjusted in order to reflect overflows of the C stack, because we
141 // cannot rely on the interleaving of frames on the simulator.
142 void AdjustStackLimitForSimulator();
144 // Threading support.
145 char* ArchiveStackGuard(char* to);
146 char* RestoreStackGuard(char* from);
147 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
148 void FreeThreadResources();
149 // Sets up the default stack guard for this thread if it has not
150 // already been set up.
151 void InitThread(const ExecutionAccess& lock);
152 // Clears the stack guard for this thread so it does not look as if
153 // it has been set up.
154 void ClearThread(const ExecutionAccess& lock);
156 #define INTERRUPT_LIST(V) \
157 V(DEBUGBREAK, DebugBreak, 0) \
158 V(DEBUGCOMMAND, DebugCommand, 1) \
159 V(TERMINATE_EXECUTION, TerminateExecution, 2) \
160 V(GC_REQUEST, GC, 3) \
161 V(INSTALL_CODE, InstallCode, 4) \
162 V(API_INTERRUPT, ApiInterrupt, 5) \
163 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6)
165 #define V(NAME, Name, id) \
166 inline bool Check##Name() { return CheckInterrupt(NAME); } \
167 inline void Request##Name() { RequestInterrupt(NAME); } \
168 inline void Clear##Name() { ClearInterrupt(NAME); }
172 // Flag used to set the interrupt causes.
174 #define V(NAME, Name, id) NAME = (1 << id),
177 #define V(NAME, Name, id) NAME |
178 ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
182 uintptr_t climit() { return thread_local_.climit(); }
183 uintptr_t jslimit() { return thread_local_.jslimit(); }
184 // This provides an asynchronous read of the stack limits for the current
185 // thread. There are no locks protecting this, but it is assumed that you
186 // have the global V8 lock if you are using multiple V8 threads.
187 uintptr_t real_climit() {
188 return thread_local_.real_climit_;
190 uintptr_t real_jslimit() {
191 return thread_local_.real_jslimit_;
193 Address address_of_jslimit() {
194 return reinterpret_cast<Address>(&thread_local_.jslimit_);
196 Address address_of_real_jslimit() {
197 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
200 // If the stack guard is triggered, but it is not an actual
201 // stack overflow, then handle the interruption accordingly.
202 Object* HandleInterrupts();
203 void HandleGCInterrupt();
208 bool CheckInterrupt(InterruptFlag flag);
209 void RequestInterrupt(InterruptFlag flag);
210 void ClearInterrupt(InterruptFlag flag);
211 bool CheckAndClearInterrupt(InterruptFlag flag);
213 // You should hold the ExecutionAccess lock when calling this method.
214 bool has_pending_interrupts(const ExecutionAccess& lock) {
215 return thread_local_.interrupt_flags_ != 0;
218 // You should hold the ExecutionAccess lock when calling this method.
219 inline void set_interrupt_limits(const ExecutionAccess& lock);
221 // Reset limits to actual values. For example after handling interrupt.
222 // You should hold the ExecutionAccess lock when calling this method.
223 inline void reset_limits(const ExecutionAccess& lock);
225 // Enable or disable interrupts.
226 void EnableInterrupts();
227 void DisableInterrupts();
229 #if V8_TARGET_ARCH_64_BIT
230 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
231 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
233 static const uintptr_t kInterruptLimit = 0xfffffffe;
234 static const uintptr_t kIllegalLimit = 0xfffffff8;
237 void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
238 void PopPostponeInterruptsScope();
240 class ThreadLocal final {
242 ThreadLocal() { Clear(); }
243 // You should hold the ExecutionAccess lock when you call Initialize or
247 // Returns true if the heap's stack limits should be set, false if not.
248 bool Initialize(Isolate* isolate);
250 // The stack limit is split into a JavaScript and a C++ stack limit. These
251 // two are the same except when running on a simulator where the C++ and
252 // JavaScript stacks are separate. Each of the two stack limits have two
253 // values. The one eith the real_ prefix is the actual stack limit
254 // set for the VM. The one without the real_ prefix has the same value as
255 // the actual stack limit except when there is an interruption (e.g. debug
256 // break or preemption) in which case it is lowered to make stack checks
257 // fail. Both the generated code and the runtime system check against the
258 // one without the real_ prefix.
259 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
260 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
262 // jslimit_ and climit_ can be read without any lock.
263 // Writing requires the ExecutionAccess lock.
264 base::AtomicWord jslimit_;
265 base::AtomicWord climit_;
267 uintptr_t jslimit() {
268 return bit_cast<uintptr_t>(base::NoBarrier_Load(&jslimit_));
270 void set_jslimit(uintptr_t limit) {
271 return base::NoBarrier_Store(&jslimit_,
272 static_cast<base::AtomicWord>(limit));
275 return bit_cast<uintptr_t>(base::NoBarrier_Load(&climit_));
277 void set_climit(uintptr_t limit) {
278 return base::NoBarrier_Store(&climit_,
279 static_cast<base::AtomicWord>(limit));
282 PostponeInterruptsScope* postpone_interrupts_;
283 int interrupt_flags_;
286 // TODO(isolates): Technically this could be calculated directly from a
287 // pointer to StackGuard.
289 ThreadLocal thread_local_;
291 friend class Isolate;
292 friend class StackLimitCheck;
293 friend class PostponeInterruptsScope;
295 DISALLOW_COPY_AND_ASSIGN(StackGuard);
298 } } // namespace v8::internal
300 #endif // V8_EXECUTION_H_