execution.h 10.5 KB
Newer Older
1 2 3
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_EXECUTION_H_
#define V8_EXECUTION_H_

8
#include "handles.h"
9

10 11
namespace v8 {
namespace internal {
12 13 14 15 16

// Flag used to set the interrupt causes.
enum InterruptFlag {
  INTERRUPT = 1 << 0,
  DEBUGBREAK = 1 << 1,
17
  DEBUGCOMMAND = 1 << 2,
18
  PREEMPT = 1 << 3,
19
  TERMINATE = 1 << 4,
20
  GC_REQUEST = 1 << 5,
21
  FULL_DEOPT = 1 << 6,
vegorov@chromium.org's avatar
vegorov@chromium.org committed
22
  INSTALL_CODE = 1 << 7,
23
  API_INTERRUPT = 1 << 8,
24
  DEOPT_MARKED_ALLOCATION_SITES = 1 << 9
25 26
};

27

28
class Execution V8_FINAL : public AllStatic {
29 30 31 32 33 34 35 36
 public:
  // Call a function, the caller supplies a receiver and an array
  // of arguments. Arguments are Object* type. After function returns,
  // pointers in 'args' might be invalid.
  //
  // *pending_exception tells whether the invoke resulted in
  // a pending exception.
  //
37 38 39 40
  // When convert_receiver is set, and the receiver is not an object,
  // and the function called is not in strict mode, receiver is converted to
  // an object.
  //
41 42 43 44 45 46 47
  MUST_USE_RESULT static MaybeHandle<Object> Call(
      Isolate* isolate,
      Handle<Object> callable,
      Handle<Object> receiver,
      int argc,
      Handle<Object> argv[],
      bool convert_receiver = false);
48 49 50 51 52 53 54 55

  // Construct object from function, the caller supplies an array of
  // arguments. Arguments are Object* type. After function returns,
  // pointers in 'args' might be invalid.
  //
  // *pending_exception tells whether the invoke resulted in
  // a pending exception.
  //
56 57 58
  MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> func,
                                                 int argc,
                                                 Handle<Object> argv[]);
59 60 61 62 63

  // Call a function, just like Call(), but make sure to silently catch
  // any thrown exceptions. The return value is either the result of
  // calling the function (if caught exception is false) or the exception
  // that occurred (if caught exception is true).
64
  static MaybeHandle<Object> TryCall(
65 66 67 68 69
      Handle<JSFunction> func,
      Handle<Object> receiver,
      int argc,
      Handle<Object> argv[],
      Handle<Object>* exception_out = NULL);
70 71

  // ECMA-262 9.3
72 73
  MUST_USE_RESULT static MaybeHandle<Object> ToNumber(
      Isolate* isolate, Handle<Object> obj);
74 75

  // ECMA-262 9.4
76 77
  MUST_USE_RESULT static MaybeHandle<Object> ToInteger(
      Isolate* isolate, Handle<Object> obj);
78 79

  // ECMA-262 9.5
80 81
  MUST_USE_RESULT static MaybeHandle<Object> ToInt32(
      Isolate* isolate, Handle<Object> obj);
82 83

  // ECMA-262 9.6
84 85
  MUST_USE_RESULT static MaybeHandle<Object> ToUint32(
      Isolate* isolate, Handle<Object> obj);
86 87

  // ECMA-262 9.8
88 89
  MUST_USE_RESULT static MaybeHandle<Object> ToString(
      Isolate* isolate, Handle<Object> obj);
90 91

  // ECMA-262 9.8
92 93
  MUST_USE_RESULT static MaybeHandle<Object> ToDetailString(
      Isolate* isolate, Handle<Object> obj);
94 95

  // ECMA-262 9.9
96 97
  MUST_USE_RESULT static MaybeHandle<Object> ToObject(
      Isolate* isolate, Handle<Object> obj);
98 99

  // Create a new date object from 'time'.
100 101
  MUST_USE_RESULT static MaybeHandle<Object> NewDate(
      Isolate* isolate, double time);
102

103
  // Create a new regular expression object from 'pattern' and 'flags'.
104 105
  MUST_USE_RESULT static MaybeHandle<JSRegExp> NewJSRegExp(
      Handle<String> pattern, Handle<String> flags);
106

107 108 109 110
  // Used to implement [] notation on strings (calls JS code)
  static Handle<Object> CharAt(Handle<String> str, uint32_t index);

