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