blob: bdbdca81bb7cfcdf5cf34316ce4d541abab5fed6 [file] [log] [blame]
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "v8.h"
31
32#include "api.h"
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +000033#include "bootstrapper.h"
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000034#include "codegen.h"
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000035#include "debug.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#include "runtime-profiler.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000037#include "simulator.h"
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000038#include "v8threads.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000039#include "vm-state-inl.h"
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000040
kasperl@chromium.org71affb52009-05-26 05:44:31 +000041namespace v8 {
42namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
44
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045StackGuard::StackGuard()
46 : isolate_(NULL) {
47}
48
49
50void StackGuard::set_interrupt_limits(const ExecutionAccess& lock) {
51 ASSERT(isolate_ != NULL);
52 // Ignore attempts to interrupt when interrupts are postponed.
53 if (should_postpone_interrupts(lock)) return;
54 thread_local_.jslimit_ = kInterruptLimit;
55 thread_local_.climit_ = kInterruptLimit;
56 isolate_->heap()->SetStackLimits();
57}
58
59
60void StackGuard::reset_limits(const ExecutionAccess& lock) {
61 ASSERT(isolate_ != NULL);
62 thread_local_.jslimit_ = thread_local_.real_jslimit_;
63 thread_local_.climit_ = thread_local_.real_climit_;
64 isolate_->heap()->SetStackLimits();
65}
66
67
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068static Handle<Object> Invoke(bool construct,
69 Handle<JSFunction> func,
70 Handle<Object> receiver,
71 int argc,
72 Object*** args,
73 bool* has_pending_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 Isolate* isolate = func->GetIsolate();
75
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076 // Entering JavaScript.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000077 VMState state(isolate, JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079 // Placeholder for return value.
lrn@chromium.org303ada72010-10-27 09:33:13 +000080 MaybeObject* value = reinterpret_cast<Object*>(kZapValue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081
82 typedef Object* (*JSEntryFunction)(
83 byte* entry,
84 Object* function,
85 Object* receiver,
86 int argc,
87 Object*** args);
88
89 Handle<Code> code;
90 if (construct) {
91 JSConstructEntryStub stub;
92 code = stub.GetCode();
93 } else {
94 JSEntryStub stub;
95 code = stub.GetCode();
96 }
97
sgjesse@chromium.org8d96e6d2009-08-07 10:18:15 +000098 // Convert calls on global objects to be calls on the global
99 // receiver instead to avoid having a 'this' pointer which refers
100 // directly to a global object.
101 if (receiver->IsGlobalObject()) {
102 Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver);
103 receiver = Handle<JSObject>(global->global_receiver());
104 }
105
kasperl@chromium.org7b9eafd2009-12-21 15:20:30 +0000106 // Make sure that the global object of the context we're about to
107 // make the current one is indeed a global object.
108 ASSERT(func->context()->global()->IsGlobalObject());
109
kasper.lund44510672008-07-25 07:37:58 +0000110 {
111 // Save and restore context around invocation and block the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 // allocation of handles without explicit handle scopes.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000113 SaveContext save(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114 NoHandleAllocation na;
115 JSEntryFunction entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
116
117 // Call the function through the right JS entry stub.
ager@chromium.org5c838252010-02-19 08:53:10 +0000118 byte* entry_address = func->code()->entry();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000119 JSFunction* function = *func;
120 Object* receiver_pointer = *receiver;
121 value = CALL_GENERATED_CODE(entry, entry_address, function,
122 receiver_pointer, argc, args);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 }
124
125#ifdef DEBUG
126 value->Verify();
127#endif
128
129 // Update the pending exception flag and return the value.
130 *has_pending_exception = value->IsException();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000131 ASSERT(*has_pending_exception == Isolate::Current()->has_pending_exception());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000132 if (*has_pending_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000133 isolate->ReportPendingMessages();
134 if (isolate->pending_exception() == Failure::OutOfMemoryException()) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000135 if (!isolate->ignore_out_of_memory()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000136 V8::FatalProcessOutOfMemory("JS", true);
137 }
138 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000139 return Handle<Object>();
ager@chromium.org8bb60582008-12-11 12:02:20 +0000140 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000141 isolate->clear_pending_message();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 }
143
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000144 return Handle<Object>(value->ToObjectUnchecked(), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145}
146
147
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000148Handle<Object> Execution::Call(Handle<Object> callable,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 Handle<Object> receiver,
150 int argc,
151 Object*** args,
152 bool* pending_exception) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000153 if (!callable->IsJSFunction()) {
154 callable = TryGetFunctionDelegate(callable, pending_exception);
155 if (*pending_exception) return callable;
156 }
157 Handle<JSFunction> func = Handle<JSFunction>::cast(callable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 return Invoke(false, func, receiver, argc, args, pending_exception);
159}
160
161
162Handle<Object> Execution::New(Handle<JSFunction> func, int argc,
163 Object*** args, bool* pending_exception) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 return Invoke(true, func, Isolate::Current()->global(), argc, args,
165 pending_exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166}
167
168
169Handle<Object> Execution::TryCall(Handle<JSFunction> func,
170 Handle<Object> receiver,
171 int argc,
172 Object*** args,
173 bool* caught_exception) {
174 // Enter a try-block while executing the JavaScript code. To avoid
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000175 // duplicate error printing it must be non-verbose. Also, to avoid
176 // creating message objects during stack overflow we shouldn't
177 // capture messages.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 v8::TryCatch catcher;
179 catcher.SetVerbose(false);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000180 catcher.SetCaptureMessage(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181
182 Handle<Object> result = Invoke(false, func, receiver, argc, args,
183 caught_exception);
184
185 if (*caught_exception) {
186 ASSERT(catcher.HasCaught());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000187 Isolate* isolate = Isolate::Current();
188 ASSERT(isolate->has_pending_exception());
189 ASSERT(isolate->external_caught_exception());
190 if (isolate->pending_exception() ==
191 isolate->heap()->termination_exception()) {
192 result = isolate->factory()->termination_exception();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000193 } else {
194 result = v8::Utils::OpenHandle(*catcher.Exception());
195 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000196 isolate->OptionalRescheduleException(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000197 }
198
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000199 ASSERT(!Isolate::Current()->has_pending_exception());
200 ASSERT(!Isolate::Current()->external_caught_exception());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 return result;
202}
203
204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205Handle<Object> Execution::GetFunctionDelegate(Handle<Object> object) {
206 ASSERT(!object->IsJSFunction());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000207 Isolate* isolate = Isolate::Current();
208 Factory* factory = isolate->factory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209
210 // If you return a function from here, it will be called when an
211 // attempt is made to call the given object as a function.
212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 // Objects created through the API can have an instance-call handler
214 // that should be used when calling the object as a function.
215 if (object->IsHeapObject() &&
216 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
217 return Handle<JSFunction>(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000218 isolate->global_context()->call_as_function_delegate());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 }
220
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000221 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222}
223
224
lrn@chromium.org1c092762011-05-09 09:42:16 +0000225Handle<Object> Execution::TryGetFunctionDelegate(Handle<Object> object,
226 bool* has_pending_exception) {
227 ASSERT(!object->IsJSFunction());
228 Isolate* isolate = Isolate::Current();
229
230 // Objects created through the API can have an instance-call handler
231 // that should be used when calling the object as a function.
232 if (object->IsHeapObject() &&
233 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
234 return Handle<JSFunction>(
235 isolate->global_context()->call_as_function_delegate());
236 }
237
238 // If the Object doesn't have an instance-call handler we should
239 // throw a non-callable exception.
240 i::Handle<i::Object> error_obj = isolate->factory()->NewTypeError(
241 "called_non_callable", i::HandleVector<i::Object>(&object, 1));
242 isolate->Throw(*error_obj);
243 *has_pending_exception = true;
244
245 return isolate->factory()->undefined_value();
246}
247
248
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000249Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) {
250 ASSERT(!object->IsJSFunction());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000251 Isolate* isolate = Isolate::Current();
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000252
253 // If you return a function from here, it will be called when an
254 // attempt is made to call the given object as a constructor.
255
256 // Objects created through the API can have an instance-call handler
257 // that should be used when calling the object as a function.
258 if (object->IsHeapObject() &&
259 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
260 return Handle<JSFunction>(
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000261 isolate->global_context()->call_as_constructor_delegate());
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000262 }
263
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000264 return isolate->factory()->undefined_value();
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000265}
266
267
lrn@chromium.org1c092762011-05-09 09:42:16 +0000268Handle<Object> Execution::TryGetConstructorDelegate(
269 Handle<Object> object,
270 bool* has_pending_exception) {
271 ASSERT(!object->IsJSFunction());
272 Isolate* isolate = Isolate::Current();
273
274 // If you return a function from here, it will be called when an
275 // attempt is made to call the given object as a constructor.
276
277 // Objects created through the API can have an instance-call handler
278 // that should be used when calling the object as a function.
279 if (object->IsHeapObject() &&
280 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
281 return Handle<JSFunction>(
282 isolate->global_context()->call_as_constructor_delegate());
283 }
284
285 // If the Object doesn't have an instance-call handler we should
286 // throw a non-callable exception.
287 i::Handle<i::Object> error_obj = isolate->factory()->NewTypeError(
288 "called_non_callable", i::HandleVector<i::Object>(&object, 1));
289 isolate->Throw(*error_obj);
290 *has_pending_exception = true;
291
292 return isolate->factory()->undefined_value();
293}
294
295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296bool StackGuard::IsStackOverflow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000297 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 return (thread_local_.jslimit_ != kInterruptLimit &&
299 thread_local_.climit_ != kInterruptLimit);
300}
301
302
303void StackGuard::EnableInterrupts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000304 ExecutionAccess access(isolate_);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000305 if (has_pending_interrupts(access)) {
306 set_interrupt_limits(access);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 }
308}
309
310
311void StackGuard::SetStackLimit(uintptr_t limit) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000312 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 // If the current limits are special (eg due to a pending interrupt) then
314 // leave them alone.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000315 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000316 if (thread_local_.jslimit_ == thread_local_.real_jslimit_) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000317 thread_local_.jslimit_ = jslimit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000319 if (thread_local_.climit_ == thread_local_.real_climit_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 thread_local_.climit_ = limit;
321 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000322 thread_local_.real_climit_ = limit;
323 thread_local_.real_jslimit_ = jslimit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324}
325
326
327void StackGuard::DisableInterrupts() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000328 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 reset_limits(access);
330}
331
332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333bool StackGuard::IsInterrupted() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 return thread_local_.interrupt_flags_ & INTERRUPT;
336}
337
338
339void StackGuard::Interrupt() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000340 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 thread_local_.interrupt_flags_ |= INTERRUPT;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000342 set_interrupt_limits(access);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343}
344
345
346bool StackGuard::IsPreempted() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000347 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 return thread_local_.interrupt_flags_ & PREEMPT;
349}
350
351
352void StackGuard::Preempt() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 thread_local_.interrupt_flags_ |= PREEMPT;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000355 set_interrupt_limits(access);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356}
357
358
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000359bool StackGuard::IsTerminateExecution() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000360 ExecutionAccess access(isolate_);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000361 return thread_local_.interrupt_flags_ & TERMINATE;
362}
363
364
365void StackGuard::TerminateExecution() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000366 ExecutionAccess access(isolate_);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000367 thread_local_.interrupt_flags_ |= TERMINATE;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000368 set_interrupt_limits(access);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000369}
370
371
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372bool StackGuard::IsRuntimeProfilerTick() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000373 ExecutionAccess access(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000374 return thread_local_.interrupt_flags_ & RUNTIME_PROFILER_TICK;
375}
376
377
378void StackGuard::RequestRuntimeProfilerTick() {
379 // Ignore calls if we're not optimizing or if we can't get the lock.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000380 if (FLAG_opt && ExecutionAccess::TryLock(isolate_)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000381 thread_local_.interrupt_flags_ |= RUNTIME_PROFILER_TICK;
382 if (thread_local_.postpone_interrupts_nesting_ == 0) {
383 thread_local_.jslimit_ = thread_local_.climit_ = kInterruptLimit;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 isolate_->heap()->SetStackLimits();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 ExecutionAccess::Unlock(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000387 }
388}
389
390
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000391#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392bool StackGuard::IsDebugBreak() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000393 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 return thread_local_.interrupt_flags_ & DEBUGBREAK;
395}
396
kasper.lund44510672008-07-25 07:37:58 +0000397
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398void StackGuard::DebugBreak() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000399 ExecutionAccess access(isolate_);
kasper.lund7276f142008-07-30 08:49:36 +0000400 thread_local_.interrupt_flags_ |= DEBUGBREAK;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000401 set_interrupt_limits(access);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402}
403
404
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000405bool StackGuard::IsDebugCommand() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 ExecutionAccess access(isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000407 return thread_local_.interrupt_flags_ & DEBUGCOMMAND;
408}
409
410
411void StackGuard::DebugCommand() {
412 if (FLAG_debugger_auto_break) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000413 ExecutionAccess access(isolate_);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000414 thread_local_.interrupt_flags_ |= DEBUGCOMMAND;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000415 set_interrupt_limits(access);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000416 }
417}
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000418#endif
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420void StackGuard::Continue(InterruptFlag after_what) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000421 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000423 if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424 reset_limits(access);
425 }
426}
427
428
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429char* StackGuard::ArchiveStackGuard(char* to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000430 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
432 ThreadLocal blank;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433
434 // Set the stack limits using the old thread_local_.
435 // TODO(isolates): This was the old semantics of constructing a ThreadLocal
436 // (as the ctor called SetStackLimits, which looked at the
437 // current thread_local_ from StackGuard)-- but is this
438 // really what was intended?
439 isolate_->heap()->SetStackLimits();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 thread_local_ = blank;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 return to + sizeof(ThreadLocal);
443}
444
445
446char* StackGuard::RestoreStackGuard(char* from) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 ExecutionAccess access(isolate_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000449 isolate_->heap()->SetStackLimits();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450 return from + sizeof(ThreadLocal);
451}
452
453
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000454void StackGuard::FreeThreadResources() {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000455 Isolate::PerIsolateThreadData* per_thread =
456 isolate_->FindOrAllocatePerThreadDataForThisThread();
457 per_thread->set_stack_limit(thread_local_.real_climit_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000458}
459
460
461void StackGuard::ThreadLocal::Clear() {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000462 real_jslimit_ = kIllegalLimit;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000463 jslimit_ = kIllegalLimit;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000464 real_climit_ = kIllegalLimit;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000465 climit_ = kIllegalLimit;
466 nesting_ = 0;
467 postpone_interrupts_nesting_ = 0;
468 interrupt_flags_ = 0;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000469}
470
471
lrn@chromium.org1c092762011-05-09 09:42:16 +0000472bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000473 bool should_set_stack_limits = false;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000474 if (real_climit_ == kIllegalLimit) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000475 // Takes the address of the limit variable in order to find out where
476 // the top of stack is right now.
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000477 const uintptr_t kLimitSize = FLAG_stack_size * KB;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000478 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize;
479 ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000480 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
481 jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000482 real_climit_ = limit;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000483 climit_ = limit;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 should_set_stack_limits = true;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000485 }
486 nesting_ = 0;
487 postpone_interrupts_nesting_ = 0;
488 interrupt_flags_ = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000489 return should_set_stack_limits;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000490}
491
492
493void StackGuard::ClearThread(const ExecutionAccess& lock) {
494 thread_local_.Clear();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000495 isolate_->heap()->SetStackLimits();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000496}
497
498
499void StackGuard::InitThread(const ExecutionAccess& lock) {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000500 if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits();
501 Isolate::PerIsolateThreadData* per_thread =
502 isolate_->FindOrAllocatePerThreadDataForThisThread();
503 uintptr_t stored_limit = per_thread->stack_limit();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000504 // You should hold the ExecutionAccess lock when you call this.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000505 if (stored_limit != 0) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000506 SetStackLimit(stored_limit);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000507 }
508}
509
510
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511// --- C a l l s t o n a t i v e s ---
512
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000513#define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \
514 do { \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000515 Isolate* isolate = Isolate::Current(); \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000516 Object** args[argc] = argv; \
517 ASSERT(has_pending_exception != NULL); \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000518 return Call(isolate->name##_fun(), \
519 isolate->js_builtins_object(), argc, args, \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000520 has_pending_exception); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 } while (false)
522
523
524Handle<Object> Execution::ToBoolean(Handle<Object> obj) {
525 // See the similar code in runtime.js:ToBoolean.
526 if (obj->IsBoolean()) return obj;
527 bool result = true;
528 if (obj->IsString()) {
529 result = Handle<String>::cast(obj)->length() != 0;
530 } else if (obj->IsNull() || obj->IsUndefined()) {
531 result = false;
532 } else if (obj->IsNumber()) {
533 double value = obj->Number();
534 result = !((value == 0) || isnan(value));
535 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000536 return Handle<Object>(HEAP->ToBoolean(result));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537}
538
539
540Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) {
541 RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc);
542}
543
544
545Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) {
546 RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc);
547}
548
549
550Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) {
551 RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc);
552}
553
554
555Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) {
556 if (obj->IsJSObject()) return obj;
557 RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc);
558}
559
560
561Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) {
562 RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc);
563}
564
565
566Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) {
567 RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc);
568}
569
570
571Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) {
572 RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc);
573}
574
575
576Handle<Object> Execution::NewDate(double time, bool* exc) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000577 Handle<Object> time_obj = FACTORY->NewNumber(time);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc);
579}
580
581
582#undef RETURN_NATIVE_CALL
583
584
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000585Handle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern,
586 Handle<String> flags,
587 bool* exc) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000588 Handle<JSFunction> function = Handle<JSFunction>(
589 pattern->GetIsolate()->global_context()->regexp_function());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000590 Handle<Object> re_obj = RegExpImpl::CreateRegExpLiteral(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000591 function, pattern, flags, exc);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000592 if (*exc) return Handle<JSRegExp>();
593 return Handle<JSRegExp>::cast(re_obj);
594}
595
596
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000598 Isolate* isolate = string->GetIsolate();
599 Factory* factory = isolate->factory();
600
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 int int_index = static_cast<int>(index);
602 if (int_index < 0 || int_index >= string->length()) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000603 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 }
605
606 Handle<Object> char_at =
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000607 GetProperty(isolate->js_builtins_object(),
608 factory->char_at_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 if (!char_at->IsJSFunction()) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000610 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 }
612
613 bool caught_exception;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000614 Handle<Object> index_object = factory->NewNumberFromInt(int_index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 Object** index_arg[] = { index_object.location() };
616 Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
617 string,
618 ARRAY_SIZE(index_arg),
619 index_arg,
620 &caught_exception);
621 if (caught_exception) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000622 return factory->undefined_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623 }
624 return result;
625}
626
627
628Handle<JSFunction> Execution::InstantiateFunction(
629 Handle<FunctionTemplateInfo> data, bool* exc) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000630 Isolate* isolate = data->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 // Fast case: see if the function has already been instantiated
632 int serial_number = Smi::cast(data->serial_number())->value();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000633 Object* elm =
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000634 isolate->global_context()->function_cache()->
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000635 GetElementNoExceptionThrown(serial_number);
ager@chromium.org32912102009-01-16 10:38:43 +0000636 if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 // The function has not yet been instantiated in this context; do it.
638 Object** args[1] = { Handle<Object>::cast(data).location() };
639 Handle<Object> result =
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000640 Call(isolate->instantiate_fun(),
641 isolate->js_builtins_object(), 1, args, exc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 if (*exc) return Handle<JSFunction>::null();
643 return Handle<JSFunction>::cast(result);
644}
645
646
647Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
648 bool* exc) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000649 Isolate* isolate = data->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 if (data->property_list()->IsUndefined() &&
651 !data->constructor()->IsUndefined()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000652 // Initialization to make gcc happy.
653 Object* result = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000655 HandleScope scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 Handle<FunctionTemplateInfo> cons_template =
657 Handle<FunctionTemplateInfo>(
658 FunctionTemplateInfo::cast(data->constructor()));
659 Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
660 if (*exc) return Handle<JSObject>::null();
661 Handle<Object> value = New(cons, 0, NULL, exc);
662 if (*exc) return Handle<JSObject>::null();
663 result = *value;
664 }
665 ASSERT(!*exc);
666 return Handle<JSObject>(JSObject::cast(result));
667 } else {
668 Object** args[1] = { Handle<Object>::cast(data).location() };
669 Handle<Object> result =
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000670 Call(isolate->instantiate_fun(),
671 isolate->js_builtins_object(), 1, args, exc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 if (*exc) return Handle<JSObject>::null();
673 return Handle<JSObject>::cast(result);
674 }
675}
676
677
678void Execution::ConfigureInstance(Handle<Object> instance,
679 Handle<Object> instance_template,
680 bool* exc) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000681 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682 Object** args[2] = { instance.location(), instance_template.location() };
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000683 Execution::Call(isolate->configure_instance_fun(),
684 isolate->js_builtins_object(), 2, args, exc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685}
686
687
688Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
689 Handle<JSFunction> fun,
690 Handle<Object> pos,
691 Handle<Object> is_global) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000692 Isolate* isolate = fun->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 const int argc = 4;
694 Object** args[argc] = { recv.location(),
695 Handle<Object>::cast(fun).location(),
696 pos.location(),
697 is_global.location() };
698 bool caught_exception = false;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000699 Handle<Object> result =
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000700 TryCall(isolate->get_stack_trace_line_fun(),
701 isolate->js_builtins_object(), argc, args,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000702 &caught_exception);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000703 if (caught_exception || !result->IsString()) {
704 return isolate->factory()->empty_symbol();
705 }
706
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 return Handle<String>::cast(result);
708}
709
710
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000711static Object* RuntimePreempt() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000712 Isolate* isolate = Isolate::Current();
713
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000714 // Clear the preempt request flag.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000715 isolate->stack_guard()->Continue(PREEMPT);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000716
717 ContextSwitcher::PreemptionReceived();
718
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000719#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000720 if (isolate->debug()->InDebugger()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000721 // If currently in the debugger don't do any actual preemption but record
722 // that preemption occoured while in the debugger.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000723 isolate->debug()->PreemptionWhileInDebugger();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000724 } else {
725 // Perform preemption.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000726 v8::Unlocker unlocker(reinterpret_cast<v8::Isolate*>(isolate));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000727 Thread::YieldCPU();
728 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000729#else
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000730 { // NOLINT
731 // Perform preemption.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000732 v8::Unlocker unlocker(reinterpret_cast<v8::Isolate*>(isolate));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 Thread::YieldCPU();
734 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000735#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000736
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000737 return isolate->heap()->undefined_value();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000738}
739
740
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000741#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000742Object* Execution::DebugBreakHelper() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000743 Isolate* isolate = Isolate::Current();
744
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000745 // Just continue if breaks are disabled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000746 if (isolate->debug()->disable_break()) {
747 return isolate->heap()->undefined_value();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000748 }
749
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000750 // Ignore debug break during bootstrapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000751 if (isolate->bootstrapper()->IsActive()) {
752 return isolate->heap()->undefined_value();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000753 }
754
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000755 {
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000756 JavaScriptFrameIterator it(isolate);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000757 ASSERT(!it.done());
758 Object* fun = it.frame()->function();
759 if (fun && fun->IsJSFunction()) {
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000760 // Don't stop in builtin functions.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000761 if (JSFunction::cast(fun)->IsBuiltin()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000762 return isolate->heap()->undefined_value();
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000763 }
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000764 GlobalObject* global = JSFunction::cast(fun)->context()->global();
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000765 // Don't stop in debugger functions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000766 if (isolate->debug()->IsDebugGlobal(global)) {
767 return isolate->heap()->undefined_value();
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000768 }
769 }
770 }
771
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000772 // Collect the break state before clearing the flags.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000773 bool debug_command_only =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000774 isolate->stack_guard()->IsDebugCommand() &&
775 !isolate->stack_guard()->IsDebugBreak();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000776
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000777 // Clear the debug break request flag.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000778 isolate->stack_guard()->Continue(DEBUGBREAK);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000779
780 ProcessDebugMesssages(debug_command_only);
781
782 // Return to continue execution.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000783 return isolate->heap()->undefined_value();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000784}
785
786void Execution::ProcessDebugMesssages(bool debug_command_only) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000787 Isolate* isolate = Isolate::Current();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000788 // Clear the debug command request flag.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000789 isolate->stack_guard()->Continue(DEBUGCOMMAND);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000790
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000791 HandleScope scope(isolate);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000792 // Enter the debugger. Just continue if we fail to enter the debugger.
793 EnterDebugger debugger;
794 if (debugger.FailedToEnter()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000795 return;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000796 }
797
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000798 // Notify the debug event listeners. Indicate auto continue if the break was
799 // a debug command break.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000800 isolate->debugger()->OnDebugBreak(isolate->factory()->undefined_value(),
801 debug_command_only);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000802}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000803
804
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000805#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000806
lrn@chromium.org303ada72010-10-27 09:33:13 +0000807MaybeObject* Execution::HandleStackGuardInterrupt() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000808 Isolate* isolate = Isolate::Current();
809 StackGuard* stack_guard = isolate->stack_guard();
810 isolate->counters()->stack_interrupts()->Increment();
811 if (stack_guard->IsRuntimeProfilerTick()) {
812 isolate->counters()->runtime_profiler_ticks()->Increment();
813 stack_guard->Continue(RUNTIME_PROFILER_TICK);
814 isolate->runtime_profiler()->OptimizeNow();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000815 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000816#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000817 if (stack_guard->IsDebugBreak() || stack_guard->IsDebugCommand()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000818 DebugBreakHelper();
819 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000820#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000821 if (stack_guard->IsPreempted()) RuntimePreempt();
822 if (stack_guard->IsTerminateExecution()) {
823 stack_guard->Continue(TERMINATE);
824 return isolate->TerminateExecution();
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000825 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000826 if (stack_guard->IsInterrupted()) {
827 stack_guard->Continue(INTERRUPT);
828 return isolate->StackOverflow();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000829 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000830 return isolate->heap()->undefined_value();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000831}
832
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833} } // namespace v8::internal