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