2011-10-06 09:31:38 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_EXECUTION_H_
|
|
|
|
#define V8_EXECUTION_H_
|
|
|
|
|
2011-05-06 06:50:20 +00:00
|
|
|
#include "allocation.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Flag used to set the interrupt causes.
|
|
|
|
enum InterruptFlag {
|
|
|
|
INTERRUPT = 1 << 0,
|
|
|
|
DEBUGBREAK = 1 << 1,
|
2009-03-13 13:26:21 +00:00
|
|
|
DEBUGCOMMAND = 1 << 2,
|
2009-08-19 15:14:11 +00:00
|
|
|
PREEMPT = 1 << 3,
|
2010-12-07 11:31:57 +00:00
|
|
|
TERMINATE = 1 << 4,
|
2011-09-19 18:36:47 +00:00
|
|
|
RUNTIME_PROFILER_TICK = 1 << 5,
|
|
|
|
GC_REQUEST = 1 << 6
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2012-02-28 10:32:02 +00:00
|
|
|
|
|
|
|
class Isolate;
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
class Execution : public AllStatic {
|
|
|
|
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.
|
|
|
|
//
|
2011-09-13 11:42:57 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2011-05-16 16:33:58 +00:00
|
|
|
static Handle<Object> Call(Handle<Object> callable,
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> receiver,
|
|
|
|
int argc,
|
2011-10-06 09:31:38 +00:00
|
|
|
Handle<Object> argv[],
|
2011-09-13 11:42:57 +00:00
|
|
|
bool* pending_exception,
|
|
|
|
bool convert_receiver = false);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
static Handle<Object> New(Handle<JSFunction> func,
|
|
|
|
int argc,
|
2011-10-06 09:31:38 +00:00
|
|
|
Handle<Object> argv[],
|
2008-07-03 15:10:15 +00:00
|
|
|
bool* pending_exception);
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
static Handle<Object> TryCall(Handle<JSFunction> func,
|
|
|
|
Handle<Object> receiver,
|
|
|
|
int argc,
|
2011-10-06 09:31:38 +00:00
|
|
|
Handle<Object> argv[],
|
2008-07-03 15:10:15 +00:00
|
|
|
bool* caught_exception);
|
|
|
|
|
|
|
|
// ECMA-262 9.2
|
|
|
|
static Handle<Object> ToBoolean(Handle<Object> obj);
|
|
|
|
|
|
|
|
// ECMA-262 9.3
|
|
|
|
static Handle<Object> ToNumber(Handle<Object> obj, bool* exc);
|
|
|
|
|
|
|
|
// ECMA-262 9.4
|
|
|
|
static Handle<Object> ToInteger(Handle<Object> obj, bool* exc);
|
|
|
|
|
|
|
|
// ECMA-262 9.5
|
|
|
|
static Handle<Object> ToInt32(Handle<Object> obj, bool* exc);
|
|
|
|
|
|
|
|
// ECMA-262 9.6
|
|
|
|
static Handle<Object> ToUint32(Handle<Object> obj, bool* exc);
|
|
|
|
|
|
|
|
// ECMA-262 9.8
|
|
|
|
static Handle<Object> ToString(Handle<Object> obj, bool* exc);
|
|
|
|
|
|
|
|
// ECMA-262 9.8
|
|
|
|
static Handle<Object> ToDetailString(Handle<Object> obj, bool* exc);
|
|
|
|
|
|
|
|
// ECMA-262 9.9
|
|
|
|
static Handle<Object> ToObject(Handle<Object> obj, bool* exc);
|
|
|
|
|
|
|
|
// Create a new date object from 'time'.
|
|
|
|
static Handle<Object> NewDate(double time, bool* exc);
|
|
|
|
|
2010-10-05 11:51:41 +00:00
|
|
|
// Create a new regular expression object from 'pattern' and 'flags'.
|
|
|
|
static Handle<JSRegExp> NewJSRegExp(Handle<String> pattern,
|
|
|
|
Handle<String> flags,
|
|
|
|
bool* exc);
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Used to implement [] notation on strings (calls JS code)
|
|
|
|
static Handle<Object> CharAt(Handle<String> str, uint32_t index);
|
|
|
|
|
|
|
|
static Handle<Object> GetFunctionFor();
|
|
|
|
static Handle<JSFunction> InstantiateFunction(
|
|
|
|
Handle<FunctionTemplateInfo> data, bool* exc);
|
|
|
|
static Handle<JSObject> InstantiateObject(Handle<ObjectTemplateInfo> data,
|
|
|
|
bool* exc);
|
|
|
|
static void ConfigureInstance(Handle<Object> instance,
|
|
|
|
Handle<Object> data,
|
|
|
|
bool* exc);
|
|
|
|
static Handle<String> GetStackTraceLine(Handle<Object> recv,
|
|
|
|
Handle<JSFunction> fun,
|
|
|
|
Handle<Object> pos,
|
|
|
|
Handle<Object> is_global);
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2008-12-12 10:49:00 +00:00
|
|
|
static Object* DebugBreakHelper();
|
2012-01-16 09:46:21 +00:00
|
|
|
static void ProcessDebugMessages(bool debug_command_only);
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2008-12-12 10:49:00 +00:00
|
|
|
|
|
|
|
// If the stack guard is triggered, but it is not an actual
|
|
|
|
// stack overflow, then handle the interruption accordingly.
|
2012-02-28 10:32:02 +00:00
|
|
|
MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt(
|
|
|
|
Isolate* isolate);
|
2008-12-12 10:49:00 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Get a function delegate (or undefined) for the given non-function
|
|
|
|
// object. Used for support calling objects as functions.
|
|
|
|
static Handle<Object> GetFunctionDelegate(Handle<Object> object);
|
2011-05-04 13:03:08 +00:00
|
|
|
static Handle<Object> TryGetFunctionDelegate(Handle<Object> object,
|
|
|
|
bool* has_pending_exception);
|
2009-05-20 19:33:44 +00:00
|
|
|
|
|
|
|
// Get a function delegate (or undefined) for the given non-function
|
|
|
|
// object. Used for support calling objects as constructors.
|
|
|
|
static Handle<Object> GetConstructorDelegate(Handle<Object> object);
|
2011-05-06 11:07:52 +00:00
|
|
|
static Handle<Object> TryGetConstructorDelegate(Handle<Object> object,
|
|
|
|
bool* has_pending_exception);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ExecutionAccess;
|
|
|
|
|
|
|
|
|
2009-10-01 10:33:05 +00:00
|
|
|
// 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.
|
2011-03-18 20:35:07 +00:00
|
|
|
class StackGuard {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2009-10-01 10:33:05 +00:00
|
|
|
// Pass the address beyond which the stack should not grow. The stack
|
|
|
|
// is assumed to grow downwards.
|
2011-03-18 20:35:07 +00:00
|
|
|
void SetStackLimit(uintptr_t limit);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Threading support.
|
2011-03-18 20:35:07 +00:00
|
|
|
char* ArchiveStackGuard(char* to);
|
|
|
|
char* RestoreStackGuard(char* from);
|
|
|
|
static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
|
|
|
|
void FreeThreadResources();
|
2009-10-01 10:33:05 +00:00
|
|
|
// Sets up the default stack guard for this thread if it has not
|
|
|
|
// already been set up.
|
2011-03-18 20:35:07 +00:00
|
|
|
void InitThread(const ExecutionAccess& lock);
|
2009-10-01 10:33:05 +00:00
|
|
|
// Clears the stack guard for this thread so it does not look as if
|
|
|
|
// it has been set up.
|
2011-03-18 20:35:07 +00:00
|
|
|
void ClearThread(const ExecutionAccess& lock);
|
|
|
|
|
|
|
|
bool IsStackOverflow();
|
|
|
|
bool IsPreempted();
|
|
|
|
void Preempt();
|
|
|
|
bool IsInterrupted();
|
|
|
|
void Interrupt();
|
|
|
|
bool IsTerminateExecution();
|
|
|
|
void TerminateExecution();
|
|
|
|
bool IsRuntimeProfilerTick();
|
|
|
|
void RequestRuntimeProfilerTick();
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2011-03-18 20:35:07 +00:00
|
|
|
bool IsDebugBreak();
|
|
|
|
void DebugBreak();
|
|
|
|
bool IsDebugCommand();
|
|
|
|
void DebugCommand();
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2011-09-19 18:36:47 +00:00
|
|
|
bool IsGCRequest();
|
|
|
|
void RequestGC();
|
2011-03-18 20:35:07 +00:00
|
|
|
void Continue(InterruptFlag after_what);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-11-05 13:59:40 +00:00
|
|
|
// This provides an asynchronous read of the stack limits for the current
|
2009-10-01 10:33:05 +00:00
|
|
|
// 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.
|
2011-03-18 20:35:07 +00:00
|
|
|
uintptr_t climit() {
|
2009-10-01 10:33:05 +00:00
|
|
|
return thread_local_.climit_;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
uintptr_t real_climit() {
|
2010-11-12 08:40:21 +00:00
|
|
|
return thread_local_.real_climit_;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
uintptr_t jslimit() {
|
2009-08-26 10:27:32 +00:00
|
|
|
return thread_local_.jslimit_;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
uintptr_t real_jslimit() {
|
2009-11-05 13:59:40 +00:00
|
|
|
return thread_local_.real_jslimit_;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
Address address_of_jslimit() {
|
2009-11-05 13:59:40 +00:00
|
|
|
return reinterpret_cast<Address>(&thread_local_.jslimit_);
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
Address address_of_real_jslimit() {
|
2009-11-05 13:59:40 +00:00
|
|
|
return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
|
|
|
|
}
|
2009-08-26 10:27:32 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
StackGuard();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// You should hold the ExecutionAccess lock when calling this method.
|
2011-03-18 20:35:07 +00:00
|
|
|
bool has_pending_interrupts(const ExecutionAccess& lock) {
|
2010-04-14 07:36:49 +00:00
|
|
|
// 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.
|
2011-03-18 20:35:07 +00:00
|
|
|
bool should_postpone_interrupts(const ExecutionAccess& lock) {
|
2010-04-14 07:36:49 +00:00
|
|
|
return thread_local_.postpone_interrupts_nesting_ > 0;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// You should hold the ExecutionAccess lock when calling this method.
|
2011-03-18 20:35:07 +00:00
|
|
|
inline void set_interrupt_limits(const ExecutionAccess& lock);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-11-05 13:59:40 +00:00
|
|
|
// Reset limits to actual values. For example after handling interrupt.
|
2008-07-03 15:10:15 +00:00
|
|
|
// You should hold the ExecutionAccess lock when calling this method.
|
2011-03-18 20:35:07 +00:00
|
|
|
inline void reset_limits(const ExecutionAccess& lock);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-07-25 07:37:58 +00:00
|
|
|
// Enable or disable interrupts.
|
2011-03-18 20:35:07 +00:00
|
|
|
void EnableInterrupts();
|
|
|
|
void DisableInterrupts();
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2009-08-19 10:18:30 +00:00
|
|
|
#ifdef V8_TARGET_ARCH_X64
|
|
|
|
static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
|
2009-10-01 10:33:05 +00:00
|
|
|
static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
|
2009-08-19 10:18:30 +00:00
|
|
|
#else
|
2008-07-03 15:10:15 +00:00
|
|
|
static const uintptr_t kInterruptLimit = 0xfffffffe;
|
2009-10-01 10:33:05 +00:00
|
|
|
static const uintptr_t kIllegalLimit = 0xfffffff8;
|
2009-08-19 10:18:30 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
class ThreadLocal {
|
|
|
|
public:
|
2009-10-01 10:33:05 +00:00
|
|
|
ThreadLocal() { Clear(); }
|
|
|
|
// You should hold the ExecutionAccess lock when you call Initialize or
|
|
|
|
// Clear.
|
|
|
|
void Clear();
|
2009-11-05 13:59:40 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Returns true if the heap's stack limits should be set, false if not.
|
2011-05-05 18:55:31 +00:00
|
|
|
bool Initialize(Isolate* isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2009-11-05 13:59:40 +00:00
|
|
|
// 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.
|
2008-07-03 15:10:15 +00:00
|
|
|
uintptr_t jslimit_;
|
2009-11-05 13:59:40 +00:00
|
|
|
uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
|
2008-07-03 15:10:15 +00:00
|
|
|
uintptr_t climit_;
|
2009-11-05 13:59:40 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
int nesting_;
|
2008-07-25 07:37:58 +00:00
|
|
|
int postpone_interrupts_nesting_;
|
2008-07-03 15:10:15 +00:00
|
|
|
int interrupt_flags_;
|
|
|
|
};
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// TODO(isolates): Technically this could be calculated directly from a
|
|
|
|
// pointer to StackGuard.
|
|
|
|
Isolate* isolate_;
|
|
|
|
ThreadLocal thread_local_;
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
friend class Isolate;
|
2008-07-03 15:10:15 +00:00
|
|
|
friend class StackLimitCheck;
|
2008-07-25 07:37:58 +00:00
|
|
|
friend class PostponeInterruptsScope;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(StackGuard);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_EXECUTION_H_
|