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" |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 33 | #include "bootstrapper.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "codegen-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "debug.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 36 | #include "simulator.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #include "v8threads.h" |
| 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | |
| 43 | static 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | // 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 79 | // 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | { |
| 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 Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 91 | byte* entry_address = func->code()->entry(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 92 | JSFunction* function = *func; |
| 93 | Object* receiver_pointer = *receiver; |
| 94 | value = CALL_GENERATED_CODE(entry, entry_address, function, |
| 95 | receiver_pointer, argc, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | } |
| 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 | |
| 116 | Handle<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 | |
| 125 | Handle<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 | |
| 131 | Handle<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 | |
| 165 | Handle<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 | |
| 190 | Handle<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. |
| 209 | StackGuard::ThreadLocal StackGuard::thread_local_; |
| 210 | |
| 211 | |
| 212 | bool StackGuard::IsStackOverflow() { |
| 213 | ExecutionAccess access; |
| 214 | return (thread_local_.jslimit_ != kInterruptLimit && |
| 215 | thread_local_.climit_ != kInterruptLimit); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void StackGuard::EnableInterrupts() { |
| 220 | ExecutionAccess access; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 221 | if (has_pending_interrupts(access)) { |
| 222 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | |
| 227 | void 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 232 | if (thread_local_.jslimit_ == thread_local_.real_jslimit_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | thread_local_.jslimit_ = jslimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 235 | if (thread_local_.climit_ == thread_local_.real_climit_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | thread_local_.climit_ = limit; |
| 237 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 238 | thread_local_.real_climit_ = limit; |
| 239 | thread_local_.real_jslimit_ = jslimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| 243 | void StackGuard::DisableInterrupts() { |
| 244 | ExecutionAccess access; |
| 245 | reset_limits(access); |
| 246 | } |
| 247 | |
| 248 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 249 | bool StackGuard::IsInterrupted() { |
| 250 | ExecutionAccess access; |
| 251 | return thread_local_.interrupt_flags_ & INTERRUPT; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void StackGuard::Interrupt() { |
| 256 | ExecutionAccess access; |
| 257 | thread_local_.interrupt_flags_ |= INTERRUPT; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 258 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | bool StackGuard::IsPreempted() { |
| 263 | ExecutionAccess access; |
| 264 | return thread_local_.interrupt_flags_ & PREEMPT; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | void StackGuard::Preempt() { |
| 269 | ExecutionAccess access; |
| 270 | thread_local_.interrupt_flags_ |= PREEMPT; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 271 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | bool StackGuard::IsTerminateExecution() { |
| 276 | ExecutionAccess access; |
| 277 | return thread_local_.interrupt_flags_ & TERMINATE; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | void StackGuard::TerminateExecution() { |
| 282 | ExecutionAccess access; |
| 283 | thread_local_.interrupt_flags_ |= TERMINATE; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 284 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 289 | bool StackGuard::IsDebugBreak() { |
| 290 | ExecutionAccess access; |
| 291 | return thread_local_.interrupt_flags_ & DEBUGBREAK; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | void StackGuard::DebugBreak() { |
| 296 | ExecutionAccess access; |
| 297 | thread_local_.interrupt_flags_ |= DEBUGBREAK; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 298 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | bool StackGuard::IsDebugCommand() { |
| 303 | ExecutionAccess access; |
| 304 | return thread_local_.interrupt_flags_ & DEBUGCOMMAND; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | void StackGuard::DebugCommand() { |
| 309 | if (FLAG_debugger_auto_break) { |
| 310 | ExecutionAccess access; |
| 311 | thread_local_.interrupt_flags_ |= DEBUGCOMMAND; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 312 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | #endif |
| 316 | |
| 317 | void StackGuard::Continue(InterruptFlag after_what) { |
| 318 | ExecutionAccess access; |
| 319 | thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 320 | if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 321 | reset_limits(access); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | int StackGuard::ArchiveSpacePerThread() { |
| 327 | return sizeof(ThreadLocal); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | char* 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 | |
| 340 | char* StackGuard::RestoreStackGuard(char* from) { |
| 341 | ExecutionAccess access; |
| 342 | memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 343 | Heap::SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | return from + sizeof(ThreadLocal); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static internal::Thread::LocalStorageKey stack_limit_key = |
| 349 | internal::Thread::CreateThreadLocalKey(); |
| 350 | |
| 351 | |
| 352 | void StackGuard::FreeThreadResources() { |
| 353 | Thread::SetThreadLocal( |
| 354 | stack_limit_key, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 355 | reinterpret_cast<void*>(thread_local_.real_climit_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | void StackGuard::ThreadLocal::Clear() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 360 | real_jslimit_ = kIllegalLimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | jslimit_ = kIllegalLimit; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 362 | real_climit_ = kIllegalLimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 363 | climit_ = kIllegalLimit; |
| 364 | nesting_ = 0; |
| 365 | postpone_interrupts_nesting_ = 0; |
| 366 | interrupt_flags_ = 0; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 367 | Heap::SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
| 371 | void StackGuard::ThreadLocal::Initialize() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 372 | if (real_climit_ == kIllegalLimit) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | // Takes the address of the limit variable in order to find out where |
| 374 | // the top of stack is right now. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 375 | uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize; |
| 376 | ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 377 | real_jslimit_ = SimulatorStack::JsLimitFromCLimit(limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | jslimit_ = SimulatorStack::JsLimitFromCLimit(limit); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 379 | real_climit_ = limit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | climit_ = limit; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 381 | Heap::SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | } |
| 383 | nesting_ = 0; |
| 384 | postpone_interrupts_nesting_ = 0; |
| 385 | interrupt_flags_ = 0; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | void StackGuard::ClearThread(const ExecutionAccess& lock) { |
| 390 | thread_local_.Clear(); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | void 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 | |
| 415 | Handle<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 | |
| 431 | Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) { |
| 432 | RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) { |
| 437 | RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) { |
| 442 | RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | Handle<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 | |
| 452 | Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) { |
| 453 | RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) { |
| 458 | RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) { |
| 463 | RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | Handle<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 | |
| 476 | Handle<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 | |
| 503 | Handle<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 | |
| 519 | Handle<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 | |
| 548 | void 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 | |
| 556 | Handle<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 | |
| 574 | static 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 |
| 601 | Object* Execution::DebugBreakHelper() { |
| 602 | // Just continue if breaks are disabled. |
| 603 | if (Debug::disable_break()) { |
| 604 | return Heap::undefined_value(); |
| 605 | } |
| 606 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 607 | // Ignore debug break during bootstrapping. |
| 608 | if (Bootstrapper::IsActive()) { |
| 609 | return Heap::undefined_value(); |
| 610 | } |
| 611 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 612 | { |
| 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 633 | // Clear the debug break request flag. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 634 | StackGuard::Continue(DEBUGBREAK); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 635 | |
| 636 | ProcessDebugMesssages(debug_command_only); |
| 637 | |
| 638 | // Return to continue execution. |
| 639 | return Heap::undefined_value(); |
| 640 | } |
| 641 | |
| 642 | void Execution::ProcessDebugMesssages(bool debug_command_only) { |
| 643 | // Clear the debug command request flag. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 644 | 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 650 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | } |
| 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 656 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 657 | |
| 658 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | #endif |
| 660 | |
| 661 | Object* 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 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 682 | const char* const GCExtension::kSource = "native function gc();"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 683 | |
| 684 | |
| 685 | v8::Handle<v8::FunctionTemplate> GCExtension::GetNativeFunction( |
| 686 | v8::Handle<v8::String> str) { |
| 687 | return v8::FunctionTemplate::New(GCExtension::GC); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | v8::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 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 698 | static GCExtension gc_extension; |
| 699 | static v8::DeclareExtension gc_extension_declaration(&gc_extension); |
| 700 | |
| 701 | |
| 702 | // --- 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 --- |
| 703 | |
| 704 | |
| 705 | template <typename Char, typename Base> |
| 706 | class SimpleStringResource : public Base { |
| 707 | public: |
| 708 | // Takes ownership of |data|. |
| 709 | SimpleStringResource(Char* data, size_t length) |
| 710 | : data_(data), |
| 711 | length_(length) {} |
| 712 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 713 | virtual ~SimpleStringResource() { delete[] data_; } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 714 | |
| 715 | virtual const Char* data() const { return data_; } |
| 716 | |
| 717 | virtual size_t length() const { return length_; } |
| 718 | |
| 719 | private: |
| 720 | Char* const data_; |
| 721 | const size_t length_; |
| 722 | }; |
| 723 | |
| 724 | |
| 725 | typedef SimpleStringResource<char, v8::String::ExternalAsciiStringResource> |
| 726 | SimpleAsciiStringResource; |
| 727 | typedef SimpleStringResource<uc16, v8::String::ExternalStringResource> |
| 728 | SimpleTwoByteStringResource; |
| 729 | |
| 730 | |
| 731 | const char* const ExternalizeStringExtension::kSource = |
| 732 | "native function externalizeString();" |
| 733 | "native function isAsciiString();"; |
| 734 | |
| 735 | |
| 736 | v8::Handle<v8::FunctionTemplate> ExternalizeStringExtension::GetNativeFunction( |
| 737 | v8::Handle<v8::String> str) { |
| 738 | if (strcmp(*v8::String::AsciiValue(str), "externalizeString") == 0) { |
| 739 | return v8::FunctionTemplate::New(ExternalizeStringExtension::Externalize); |
| 740 | } else { |
| 741 | ASSERT(strcmp(*v8::String::AsciiValue(str), "isAsciiString") == 0); |
| 742 | return v8::FunctionTemplate::New(ExternalizeStringExtension::IsAscii); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | |
| 747 | v8::Handle<v8::Value> ExternalizeStringExtension::Externalize( |
| 748 | const v8::Arguments& args) { |
| 749 | if (args.Length() < 1 || !args[0]->IsString()) { |
| 750 | return v8::ThrowException(v8::String::New( |
| 751 | "First parameter to externalizeString() must be a string.")); |
| 752 | } |
| 753 | bool force_two_byte = false; |
| 754 | if (args.Length() >= 2) { |
| 755 | if (args[1]->IsBoolean()) { |
| 756 | force_two_byte = args[1]->BooleanValue(); |
| 757 | } else { |
| 758 | return v8::ThrowException(v8::String::New( |
| 759 | "Second parameter to externalizeString() must be a boolean.")); |
| 760 | } |
| 761 | } |
| 762 | bool result = false; |
| 763 | Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>()); |
| 764 | if (string->IsExternalString()) { |
| 765 | return v8::ThrowException(v8::String::New( |
| 766 | "externalizeString() can't externalize twice.")); |
| 767 | } |
| 768 | if (string->IsAsciiRepresentation() && !force_two_byte) { |
| 769 | char* data = new char[string->length()]; |
| 770 | String::WriteToFlat(*string, data, 0, string->length()); |
| 771 | SimpleAsciiStringResource* resource = new SimpleAsciiStringResource( |
| 772 | data, string->length()); |
| 773 | result = string->MakeExternal(resource); |
| 774 | if (result && !string->IsSymbol()) { |
| 775 | i::ExternalStringTable::AddString(*string); |
| 776 | } |
| 777 | } else { |
| 778 | uc16* data = new uc16[string->length()]; |
| 779 | String::WriteToFlat(*string, data, 0, string->length()); |
| 780 | SimpleTwoByteStringResource* resource = new SimpleTwoByteStringResource( |
| 781 | data, string->length()); |
| 782 | result = string->MakeExternal(resource); |
| 783 | if (result && !string->IsSymbol()) { |
| 784 | i::ExternalStringTable::AddString(*string); |
| 785 | } |
| 786 | } |
| 787 | if (!result) { |
| 788 | return v8::ThrowException(v8::String::New("externalizeString() failed.")); |
| 789 | } |
| 790 | return v8::Undefined(); |
| 791 | } |
| 792 | |
| 793 | |
| 794 | v8::Handle<v8::Value> ExternalizeStringExtension::IsAscii( |
| 795 | const v8::Arguments& args) { |
| 796 | if (args.Length() != 1 || !args[0]->IsString()) { |
| 797 | return v8::ThrowException(v8::String::New( |
| 798 | "isAsciiString() requires a single string argument.")); |
| 799 | } |
| 800 | return Utils::OpenHandle(*args[0].As<v8::String>())->IsAsciiRepresentation() ? |
| 801 | v8::True() : v8::False(); |
| 802 | } |
| 803 | |
| 804 | |
| 805 | static ExternalizeStringExtension externalize_extension; |
| 806 | static v8::DeclareExtension externalize_extension_declaration( |
| 807 | &externalize_extension); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 808 | |
| 809 | } } // namespace v8::internal |