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/handles.h"
13 class Execution FINAL : public AllStatic {
15 // Call a function, the caller supplies a receiver and an array
16 // of arguments. Arguments are Object* type. After function returns,
17 // pointers in 'args' might be invalid.
19 // *pending_exception tells whether the invoke resulted in
20 // a pending exception.
22 // When convert_receiver is set, and the receiver is not an object,
23 // and the function called is not in strict mode, receiver is converted to
26 MUST_USE_RESULT static MaybeHandle<Object> Call(
28 Handle<Object> callable,
29 Handle<Object> receiver,
31 Handle<Object> argv[],
32 bool convert_receiver = false);
34 // Construct object from function, the caller supplies an array of
35 // arguments. Arguments are Object* type. After function returns,
36 // pointers in 'args' might be invalid.
38 // *pending_exception tells whether the invoke resulted in
39 // a pending exception.
41 MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> func,
43 Handle<Object> argv[]);
45 // Call a function, just like Call(), but make sure to silently catch
46 // any thrown exceptions. The return value is either the result of
47 // calling the function (if caught exception is false) or the exception
48 // that occurred (if caught exception is true).
49 // In the exception case, exception_out holds the caught exceptions, unless
50 // it is a termination exception.
51 static MaybeHandle<Object> TryCall(Handle<JSFunction> func,
52 Handle<Object> receiver, int argc,
53 Handle<Object> argv[],
54 MaybeHandle<Object>* exception_out = NULL);
57 MUST_USE_RESULT static MaybeHandle<Object> ToNumber(
58 Isolate* isolate, Handle<Object> obj);
61 MUST_USE_RESULT static MaybeHandle<Object> ToInteger(
62 Isolate* isolate, Handle<Object> obj);
65 MUST_USE_RESULT static MaybeHandle<Object> ToInt32(
66 Isolate* isolate, Handle<Object> obj);
69 MUST_USE_RESULT static MaybeHandle<Object> ToUint32(
70 Isolate* isolate, Handle<Object> obj);
73 // ES6, draft 10-14-14, section 7.1.15
74 MUST_USE_RESULT static MaybeHandle<Object> ToLength(
75 Isolate* isolate, Handle<Object> obj);
78 MUST_USE_RESULT static MaybeHandle<Object> ToString(
79 Isolate* isolate, Handle<Object> obj);
82 MUST_USE_RESULT static MaybeHandle<Object> ToDetailString(
83 Isolate* isolate, Handle<Object> obj);
86 MUST_USE_RESULT static MaybeHandle<Object> ToObject(
87 Isolate* isolate, Handle<Object> obj);
89 // Create a new date object from 'time'.
90 MUST_USE_RESULT static MaybeHandle<Object> NewDate(
91 Isolate* isolate, double time);
93 // Create a new regular expression object from 'pattern' and 'flags'.
94 MUST_USE_RESULT static MaybeHandle<JSRegExp> NewJSRegExp(
95 Handle<String> pattern, Handle<String> flags);
97 // Used to implement [] notation on strings (calls JS code)
98 static Handle<Object> CharAt(Handle<String> str, uint32_t index);
100 static Handle<Object> GetFunctionFor();
101 static Handle<String> GetStackTraceLine(Handle<Object> recv,
102 Handle<JSFunction> fun,
104 Handle<Object> is_global);
106 // Get a function delegate (or undefined) for the given non-function
107 // object. Used for support calling objects as functions.
108 static Handle<Object> GetFunctionDelegate(Isolate* isolate,
109 Handle<Object> object);
110 MUST_USE_RESULT static MaybeHandle<Object> TryGetFunctionDelegate(
112 Handle<Object> object);
114 // Get a function delegate (or undefined) for the given non-function
115 // object. Used for support calling objects as constructors.
116 static Handle<Object> GetConstructorDelegate(Isolate* isolate,
117 Handle<Object> object);
118 static MaybeHandle<Object> TryGetConstructorDelegate(Isolate* isolate,
119 Handle<Object> object);
123 class ExecutionAccess;
124 class PostponeInterruptsScope;
127 // StackGuard contains the handling of the limits that are used to limit the
128 // number of nested invocations of JavaScript and the stack size used in each
130 class StackGuard FINAL {
132 // Pass the address beyond which the stack should not grow. The stack
133 // is assumed to grow downwards.
134 void SetStackLimit(uintptr_t limit);
136 // Threading support.
137 char* ArchiveStackGuard(char* to);
138 char* RestoreStackGuard(char* from);
139 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
140 void FreeThreadResources();
141 // Sets up the default stack guard for this thread if it has not
142 // already been set up.
143 void InitThread(const ExecutionAccess& lock);
144 // Clears the stack guard for this thread so it does not look as if
145 // it has been set up.
146 void ClearThread(const ExecutionAccess& lock);
148 #define INTERRUPT_LIST(V) \
149 V(DEBUGBREAK, DebugBreak, 0) \
150 V(DEBUGCOMMAND, DebugCommand, 1) \
151 V(TERMINATE_EXECUTION, TerminateExecution, 2) \
152 V(GC_REQUEST, GC, 3) \
153 V(INSTALL_CODE, InstallCode, 4) \
154 V(API_INTERRUPT, ApiInterrupt, 5) \
155 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6)
157 #define V(NAME, Name, id) \
158 inline bool Check##Name() { return CheckInterrupt(NAME); } \
159 inline void Request##Name() { RequestInterrupt(NAME); } \
160 inline void Clear##Name() { ClearInterrupt(NAME); }
164 // Flag used to set the interrupt causes.
166 #define V(NAME, Name, id) NAME = (1 << id),
169 #define V(NAME, Name, id) NAME |
170 ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
174 // This provides an asynchronous read of the stack limits for the current
175 // thread. There are no locks protecting this, but it is assumed that you
176 // have the global V8 lock if you are using multiple V8 threads.
178 return thread_local_.climit_;
180 uintptr_t real_climit() {
181 return thread_local_.real_climit_;
183 uintptr_t jslimit() {
184 return thread_local_.jslimit_;
186 uintptr_t real_jslimit() {
187 return thread_local_.real_jslimit_;
189 Address address_of_jslimit() {
190 return reinterpret_cast<Address>(&thread_local_.jslimit_);
192 Address address_of_real_jslimit() {
193 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
196 // If the stack guard is triggered, but it is not an actual
197 // stack overflow, then handle the interruption accordingly.
198 Object* HandleInterrupts();
200 bool InterruptRequested() { return GetCurrentStackPosition() < climit(); }
202 void CheckAndHandleGCInterrupt();
207 bool CheckInterrupt(InterruptFlag flag);
208 void RequestInterrupt(InterruptFlag flag);
209 void ClearInterrupt(InterruptFlag flag);
210 bool CheckAndClearInterrupt(InterruptFlag flag);
212 // You should hold the ExecutionAccess lock when calling this method.
213 bool has_pending_interrupts(const ExecutionAccess& lock) {
214 return thread_local_.interrupt_flags_ != 0;
217 // You should hold the ExecutionAccess lock when calling this method.
218 inline void set_interrupt_limits(const ExecutionAccess& lock);
220 // Reset limits to actual values. For example after handling interrupt.
221 // You should hold the ExecutionAccess lock when calling this method.
222 inline void reset_limits(const ExecutionAccess& lock);
224 // Enable or disable interrupts.
225 void EnableInterrupts();
226 void DisableInterrupts();
228 #if V8_TARGET_ARCH_64_BIT
229 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
230 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
232 static const uintptr_t kInterruptLimit = 0xfffffffe;
233 static const uintptr_t kIllegalLimit = 0xfffffff8;
236 void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
237 void PopPostponeInterruptsScope();
239 class ThreadLocal FINAL {
241 ThreadLocal() { Clear(); }
242 // You should hold the ExecutionAccess lock when you call Initialize or
246 // Returns true if the heap's stack limits should be set, false if not.
247 bool Initialize(Isolate* isolate);
249 // The stack limit is split into a JavaScript and a C++ stack limit. These
250 // two are the same except when running on a simulator where the C++ and
251 // JavaScript stacks are separate. Each of the two stack limits have two
252 // values. The one eith the real_ prefix is the actual stack limit
253 // set for the VM. The one without the real_ prefix has the same value as
254 // the actual stack limit except when there is an interruption (e.g. debug
255 // break or preemption) in which case it is lowered to make stack checks
256 // fail. Both the generated code and the runtime system check against the
257 // one without the real_ prefix.
258 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.
263 PostponeInterruptsScope* postpone_interrupts_;
264 int interrupt_flags_;
267 // TODO(isolates): Technically this could be calculated directly from a
268 // pointer to StackGuard.
270 ThreadLocal thread_local_;
272 friend class Isolate;
273 friend class StackLimitCheck;
274 friend class PostponeInterruptsScope;
276 DISALLOW_COPY_AND_ASSIGN(StackGuard);
279 } } // namespace v8::internal
281 #endif // V8_EXECUTION_H_