  static Handle<Object> GetFunctionFor();
111 112 113 114 115 116
  MUST_USE_RESULT static MaybeHandle<JSFunction> InstantiateFunction(
      Handle<FunctionTemplateInfo> data);
  MUST_USE_RESULT static MaybeHandle<JSObject> InstantiateObject(
      Handle<ObjectTemplateInfo> data);
  MUST_USE_RESULT static MaybeHandle<Object> ConfigureInstance(
      Isolate* isolate,  Handle<Object> instance, Handle<Object> data);
117 118 119 120
  static Handle<String> GetStackTraceLine(Handle<Object> recv,
                                          Handle<JSFunction> fun,
                                          Handle<Object> pos,
                                          Handle<Object> is_global);
121

122 123
  static Object* DebugBreakHelper(Isolate* isolate);
  static void ProcessDebugMessages(Isolate* isolate, bool debug_command_only);
124 125 126

  // If the stack guard is triggered, but it is not an actual
  // stack overflow, then handle the interruption accordingly.
127
  static Object* HandleStackGuardInterrupt(Isolate* isolate);
128

129 130
  // Get a function delegate (or undefined) for the given non-function
  // object. Used for support calling objects as functions.
131 132
  static Handle<Object> GetFunctionDelegate(Isolate* isolate,
                                            Handle<Object> object);
133 134 135
  MUST_USE_RESULT static MaybeHandle<Object> TryGetFunctionDelegate(
      Isolate* isolate,
      Handle<Object> object);
136 137 138

  // Get a function delegate (or undefined) for the given non-function
  // object. Used for support calling objects as constructors.
139 140
  static Handle<Object> GetConstructorDelegate(Isolate* isolate,
                                               Handle<Object> object);
141 142
  static MaybeHandle<Object> TryGetConstructorDelegate(Isolate* isolate,
                                                       Handle<Object> object);
rossberg@chromium.org's avatar
rossberg@chromium.org committed
143 144

  static void RunMicrotasks(Isolate* isolate);
145
  static void EnqueueMicrotask(Isolate* isolate, Handle<Object> microtask);
146 147 148 149 150 151
};


class ExecutionAccess;


152 153 154
// StackGuard contains the handling of the limits that are used to limit the
// number of nested invocations of JavaScript and the stack size used in each
// invocation.
155
class StackGuard V8_FINAL {
156
 public:
157 158
  // Pass the address beyond which the stack should not grow.  The stack
  // is assumed to grow downwards.
159
  void SetStackLimit(uintptr_t limit);
160 161

  // Threading support.
162 163 164 165
  char* ArchiveStackGuard(char* to);
  char* RestoreStackGuard(char* from);
  static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
  void FreeThreadResources();
166 167
  // Sets up the default stack guard for this thread if it has not
  // already been set up.
168
  void InitThread(const ExecutionAccess& lock);
169 170
  // Clears the stack guard for this thread so it does not look as if
  // it has been set up.
171 172 173 174 175 176 177 178 179
  void ClearThread(const ExecutionAccess& lock);

