blob: 68623247474fc9dbf85ea89a240dcf04ab07cd03 [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
Ben Murdochf87a2032010-10-22 12:50:53 +0100476Handle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern,
477 Handle<String> flags,
478 bool* exc) {
479 Handle<Object> re_obj = RegExpImpl::CreateRegExpLiteral(
480 Handle<JSFunction>(Top::global_context()->regexp_function()),
481 pattern,
482 flags,
483 exc);
484 if (*exc) return Handle<JSRegExp>();
485 return Handle<JSRegExp>::cast(re_obj);
486}
487
488
Steve Blocka7e24c12009-10-30 11:49:00 +0000489Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
490 int int_index = static_cast<int>(index);
491 if (int_index < 0 || int_index >= string->length()) {
492 return Factory::undefined_value();
493 }
494
495 Handle<Object> char_at =
496 GetProperty(Top::builtins(), Factory::char_at_symbol());
497 if (!char_at->IsJSFunction()) {
498 return Factory::undefined_value();
499 }
500
501 bool caught_exception;
502 Handle<Object> index_object = Factory::NewNumberFromInt(int_index);
503 Object** index_arg[] = { index_object.location() };
504 Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
505 string,
506 ARRAY_SIZE(index_arg),
507 index_arg,
508 &caught_exception);
509 if (caught_exception) {
510 return Factory::undefined_value();
511 }
512 return result;
513}
514
515
516Handle<JSFunction> Execution::InstantiateFunction(
517 Handle<FunctionTemplateInfo> data, bool* exc) {
518 // Fast case: see if the function has already been instantiated
519 int serial_number = Smi::cast(data->serial_number())->value();
520 Object* elm =
521 Top::global_context()->function_cache()->GetElement(serial_number);
522 if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
523 // The function has not yet been instantiated in this context; do it.
524 Object** args[1] = { Handle<Object>::cast(data).location() };
525 Handle<Object> result =
526 Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
527 if (*exc) return Handle<JSFunction>::null();
528 return Handle<JSFunction>::cast(result);
529}
530
531
532Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
533 bool* exc) {
534 if (data->property_list()->IsUndefined() &&
535 !data->constructor()->IsUndefined()) {
536 // Initialization to make gcc happy.
537 Object* result = NULL;
538 {
539 HandleScope scope;
540 Handle<FunctionTemplateInfo> cons_template =
541 Handle<FunctionTemplateInfo>(
542 FunctionTemplateInfo::cast(data->constructor()));
543 Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
544 if (*exc) return Handle<JSObject>::null();
545 Handle<Object> value = New(cons, 0, NULL, exc);
546 if (*exc) return Handle<JSObject>::null();
547 result = *value;
548 }
549 ASSERT(!*exc);
550 return Handle<JSObject>(JSObject::cast(result));
551 } else {
552 Object** args[1] = { Handle<Object>::cast(data).location() };
553 Handle<Object> result =
554 Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
555 if (*exc) return Handle<JSObject>::null();
556 return Handle<JSObject>::cast(result);
557 }
558}
559
560
561void Execution::ConfigureInstance(Handle<Object> instance,
562 Handle<Object> instance_template,
563 bool* exc) {
564 Object** args[2] = { instance.location(), instance_template.location() };
565 Execution::Call(Top::configure_instance_fun(), Top::builtins(), 2, args, exc);
566}
567
568
569Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
570 Handle<JSFunction> fun,
571 Handle<Object> pos,
572 Handle<Object> is_global) {
573 const int argc = 4;
574 Object** args[argc] = { recv.location(),
575 Handle<Object>::cast(fun).location(),
576 pos.location(),
577 is_global.location() };
578 bool caught_exception = false;
579 Handle<Object> result = TryCall(Top::get_stack_trace_line_fun(),
580 Top::builtins(), argc, args,
581 &caught_exception);
582 if (caught_exception || !result->IsString()) return Factory::empty_symbol();
583 return Handle<String>::cast(result);
584}
585
586
587static Object* RuntimePreempt() {
588 // Clear the preempt request flag.
589 StackGuard::Continue(PREEMPT);
590
591 ContextSwitcher::PreemptionReceived();
592
593#ifdef ENABLE_DEBUGGER_SUPPORT
594 if (Debug::InDebugger()) {
595 // If currently in the debugger don't do any actual preemption but record
596 // that preemption occoured while in the debugger.
597 Debug::PreemptionWhileInDebugger();
598 } else {
599 // Perform preemption.
600 v8::Unlocker unlocker;
601 Thread::YieldCPU();
602 }
603#else
604 // Perform preemption.
605 v8::Unlocker unlocker;
606 Thread::YieldCPU();
607#endif
608
609 return Heap::undefined_value();
610}
611
612
613#ifdef ENABLE_DEBUGGER_SUPPORT
614Object* Execution::DebugBreakHelper() {
615 // Just continue if breaks are disabled.
616 if (Debug::disable_break()) {
617 return Heap::undefined_value();
618 }
619
Leon Clarkee46be812010-01-19 14:06:41 +0000620 // Ignore debug break during bootstrapping.
621 if (Bootstrapper::IsActive()) {
622 return Heap::undefined_value();
623 }
624
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 {
626 JavaScriptFrameIterator it;
627 ASSERT(!it.done());
628 Object* fun = it.frame()->function();
629 if (fun && fun->IsJSFunction()) {
630 // Don't stop in builtin functions.
631 if (JSFunction::cast(fun)->IsBuiltin()) {
632 return Heap::undefined_value();
633 }
634 GlobalObject* global = JSFunction::cast(fun)->context()->global();
635 // Don't stop in debugger functions.
636 if (Debug::IsDebugGlobal(global)) {
637 return Heap::undefined_value();
638 }
639 }
640 }
641
642 // Collect the break state before clearing the flags.
643 bool debug_command_only =
644 StackGuard::IsDebugCommand() && !StackGuard::IsDebugBreak();
645
Leon Clarkee46be812010-01-19 14:06:41 +0000646 // Clear the debug break request flag.
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 StackGuard::Continue(DEBUGBREAK);
Leon Clarkee46be812010-01-19 14:06:41 +0000648
649 ProcessDebugMesssages(debug_command_only);
650
651 // Return to continue execution.
652 return Heap::undefined_value();
653}
654
655void Execution::ProcessDebugMesssages(bool debug_command_only) {
656 // Clear the debug command request flag.
Steve Blocka7e24c12009-10-30 11:49:00 +0000657 StackGuard::Continue(DEBUGCOMMAND);
658
659 HandleScope scope;
660 // Enter the debugger. Just continue if we fail to enter the debugger.
661 EnterDebugger debugger;
662 if (debugger.FailedToEnter()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000663 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000664 }
665
666 // Notify the debug event listeners. Indicate auto continue if the break was
667 // a debug command break.
668 Debugger::OnDebugBreak(Factory::undefined_value(), debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000669}
Leon Clarkee46be812010-01-19 14:06:41 +0000670
671
Steve Blocka7e24c12009-10-30 11:49:00 +0000672#endif
673
674Object* Execution::HandleStackGuardInterrupt() {
675#ifdef ENABLE_DEBUGGER_SUPPORT
676 if (StackGuard::IsDebugBreak() || StackGuard::IsDebugCommand()) {
677 DebugBreakHelper();
678 }
679#endif
680 if (StackGuard::IsPreempted()) RuntimePreempt();
681 if (StackGuard::IsTerminateExecution()) {
682 StackGuard::Continue(TERMINATE);
683 return Top::TerminateExecution();
684 }
685 if (StackGuard::IsInterrupted()) {
686 // interrupt
687 StackGuard::Continue(INTERRUPT);
688 return Top::StackOverflow();
689 }
690 return Heap::undefined_value();
691}
692
693// --- G C E x t e n s i o n ---
694
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100695const char* const GCExtension::kSource = "native function gc();";
Steve Blocka7e24c12009-10-30 11:49:00 +0000696
697
698v8::Handle<v8::FunctionTemplate> GCExtension::GetNativeFunction(
699 v8::Handle<v8::String> str) {
700 return v8::FunctionTemplate::New(GCExtension::GC);
701}
702
703
704v8::Handle<v8::Value> GCExtension::GC(const v8::Arguments& args) {
705 // All allocation spaces other than NEW_SPACE have the same effect.
706 Heap::CollectAllGarbage(false);
707 return v8::Undefined();
708}
709
710
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100711static GCExtension gc_extension;
712static v8::DeclareExtension gc_extension_declaration(&gc_extension);
713
714
715// --- E x t e r n a l i z e S t r i n g E x t e n s i o n ---
716
717
718template <typename Char, typename Base>
719class SimpleStringResource : public Base {
720 public:
721 // Takes ownership of |data|.
722 SimpleStringResource(Char* data, size_t length)
723 : data_(data),
724 length_(length) {}
725
Steve Block791712a2010-08-27 10:21:07 +0100726 virtual ~SimpleStringResource() { delete[] data_; }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100727
728 virtual const Char* data() const { return data_; }
729
730 virtual size_t length() const { return length_; }
731
732 private:
733 Char* const data_;
734 const size_t length_;
735};
736
737
738typedef SimpleStringResource<char, v8::String::ExternalAsciiStringResource>
739 SimpleAsciiStringResource;
740typedef SimpleStringResource<uc16, v8::String::ExternalStringResource>
741 SimpleTwoByteStringResource;
742
743
744const char* const ExternalizeStringExtension::kSource =
745 "native function externalizeString();"
746 "native function isAsciiString();";
747
748
749v8::Handle<v8::FunctionTemplate> ExternalizeStringExtension::GetNativeFunction(
750 v8::Handle<v8::String> str) {
751 if (strcmp(*v8::String::AsciiValue(str), "externalizeString") == 0) {
752 return v8::FunctionTemplate::New(ExternalizeStringExtension::Externalize);
753 } else {
754 ASSERT(strcmp(*v8::String::AsciiValue(str), "isAsciiString") == 0);
755 return v8::FunctionTemplate::New(ExternalizeStringExtension::IsAscii);
756 }
757}
758
759
760v8::Handle<v8::Value> ExternalizeStringExtension::Externalize(
761 const v8::Arguments& args) {
762 if (args.Length() < 1 || !args[0]->IsString()) {
763 return v8::ThrowException(v8::String::New(
764 "First parameter to externalizeString() must be a string."));
765 }
766 bool force_two_byte = false;
767 if (args.Length() >= 2) {
768 if (args[1]->IsBoolean()) {
769 force_two_byte = args[1]->BooleanValue();
770 } else {
771 return v8::ThrowException(v8::String::New(
772 "Second parameter to externalizeString() must be a boolean."));
773 }
774 }
775 bool result = false;
776 Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>());
777 if (string->IsExternalString()) {
778 return v8::ThrowException(v8::String::New(
779 "externalizeString() can't externalize twice."));
780 }
781 if (string->IsAsciiRepresentation() && !force_two_byte) {
782 char* data = new char[string->length()];
783 String::WriteToFlat(*string, data, 0, string->length());
784 SimpleAsciiStringResource* resource = new SimpleAsciiStringResource(
785 data, string->length());
786 result = string->MakeExternal(resource);
787 if (result && !string->IsSymbol()) {
788 i::ExternalStringTable::AddString(*string);
789 }
790 } else {
791 uc16* data = new uc16[string->length()];
792 String::WriteToFlat(*string, data, 0, string->length());
793 SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource(
794 data, string->length());
795 result = string->MakeExternal(resource);
796 if (result && !string->IsSymbol()) {
797 i::ExternalStringTable::AddString(*string);
798 }
799 }
800 if (!result) {
801 return v8::ThrowException(v8::String::New("externalizeString() failed."));
802 }
803 return v8::Undefined();
804}
805
806
807v8::Handle<v8::Value> ExternalizeStringExtension::IsAscii(
808 const v8::Arguments& args) {
809 if (args.Length() != 1 || !args[0]->IsString()) {
810 return v8::ThrowException(v8::String::New(
811 "isAsciiString() requires a single string argument."));
812 }
813 return Utils::OpenHandle(*args[0].As<v8::String>())->IsAsciiRepresentation() ?
814 v8::True() : v8::False();
815}
816
817
818static ExternalizeStringExtension externalize_extension;
819static v8::DeclareExtension externalize_extension_declaration(
820 &externalize_extension);
Steve Blocka7e24c12009-10-30 11:49:00 +0000821
822} } // namespace v8::internal