Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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" |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 34 | #include "codegen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "debug.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 36 | #include "runtime-profiler.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 37 | #include "simulator.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | #include "v8threads.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 39 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | |
| 41 | namespace v8 { |
| 42 | namespace internal { |
| 43 | |
| 44 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 45 | StackGuard::StackGuard() |
| 46 | : isolate_(NULL) { |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void StackGuard::set_interrupt_limits(const ExecutionAccess& lock) { |
| 51 | ASSERT(isolate_ != NULL); |
| 52 | // Ignore attempts to interrupt when interrupts are postponed. |
| 53 | if (should_postpone_interrupts(lock)) return; |
| 54 | thread_local_.jslimit_ = kInterruptLimit; |
| 55 | thread_local_.climit_ = kInterruptLimit; |
| 56 | isolate_->heap()->SetStackLimits(); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void StackGuard::reset_limits(const ExecutionAccess& lock) { |
| 61 | ASSERT(isolate_ != NULL); |
| 62 | thread_local_.jslimit_ = thread_local_.real_jslimit_; |
| 63 | thread_local_.climit_ = thread_local_.real_climit_; |
| 64 | isolate_->heap()->SetStackLimits(); |
| 65 | } |
| 66 | |
| 67 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | static Handle<Object> Invoke(bool construct, |
| 69 | Handle<JSFunction> func, |
| 70 | Handle<Object> receiver, |
| 71 | int argc, |
| 72 | Object*** args, |
| 73 | bool* has_pending_exception) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 74 | Isolate* isolate = func->GetIsolate(); |
| 75 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | // Entering JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 77 | VMState state(isolate, JS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | |
| 79 | // Placeholder for return value. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 80 | MaybeObject* value = reinterpret_cast<Object*>(kZapValue); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | |
| 82 | typedef Object* (*JSEntryFunction)( |
| 83 | byte* entry, |
| 84 | Object* function, |
| 85 | Object* receiver, |
| 86 | int argc, |
| 87 | Object*** args); |
| 88 | |
| 89 | Handle<Code> code; |
| 90 | if (construct) { |
| 91 | JSConstructEntryStub stub; |
| 92 | code = stub.GetCode(); |
| 93 | } else { |
| 94 | JSEntryStub stub; |
| 95 | code = stub.GetCode(); |
| 96 | } |
| 97 | |
| 98 | // Convert calls on global objects to be calls on the global |
| 99 | // receiver instead to avoid having a 'this' pointer which refers |
| 100 | // directly to a global object. |
| 101 | if (receiver->IsGlobalObject()) { |
| 102 | Handle<GlobalObject> global = Handle<GlobalObject>::cast(receiver); |
| 103 | receiver = Handle<JSObject>(global->global_receiver()); |
| 104 | } |
| 105 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 106 | // Make sure that the global object of the context we're about to |
| 107 | // make the current one is indeed a global object. |
| 108 | ASSERT(func->context()->global()->IsGlobalObject()); |
| 109 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | { |
| 111 | // Save and restore context around invocation and block the |
| 112 | // allocation of handles without explicit handle scopes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 113 | SaveContext save(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | NoHandleAllocation na; |
| 115 | JSEntryFunction entry = FUNCTION_CAST<JSEntryFunction>(code->entry()); |
| 116 | |
| 117 | // Call the function through the right JS entry stub. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 118 | byte* entry_address = func->code()->entry(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 119 | JSFunction* function = *func; |
| 120 | Object* receiver_pointer = *receiver; |
| 121 | value = CALL_GENERATED_CODE(entry, entry_address, function, |
| 122 | receiver_pointer, argc, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | #ifdef DEBUG |
| 126 | value->Verify(); |
| 127 | #endif |
| 128 | |
| 129 | // Update the pending exception flag and return the value. |
| 130 | *has_pending_exception = value->IsException(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 131 | ASSERT(*has_pending_exception == Isolate::Current()->has_pending_exception()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | if (*has_pending_exception) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 133 | isolate->ReportPendingMessages(); |
| 134 | if (isolate->pending_exception() == Failure::OutOfMemoryException()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 135 | if (!isolate->handle_scope_implementer()->ignore_out_of_memory()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 136 | V8::FatalProcessOutOfMemory("JS", true); |
| 137 | } |
| 138 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | return Handle<Object>(); |
| 140 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 141 | isolate->clear_pending_message(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 144 | return Handle<Object>(value->ToObjectUnchecked(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 148 | Handle<Object> Execution::Call(Handle<Object> callable, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | Handle<Object> receiver, |
| 150 | int argc, |
| 151 | Object*** args, |
| 152 | bool* pending_exception) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 153 | if (!callable->IsJSFunction()) { |
| 154 | callable = TryGetFunctionDelegate(callable, pending_exception); |
| 155 | if (*pending_exception) return callable; |
| 156 | } |
| 157 | Handle<JSFunction> func = Handle<JSFunction>::cast(callable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | return Invoke(false, func, receiver, argc, args, pending_exception); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | Handle<Object> Execution::New(Handle<JSFunction> func, int argc, |
| 163 | Object*** args, bool* pending_exception) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 164 | return Invoke(true, func, Isolate::Current()->global(), argc, args, |
| 165 | pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | Handle<Object> Execution::TryCall(Handle<JSFunction> func, |
| 170 | Handle<Object> receiver, |
| 171 | int argc, |
| 172 | Object*** args, |
| 173 | bool* caught_exception) { |
| 174 | // Enter a try-block while executing the JavaScript code. To avoid |
| 175 | // duplicate error printing it must be non-verbose. Also, to avoid |
| 176 | // creating message objects during stack overflow we shouldn't |
| 177 | // capture messages. |
| 178 | v8::TryCatch catcher; |
| 179 | catcher.SetVerbose(false); |
| 180 | catcher.SetCaptureMessage(false); |
| 181 | |
| 182 | Handle<Object> result = Invoke(false, func, receiver, argc, args, |
| 183 | caught_exception); |
| 184 | |
| 185 | if (*caught_exception) { |
| 186 | ASSERT(catcher.HasCaught()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 187 | Isolate* isolate = Isolate::Current(); |
| 188 | ASSERT(isolate->has_pending_exception()); |
| 189 | ASSERT(isolate->external_caught_exception()); |
| 190 | if (isolate->pending_exception() == |
| 191 | isolate->heap()->termination_exception()) { |
| 192 | result = isolate->factory()->termination_exception(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | } else { |
| 194 | result = v8::Utils::OpenHandle(*catcher.Exception()); |
| 195 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 196 | isolate->OptionalRescheduleException(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 199 | ASSERT(!Isolate::Current()->has_pending_exception()); |
| 200 | ASSERT(!Isolate::Current()->external_caught_exception()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | return result; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | Handle<Object> Execution::GetFunctionDelegate(Handle<Object> object) { |
| 206 | ASSERT(!object->IsJSFunction()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 207 | Isolate* isolate = Isolate::Current(); |
| 208 | Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 function. |
| 212 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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>( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 218 | isolate->global_context()->call_as_function_delegate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 221 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 225 | Handle<Object> Execution::TryGetFunctionDelegate(Handle<Object> object, |
| 226 | bool* has_pending_exception) { |
| 227 | ASSERT(!object->IsJSFunction()); |
| 228 | Isolate* isolate = Isolate::Current(); |
| 229 | |
| 230 | // Objects created through the API can have an instance-call handler |
| 231 | // that should be used when calling the object as a function. |
| 232 | if (object->IsHeapObject() && |
| 233 | HeapObject::cast(*object)->map()->has_instance_call_handler()) { |
| 234 | return Handle<JSFunction>( |
| 235 | isolate->global_context()->call_as_function_delegate()); |
| 236 | } |
| 237 | |
| 238 | // If the Object doesn't have an instance-call handler we should |
| 239 | // throw a non-callable exception. |
| 240 | i::Handle<i::Object> error_obj = isolate->factory()->NewTypeError( |
| 241 | "called_non_callable", i::HandleVector<i::Object>(&object, 1)); |
| 242 | isolate->Throw(*error_obj); |
| 243 | *has_pending_exception = true; |
| 244 | |
| 245 | return isolate->factory()->undefined_value(); |
| 246 | } |
| 247 | |
| 248 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 249 | Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) { |
| 250 | ASSERT(!object->IsJSFunction()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 251 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 252 | |
| 253 | // If you return a function from here, it will be called when an |
| 254 | // attempt is made to call the given object as a constructor. |
| 255 | |
| 256 | // Objects created through the API can have an instance-call handler |
| 257 | // that should be used when calling the object as a function. |
| 258 | if (object->IsHeapObject() && |
| 259 | HeapObject::cast(*object)->map()->has_instance_call_handler()) { |
| 260 | return Handle<JSFunction>( |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 261 | isolate->global_context()->call_as_constructor_delegate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 264 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 268 | Handle<Object> Execution::TryGetConstructorDelegate( |
| 269 | Handle<Object> object, |
| 270 | bool* has_pending_exception) { |
| 271 | ASSERT(!object->IsJSFunction()); |
| 272 | Isolate* isolate = Isolate::Current(); |
| 273 | |
| 274 | // If you return a function from here, it will be called when an |
| 275 | // attempt is made to call the given object as a constructor. |
| 276 | |
| 277 | // Objects created through the API can have an instance-call handler |
| 278 | // that should be used when calling the object as a function. |
| 279 | if (object->IsHeapObject() && |
| 280 | HeapObject::cast(*object)->map()->has_instance_call_handler()) { |
| 281 | return Handle<JSFunction>( |
| 282 | isolate->global_context()->call_as_constructor_delegate()); |
| 283 | } |
| 284 | |
| 285 | // If the Object doesn't have an instance-call handler we should |
| 286 | // throw a non-callable exception. |
| 287 | i::Handle<i::Object> error_obj = isolate->factory()->NewTypeError( |
| 288 | "called_non_callable", i::HandleVector<i::Object>(&object, 1)); |
| 289 | isolate->Throw(*error_obj); |
| 290 | *has_pending_exception = true; |
| 291 | |
| 292 | return isolate->factory()->undefined_value(); |
| 293 | } |
| 294 | |
| 295 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | bool StackGuard::IsStackOverflow() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 297 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 298 | return (thread_local_.jslimit_ != kInterruptLimit && |
| 299 | thread_local_.climit_ != kInterruptLimit); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | void StackGuard::EnableInterrupts() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 304 | ExecutionAccess access(isolate_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 305 | if (has_pending_interrupts(access)) { |
| 306 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | |
| 311 | void StackGuard::SetStackLimit(uintptr_t limit) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 312 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | // If the current limits are special (eg due to a pending interrupt) then |
| 314 | // leave them alone. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 315 | uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 316 | if (thread_local_.jslimit_ == thread_local_.real_jslimit_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | thread_local_.jslimit_ = jslimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 319 | if (thread_local_.climit_ == thread_local_.real_climit_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | thread_local_.climit_ = limit; |
| 321 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 322 | thread_local_.real_climit_ = limit; |
| 323 | thread_local_.real_jslimit_ = jslimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
| 327 | void StackGuard::DisableInterrupts() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 328 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | reset_limits(access); |
| 330 | } |
| 331 | |
| 332 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 333 | bool StackGuard::IsInterrupted() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 334 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | return thread_local_.interrupt_flags_ & INTERRUPT; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | void StackGuard::Interrupt() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 340 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 341 | thread_local_.interrupt_flags_ |= INTERRUPT; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 342 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
| 346 | bool StackGuard::IsPreempted() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 347 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | return thread_local_.interrupt_flags_ & PREEMPT; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | void StackGuard::Preempt() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 353 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | thread_local_.interrupt_flags_ |= PREEMPT; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 355 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | bool StackGuard::IsTerminateExecution() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 360 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | return thread_local_.interrupt_flags_ & TERMINATE; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | void StackGuard::TerminateExecution() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 366 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 367 | thread_local_.interrupt_flags_ |= TERMINATE; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 368 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 372 | bool StackGuard::IsRuntimeProfilerTick() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 373 | ExecutionAccess access(isolate_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 374 | return thread_local_.interrupt_flags_ & RUNTIME_PROFILER_TICK; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void StackGuard::RequestRuntimeProfilerTick() { |
| 379 | // Ignore calls if we're not optimizing or if we can't get the lock. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 380 | if (FLAG_opt && ExecutionAccess::TryLock(isolate_)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 381 | thread_local_.interrupt_flags_ |= RUNTIME_PROFILER_TICK; |
| 382 | if (thread_local_.postpone_interrupts_nesting_ == 0) { |
| 383 | thread_local_.jslimit_ = thread_local_.climit_ = kInterruptLimit; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 384 | isolate_->heap()->SetStackLimits(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 385 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 386 | ExecutionAccess::Unlock(isolate_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 391 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 392 | bool StackGuard::IsDebugBreak() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 393 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | return thread_local_.interrupt_flags_ & DEBUGBREAK; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | void StackGuard::DebugBreak() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 399 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | thread_local_.interrupt_flags_ |= DEBUGBREAK; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 401 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | |
| 405 | bool StackGuard::IsDebugCommand() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 406 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | return thread_local_.interrupt_flags_ & DEBUGCOMMAND; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | void StackGuard::DebugCommand() { |
| 412 | if (FLAG_debugger_auto_break) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 413 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 414 | thread_local_.interrupt_flags_ |= DEBUGCOMMAND; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 415 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | #endif |
| 419 | |
| 420 | void StackGuard::Continue(InterruptFlag after_what) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 421 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 422 | thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 423 | if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 424 | reset_limits(access); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 429 | char* StackGuard::ArchiveStackGuard(char* to) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 430 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 431 | memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal)); |
| 432 | ThreadLocal blank; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 433 | |
| 434 | // Set the stack limits using the old thread_local_. |
| 435 | // TODO(isolates): This was the old semantics of constructing a ThreadLocal |
| 436 | // (as the ctor called SetStackLimits, which looked at the |
| 437 | // current thread_local_ from StackGuard)-- but is this |
| 438 | // really what was intended? |
| 439 | isolate_->heap()->SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 440 | thread_local_ = blank; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 441 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 442 | return to + sizeof(ThreadLocal); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | char* StackGuard::RestoreStackGuard(char* from) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 447 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 448 | memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 449 | isolate_->heap()->SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 450 | return from + sizeof(ThreadLocal); |
| 451 | } |
| 452 | |
| 453 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 454 | void StackGuard::FreeThreadResources() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 455 | Isolate::PerIsolateThreadData* per_thread = |
| 456 | isolate_->FindOrAllocatePerThreadDataForThisThread(); |
| 457 | per_thread->set_stack_limit(thread_local_.real_climit_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
| 461 | void StackGuard::ThreadLocal::Clear() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 462 | real_jslimit_ = kIllegalLimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | jslimit_ = kIllegalLimit; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 464 | real_climit_ = kIllegalLimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 465 | climit_ = kIllegalLimit; |
| 466 | nesting_ = 0; |
| 467 | postpone_interrupts_nesting_ = 0; |
| 468 | interrupt_flags_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 472 | bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 473 | bool should_set_stack_limits = false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 474 | if (real_climit_ == kIllegalLimit) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 475 | // Takes the address of the limit variable in order to find out where |
| 476 | // the top of stack is right now. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 477 | const uintptr_t kLimitSize = FLAG_stack_size * KB; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 478 | uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize; |
| 479 | ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 480 | real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit); |
| 481 | jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 482 | real_climit_ = limit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 483 | climit_ = limit; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 484 | should_set_stack_limits = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 485 | } |
| 486 | nesting_ = 0; |
| 487 | postpone_interrupts_nesting_ = 0; |
| 488 | interrupt_flags_ = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 489 | return should_set_stack_limits; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
| 493 | void StackGuard::ClearThread(const ExecutionAccess& lock) { |
| 494 | thread_local_.Clear(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | isolate_->heap()->SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
| 499 | void StackGuard::InitThread(const ExecutionAccess& lock) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 500 | if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits(); |
| 501 | Isolate::PerIsolateThreadData* per_thread = |
| 502 | isolate_->FindOrAllocatePerThreadDataForThisThread(); |
| 503 | uintptr_t stored_limit = per_thread->stack_limit(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 504 | // You should hold the ExecutionAccess lock when you call this. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 505 | if (stored_limit != 0) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 506 | SetStackLimit(stored_limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | // --- C a l l s t o n a t i v e s --- |
| 512 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 513 | #define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \ |
| 514 | do { \ |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 515 | Isolate* isolate = Isolate::Current(); \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 516 | Object** args[argc] = argv; \ |
| 517 | ASSERT(has_pending_exception != NULL); \ |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 518 | return Call(isolate->name##_fun(), \ |
| 519 | isolate->js_builtins_object(), argc, args, \ |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 520 | has_pending_exception); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 521 | } while (false) |
| 522 | |
| 523 | |
| 524 | Handle<Object> Execution::ToBoolean(Handle<Object> obj) { |
| 525 | // See the similar code in runtime.js:ToBoolean. |
| 526 | if (obj->IsBoolean()) return obj; |
| 527 | bool result = true; |
| 528 | if (obj->IsString()) { |
| 529 | result = Handle<String>::cast(obj)->length() != 0; |
| 530 | } else if (obj->IsNull() || obj->IsUndefined()) { |
| 531 | result = false; |
| 532 | } else if (obj->IsNumber()) { |
| 533 | double value = obj->Number(); |
| 534 | result = !((value == 0) || isnan(value)); |
| 535 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 536 | return Handle<Object>(HEAP->ToBoolean(result)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | |
| 540 | Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) { |
| 541 | RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) { |
| 546 | RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc); |
| 547 | } |
| 548 | |
| 549 | |
| 550 | Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) { |
| 551 | RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) { |
| 556 | if (obj->IsJSObject()) return obj; |
| 557 | RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) { |
| 562 | RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc); |
| 563 | } |
| 564 | |
| 565 | |
| 566 | Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) { |
| 567 | RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) { |
| 572 | RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | Handle<Object> Execution::NewDate(double time, bool* exc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 577 | Handle<Object> time_obj = FACTORY->NewNumber(time); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 578 | RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | #undef RETURN_NATIVE_CALL |
| 583 | |
| 584 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 585 | Handle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern, |
| 586 | Handle<String> flags, |
| 587 | bool* exc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 588 | Handle<JSFunction> function = Handle<JSFunction>( |
| 589 | pattern->GetIsolate()->global_context()->regexp_function()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 590 | Handle<Object> re_obj = RegExpImpl::CreateRegExpLiteral( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 591 | function, pattern, flags, exc); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 592 | if (*exc) return Handle<JSRegExp>(); |
| 593 | return Handle<JSRegExp>::cast(re_obj); |
| 594 | } |
| 595 | |
| 596 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 597 | Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 598 | Isolate* isolate = string->GetIsolate(); |
| 599 | Factory* factory = isolate->factory(); |
| 600 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 601 | int int_index = static_cast<int>(index); |
| 602 | if (int_index < 0 || int_index >= string->length()) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 603 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | Handle<Object> char_at = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 607 | GetProperty(isolate->js_builtins_object(), |
| 608 | factory->char_at_symbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 609 | if (!char_at->IsJSFunction()) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 610 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | bool caught_exception; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 614 | Handle<Object> index_object = factory->NewNumberFromInt(int_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 615 | Object** index_arg[] = { index_object.location() }; |
| 616 | Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at), |
| 617 | string, |
| 618 | ARRAY_SIZE(index_arg), |
| 619 | index_arg, |
| 620 | &caught_exception); |
| 621 | if (caught_exception) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 622 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 623 | } |
| 624 | return result; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | Handle<JSFunction> Execution::InstantiateFunction( |
| 629 | Handle<FunctionTemplateInfo> data, bool* exc) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 630 | Isolate* isolate = data->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 631 | // Fast case: see if the function has already been instantiated |
| 632 | int serial_number = Smi::cast(data->serial_number())->value(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 633 | Object* elm = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 634 | isolate->global_context()->function_cache()-> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 635 | GetElementNoExceptionThrown(serial_number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 636 | if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm)); |
| 637 | // The function has not yet been instantiated in this context; do it. |
| 638 | Object** args[1] = { Handle<Object>::cast(data).location() }; |
| 639 | Handle<Object> result = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 640 | Call(isolate->instantiate_fun(), |
| 641 | isolate->js_builtins_object(), 1, args, exc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 642 | if (*exc) return Handle<JSFunction>::null(); |
| 643 | return Handle<JSFunction>::cast(result); |
| 644 | } |
| 645 | |
| 646 | |
| 647 | Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data, |
| 648 | bool* exc) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 649 | Isolate* isolate = data->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 650 | if (data->property_list()->IsUndefined() && |
| 651 | !data->constructor()->IsUndefined()) { |
| 652 | // Initialization to make gcc happy. |
| 653 | Object* result = NULL; |
| 654 | { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 655 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 656 | Handle<FunctionTemplateInfo> cons_template = |
| 657 | Handle<FunctionTemplateInfo>( |
| 658 | FunctionTemplateInfo::cast(data->constructor())); |
| 659 | Handle<JSFunction> cons = InstantiateFunction(cons_template, exc); |
| 660 | if (*exc) return Handle<JSObject>::null(); |
| 661 | Handle<Object> value = New(cons, 0, NULL, exc); |
| 662 | if (*exc) return Handle<JSObject>::null(); |
| 663 | result = *value; |
| 664 | } |
| 665 | ASSERT(!*exc); |
| 666 | return Handle<JSObject>(JSObject::cast(result)); |
| 667 | } else { |
| 668 | Object** args[1] = { Handle<Object>::cast(data).location() }; |
| 669 | Handle<Object> result = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 670 | Call(isolate->instantiate_fun(), |
| 671 | isolate->js_builtins_object(), 1, args, exc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 672 | if (*exc) return Handle<JSObject>::null(); |
| 673 | return Handle<JSObject>::cast(result); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | |
| 678 | void Execution::ConfigureInstance(Handle<Object> instance, |
| 679 | Handle<Object> instance_template, |
| 680 | bool* exc) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 681 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 682 | Object** args[2] = { instance.location(), instance_template.location() }; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 683 | Execution::Call(isolate->configure_instance_fun(), |
| 684 | isolate->js_builtins_object(), 2, args, exc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
| 688 | Handle<String> Execution::GetStackTraceLine(Handle<Object> recv, |
| 689 | Handle<JSFunction> fun, |
| 690 | Handle<Object> pos, |
| 691 | Handle<Object> is_global) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 692 | Isolate* isolate = fun->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 693 | const int argc = 4; |
| 694 | Object** args[argc] = { recv.location(), |
| 695 | Handle<Object>::cast(fun).location(), |
| 696 | pos.location(), |
| 697 | is_global.location() }; |
| 698 | bool caught_exception = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 699 | Handle<Object> result = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 700 | TryCall(isolate->get_stack_trace_line_fun(), |
| 701 | isolate->js_builtins_object(), argc, args, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 702 | &caught_exception); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 703 | if (caught_exception || !result->IsString()) { |
| 704 | return isolate->factory()->empty_symbol(); |
| 705 | } |
| 706 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 707 | return Handle<String>::cast(result); |
| 708 | } |
| 709 | |
| 710 | |
| 711 | static Object* RuntimePreempt() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 712 | Isolate* isolate = Isolate::Current(); |
| 713 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 714 | // Clear the preempt request flag. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 715 | isolate->stack_guard()->Continue(PREEMPT); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 716 | |
| 717 | ContextSwitcher::PreemptionReceived(); |
| 718 | |
| 719 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 720 | if (isolate->debug()->InDebugger()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 721 | // If currently in the debugger don't do any actual preemption but record |
| 722 | // that preemption occoured while in the debugger. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 723 | isolate->debug()->PreemptionWhileInDebugger(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 724 | } else { |
| 725 | // Perform preemption. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 726 | v8::Unlocker unlocker(reinterpret_cast<v8::Isolate*>(isolate)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | Thread::YieldCPU(); |
| 728 | } |
| 729 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 730 | { // NOLINT |
| 731 | // Perform preemption. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 732 | v8::Unlocker unlocker(reinterpret_cast<v8::Isolate*>(isolate)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 733 | Thread::YieldCPU(); |
| 734 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 735 | #endif |
| 736 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 737 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | |
| 741 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 742 | Object* Execution::DebugBreakHelper() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 743 | Isolate* isolate = Isolate::Current(); |
| 744 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 745 | // Just continue if breaks are disabled. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 746 | if (isolate->debug()->disable_break()) { |
| 747 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 750 | // Ignore debug break during bootstrapping. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 751 | if (isolate->bootstrapper()->IsActive()) { |
| 752 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 755 | { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 756 | JavaScriptFrameIterator it(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 757 | ASSERT(!it.done()); |
| 758 | Object* fun = it.frame()->function(); |
| 759 | if (fun && fun->IsJSFunction()) { |
| 760 | // Don't stop in builtin functions. |
| 761 | if (JSFunction::cast(fun)->IsBuiltin()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 762 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 763 | } |
| 764 | GlobalObject* global = JSFunction::cast(fun)->context()->global(); |
| 765 | // Don't stop in debugger functions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 766 | if (isolate->debug()->IsDebugGlobal(global)) { |
| 767 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Collect the break state before clearing the flags. |
| 773 | bool debug_command_only = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 774 | isolate->stack_guard()->IsDebugCommand() && |
| 775 | !isolate->stack_guard()->IsDebugBreak(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 776 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 777 | // Clear the debug break request flag. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 778 | isolate->stack_guard()->Continue(DEBUGBREAK); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 779 | |
| 780 | ProcessDebugMesssages(debug_command_only); |
| 781 | |
| 782 | // Return to continue execution. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 783 | return isolate->heap()->undefined_value(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void Execution::ProcessDebugMesssages(bool debug_command_only) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 787 | Isolate* isolate = Isolate::Current(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 788 | // Clear the debug command request flag. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 789 | isolate->stack_guard()->Continue(DEBUGCOMMAND); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 790 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 791 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 792 | // Enter the debugger. Just continue if we fail to enter the debugger. |
| 793 | EnterDebugger debugger; |
| 794 | if (debugger.FailedToEnter()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 795 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | // Notify the debug event listeners. Indicate auto continue if the break was |
| 799 | // a debug command break. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 800 | isolate->debugger()->OnDebugBreak(isolate->factory()->undefined_value(), |
| 801 | debug_command_only); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 802 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 803 | |
| 804 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 805 | #endif |
| 806 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 807 | MaybeObject* Execution::HandleStackGuardInterrupt() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 808 | Isolate* isolate = Isolate::Current(); |
| 809 | StackGuard* stack_guard = isolate->stack_guard(); |
| 810 | isolate->counters()->stack_interrupts()->Increment(); |
| 811 | if (stack_guard->IsRuntimeProfilerTick()) { |
| 812 | isolate->counters()->runtime_profiler_ticks()->Increment(); |
| 813 | stack_guard->Continue(RUNTIME_PROFILER_TICK); |
| 814 | isolate->runtime_profiler()->OptimizeNow(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 815 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 816 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 817 | if (stack_guard->IsDebugBreak() || stack_guard->IsDebugCommand()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 818 | DebugBreakHelper(); |
| 819 | } |
| 820 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 821 | if (stack_guard->IsPreempted()) RuntimePreempt(); |
| 822 | if (stack_guard->IsTerminateExecution()) { |
| 823 | stack_guard->Continue(TERMINATE); |
| 824 | return isolate->TerminateExecution(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 825 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 826 | if (stack_guard->IsInterrupted()) { |
| 827 | stack_guard->Continue(INTERRUPT); |
| 828 | return isolate->StackOverflow(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 829 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 830 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 833 | } } // namespace v8::internal |