blob: 89175cd906d80a9533a47013ba2f0ec129b67fdf [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_EXECUTION_H_
6#define V8_EXECUTION_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/handles.h"
Ben Murdoch257744e2011-11-30 15:57:28 +00009
Steve Blocka7e24c12009-10-30 11:49:00 +000010namespace v8 {
11namespace internal {
12
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013class Execution FINAL : public AllStatic {
Steve Blocka7e24c12009-10-30 11:49:00 +000014 public:
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.
18 //
19 // *pending_exception tells whether the invoke resulted in
20 // a pending exception.
21 //
Ben Murdoch589d6972011-11-30 16:04:58 +000022 // 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
24 // an object.
25 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026 MUST_USE_RESULT static MaybeHandle<Object> Call(
27 Isolate* isolate,
28 Handle<Object> callable,
29 Handle<Object> receiver,
30 int argc,
31 Handle<Object> argv[],
32 bool convert_receiver = false);
Steve Blocka7e24c12009-10-30 11:49:00 +000033
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.
37 //
38 // *pending_exception tells whether the invoke resulted in
39 // a pending exception.
40 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> func,
42 int argc,
43 Handle<Object> argv[]);
Steve Blocka7e24c12009-10-30 11:49:00 +000044
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).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 // 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);
Steve Blocka7e24c12009-10-30 11:49:00 +000055
56 // ECMA-262 9.3
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 MUST_USE_RESULT static MaybeHandle<Object> ToNumber(
58 Isolate* isolate, Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000059
60 // ECMA-262 9.4
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 MUST_USE_RESULT static MaybeHandle<Object> ToInteger(
62 Isolate* isolate, Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64 // ECMA-262 9.5
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 MUST_USE_RESULT static MaybeHandle<Object> ToInt32(
66 Isolate* isolate, Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000067
68 // ECMA-262 9.6
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 MUST_USE_RESULT static MaybeHandle<Object> ToUint32(
70 Isolate* isolate, Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000071
72 // ECMA-262 9.8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 MUST_USE_RESULT static MaybeHandle<Object> ToString(
74 Isolate* isolate, Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000075
76 // ECMA-262 9.8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 MUST_USE_RESULT static MaybeHandle<Object> ToDetailString(
78 Isolate* isolate, Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000079
80 // ECMA-262 9.9
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 MUST_USE_RESULT static MaybeHandle<Object> ToObject(
82 Isolate* isolate, Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000083
84 // Create a new date object from 'time'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 MUST_USE_RESULT static MaybeHandle<Object> NewDate(
86 Isolate* isolate, double time);
Steve Blocka7e24c12009-10-30 11:49:00 +000087
Ben Murdochf87a2032010-10-22 12:50:53 +010088 // Create a new regular expression object from 'pattern' and 'flags'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 MUST_USE_RESULT static MaybeHandle<JSRegExp> NewJSRegExp(
90 Handle<String> pattern, Handle<String> flags);
Ben Murdochf87a2032010-10-22 12:50:53 +010091
Steve Blocka7e24c12009-10-30 11:49:00 +000092 // Used to implement [] notation on strings (calls JS code)
93 static Handle<Object> CharAt(Handle<String> str, uint32_t index);
94
95 static Handle<Object> GetFunctionFor();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 MUST_USE_RESULT static MaybeHandle<JSFunction> InstantiateFunction(
97 Handle<FunctionTemplateInfo> data);
98 MUST_USE_RESULT static MaybeHandle<JSObject> InstantiateObject(
99 Handle<ObjectTemplateInfo> data);
100 MUST_USE_RESULT static MaybeHandle<Object> ConfigureInstance(
101 Isolate* isolate, Handle<Object> instance, Handle<Object> data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 static Handle<String> GetStackTraceLine(Handle<Object> recv,
103 Handle<JSFunction> fun,
104 Handle<Object> pos,
105 Handle<Object> is_global);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106
107 // Get a function delegate (or undefined) for the given non-function
108 // object. Used for support calling objects as functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 static Handle<Object> GetFunctionDelegate(Isolate* isolate,
110 Handle<Object> object);
111 MUST_USE_RESULT static MaybeHandle<Object> TryGetFunctionDelegate(
112 Isolate* isolate,
113 Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
115 // Get a function delegate (or undefined) for the given non-function
116 // object. Used for support calling objects as constructors.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 static Handle<Object> GetConstructorDelegate(Isolate* isolate,
118 Handle<Object> object);
119 static MaybeHandle<Object> TryGetConstructorDelegate(Isolate* isolate,
120 Handle<Object> object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000121};
122
123
124class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125class PostponeInterruptsScope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000126
127
128// StackGuard contains the handling of the limits that are used to limit the
129// number of nested invocations of JavaScript and the stack size used in each
130// invocation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131class StackGuard FINAL {
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 public:
133 // Pass the address beyond which the stack should not grow. The stack
134 // is assumed to grow downwards.
Steve Block44f0eee2011-05-26 01:26:41 +0100135 void SetStackLimit(uintptr_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 // Threading support.
Steve Block44f0eee2011-05-26 01:26:41 +0100138 char* ArchiveStackGuard(char* to);
139 char* RestoreStackGuard(char* from);
140 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
141 void FreeThreadResources();
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 // Sets up the default stack guard for this thread if it has not
143 // already been set up.
Steve Block44f0eee2011-05-26 01:26:41 +0100144 void InitThread(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 // Clears the stack guard for this thread so it does not look as if
146 // it has been set up.
Steve Block44f0eee2011-05-26 01:26:41 +0100147 void ClearThread(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000148
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149#define INTERRUPT_LIST(V) \
150 V(DEBUGBREAK, DebugBreak, 0) \
151 V(DEBUGCOMMAND, DebugCommand, 1) \
152 V(TERMINATE_EXECUTION, TerminateExecution, 2) \
153 V(GC_REQUEST, GC, 3) \
154 V(INSTALL_CODE, InstallCode, 4) \
155 V(API_INTERRUPT, ApiInterrupt, 5) \
156 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6)
157
158#define V(NAME, Name, id) \
159 inline bool Check##Name() { return CheckInterrupt(NAME); } \
160 inline void Request##Name() { RequestInterrupt(NAME); } \
161 inline void Clear##Name() { ClearInterrupt(NAME); }
162 INTERRUPT_LIST(V)
163#undef V
164
165 // Flag used to set the interrupt causes.
166 enum InterruptFlag {
167 #define V(NAME, Name, id) NAME = (1 << id),
168 INTERRUPT_LIST(V)
169 #undef V
170 #define V(NAME, Name, id) NAME |
171 ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
172 #undef V
173 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000174
Steve Blockd0582a62009-12-15 09:54:21 +0000175 // This provides an asynchronous read of the stack limits for the current
Steve Blocka7e24c12009-10-30 11:49:00 +0000176 // thread. There are no locks protecting this, but it is assumed that you
177 // have the global V8 lock if you are using multiple V8 threads.
Steve Block44f0eee2011-05-26 01:26:41 +0100178 uintptr_t climit() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 return thread_local_.climit_;
180 }
Steve Block44f0eee2011-05-26 01:26:41 +0100181 uintptr_t real_climit() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800182 return thread_local_.real_climit_;
183 }
Steve Block44f0eee2011-05-26 01:26:41 +0100184 uintptr_t jslimit() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 return thread_local_.jslimit_;
186 }
Steve Block44f0eee2011-05-26 01:26:41 +0100187 uintptr_t real_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000188 return thread_local_.real_jslimit_;
189 }
Steve Block44f0eee2011-05-26 01:26:41 +0100190 Address address_of_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000191 return reinterpret_cast<Address>(&thread_local_.jslimit_);
192 }
Steve Block44f0eee2011-05-26 01:26:41 +0100193 Address address_of_real_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000194 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
195 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196
197 // If the stack guard is triggered, but it is not an actual
198 // stack overflow, then handle the interruption accordingly.
199 Object* HandleInterrupts();
Steve Blocka7e24c12009-10-30 11:49:00 +0000200
201 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100202 StackGuard();
203
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 bool CheckInterrupt(InterruptFlag flag);
205 void RequestInterrupt(InterruptFlag flag);
206 void ClearInterrupt(InterruptFlag flag);
207 bool CheckAndClearInterrupt(InterruptFlag flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000208
209 // You should hold the ExecutionAccess lock when calling this method.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 bool has_pending_interrupts(const ExecutionAccess& lock) {
211 return thread_local_.interrupt_flags_ != 0;
Steve Block6ded16b2010-05-10 14:33:55 +0100212 }
213
214 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100215 inline void set_interrupt_limits(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000216
Steve Blockd0582a62009-12-15 09:54:21 +0000217 // Reset limits to actual values. For example after handling interrupt.
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100219 inline void reset_limits(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000220
221 // Enable or disable interrupts.
Steve Block44f0eee2011-05-26 01:26:41 +0100222 void EnableInterrupts();
223 void DisableInterrupts();
Steve Blocka7e24c12009-10-30 11:49:00 +0000224
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225#if V8_TARGET_ARCH_64_BIT
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
227 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
228#else
229 static const uintptr_t kInterruptLimit = 0xfffffffe;
230 static const uintptr_t kIllegalLimit = 0xfffffff8;
231#endif
232
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
234 void PopPostponeInterruptsScope();
235
236 class ThreadLocal FINAL {
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 public:
238 ThreadLocal() { Clear(); }
239 // You should hold the ExecutionAccess lock when you call Initialize or
240 // Clear.
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 void Clear();
Steve Blockd0582a62009-12-15 09:54:21 +0000242
Steve Block44f0eee2011-05-26 01:26:41 +0100243 // Returns true if the heap's stack limits should be set, false if not.
Ben Murdoch257744e2011-11-30 15:57:28 +0000244 bool Initialize(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100245
Steve Blockd0582a62009-12-15 09:54:21 +0000246 // The stack limit is split into a JavaScript and a C++ stack limit. These
247 // two are the same except when running on a simulator where the C++ and
248 // JavaScript stacks are separate. Each of the two stack limits have two
249 // values. The one eith the real_ prefix is the actual stack limit
250 // set for the VM. The one without the real_ prefix has the same value as
251 // the actual stack limit except when there is an interruption (e.g. debug
252 // break or preemption) in which case it is lowered to make stack checks
253 // fail. Both the generated code and the runtime system check against the
254 // one without the real_ prefix.
255 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 uintptr_t jslimit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000257 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 uintptr_t climit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000259
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 PostponeInterruptsScope* postpone_interrupts_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 int interrupt_flags_;
262 };
263
Steve Block44f0eee2011-05-26 01:26:41 +0100264 // TODO(isolates): Technically this could be calculated directly from a
265 // pointer to StackGuard.
266 Isolate* isolate_;
267 ThreadLocal thread_local_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000268
Steve Block44f0eee2011-05-26 01:26:41 +0100269 friend class Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 friend class StackLimitCheck;
271 friend class PostponeInterruptsScope;
Steve Block44f0eee2011-05-26 01:26:41 +0100272
273 DISALLOW_COPY_AND_ASSIGN(StackGuard);
Steve Blocka7e24c12009-10-30 11:49:00 +0000274};
275
Steve Blocka7e24c12009-10-30 11:49:00 +0000276} } // namespace v8::internal
277
278#endif // V8_EXECUTION_H_