blob: 006d358eaaa5c84d7323a15bb85cf88ec03ec4bc [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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"
Leon Clarkee46be812010-01-19 14:06:41 +000033#include "bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "codegen-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "debug.h"
Steve Blockd0582a62009-12-15 09:54:21 +000036#include "simulator.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037#include "v8threads.h"
38
39namespace v8 {
40namespace internal {
41
42
43static Handle<Object> Invoke(bool construct,
44 Handle<JSFunction> func,
45 Handle<Object> receiver,
46 int argc,
47 Object*** args,
48 bool* has_pending_exception) {
Steve Blocka7e24c12009-10-30 11:49:00 +000049 // Entering JavaScript.
50 VMState state(JS);
51
52 // Placeholder for return value.
53 Object* value = reinterpret_cast<Object*>(kZapValue);
54
55 typedef Object* (*JSEntryFunction)(
56 byte* entry,
57 Object* function,
58 Object* receiver,
59 int argc,
60 Object*** args);
61
62 Handle<Code> code;
63 if (construct) {
64 JSConstructEntryStub stub;
65 code = stub.GetCode();
66 } else {
67 JSEntryStub stub;
68 code = stub.GetCode();
69 }
70
71 // Convert calls on global objects to be calls on the global
72 // receiver instead to avoid having a 'this' pointer which refers
73 // directly to a global object.
74 if (receiver->IsGlobalObject()) {
75 Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver);
76 receiver = Handle<JSObject>(global->global_receiver());
77 }
78
Leon Clarkee46be812010-01-19 14:06:41 +000079 // Make sure that the global object of the context we're about to
80 // make the current one is indeed a global object.
81 ASSERT(func->context()->global()->IsGlobalObject());
82
Steve Blocka7e24c12009-10-30 11:49:00 +000083 {
84 // Save and restore context around invocation and block the
85 // allocation of handles without explicit handle scopes.
86 SaveContext save;
87 NoHandleAllocation na;
88 JSEntryFunction entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
89
90 // Call the function through the right JS entry stub.
Andrei Popescu402d9372010-02-26 13:31:12 +000091 byte* entry_address = func->code()->entry();
Steve Block3ce2e202009-11-05 08:53:23 +000092 JSFunction* function = *func;
93 Object* receiver_pointer = *receiver;
94 value = CALL_GENERATED_CODE(entry, entry_address, function,
95 receiver_pointer, argc, args);
Steve Blocka7e24c12009-10-30 11:49:00 +000096 }
97
98#ifdef DEBUG
99 value->Verify();
100#endif
101
102 // Update the pending exception flag and return the value.
103 *has_pending_exception = value->IsException();
104 ASSERT(*has_pending_exception == Top::has_pending_exception());
105 if (*has_pending_exception) {
106 Top::ReportPendingMessages();
107 return Handle<Object>();
108 } else {
109 Top::clear_pending_message();
110 }
111
112 return Handle<Object>(value);
113}
114
115
116Handle<Object> Execution::Call(Handle<JSFunction> func,
117 Handle<Object> receiver,
118 int argc,
119 Object*** args,
120 bool* pending_exception) {
121 return Invoke(false, func, receiver, argc, args, pending_exception);
122}
123
124
125Handle<Object> Execution::New(Handle<JSFunction> func, int argc,
126 Object*** args, bool* pending_exception) {
127 return Invoke(true, func, Top::global(), argc, args, pending_exception);
128}
129
130
131Handle<Object> Execution::TryCall(Handle<JSFunction> func,
132 Handle<Object> receiver,
133 int argc,
134 Object*** args,
135 bool* caught_exception) {
136 // Enter a try-block while executing the JavaScript code. To avoid
137 // duplicate error printing it must be non-verbose. Also, to avoid
138 // creating message objects during stack overflow we shouldn't
139 // capture messages.
140 v8::TryCatch catcher;
141 catcher.SetVerbose(false);
142 catcher.SetCaptureMessage(false);
143
144 Handle<Object> result = Invoke(false, func, receiver, argc, args,
145 caught_exception);
146
147 if (*caught_exception) {
148 ASSERT(catcher.HasCaught());
149 ASSERT(Top::has_pending_exception());
150 ASSERT(Top::external_caught_exception());
151 if (Top::pending_exception() == Heap::termination_exception()) {
152 result = Factory::termination_exception();
153 } else {
154 result = v8::Utils::OpenHandle(*catcher.Exception());
155 }
156 Top::OptionalRescheduleException(true);
157 }
158
159 ASSERT(!Top::has_pending_exception());
160 ASSERT(!Top::external_caught_exception());
161 return result;
162}
163
164
165Handle<Object> Execution::GetFunctionDelegate(Handle<Object> object) {
166 ASSERT(!object->IsJSFunction());
167
168 // If you return a function from here, it will be called when an
169 // attempt is made to call the given object as a function.
170
171 // Regular expressions can be called as functions in both Firefox
172 // and Safari so we allow it too.
173 if (object->IsJSRegExp()) {
174 Handle<String> exec = Factory::exec_symbol();
175 return Handle<Object>(object->GetProperty(*exec));
176 }
177
178 // Objects created through the API can have an instance-call handler
179 // that should be used when calling the object as a function.
180 if (object->IsHeapObject() &&
181 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
182 return Handle<JSFunction>(
183 Top::global_context()->call_as_function_delegate());
184 }
185
186 return Factory::undefined_value();
187}
188
189
190Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) {
191 ASSERT(!object->IsJSFunction());
192
193 // If you return a function from here, it will be called when an
194 // attempt is made to call the given object as a constructor.
195
196 // Objects created through the API can have an instance-call handler
197 // that should be used when calling the object as a function.
198 if (object->IsHeapObject() &&
199 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
200 return Handle<JSFunction>(
201 Top::global_context()->call_as_constructor_delegate());
202 }
203
204 return Factory::undefined_value();
205}
206
207
208// Static state for stack guards.
209StackGuard::ThreadLocal StackGuard::thread_local_;
210
211
212bool StackGuard::IsStackOverflow() {
213 ExecutionAccess access;
214 return (thread_local_.jslimit_ != kInterruptLimit &&
215 thread_local_.climit_ != kInterruptLimit);
216}
217
218
219void StackGuard::EnableInterrupts() {
220 ExecutionAccess access;
Steve Block6ded16b2010-05-10 14:33:55 +0100221 if (has_pending_interrupts(access)) {
222 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 }
224}
225
226
227void StackGuard::SetStackLimit(uintptr_t limit) {
228 ExecutionAccess access;
229 // If the current limits are special (eg due to a pending interrupt) then
230 // leave them alone.
231 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000232 if (thread_local_.jslimit_ == thread_local_.real_jslimit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 thread_local_.jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 }
Steve Blockd0582a62009-12-15 09:54:21 +0000235 if (thread_local_.climit_ == thread_local_.real_climit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 thread_local_.climit_ = limit;
237 }
Steve Blockd0582a62009-12-15 09:54:21 +0000238 thread_local_.real_climit_ = limit;
239 thread_local_.real_jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000240}
241
242
243void StackGuard::DisableInterrupts() {
244 ExecutionAccess access;
245 reset_limits(access);
246}
247
248
Steve Blocka7e24c12009-10-30 11:49:00 +0000249bool StackGuard::IsInterrupted() {
250 ExecutionAccess access;
251 return thread_local_.interrupt_flags_ & INTERRUPT;
252}
253
254
255void StackGuard::Interrupt() {
256 ExecutionAccess access;
257 thread_local_.interrupt_flags_ |= INTERRUPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100258 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259}
260
261
262bool StackGuard::IsPreempted() {
263 ExecutionAccess access;
264 return thread_local_.interrupt_flags_ & PREEMPT;
265}
266
267
268void StackGuard::Preempt() {
269 ExecutionAccess access;
270 thread_local_.interrupt_flags_ |= PREEMPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100271 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000272}
273
274
275bool StackGuard::IsTerminateExecution() {
276 ExecutionAccess access;
277 return thread_local_.interrupt_flags_ & TERMINATE;
278}
279
280
281void StackGuard::TerminateExecution() {
282 ExecutionAccess access;
283 thread_local_.interrupt_flags_ |= TERMINATE;
Steve Block6ded16b2010-05-10 14:33:55 +0100284 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000285}
286
287
288#ifdef ENABLE_DEBUGGER_SUPPORT
289bool StackGuard::IsDebugBreak() {
290 ExecutionAccess access;
291 return thread_local_.interrupt_flags_ & DEBUGBREAK;
292}
293
294
295void StackGuard::DebugBreak() {
296 ExecutionAccess access;
297 thread_local_.interrupt_flags_ |= DEBUGBREAK;
Steve Block6ded16b2010-05-10 14:33:55 +0100298 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000299}
300
301
302bool StackGuard::IsDebugCommand() {
303 ExecutionAccess access;
304 return thread_local_.interrupt_flags_ & DEBUGCOMMAND;
305}
306
307
308void StackGuard::DebugCommand() {
309 if (FLAG_debugger_auto_break) {
310 ExecutionAccess access;
311 thread_local_.interrupt_flags_ |= DEBUGCOMMAND;
Steve Block6ded16b2010-05-10 14:33:55 +0100312 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 }
314}
315#endif
316
317void StackGuard::Continue(InterruptFlag after_what) {
318 ExecutionAccess access;
319 thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what);
Steve Block6ded16b2010-05-10 14:33:55 +0100320 if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 reset_limits(access);
322 }
323}
324
325
326int StackGuard::ArchiveSpacePerThread() {
327 return sizeof(ThreadLocal);
328}
329
330
331char* StackGuard::ArchiveStackGuard(char* to) {
332 ExecutionAccess access;
333 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
334 ThreadLocal blank;
335 thread_local_ = blank;
336 return to + sizeof(ThreadLocal);
337}
338
339
340char* StackGuard::RestoreStackGuard(char* from) {
341 ExecutionAccess access;
342 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
Steve Blockd0582a62009-12-15 09:54:21 +0000343 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 return from + sizeof(ThreadLocal);
345}
346
347
348static internal::Thread::LocalStorageKey stack_limit_key =
349 internal::Thread::CreateThreadLocalKey();
350
351
352void StackGuard::FreeThreadResources() {
353 Thread::SetThreadLocal(
354 stack_limit_key,
Steve Blockd0582a62009-12-15 09:54:21 +0000355 reinterpret_cast<void*>(thread_local_.real_climit_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000356}
357
358
359void StackGuard::ThreadLocal::Clear() {
Steve Blockd0582a62009-12-15 09:54:21 +0000360 real_jslimit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 jslimit_ = kIllegalLimit;
Steve Blockd0582a62009-12-15 09:54:21 +0000362 real_climit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 climit_ = kIllegalLimit;
364 nesting_ = 0;
365 postpone_interrupts_nesting_ = 0;
366 interrupt_flags_ = 0;
Steve Blockd0582a62009-12-15 09:54:21 +0000367 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000368}
369
370
371void StackGuard::ThreadLocal::Initialize() {
Steve Blockd0582a62009-12-15 09:54:21 +0000372 if (real_climit_ == kIllegalLimit) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 // Takes the address of the limit variable in order to find out where
374 // the top of stack is right now.
Steve Block3ce2e202009-11-05 08:53:23 +0000375 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize;
376 ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize);
Steve Blockd0582a62009-12-15 09:54:21 +0000377 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000379 real_climit_ = limit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 climit_ = limit;
Steve Blockd0582a62009-12-15 09:54:21 +0000381 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 }
383 nesting_ = 0;
384 postpone_interrupts_nesting_ = 0;
385 interrupt_flags_ = 0;
386}
387
388
389void StackGuard::ClearThread(const ExecutionAccess& lock) {
390 thread_local_.Clear();
391}
392
393
394void StackGuard::InitThread(const ExecutionAccess& lock) {
395 thread_local_.Initialize();
396 void* stored_limit = Thread::GetThreadLocal(stack_limit_key);
397 // You should hold the ExecutionAccess lock when you call this.
398 if (stored_limit != NULL) {
399 StackGuard::SetStackLimit(reinterpret_cast<intptr_t>(stored_limit));
400 }
401}
402
403
404// --- C a l l s t o n a t i v e s ---
405
406#define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \
407 do { \
408 Object** args[argc] = argv; \
409 ASSERT(has_pending_exception != NULL); \
410 return Call(Top::name##_fun(), Top::builtins(), argc, args, \
411 has_pending_exception); \
412 } while (false)
413
414
415Handle<Object> Execution::ToBoolean(Handle<Object> obj) {
416 // See the similar code in runtime.js:ToBoolean.
417 if (obj->IsBoolean()) return obj;
418 bool result = true;
419 if (obj->IsString()) {
420 result = Handle<String>::cast(obj)->length() != 0;
421 } else if (obj->IsNull() || obj->IsUndefined()) {
422 result = false;
423 } else if (obj->IsNumber()) {
424 double value = obj->Number();
425 result = !((value == 0) || isnan(value));
426 }
427 return Handle<Object>(Heap::ToBoolean(result));
428}
429
430
431Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) {
432 RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc);
433}
434
435
436Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) {
437 RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc);
438}
439
440
441Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) {
442 RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc);
443}
444
445
446Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) {
447 if (obj->IsJSObject()) return obj;
448 RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc);
449}
450
451
452Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) {
453 RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc);
454}
455
456
457Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) {
458 RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc);
459}
460
461
462Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) {
463 RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc);
464}
465
466
467Handle<Object> Execution::NewDate(double time, bool* exc) {
468 Handle<Object> time_obj = Factory::NewNumber(time);
469 RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc);
470}
471
472
473#undef RETURN_NATIVE_CALL
474
475
476Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
477 int int_index = static_cast<int>(index);
478 if (int_index < 0 || int_index >= string->length()) {
479 return Factory::undefined_value();
480 }
481
482 Handle<Object> char_at =
483 GetProperty(Top::builtins(), Factory::char_at_symbol());
484 if (!char_at->IsJSFunction()) {
485 return Factory::undefined_value();
486 }
487
488 bool caught_exception;
489 Handle<Object> index_object = Factory::NewNumberFromInt(int_index);
490 Object** index_arg[] = { index_object.location() };
491 Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
492 string,
493 ARRAY_SIZE(index_arg),
494 index_arg,
495 &caught_exception);
496 if (caught_exception) {
497 return Factory::undefined_value();
498 }
499 return result;
500}
501
502
503Handle<JSFunction> Execution::InstantiateFunction(
504 Handle<FunctionTemplateInfo> data, bool* exc) {
505 // Fast case: see if the function has already been instantiated
506 int serial_number = Smi::cast(data->serial_number())->value();
507 Object* elm =
508 Top::global_context()->function_cache()->GetElement(serial_number);
509 if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
510 // The function has not yet been instantiated in this context; do it.
511 Object** args[1] = { Handle<Object>::cast(data).location() };
512 Handle<Object> result =
513 Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
514 if (*exc) return Handle<JSFunction>::null();
515 return Handle<JSFunction>::cast(result);
516}
517
518
519Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
520 bool* exc) {
521 if (data->property_list()->IsUndefined() &&
522 !data->constructor()->IsUndefined()) {
523 // Initialization to make gcc happy.
524 Object* result = NULL;
525 {
526 HandleScope scope;
527 Handle<FunctionTemplateInfo> cons_template =
528 Handle<FunctionTemplateInfo>(
529 FunctionTemplateInfo::cast(data->constructor()));
530 Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
531 if (*exc) return Handle<JSObject>::null();
532 Handle<Object> value = New(cons, 0, NULL, exc);
533 if (*exc) return Handle<JSObject>::null();
534 result = *value;
535 }
536 ASSERT(!*exc);
537 return Handle<JSObject>(JSObject::cast(result));
538 } else {
539 Object** args[1] = { Handle<Object>::cast(data).location() };
540 Handle<Object> result =
541 Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
542 if (*exc) return Handle<JSObject>::null();
543 return Handle<JSObject>::cast(result);
544 }
545}
546
547
548void Execution::ConfigureInstance(Handle<Object> instance,
549 Handle<Object> instance_template,
550 bool* exc) {
551 Object** args[2] = { instance.location(), instance_template.location() };
552 Execution::Call(Top::configure_instance_fun(), Top::builtins(), 2, args, exc);
553}
554
555
556Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
557 Handle<JSFunction> fun,
558 Handle<Object> pos,
559 Handle<Object> is_global) {
560 const int argc = 4;
561 Object** args[argc] = { recv.location(),
562 Handle<Object>::cast(fun).location(),
563 pos.location(),
564 is_global.location() };
565 bool caught_exception = false;
566 Handle<Object> result = TryCall(Top::get_stack_trace_line_fun(),
567 Top::builtins(), argc, args,
568 &caught_exception);
569 if (caught_exception || !result->IsString()) return Factory::empty_symbol();
570 return Handle<String>::cast(result);
571}
572
573
574static Object* RuntimePreempt() {
575 // Clear the preempt request flag.
576 StackGuard::Continue(PREEMPT);
577
578 ContextSwitcher::PreemptionReceived();
579
580#ifdef ENABLE_DEBUGGER_SUPPORT
581 if (Debug::InDebugger()) {
582 // If currently in the debugger don't do any actual preemption but record
583 // that preemption occoured while in the debugger.
584 Debug::PreemptionWhileInDebugger();
585 } else {
586 // Perform preemption.
587 v8::Unlocker unlocker;
588 Thread::YieldCPU();
589 }
590#else
591 // Perform preemption.
592 v8::Unlocker unlocker;
593 Thread::YieldCPU();
594#endif
595
596 return Heap::undefined_value();
597}
598
599
600#ifdef ENABLE_DEBUGGER_SUPPORT
601Object* Execution::DebugBreakHelper() {
602 // Just continue if breaks are disabled.
603 if (Debug::disable_break()) {
604 return Heap::undefined_value();
605 }
606
Leon Clarkee46be812010-01-19 14:06:41 +0000607 // Ignore debug break during bootstrapping.
608 if (Bootstrapper::IsActive()) {
609 return Heap::undefined_value();
610 }
611
Steve Blocka7e24c12009-10-30 11:49:00 +0000612 {
613 JavaScriptFrameIterator it;
614 ASSERT(!it.done());
615 Object* fun = it.frame()->function();
616 if (fun && fun->IsJSFunction()) {
617 // Don't stop in builtin functions.
618 if (JSFunction::cast(fun)->IsBuiltin()) {
619 return Heap::undefined_value();
620 }
621 GlobalObject* global = JSFunction::cast(fun)->context()->global();
622 // Don't stop in debugger functions.
623 if (Debug::IsDebugGlobal(global)) {
624 return Heap::undefined_value();
625 }
626 }
627 }
628
629 // Collect the break state before clearing the flags.
630 bool debug_command_only =
631 StackGuard::IsDebugCommand() && !StackGuard::IsDebugBreak();
632
Leon Clarkee46be812010-01-19 14:06:41 +0000633 // Clear the debug break request flag.
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 StackGuard::Continue(DEBUGBREAK);
Leon Clarkee46be812010-01-19 14:06:41 +0000635
636 ProcessDebugMesssages(debug_command_only);
637
638 // Return to continue execution.
639 return Heap::undefined_value();
640}
641
642void Execution::ProcessDebugMesssages(bool debug_command_only) {
643 // Clear the debug command request flag.
Steve Blocka7e24c12009-10-30 11:49:00 +0000644 StackGuard::Continue(DEBUGCOMMAND);
645
646 HandleScope scope;
647 // Enter the debugger. Just continue if we fail to enter the debugger.
648 EnterDebugger debugger;
649 if (debugger.FailedToEnter()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000650 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 }
652
653 // Notify the debug event listeners. Indicate auto continue if the break was
654 // a debug command break.
655 Debugger::OnDebugBreak(Factory::undefined_value(), debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000656}
Leon Clarkee46be812010-01-19 14:06:41 +0000657
658
Steve Blocka7e24c12009-10-30 11:49:00 +0000659#endif
660
661Object* Execution::HandleStackGuardInterrupt() {
662#ifdef ENABLE_DEBUGGER_SUPPORT
663 if (StackGuard::IsDebugBreak() || StackGuard::IsDebugCommand()) {
664 DebugBreakHelper();
665 }
666#endif
667 if (StackGuard::IsPreempted()) RuntimePreempt();
668 if (StackGuard::IsTerminateExecution()) {
669 StackGuard::Continue(TERMINATE);
670 return Top::TerminateExecution();
671 }
672 if (StackGuard::IsInterrupted()) {
673 // interrupt
674 StackGuard::Continue(INTERRUPT);
675 return Top::StackOverflow();
676 }
677 return Heap::undefined_value();
678}
679
680// --- G C E x t e n s i o n ---
681
682const char* GCExtension::kSource = "native function gc();";
683
684
685v8::Handle<v8::FunctionTemplate> GCExtension::GetNativeFunction(
686 v8::Handle<v8::String> str) {
687 return v8::FunctionTemplate::New(GCExtension::GC);
688}
689
690
691v8::Handle<v8::Value> GCExtension::GC(const v8::Arguments& args) {
692 // All allocation spaces other than NEW_SPACE have the same effect.
693 Heap::CollectAllGarbage(false);
694 return v8::Undefined();
695}
696
697
698static GCExtension kGCExtension;
699v8::DeclareExtension kGCExtensionDeclaration(&kGCExtension);
700
701} } // namespace v8::internal