2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 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.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "api.h"
|
|
|
|
#include "codegen-inl.h"
|
|
|
|
|
2009-05-05 12:06:20 +00:00
|
|
|
#if V8_TARGET_ARCH_IA32
|
2009-04-23 12:06:38 +00:00
|
|
|
#include "ia32/simulator-ia32.h"
|
2009-05-05 12:06:20 +00:00
|
|
|
#elif V8_TARGET_ARCH_X64
|
|
|
|
#include "x64/simulator-x64.h"
|
|
|
|
#elif V8_TARGET_ARCH_ARM
|
|
|
|
#include "arm/simulator-arm.h"
|
2009-06-29 17:07:30 +00:00
|
|
|
#else
|
|
|
|
#error Unsupported target architecture.
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
|
|
|
|
2008-12-12 10:49:00 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "v8threads.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
static Handle<Object> Invoke(bool construct,
|
|
|
|
Handle<JSFunction> func,
|
|
|
|
Handle<Object> receiver,
|
|
|
|
int argc,
|
|
|
|
Object*** args,
|
|
|
|
bool* has_pending_exception) {
|
|
|
|
// Make sure we have a real function, not a boilerplate function.
|
|
|
|
ASSERT(!func->IsBoilerplate());
|
|
|
|
|
|
|
|
// Entering JavaScript.
|
|
|
|
VMState state(JS);
|
|
|
|
|
|
|
|
// Placeholder for return value.
|
|
|
|
Object* value = reinterpret_cast<Object*>(kZapValue);
|
|
|
|
|
|
|
|
typedef Object* (*JSEntryFunction)(
|
|
|
|
byte* entry,
|
|
|
|
Object* function,
|
|
|
|
Object* receiver,
|
|
|
|
int argc,
|
|
|
|
Object*** args);
|
|
|
|
|
|
|
|
Handle<Code> code;
|
|
|
|
if (construct) {
|
|
|
|
JSConstructEntryStub stub;
|
|
|
|
code = stub.GetCode();
|
|
|
|
} else {
|
|
|
|
JSEntryStub stub;
|
|
|
|
code = stub.GetCode();
|
|
|
|
}
|
|
|
|
|
2009-08-07 07:15:16 +00:00
|
|
|
// Convert calls on global objects to be calls on the global
|
|
|
|
// receiver instead to avoid having a 'this' pointer which refers
|
|
|
|
// directly to a global object.
|
|
|
|
if (receiver->IsGlobalObject()) {
|
|
|
|
Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver);
|
|
|
|
receiver = Handle<JSObject>(global->global_receiver());
|
|
|
|
}
|
|
|
|
|
2008-07-25 07:37:58 +00:00
|
|
|
{
|
|
|
|
// Save and restore context around invocation and block the
|
2008-07-03 15:10:15 +00:00
|
|
|
// allocation of handles without explicit handle scopes.
|
|
|
|
SaveContext save;
|
|
|
|
NoHandleAllocation na;
|
|
|
|
JSEntryFunction entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
|
|
|
|
|
|
|
|
// Call the function through the right JS entry stub.
|
|
|
|
value = CALL_GENERATED_CODE(entry, func->code()->entry(), *func,
|
|
|
|
*receiver, argc, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
value->Verify();
|
|
|
|
#endif
|
|
|
|
|
2008-10-28 07:30:10 +00:00
|
|
|
// Update the pending exception flag and return the value.
|
2008-07-03 15:10:15 +00:00
|
|
|
*has_pending_exception = value->IsException();
|
|
|
|
ASSERT(*has_pending_exception == Top::has_pending_exception());
|
2008-10-28 07:30:10 +00:00
|
|
|
if (*has_pending_exception) {
|
2008-12-05 08:35:52 +00:00
|
|
|
Top::ReportPendingMessages();
|
2008-11-04 16:23:56 +00:00
|
|
|
return Handle<Object>();
|
2008-12-05 08:35:52 +00:00
|
|
|
} else {
|
|
|
|
Top::clear_pending_message();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Handle<Object>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::Call(Handle<JSFunction> func,
|
|
|
|
Handle<Object> receiver,
|
|
|
|
int argc,
|
|
|
|
Object*** args,
|
|
|
|
bool* pending_exception) {
|
|
|
|
return Invoke(false, func, receiver, argc, args, pending_exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::New(Handle<JSFunction> func, int argc,
|
|
|
|
Object*** args, bool* pending_exception) {
|
|
|
|
return Invoke(true, func, Top::global(), argc, args, pending_exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::TryCall(Handle<JSFunction> func,
|
|
|
|
Handle<Object> receiver,
|
|
|
|
int argc,
|
|
|
|
Object*** args,
|
|
|
|
bool* caught_exception) {
|
|
|
|
// Enter a try-block while executing the JavaScript code. To avoid
|
2008-09-10 14:54:15 +00:00
|
|
|
// duplicate error printing it must be non-verbose. Also, to avoid
|
|
|
|
// creating message objects during stack overflow we shouldn't
|
|
|
|
// capture messages.
|
2008-07-03 15:10:15 +00:00
|
|
|
v8::TryCatch catcher;
|
|
|
|
catcher.SetVerbose(false);
|
2008-09-10 14:54:15 +00:00
|
|
|
catcher.SetCaptureMessage(false);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Handle<Object> result = Invoke(false, func, receiver, argc, args,
|
|
|
|
caught_exception);
|
|
|
|
|
|
|
|
if (*caught_exception) {
|
|
|
|
ASSERT(catcher.HasCaught());
|
|
|
|
ASSERT(Top::has_pending_exception());
|
|
|
|
ASSERT(Top::external_caught_exception());
|
2009-09-01 13:55:45 +00:00
|
|
|
if (Top::pending_exception() == Heap::termination_exception()) {
|
|
|
|
result = Factory::termination_exception();
|
|
|
|
} else {
|
|
|
|
result = v8::Utils::OpenHandle(*catcher.Exception());
|
|
|
|
}
|
|
|
|
Top::OptionalRescheduleException(true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(!Top::has_pending_exception());
|
|
|
|
ASSERT(!Top::external_caught_exception());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::GetFunctionDelegate(Handle<Object> object) {
|
|
|
|
ASSERT(!object->IsJSFunction());
|
|
|
|
|
|
|
|
// If you return a function from here, it will be called when an
|
|
|
|
// attempt is made to call the given object as a function.
|
|
|
|
|
2009-07-13 23:41:17 +00:00
|
|
|
// Regular expressions can be called as functions in both Firefox
|
|
|
|
// and Safari so we allow it too.
|
2009-08-17 10:19:00 +00:00
|
|
|
if (object->IsJSRegExp()) {
|
2009-07-13 23:41:17 +00:00
|
|
|
Handle<String> exec = Factory::exec_symbol();
|
|
|
|
return Handle<Object>(object->GetProperty(*exec));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Objects created through the API can have an instance-call handler
|
|
|
|
// that should be used when calling the object as a function.
|
|
|
|
if (object->IsHeapObject() &&
|
|
|
|
HeapObject::cast(*object)->map()->has_instance_call_handler()) {
|
|
|
|
return Handle<JSFunction>(
|
|
|
|
Top::global_context()->call_as_function_delegate());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Factory::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-20 19:33:44 +00:00
|
|
|
Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) {
|
|
|
|
ASSERT(!object->IsJSFunction());
|
|
|
|
|
|
|
|
// If you return a function from here, it will be called when an
|
|
|
|
// attempt is made to call the given object as a constructor.
|
|
|
|
|
|
|
|
// Objects created through the API can have an instance-call handler
|
|
|
|
// that should be used when calling the object as a function.
|
|
|
|
if (object->IsHeapObject() &&
|
|
|
|
HeapObject::cast(*object)->map()->has_instance_call_handler()) {
|
|
|
|
return Handle<JSFunction>(
|
|
|
|
Top::global_context()->call_as_constructor_delegate());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Factory::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Static state for stack guards.
|
|
|
|
StackGuard::ThreadLocal StackGuard::thread_local_;
|
|
|
|
|
|
|
|
|
|
|
|
bool StackGuard::IsStackOverflow() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
return (thread_local_.jslimit_ != kInterruptLimit &&
|
|
|
|
thread_local_.climit_ != kInterruptLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::EnableInterrupts() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
if (IsSet(access)) {
|
|
|
|
set_limits(kInterruptLimit, access);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::SetStackLimit(uintptr_t limit) {
|
|
|
|
ExecutionAccess access;
|
|
|
|
// If the current limits are special (eg due to a pending interrupt) then
|
|
|
|
// leave them alone.
|
2009-10-01 10:33:05 +00:00
|
|
|
uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(limit);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (thread_local_.jslimit_ == thread_local_.initial_jslimit_) {
|
2009-10-01 10:33:05 +00:00
|
|
|
thread_local_.jslimit_ = jslimit;
|
|
|
|
Heap::SetStackLimit(jslimit);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
if (thread_local_.climit_ == thread_local_.initial_climit_) {
|
|
|
|
thread_local_.climit_ = limit;
|
|
|
|
}
|
|
|
|
thread_local_.initial_climit_ = limit;
|
2009-10-01 10:33:05 +00:00
|
|
|
thread_local_.initial_jslimit_ = jslimit;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::DisableInterrupts() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
reset_limits(access);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool StackGuard::IsSet(const ExecutionAccess& lock) {
|
|
|
|
return thread_local_.interrupt_flags_ != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool StackGuard::IsInterrupted() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
return thread_local_.interrupt_flags_ & INTERRUPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::Interrupt() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
thread_local_.interrupt_flags_ |= INTERRUPT;
|
2008-07-30 08:49:36 +00:00
|
|
|
set_limits(kInterruptLimit, access);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool StackGuard::IsPreempted() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
return thread_local_.interrupt_flags_ & PREEMPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::Preempt() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
thread_local_.interrupt_flags_ |= PREEMPT;
|
2008-07-30 08:49:36 +00:00
|
|
|
set_limits(kInterruptLimit, access);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 15:14:11 +00:00
|
|
|
bool StackGuard::IsTerminateExecution() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
return thread_local_.interrupt_flags_ & TERMINATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::TerminateExecution() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
thread_local_.interrupt_flags_ |= TERMINATE;
|
|
|
|
set_limits(kInterruptLimit, access);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2008-07-03 15:10:15 +00:00
|
|
|
bool StackGuard::IsDebugBreak() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
return thread_local_.interrupt_flags_ & DEBUGBREAK;
|
|
|
|
}
|
|
|
|
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void StackGuard::DebugBreak() {
|
|
|
|
ExecutionAccess access;
|
2008-07-30 08:49:36 +00:00
|
|
|
thread_local_.interrupt_flags_ |= DEBUGBREAK;
|
|
|
|
set_limits(kInterruptLimit, access);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-13 13:26:21 +00:00
|
|
|
bool StackGuard::IsDebugCommand() {
|
|
|
|
ExecutionAccess access;
|
|
|
|
return thread_local_.interrupt_flags_ & DEBUGCOMMAND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::DebugCommand() {
|
|
|
|
if (FLAG_debugger_auto_break) {
|
|
|
|
ExecutionAccess access;
|
|
|
|
thread_local_.interrupt_flags_ |= DEBUGCOMMAND;
|
|
|
|
set_limits(kInterruptLimit, access);
|
|
|
|
}
|
|
|
|
}
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2009-03-13 13:26:21 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void StackGuard::Continue(InterruptFlag after_what) {
|
|
|
|
ExecutionAccess access;
|
|
|
|
thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what);
|
|
|
|
if (thread_local_.interrupt_flags_ == 0) {
|
|
|
|
reset_limits(access);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int StackGuard::ArchiveSpacePerThread() {
|
|
|
|
return sizeof(ThreadLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char* StackGuard::ArchiveStackGuard(char* to) {
|
|
|
|
ExecutionAccess access;
|
|
|
|
memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
|
|
|
|
ThreadLocal blank;
|
|
|
|
thread_local_ = blank;
|
|
|
|
return to + sizeof(ThreadLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char* StackGuard::RestoreStackGuard(char* from) {
|
|
|
|
ExecutionAccess access;
|
|
|
|
memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
|
2009-08-26 10:27:32 +00:00
|
|
|
Heap::SetStackLimit(thread_local_.jslimit_);
|
2008-07-03 15:10:15 +00:00
|
|
|
return from + sizeof(ThreadLocal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-01 10:33:05 +00:00
|
|
|
static internal::Thread::LocalStorageKey stack_limit_key =
|
|
|
|
internal::Thread::CreateThreadLocalKey();
|
|
|
|
|
|
|
|
|
2009-09-28 12:25:21 +00:00
|
|
|
void StackGuard::FreeThreadResources() {
|
2009-10-01 10:33:05 +00:00
|
|
|
Thread::SetThreadLocal(
|
|
|
|
stack_limit_key,
|
|
|
|
reinterpret_cast<void*>(thread_local_.initial_climit_));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::ThreadLocal::Clear() {
|
|
|
|
initial_jslimit_ = kIllegalLimit;
|
|
|
|
jslimit_ = kIllegalLimit;
|
|
|
|
initial_climit_ = kIllegalLimit;
|
|
|
|
climit_ = kIllegalLimit;
|
|
|
|
nesting_ = 0;
|
|
|
|
postpone_interrupts_nesting_ = 0;
|
|
|
|
interrupt_flags_ = 0;
|
|
|
|
Heap::SetStackLimit(kIllegalLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::ThreadLocal::Initialize() {
|
|
|
|
if (initial_climit_ == kIllegalLimit) {
|
|
|
|
// Takes the address of the limit variable in order to find out where
|
|
|
|
// the top of stack is right now.
|
|
|
|
intptr_t limit = reinterpret_cast<intptr_t>(&limit) - kLimitSize;
|
|
|
|
initial_jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
|
|
|
|
jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
|
|
|
|
initial_climit_ = limit;
|
|
|
|
climit_ = limit;
|
|
|
|
Heap::SetStackLimit(SimulatorStack::JsLimitFromCLimit(limit));
|
|
|
|
}
|
|
|
|
nesting_ = 0;
|
|
|
|
postpone_interrupts_nesting_ = 0;
|
|
|
|
interrupt_flags_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::ClearThread(const ExecutionAccess& lock) {
|
|
|
|
thread_local_.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StackGuard::InitThread(const ExecutionAccess& lock) {
|
|
|
|
thread_local_.Initialize();
|
|
|
|
void* stored_limit = Thread::GetThreadLocal(stack_limit_key);
|
|
|
|
// You should hold the ExecutionAccess lock when you call this.
|
|
|
|
if (stored_limit != NULL) {
|
|
|
|
StackGuard::SetStackLimit(reinterpret_cast<intptr_t>(stored_limit));
|
|
|
|
}
|
2009-09-28 12:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// --- C a l l s t o n a t i v e s ---
|
|
|
|
|
|
|
|
#define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \
|
|
|
|
do { \
|
|
|
|
Object** args[argc] = argv; \
|
|
|
|
ASSERT(has_pending_exception != NULL); \
|
|
|
|
return Call(Top::name##_fun(), Top::builtins(), argc, args, \
|
|
|
|
has_pending_exception); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToBoolean(Handle<Object> obj) {
|
|
|
|
// See the similar code in runtime.js:ToBoolean.
|
|
|
|
if (obj->IsBoolean()) return obj;
|
|
|
|
bool result = true;
|
|
|
|
if (obj->IsString()) {
|
|
|
|
result = Handle<String>::cast(obj)->length() != 0;
|
|
|
|
} else if (obj->IsNull() || obj->IsUndefined()) {
|
|
|
|
result = false;
|
|
|
|
} else if (obj->IsNumber()) {
|
|
|
|
double value = obj->Number();
|
|
|
|
result = !((value == 0) || isnan(value));
|
|
|
|
}
|
|
|
|
return Handle<Object>(Heap::ToBoolean(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) {
|
|
|
|
RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) {
|
|
|
|
RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) {
|
|
|
|
RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) {
|
|
|
|
if (obj->IsJSObject()) return obj;
|
|
|
|
RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) {
|
|
|
|
RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) {
|
|
|
|
RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) {
|
|
|
|
RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::NewDate(double time, bool* exc) {
|
|
|
|
Handle<Object> time_obj = Factory::NewNumber(time);
|
|
|
|
RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#undef RETURN_NATIVE_CALL
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
|
|
|
|
int int_index = static_cast<int>(index);
|
|
|
|
if (int_index < 0 || int_index >= string->length()) {
|
|
|
|
return Factory::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> char_at =
|
|
|
|
GetProperty(Top::builtins(), Factory::char_at_symbol());
|
|
|
|
if (!char_at->IsJSFunction()) {
|
|
|
|
return Factory::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool caught_exception;
|
|
|
|
Handle<Object> index_object = Factory::NewNumberFromInt(int_index);
|
|
|
|
Object** index_arg[] = { index_object.location() };
|
|
|
|
Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
|
|
|
|
string,
|
|
|
|
ARRAY_SIZE(index_arg),
|
|
|
|
index_arg,
|
|
|
|
&caught_exception);
|
|
|
|
if (caught_exception) {
|
|
|
|
return Factory::undefined_value();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSFunction> Execution::InstantiateFunction(
|
|
|
|
Handle<FunctionTemplateInfo> data, bool* exc) {
|
|
|
|
// Fast case: see if the function has already been instantiated
|
|
|
|
int serial_number = Smi::cast(data->serial_number())->value();
|
|
|
|
Object* elm =
|
|
|
|
Top::global_context()->function_cache()->GetElement(serial_number);
|
2009-01-13 07:13:33 +00:00
|
|
|
if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
|
2008-07-03 15:10:15 +00:00
|
|
|
// The function has not yet been instantiated in this context; do it.
|
|
|
|
Object** args[1] = { Handle<Object>::cast(data).location() };
|
|
|
|
Handle<Object> result =
|
|
|
|
Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
|
|
|
|
if (*exc) return Handle<JSFunction>::null();
|
|
|
|
return Handle<JSFunction>::cast(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
|
|
|
|
bool* exc) {
|
|
|
|
if (data->property_list()->IsUndefined() &&
|
|
|
|
!data->constructor()->IsUndefined()) {
|
2009-03-23 12:37:53 +00:00
|
|
|
// Initialization to make gcc happy.
|
|
|
|
Object* result = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
{
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<FunctionTemplateInfo> cons_template =
|
|
|
|
Handle<FunctionTemplateInfo>(
|
|
|
|
FunctionTemplateInfo::cast(data->constructor()));
|
|
|
|
Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
|
|
|
|
if (*exc) return Handle<JSObject>::null();
|
|
|
|
Handle<Object> value = New(cons, 0, NULL, exc);
|
|
|
|
if (*exc) return Handle<JSObject>::null();
|
|
|
|
result = *value;
|
|
|
|
}
|
|
|
|
ASSERT(!*exc);
|
|
|
|
return Handle<JSObject>(JSObject::cast(result));
|
|
|
|
} else {
|
|
|
|
Object** args[1] = { Handle<Object>::cast(data).location() };
|
|
|
|
Handle<Object> result =
|
|
|
|
Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
|
|
|
|
if (*exc) return Handle<JSObject>::null();
|
|
|
|
return Handle<JSObject>::cast(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Execution::ConfigureInstance(Handle<Object> instance,
|
|
|
|
Handle<Object> instance_template,
|
|
|
|
bool* exc) {
|
|
|
|
Object** args[2] = { instance.location(), instance_template.location() };
|
|
|
|
Execution::Call(Top::configure_instance_fun(), Top::builtins(), 2, args, exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
|
|
|
|
Handle<JSFunction> fun,
|
|
|
|
Handle<Object> pos,
|
|
|
|
Handle<Object> is_global) {
|
|
|
|
const int argc = 4;
|
|
|
|
Object** args[argc] = { recv.location(),
|
|
|
|
Handle<Object>::cast(fun).location(),
|
|
|
|
pos.location(),
|
|
|
|
is_global.location() };
|
|
|
|
bool caught_exception = false;
|
|
|
|
Handle<Object> result = TryCall(Top::get_stack_trace_line_fun(),
|
|
|
|
Top::builtins(), argc, args,
|
|
|
|
&caught_exception);
|
|
|
|
if (caught_exception || !result->IsString()) return Factory::empty_symbol();
|
|
|
|
return Handle<String>::cast(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-12 10:49:00 +00:00
|
|
|
static Object* RuntimePreempt() {
|
|
|
|
// Clear the preempt request flag.
|
|
|
|
StackGuard::Continue(PREEMPT);
|
|
|
|
|
|
|
|
ContextSwitcher::PreemptionReceived();
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2009-03-06 11:03:14 +00:00
|
|
|
if (Debug::InDebugger()) {
|
|
|
|
// If currently in the debugger don't do any actual preemption but record
|
|
|
|
// that preemption occoured while in the debugger.
|
|
|
|
Debug::PreemptionWhileInDebugger();
|
|
|
|
} else {
|
|
|
|
// Perform preemption.
|
2008-12-12 10:49:00 +00:00
|
|
|
v8::Unlocker unlocker;
|
|
|
|
Thread::YieldCPU();
|
|
|
|
}
|
2009-04-20 16:36:13 +00:00
|
|
|
#else
|
|
|
|
// Perform preemption.
|
|
|
|
v8::Unlocker unlocker;
|
|
|
|
Thread::YieldCPU();
|
|
|
|
#endif
|
2008-12-12 10:49:00 +00:00
|
|
|
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2008-12-12 10:49:00 +00:00
|
|
|
Object* Execution::DebugBreakHelper() {
|
|
|
|
// Just continue if breaks are disabled.
|
|
|
|
if (Debug::disable_break()) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
2009-07-24 06:14:23 +00:00
|
|
|
{
|
|
|
|
JavaScriptFrameIterator it;
|
|
|
|
ASSERT(!it.done());
|
|
|
|
Object* fun = it.frame()->function();
|
|
|
|
if (fun && fun->IsJSFunction()) {
|
|
|
|
// Don't stop in builtin functions.
|
2009-07-29 11:55:26 +00:00
|
|
|
if (JSFunction::cast(fun)->IsBuiltin()) {
|
|
|
|
return Heap::undefined_value();
|
2009-07-24 06:14:23 +00:00
|
|
|
}
|
2009-07-29 11:55:26 +00:00
|
|
|
GlobalObject* global = JSFunction::cast(fun)->context()->global();
|
2009-07-24 06:14:23 +00:00
|
|
|
// Don't stop in debugger functions.
|
|
|
|
if (Debug::IsDebugGlobal(global)) {
|
2009-07-29 11:55:26 +00:00
|
|
|
return Heap::undefined_value();
|
2009-07-24 06:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 08:42:02 +00:00
|
|
|
// Collect the break state before clearing the flags.
|
2009-03-13 13:26:21 +00:00
|
|
|
bool debug_command_only =
|
|
|
|
StackGuard::IsDebugCommand() && !StackGuard::IsDebugBreak();
|
|
|
|
|
|
|
|
// Clear the debug request flags.
|
2008-12-12 10:49:00 +00:00
|
|
|
StackGuard::Continue(DEBUGBREAK);
|
2009-03-13 13:26:21 +00:00
|
|
|
StackGuard::Continue(DEBUGCOMMAND);
|
|
|
|
|
2008-12-12 10:49:00 +00:00
|
|
|
HandleScope scope;
|
|
|
|
// Enter the debugger. Just continue if we fail to enter the debugger.
|
|
|
|
EnterDebugger debugger;
|
|
|
|
if (debugger.FailedToEnter()) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
2009-05-29 08:42:02 +00:00
|
|
|
// Notify the debug event listeners. Indicate auto continue if the break was
|
|
|
|
// a debug command break.
|
2009-03-13 13:26:21 +00:00
|
|
|
Debugger::OnDebugBreak(Factory::undefined_value(), debug_command_only);
|
2008-12-12 10:49:00 +00:00
|
|
|
|
|
|
|
// Return to continue execution.
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2008-12-12 10:49:00 +00:00
|
|
|
|
|
|
|
Object* Execution::HandleStackGuardInterrupt() {
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2009-03-13 13:26:21 +00:00
|
|
|
if (StackGuard::IsDebugBreak() || StackGuard::IsDebugCommand()) {
|
|
|
|
DebugBreakHelper();
|
|
|
|
}
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2008-12-12 10:49:00 +00:00
|
|
|
if (StackGuard::IsPreempted()) RuntimePreempt();
|
2009-08-19 15:14:11 +00:00
|
|
|
if (StackGuard::IsTerminateExecution()) {
|
|
|
|
StackGuard::Continue(TERMINATE);
|
|
|
|
return Top::TerminateExecution();
|
|
|
|
}
|
2008-12-12 10:49:00 +00:00
|
|
|
if (StackGuard::IsInterrupted()) {
|
|
|
|
// interrupt
|
|
|
|
StackGuard::Continue(INTERRUPT);
|
|
|
|
return Top::StackOverflow();
|
|
|
|
}
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// --- G C E x t e n s i o n ---
|
|
|
|
|
|
|
|
const char* GCExtension::kSource = "native function gc();";
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<v8::FunctionTemplate> GCExtension::GetNativeFunction(
|
|
|
|
v8::Handle<v8::String> str) {
|
|
|
|
return v8::FunctionTemplate::New(GCExtension::GC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> GCExtension::GC(const v8::Arguments& args) {
|
|
|
|
// All allocation spaces other than NEW_SPACE have the same effect.
|
2009-08-25 02:54:39 +00:00
|
|
|
Heap::CollectAllGarbage(false);
|
2008-07-03 15:10:15 +00:00
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GCExtension kGCExtension;
|
|
|
|
v8::DeclareExtension kGCExtensionDeclaration(&kGCExtension);
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|