  bool IsStackOverflow();
  bool IsPreempted();
  void Preempt();
  bool IsInterrupted();
  void Interrupt();
  bool IsTerminateExecution();
  void TerminateExecution();
180
  void CancelTerminateExecution();
181 182 183 184
  bool IsDebugBreak();
  void DebugBreak();
  bool IsDebugCommand();
  void DebugCommand();
185 186
  bool IsGCRequest();
  void RequestGC();
187 188
  bool IsInstallCodeRequest();
  void RequestInstallCode();
189 190
  bool IsFullDeopt();
  void FullDeopt();
191 192
  bool IsDeoptMarkedAllocationSites();
  void DeoptMarkedAllocationSites();
193
  void Continue(InterruptFlag after_what);
194

vegorov@chromium.org's avatar
vegorov@chromium.org committed
195 196 197 198 199
  void RequestInterrupt(InterruptCallback callback, void* data);
  void ClearInterrupt();
  bool IsAPIInterrupt();
  void InvokeInterruptCallback();

200
  // This provides an asynchronous read of the stack limits for the current
201 202
  // thread.  There are no locks protecting this, but it is assumed that you
  // have the global V8 lock if you are using multiple V8 threads.
203
  uintptr_t climit() {
204 205
    return thread_local_.climit_;
  }
206
  uintptr_t real_climit() {
207 208
    return thread_local_.real_climit_;
  }
209
  uintptr_t jslimit() {
210 211
    return thread_local_.jslimit_;
  }
212
  uintptr_t real_jslimit() {
213 214
    return thread_local_.real_jslimit_;
  }
215
  Address address_of_jslimit() {
216 217
    return reinterpret_cast<Address>(&thread_local_.jslimit_);
  }
218
  Address address_of_real_jslimit() {
219 220
    return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
  }
221
  bool ShouldPostponeInterrupts();
222

223
 private:
224 225
  StackGuard();

226
  // You should hold the ExecutionAccess lock when calling this method.
227
  bool has_pending_interrupts(const ExecutionAccess& lock) {
228 229 230 231 232 233 234
    // Sanity check: We shouldn't be asking about pending interrupts
    // unless we're not postponing them anymore.
    ASSERT(!should_postpone_interrupts(lock));
    return thread_local_.interrupt_flags_ != 0;
  }

  // You should hold the ExecutionAccess lock when calling this method.
235
  bool should_postpone_interrupts(const ExecutionAccess& lock) {
236 237
    return thread_local_.postpone_interrupts_nesting_ > 0;
  }
238 239

  // You should hold the ExecutionAccess lock when calling this method.
240
  inline void set_interrupt_limits(const ExecutionAccess& lock);
241

242
  // Reset limits to actual values. For example after handling interrupt.
243
  // You should hold the ExecutionAccess lock when calling this method.
244
  inline void reset_limits(const ExecutionAccess& lock);
245

246
  // Enable or disable interrupts.
247 248
  void EnableInterrupts();
  void DisableInterrupts();
249

250
#if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_ARM64
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 V8_FINAL {
259
   public:
260 261 262 263
    ThreadLocal() { Clear(); }
    // You should hold the ExecutionAccess lock when you call Initialize or
    // Clear.
    void Clear();
264

265
    // Returns true if the heap's stack limits should be set, false if not.
266
    bool Initialize(Isolate* isolate);
267

268 269 270 271 272 273 274 275 276 277
    // The stack limit is split into a JavaScript and a C++ stack limit. These
    // two are the same except when running on a simulator where the C++ and
    // JavaScript stacks are separate. Each of the two stack limits have two
    // values. The one eith the real_ prefix is the actual stack limit
    // set for the VM. The one without the real_ prefix has the same value as
    // the actual stack limit except when there is an interruption (e.g. debug
    // break or preemption) in which case it is lowered to make stack checks
    // fail. Both the generated code and the runtime system check against the
    // one without the real_ prefix.
    uintptr_t real_jslimit_;  // Actual JavaScript stack limit set for the VM.
278
    uintptr_t jslimit_;
279
    uintptr_t real_climit_;  // Actual C++ stack limit set for the VM.
280
    uintptr_t climit_;
281

282
    int nesting_;
283
    int postpone_interrupts_nesting_;
284
    int interrupt_flags_;
vegorov@chromium.org's avatar
vegorov@chromium.org committed
285 286 287

    InterruptCallback interrupt_callback_;
    void* interrupt_callback_data_;
288
  };
289

290 291 292 293
  // TODO(isolates): Technically this could be calculated directly from a
  //                 pointer to StackGuard.
  Isolate* isolate_;
  ThreadLocal thread_local_;
294

295
  friend class Isolate;
296
  friend class StackLimitCheck;
297
  friend class PostponeInterruptsScope;
298

299
  DISALLOW_COPY_AND_ASSIGN(StackGuard);
300 301 302 303 304
};

} }  // namespace v8::internal

#endif  // V8_EXECUTION_H_