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