blob: de8f0a4661544e5fb6ac9aa7c9467ea4afb4a459 [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"
Ben Murdochb0fe1622011-05-05 13:52:32 +010036#include "runtime-profiler.h"
Steve Blockd0582a62009-12-15 09:54:21 +000037#include "simulator.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038#include "v8threads.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010039#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41namespace v8 {
42namespace internal {
43
44
45static Handle<Object> Invoke(bool construct,
46 Handle<JSFunction> func,
47 Handle<Object> receiver,
48 int argc,
49 Object*** args,
50 bool* has_pending_exception) {
Steve Blocka7e24c12009-10-30 11:49:00 +000051 // Entering JavaScript.
52 VMState state(JS);
53
54 // Placeholder for return value.
John Reck59135872010-11-02 12:39:01 -070055 MaybeObject* value = reinterpret_cast<Object*>(kZapValue);
Steve Blocka7e24c12009-10-30 11:49:00 +000056
57 typedef Object* (*JSEntryFunction)(
58 byte* entry,
59 Object* function,
60 Object* receiver,
61 int argc,
62 Object*** args);
63
64 Handle<Code> code;
65 if (construct) {
66 JSConstructEntryStub stub;
67 code = stub.GetCode();
68 } else {
69 JSEntryStub stub;
70 code = stub.GetCode();
71 }
72
73 // Convert calls on global objects to be calls on the global
74 // receiver instead to avoid having a 'this' pointer which refers
75 // directly to a global object.
76 if (receiver->IsGlobalObject()) {
77 Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver);
78 receiver = Handle<JSObject>(global->global_receiver());
79 }
80
Leon Clarkee46be812010-01-19 14:06:41 +000081 // Make sure that the global object of the context we're about to
82 // make the current one is indeed a global object.
83 ASSERT(func->context()->global()->IsGlobalObject());
84
Steve Blocka7e24c12009-10-30 11:49:00 +000085 {
86 // Save and restore context around invocation and block the
87 // allocation of handles without explicit handle scopes.
88 SaveContext save;
89 NoHandleAllocation na;
90 JSEntryFunction entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
91
92 // Call the function through the right JS entry stub.
Andrei Popescu402d9372010-02-26 13:31:12 +000093 byte* entry_address = func->code()->entry();
Steve Block3ce2e202009-11-05 08:53:23 +000094 JSFunction* function = *func;
95 Object* receiver_pointer = *receiver;
96 value = CALL_GENERATED_CODE(entry, entry_address, function,
97 receiver_pointer, argc, args);
Steve Blocka7e24c12009-10-30 11:49:00 +000098 }
99
100#ifdef DEBUG
101 value->Verify();
102#endif
103
104 // Update the pending exception flag and return the value.
105 *has_pending_exception = value->IsException();
106 ASSERT(*has_pending_exception == Top::has_pending_exception());
107 if (*has_pending_exception) {
108 Top::ReportPendingMessages();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100109 if (Top::pending_exception() == Failure::OutOfMemoryException()) {
110 if (!HandleScopeImplementer::instance()->ignore_out_of_memory()) {
111 V8::FatalProcessOutOfMemory("JS", true);
112 }
113 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 return Handle<Object>();
115 } else {
116 Top::clear_pending_message();
117 }
118
John Reck59135872010-11-02 12:39:01 -0700119 return Handle<Object>(value->ToObjectUnchecked());
Steve Blocka7e24c12009-10-30 11:49:00 +0000120}
121
122
123Handle<Object> Execution::Call(Handle<JSFunction> func,
124 Handle<Object> receiver,
125 int argc,
126 Object*** args,
127 bool* pending_exception) {
128 return Invoke(false, func, receiver, argc, args, pending_exception);
129}
130
131
132Handle<Object> Execution::New(Handle<JSFunction> func, int argc,
133 Object*** args, bool* pending_exception) {
134 return Invoke(true, func, Top::global(), argc, args, pending_exception);
135}
136
137
138Handle<Object> Execution::TryCall(Handle<JSFunction> func,
139 Handle<Object> receiver,
140 int argc,
141 Object*** args,
142 bool* caught_exception) {
143 // Enter a try-block while executing the JavaScript code. To avoid
144 // duplicate error printing it must be non-verbose. Also, to avoid
145 // creating message objects during stack overflow we shouldn't
146 // capture messages.
147 v8::TryCatch catcher;
148 catcher.SetVerbose(false);
149 catcher.SetCaptureMessage(false);
150
151 Handle<Object> result = Invoke(false, func, receiver, argc, args,
152 caught_exception);
153
154 if (*caught_exception) {
155 ASSERT(catcher.HasCaught());
156 ASSERT(Top::has_pending_exception());
157 ASSERT(Top::external_caught_exception());
158 if (Top::pending_exception() == Heap::termination_exception()) {
159 result = Factory::termination_exception();
160 } else {
161 result = v8::Utils::OpenHandle(*catcher.Exception());
162 }
163 Top::OptionalRescheduleException(true);
164 }
165
166 ASSERT(!Top::has_pending_exception());
167 ASSERT(!Top::external_caught_exception());
168 return result;
169}
170
171
172Handle<Object> Execution::GetFunctionDelegate(Handle<Object> object) {
173 ASSERT(!object->IsJSFunction());
174
175 // If you return a function from here, it will be called when an
176 // attempt is made to call the given object as a function.
177
178 // Regular expressions can be called as functions in both Firefox
179 // and Safari so we allow it too.
180 if (object->IsJSRegExp()) {
181 Handle<String> exec = Factory::exec_symbol();
John Reck59135872010-11-02 12:39:01 -0700182 // TODO(lrn): Bug 617. We should use the default function here, not the
183 // one on the RegExp object.
184 Object* exec_function;
185 { MaybeObject* maybe_exec_function = object->GetProperty(*exec);
186 // This can lose an exception, but the alternative is to put a failure
187 // object in a handle, which is not GC safe.
188 if (!maybe_exec_function->ToObject(&exec_function)) {
189 return Factory::undefined_value();
190 }
191 }
192 return Handle<Object>(exec_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 }
194
195 // Objects created through the API can have an instance-call handler
196 // that should be used when calling the object as a function.
197 if (object->IsHeapObject() &&
198 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
199 return Handle<JSFunction>(
200 Top::global_context()->call_as_function_delegate());
201 }
202
203 return Factory::undefined_value();
204}
205
206
207Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) {
208 ASSERT(!object->IsJSFunction());
209
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 constructor.
212
213 // 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>(
218 Top::global_context()->call_as_constructor_delegate());
219 }
220
221 return Factory::undefined_value();
222}
223
224
225// Static state for stack guards.
226StackGuard::ThreadLocal StackGuard::thread_local_;
227
228
229bool StackGuard::IsStackOverflow() {
230 ExecutionAccess access;
231 return (thread_local_.jslimit_ != kInterruptLimit &&
232 thread_local_.climit_ != kInterruptLimit);
233}
234
235
236void StackGuard::EnableInterrupts() {
237 ExecutionAccess access;
Steve Block6ded16b2010-05-10 14:33:55 +0100238 if (has_pending_interrupts(access)) {
239 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 }
241}
242
243
244void StackGuard::SetStackLimit(uintptr_t limit) {
245 ExecutionAccess access;
246 // If the current limits are special (eg due to a pending interrupt) then
247 // leave them alone.
248 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000249 if (thread_local_.jslimit_ == thread_local_.real_jslimit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 thread_local_.jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 }
Steve Blockd0582a62009-12-15 09:54:21 +0000252 if (thread_local_.climit_ == thread_local_.real_climit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 thread_local_.climit_ = limit;
254 }
Steve Blockd0582a62009-12-15 09:54:21 +0000255 thread_local_.real_climit_ = limit;
256 thread_local_.real_jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000257}
258
259
260void StackGuard::DisableInterrupts() {
261 ExecutionAccess access;
262 reset_limits(access);
263}
264
265
Steve Blocka7e24c12009-10-30 11:49:00 +0000266bool StackGuard::IsInterrupted() {
267 ExecutionAccess access;
268 return thread_local_.interrupt_flags_ & INTERRUPT;
269}
270
271
272void StackGuard::Interrupt() {
273 ExecutionAccess access;
274 thread_local_.interrupt_flags_ |= INTERRUPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100275 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000276}
277
278
279bool StackGuard::IsPreempted() {
280 ExecutionAccess access;
281 return thread_local_.interrupt_flags_ & PREEMPT;
282}
283
284
285void StackGuard::Preempt() {
286 ExecutionAccess access;
287 thread_local_.interrupt_flags_ |= PREEMPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100288 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289}
290
291
292bool StackGuard::IsTerminateExecution() {
293 ExecutionAccess access;
294 return thread_local_.interrupt_flags_ & TERMINATE;
295}
296
297
298void StackGuard::TerminateExecution() {
299 ExecutionAccess access;
300 thread_local_.interrupt_flags_ |= TERMINATE;
Steve Block6ded16b2010-05-10 14:33:55 +0100301 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000302}
303
304
Ben Murdochb0fe1622011-05-05 13:52:32 +0100305bool StackGuard::IsRuntimeProfilerTick() {
306 ExecutionAccess access;
307 return thread_local_.interrupt_flags_ & RUNTIME_PROFILER_TICK;
308}
309
310
311void StackGuard::RequestRuntimeProfilerTick() {
312 // Ignore calls if we're not optimizing or if we can't get the lock.
313 if (FLAG_opt && ExecutionAccess::TryLock()) {
314 thread_local_.interrupt_flags_ |= RUNTIME_PROFILER_TICK;
315 if (thread_local_.postpone_interrupts_nesting_ == 0) {
316 thread_local_.jslimit_ = thread_local_.climit_ = kInterruptLimit;
317 Heap::SetStackLimits();
318 }
319 ExecutionAccess::Unlock();
320 }
321}
322
323
Steve Blocka7e24c12009-10-30 11:49:00 +0000324#ifdef ENABLE_DEBUGGER_SUPPORT
325bool StackGuard::IsDebugBreak() {
326 ExecutionAccess access;
327 return thread_local_.interrupt_flags_ & DEBUGBREAK;
328}
329
330
331void StackGuard::DebugBreak() {
332 ExecutionAccess access;
333 thread_local_.interrupt_flags_ |= DEBUGBREAK;
Steve Block6ded16b2010-05-10 14:33:55 +0100334 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000335}
336
337
338bool StackGuard::IsDebugCommand() {
339 ExecutionAccess access;
340 return thread_local_.interrupt_flags_ & DEBUGCOMMAND;
341}
342
343
344void StackGuard::DebugCommand() {
345 if (FLAG_debugger_auto_break) {
346 ExecutionAccess access;
347 thread_local_.interrupt_flags_ |= DEBUGCOMMAND;
Steve Block6ded16b2010-05-10 14:33:55 +0100348 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 }
350}
351#endif
352
353void StackGuard::Continue(InterruptFlag after_what) {
354 ExecutionAccess access;
355 thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what);
Steve Block6ded16b2010-05-10 14:33:55 +0100356 if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 reset_limits(access);
358 }
359}
360
361
362int StackGuard::ArchiveSpacePerThread() {
363 return sizeof(ThreadLocal);
364}
365
366
367char* StackGuard::ArchiveStackGuard(char* to) {
368 ExecutionAccess access;
369 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
370 ThreadLocal blank;
371 thread_local_ = blank;
372 return to + sizeof(ThreadLocal);
373}
374
375
376char* StackGuard::RestoreStackGuard(char* from) {
377 ExecutionAccess access;
378 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
Steve Blockd0582a62009-12-15 09:54:21 +0000379 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 return from + sizeof(ThreadLocal);
381}
382
383
384static internal::Thread::LocalStorageKey stack_limit_key =
385 internal::Thread::CreateThreadLocalKey();
386
387
388void StackGuard::FreeThreadResources() {
389 Thread::SetThreadLocal(
390 stack_limit_key,
Steve Blockd0582a62009-12-15 09:54:21 +0000391 reinterpret_cast<void*>(thread_local_.real_climit_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000392}
393
394
395void StackGuard::ThreadLocal::Clear() {
Steve Blockd0582a62009-12-15 09:54:21 +0000396 real_jslimit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 jslimit_ = kIllegalLimit;
Steve Blockd0582a62009-12-15 09:54:21 +0000398 real_climit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 climit_ = kIllegalLimit;
400 nesting_ = 0;
401 postpone_interrupts_nesting_ = 0;
402 interrupt_flags_ = 0;
Steve Blockd0582a62009-12-15 09:54:21 +0000403 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000404}
405
406
407void StackGuard::ThreadLocal::Initialize() {
Steve Blockd0582a62009-12-15 09:54:21 +0000408 if (real_climit_ == kIllegalLimit) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 // Takes the address of the limit variable in order to find out where
410 // the top of stack is right now.
Steve Block1e0659c2011-05-24 12:43:12 +0100411 const uintptr_t kLimitSize = FLAG_stack_size * KB;
Steve Block3ce2e202009-11-05 08:53:23 +0000412 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize;
413 ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize);
Steve Blockd0582a62009-12-15 09:54:21 +0000414 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000416 real_climit_ = limit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 climit_ = limit;
Steve Blockd0582a62009-12-15 09:54:21 +0000418 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000419 }
420 nesting_ = 0;
421 postpone_interrupts_nesting_ = 0;
422 interrupt_flags_ = 0;
423}
424
425
426void StackGuard::ClearThread(const ExecutionAccess& lock) {
427 thread_local_.Clear();
428}
429
430
431void StackGuard::InitThread(const ExecutionAccess& lock) {
432 thread_local_.Initialize();
433 void* stored_limit = Thread::GetThreadLocal(stack_limit_key);
434 // You should hold the ExecutionAccess lock when you call this.
435 if (stored_limit != NULL) {
436 StackGuard::SetStackLimit(reinterpret_cast<intptr_t>(stored_limit));
437 }
438}
439
440
441// --- C a l l s t o n a t i v e s ---
442
443#define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \
444 do { \
445 Object** args[argc] = argv; \
446 ASSERT(has_pending_exception != NULL); \
447 return Call(Top::name##_fun(), Top::builtins(), argc, args, \
448 has_pending_exception); \
449 } while (false)
450
451
452Handle<Object> Execution::ToBoolean(Handle<Object> obj) {
453 // See the similar code in runtime.js:ToBoolean.
454 if (obj->IsBoolean()) return obj;
455 bool result = true;
456 if (obj->IsString()) {
457 result = Handle<String>::cast(obj)->length() != 0;
458 } else if (obj->IsNull() || obj->IsUndefined()) {
459 result = false;
460 } else if (obj->IsNumber()) {
461 double value = obj->Number();
462 result = !((value == 0) || isnan(value));
463 }
464 return Handle<Object>(Heap::ToBoolean(result));
465}
466
467
468Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) {
469 RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc);
470}
471
472
473Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) {
474 RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc);
475}
476
477
478Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) {
479 RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc);
480}
481
482
483Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) {
484 if (obj->IsJSObject()) return obj;
485 RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc);
486}
487
488
489Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) {
490 RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc);
491}
492
493
494Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) {
495 RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc);
496}
497
498
499Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) {
500 RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc);
501}
502
503
504Handle<Object> Execution::NewDate(double time, bool* exc) {
505 Handle<Object> time_obj = Factory::NewNumber(time);
506 RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc);
507}
508
509
510#undef RETURN_NATIVE_CALL
511
512
Ben Murdochf87a2032010-10-22 12:50:53 +0100513Handle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern,
514 Handle<String> flags,
515 bool* exc) {
516 Handle<Object> re_obj = RegExpImpl::CreateRegExpLiteral(
517 Handle<JSFunction>(Top::global_context()->regexp_function()),
518 pattern,
519 flags,
520 exc);
521 if (*exc) return Handle<JSRegExp>();
522 return Handle<JSRegExp>::cast(re_obj);
523}
524
525
Steve Blocka7e24c12009-10-30 11:49:00 +0000526Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
527 int int_index = static_cast<int>(index);
528 if (int_index < 0 || int_index >= string->length()) {
529 return Factory::undefined_value();
530 }
531
532 Handle<Object> char_at =
533 GetProperty(Top::builtins(), Factory::char_at_symbol());
534 if (!char_at->IsJSFunction()) {
535 return Factory::undefined_value();
536 }
537
538 bool caught_exception;
539 Handle<Object> index_object = Factory::NewNumberFromInt(int_index);
540 Object** index_arg[] = { index_object.location() };
541 Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
542 string,
543 ARRAY_SIZE(index_arg),
544 index_arg,
545 &caught_exception);
546 if (caught_exception) {
547 return Factory::undefined_value();
548 }
549 return result;
550}
551
552
553Handle<JSFunction> Execution::InstantiateFunction(
554 Handle<FunctionTemplateInfo> data, bool* exc) {
555 // Fast case: see if the function has already been instantiated
556 int serial_number = Smi::cast(data->serial_number())->value();
John Reck59135872010-11-02 12:39:01 -0700557 Object* elm = Top::global_context()->function_cache()->
558 GetElementNoExceptionThrown(serial_number);
Steve Blocka7e24c12009-10-30 11:49:00 +0000559 if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
560 // The function has not yet been instantiated in this context; do it.
561 Object** args[1] = { Handle<Object>::cast(data).location() };
562 Handle<Object> result =
563 Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
564 if (*exc) return Handle<JSFunction>::null();
565 return Handle<JSFunction>::cast(result);
566}
567
568
569Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
570 bool* exc) {
571 if (data->property_list()->IsUndefined() &&
572 !data->constructor()->IsUndefined()) {
573 // Initialization to make gcc happy.
574 Object* result = NULL;
575 {
576 HandleScope scope;
577 Handle<FunctionTemplateInfo> cons_template =
578 Handle<FunctionTemplateInfo>(
579 FunctionTemplateInfo::cast(data->constructor()));
580 Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
581 if (*exc) return Handle<JSObject>::null();
582 Handle<Object> value = New(cons, 0, NULL, exc);
583 if (*exc) return Handle<JSObject>::null();
584 result = *value;
585 }
586 ASSERT(!*exc);
587 return Handle<JSObject>(JSObject::cast(result));
588 } else {
589 Object** args[1] = { Handle<Object>::cast(data).location() };
590 Handle<Object> result =
591 Call(Top::instantiate_fun(), Top::builtins(), 1, args, exc);
592 if (*exc) return Handle<JSObject>::null();
593 return Handle<JSObject>::cast(result);
594 }
595}
596
597
598void Execution::ConfigureInstance(Handle<Object> instance,
599 Handle<Object> instance_template,
600 bool* exc) {
601 Object** args[2] = { instance.location(), instance_template.location() };
602 Execution::Call(Top::configure_instance_fun(), Top::builtins(), 2, args, exc);
603}
604
605
606Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
607 Handle<JSFunction> fun,
608 Handle<Object> pos,
609 Handle<Object> is_global) {
610 const int argc = 4;
611 Object** args[argc] = { recv.location(),
612 Handle<Object>::cast(fun).location(),
613 pos.location(),
614 is_global.location() };
615 bool caught_exception = false;
616 Handle<Object> result = TryCall(Top::get_stack_trace_line_fun(),
617 Top::builtins(), argc, args,
618 &caught_exception);
619 if (caught_exception || !result->IsString()) return Factory::empty_symbol();
620 return Handle<String>::cast(result);
621}
622
623
624static Object* RuntimePreempt() {
625 // Clear the preempt request flag.
626 StackGuard::Continue(PREEMPT);
627
628 ContextSwitcher::PreemptionReceived();
629
630#ifdef ENABLE_DEBUGGER_SUPPORT
631 if (Debug::InDebugger()) {
632 // If currently in the debugger don't do any actual preemption but record
633 // that preemption occoured while in the debugger.
634 Debug::PreemptionWhileInDebugger();
635 } else {
636 // Perform preemption.
637 v8::Unlocker unlocker;
638 Thread::YieldCPU();
639 }
640#else
641 // Perform preemption.
642 v8::Unlocker unlocker;
643 Thread::YieldCPU();
644#endif
645
646 return Heap::undefined_value();
647}
648
649
650#ifdef ENABLE_DEBUGGER_SUPPORT
651Object* Execution::DebugBreakHelper() {
652 // Just continue if breaks are disabled.
653 if (Debug::disable_break()) {
654 return Heap::undefined_value();
655 }
656
Leon Clarkee46be812010-01-19 14:06:41 +0000657 // Ignore debug break during bootstrapping.
658 if (Bootstrapper::IsActive()) {
659 return Heap::undefined_value();
660 }
661
Steve Blocka7e24c12009-10-30 11:49:00 +0000662 {
663 JavaScriptFrameIterator it;
664 ASSERT(!it.done());
665 Object* fun = it.frame()->function();
666 if (fun && fun->IsJSFunction()) {
667 // Don't stop in builtin functions.
668 if (JSFunction::cast(fun)->IsBuiltin()) {
669 return Heap::undefined_value();
670 }
671 GlobalObject* global = JSFunction::cast(fun)->context()->global();
672 // Don't stop in debugger functions.
673 if (Debug::IsDebugGlobal(global)) {
674 return Heap::undefined_value();
675 }
676 }
677 }
678
679 // Collect the break state before clearing the flags.
680 bool debug_command_only =
681 StackGuard::IsDebugCommand() && !StackGuard::IsDebugBreak();
682
Leon Clarkee46be812010-01-19 14:06:41 +0000683 // Clear the debug break request flag.
Steve Blocka7e24c12009-10-30 11:49:00 +0000684 StackGuard::Continue(DEBUGBREAK);
Leon Clarkee46be812010-01-19 14:06:41 +0000685
686 ProcessDebugMesssages(debug_command_only);
687
688 // Return to continue execution.
689 return Heap::undefined_value();
690}
691
692void Execution::ProcessDebugMesssages(bool debug_command_only) {
693 // Clear the debug command request flag.
Steve Blocka7e24c12009-10-30 11:49:00 +0000694 StackGuard::Continue(DEBUGCOMMAND);
695
696 HandleScope scope;
697 // Enter the debugger. Just continue if we fail to enter the debugger.
698 EnterDebugger debugger;
699 if (debugger.FailedToEnter()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000700 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000701 }
702
703 // Notify the debug event listeners. Indicate auto continue if the break was
704 // a debug command break.
705 Debugger::OnDebugBreak(Factory::undefined_value(), debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000706}
Leon Clarkee46be812010-01-19 14:06:41 +0000707
708
Steve Blocka7e24c12009-10-30 11:49:00 +0000709#endif
710
John Reck59135872010-11-02 12:39:01 -0700711MaybeObject* Execution::HandleStackGuardInterrupt() {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100712 Counters::stack_interrupts.Increment();
713 if (StackGuard::IsRuntimeProfilerTick()) {
714 Counters::runtime_profiler_ticks.Increment();
715 StackGuard::Continue(RUNTIME_PROFILER_TICK);
716 RuntimeProfiler::OptimizeNow();
717 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000718#ifdef ENABLE_DEBUGGER_SUPPORT
719 if (StackGuard::IsDebugBreak() || StackGuard::IsDebugCommand()) {
720 DebugBreakHelper();
721 }
722#endif
723 if (StackGuard::IsPreempted()) RuntimePreempt();
724 if (StackGuard::IsTerminateExecution()) {
725 StackGuard::Continue(TERMINATE);
726 return Top::TerminateExecution();
727 }
728 if (StackGuard::IsInterrupted()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000729 StackGuard::Continue(INTERRUPT);
730 return Top::StackOverflow();
731 }
732 return Heap::undefined_value();
733}
734
Steve Blocka7e24c12009-10-30 11:49:00 +0000735} } // namespace v8::internal