blob: 98c8b680053fa78dbffe97ae1628b1232e0892c3 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "v8.h"
31
32#include "api.h"
Leon Clarkee46be812010-01-19 14:06:41 +000033#include "bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "codegen-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "debug.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010036#include "runtime-profiler.h"
Steve Blockd0582a62009-12-15 09:54:21 +000037#include "simulator.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038#include "v8threads.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010039#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41namespace v8 {
42namespace internal {
43
44
Steve Block44f0eee2011-05-26 01:26:41 +010045StackGuard::StackGuard()
46 : isolate_(NULL) {
47}
48
49
50void 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
60void 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 Blocka7e24c12009-10-30 11:49:00 +000068static 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 Block44f0eee2011-05-26 01:26:41 +010074 Isolate* isolate = func->GetIsolate();
75
Steve Blocka7e24c12009-10-30 11:49:00 +000076 // Entering JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +010077 VMState state(isolate, JS);
Steve Blocka7e24c12009-10-30 11:49:00 +000078
79 // Placeholder for return value.
John Reck59135872010-11-02 12:39:01 -070080 MaybeObject* value = reinterpret_cast<Object*>(kZapValue);
Steve Blocka7e24c12009-10-30 11:49:00 +000081
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 Clarkee46be812010-01-19 14:06:41 +0000106 // 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 Blocka7e24c12009-10-30 11:49:00 +0000110 {
111 // Save and restore context around invocation and block the
112 // allocation of handles without explicit handle scopes.
Steve Block44f0eee2011-05-26 01:26:41 +0100113 SaveContext save(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 NoHandleAllocation na;
115 JSEntryFunction entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
116
117 // Call the function through the right JS entry stub.
Andrei Popescu402d9372010-02-26 13:31:12 +0000118 byte* entry_address = func->code()->entry();
Steve Block3ce2e202009-11-05 08:53:23 +0000119 JSFunction* function = *func;
120 Object* receiver_pointer = *receiver;
121 value = CALL_GENERATED_CODE(entry, entry_address, function,
122 receiver_pointer, argc, args);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 }
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 Block44f0eee2011-05-26 01:26:41 +0100131 ASSERT(*has_pending_exception == Isolate::Current()->has_pending_exception());
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 if (*has_pending_exception) {
Steve Block44f0eee2011-05-26 01:26:41 +0100133 isolate->ReportPendingMessages();
134 if (isolate->pending_exception() == Failure::OutOfMemoryException()) {
135 if (!isolate->handle_scope_implementer()->ignore_out_of_memory()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100136 V8::FatalProcessOutOfMemory("JS", true);
137 }
138 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 return Handle<Object>();
140 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100141 isolate->clear_pending_message();
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 }
143
Steve Block44f0eee2011-05-26 01:26:41 +0100144 return Handle<Object>(value->ToObjectUnchecked(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000145}
146
147
148Handle<Object> Execution::Call(Handle<JSFunction> func,
149 Handle<Object> receiver,
150 int argc,
151 Object*** args,
152 bool* pending_exception) {
153 return Invoke(false, func, receiver, argc, args, pending_exception);
154}
155
156
157Handle<Object> Execution::New(Handle<JSFunction> func, int argc,
158 Object*** args, bool* pending_exception) {
Steve Block44f0eee2011-05-26 01:26:41 +0100159 return Invoke(true, func, Isolate::Current()->global(), argc, args,
160 pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000161}
162
163
164Handle<Object> Execution::TryCall(Handle<JSFunction> func,
165 Handle<Object> receiver,
166 int argc,
167 Object*** args,
168 bool* caught_exception) {
169 // Enter a try-block while executing the JavaScript code. To avoid
170 // duplicate error printing it must be non-verbose. Also, to avoid
171 // creating message objects during stack overflow we shouldn't
172 // capture messages.
173 v8::TryCatch catcher;
174 catcher.SetVerbose(false);
175 catcher.SetCaptureMessage(false);
176
177 Handle<Object> result = Invoke(false, func, receiver, argc, args,
178 caught_exception);
179
180 if (*caught_exception) {
181 ASSERT(catcher.HasCaught());
Steve Block44f0eee2011-05-26 01:26:41 +0100182 Isolate* isolate = Isolate::Current();
183 ASSERT(isolate->has_pending_exception());
184 ASSERT(isolate->external_caught_exception());
185 if (isolate->pending_exception() ==
186 isolate->heap()->termination_exception()) {
187 result = isolate->factory()->termination_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 } else {
189 result = v8::Utils::OpenHandle(*catcher.Exception());
190 }
Steve Block44f0eee2011-05-26 01:26:41 +0100191 isolate->OptionalRescheduleException(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 }
193
Steve Block44f0eee2011-05-26 01:26:41 +0100194 ASSERT(!Isolate::Current()->has_pending_exception());
195 ASSERT(!Isolate::Current()->external_caught_exception());
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 return result;
197}
198
199
200Handle<Object> Execution::GetFunctionDelegate(Handle<Object> object) {
201 ASSERT(!object->IsJSFunction());
202
203 // If you return a function from here, it will be called when an
204 // attempt is made to call the given object as a function.
205
206 // Regular expressions can be called as functions in both Firefox
207 // and Safari so we allow it too.
208 if (object->IsJSRegExp()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100209 Handle<String> exec = FACTORY->exec_symbol();
John Reck59135872010-11-02 12:39:01 -0700210 // TODO(lrn): Bug 617. We should use the default function here, not the
211 // one on the RegExp object.
212 Object* exec_function;
213 { MaybeObject* maybe_exec_function = object->GetProperty(*exec);
214 // This can lose an exception, but the alternative is to put a failure
215 // object in a handle, which is not GC safe.
216 if (!maybe_exec_function->ToObject(&exec_function)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100217 return FACTORY->undefined_value();
John Reck59135872010-11-02 12:39:01 -0700218 }
219 }
220 return Handle<Object>(exec_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000221 }
222
223 // Objects created through the API can have an instance-call handler
224 // that should be used when calling the object as a function.
225 if (object->IsHeapObject() &&
226 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
227 return Handle<JSFunction>(
Steve Block44f0eee2011-05-26 01:26:41 +0100228 Isolate::Current()->global_context()->call_as_function_delegate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 }
230
Steve Block44f0eee2011-05-26 01:26:41 +0100231 return FACTORY->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000232}
233
234
235Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) {
236 ASSERT(!object->IsJSFunction());
237
238 // If you return a function from here, it will be called when an
239 // attempt is made to call the given object as a constructor.
240
241 // Objects created through the API can have an instance-call handler
242 // that should be used when calling the object as a function.
243 if (object->IsHeapObject() &&
244 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
245 return Handle<JSFunction>(
Steve Block44f0eee2011-05-26 01:26:41 +0100246 Isolate::Current()->global_context()->call_as_constructor_delegate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000247 }
248
Steve Block44f0eee2011-05-26 01:26:41 +0100249 return FACTORY->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000250}
251
252
Steve Blocka7e24c12009-10-30 11:49:00 +0000253bool StackGuard::IsStackOverflow() {
Steve Block44f0eee2011-05-26 01:26:41 +0100254 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 return (thread_local_.jslimit_ != kInterruptLimit &&
256 thread_local_.climit_ != kInterruptLimit);
257}
258
259
260void StackGuard::EnableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100261 ExecutionAccess access(isolate_);
Steve Block6ded16b2010-05-10 14:33:55 +0100262 if (has_pending_interrupts(access)) {
263 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000264 }
265}
266
267
268void StackGuard::SetStackLimit(uintptr_t limit) {
Steve Block44f0eee2011-05-26 01:26:41 +0100269 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 // If the current limits are special (eg due to a pending interrupt) then
271 // leave them alone.
272 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000273 if (thread_local_.jslimit_ == thread_local_.real_jslimit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 thread_local_.jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 }
Steve Blockd0582a62009-12-15 09:54:21 +0000276 if (thread_local_.climit_ == thread_local_.real_climit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 thread_local_.climit_ = limit;
278 }
Steve Blockd0582a62009-12-15 09:54:21 +0000279 thread_local_.real_climit_ = limit;
280 thread_local_.real_jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000281}
282
283
284void StackGuard::DisableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100285 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 reset_limits(access);
287}
288
289
Steve Blocka7e24c12009-10-30 11:49:00 +0000290bool StackGuard::IsInterrupted() {
Steve Block44f0eee2011-05-26 01:26:41 +0100291 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 return thread_local_.interrupt_flags_ & INTERRUPT;
293}
294
295
296void StackGuard::Interrupt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100297 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 thread_local_.interrupt_flags_ |= INTERRUPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100299 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000300}
301
302
303bool StackGuard::IsPreempted() {
Steve Block44f0eee2011-05-26 01:26:41 +0100304 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 return thread_local_.interrupt_flags_ & PREEMPT;
306}
307
308
309void StackGuard::Preempt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100310 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 thread_local_.interrupt_flags_ |= PREEMPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100312 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000313}
314
315
316bool StackGuard::IsTerminateExecution() {
Steve Block44f0eee2011-05-26 01:26:41 +0100317 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 return thread_local_.interrupt_flags_ & TERMINATE;
319}
320
321
322void StackGuard::TerminateExecution() {
Steve Block44f0eee2011-05-26 01:26:41 +0100323 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 thread_local_.interrupt_flags_ |= TERMINATE;
Steve Block6ded16b2010-05-10 14:33:55 +0100325 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326}
327
328
Ben Murdochb0fe1622011-05-05 13:52:32 +0100329bool StackGuard::IsRuntimeProfilerTick() {
Steve Block44f0eee2011-05-26 01:26:41 +0100330 ExecutionAccess access(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100331 return thread_local_.interrupt_flags_ & RUNTIME_PROFILER_TICK;
332}
333
334
335void StackGuard::RequestRuntimeProfilerTick() {
336 // Ignore calls if we're not optimizing or if we can't get the lock.
Steve Block44f0eee2011-05-26 01:26:41 +0100337 if (FLAG_opt && ExecutionAccess::TryLock(isolate_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100338 thread_local_.interrupt_flags_ |= RUNTIME_PROFILER_TICK;
339 if (thread_local_.postpone_interrupts_nesting_ == 0) {
340 thread_local_.jslimit_ = thread_local_.climit_ = kInterruptLimit;
Steve Block44f0eee2011-05-26 01:26:41 +0100341 isolate_->heap()->SetStackLimits();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100342 }
Steve Block44f0eee2011-05-26 01:26:41 +0100343 ExecutionAccess::Unlock(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100344 }
345}
346
347
Steve Blocka7e24c12009-10-30 11:49:00 +0000348#ifdef ENABLE_DEBUGGER_SUPPORT
349bool StackGuard::IsDebugBreak() {
Steve Block44f0eee2011-05-26 01:26:41 +0100350 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 return thread_local_.interrupt_flags_ & DEBUGBREAK;
352}
353
354
355void StackGuard::DebugBreak() {
Steve Block44f0eee2011-05-26 01:26:41 +0100356 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 thread_local_.interrupt_flags_ |= DEBUGBREAK;
Steve Block6ded16b2010-05-10 14:33:55 +0100358 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000359}
360
361
362bool StackGuard::IsDebugCommand() {
Steve Block44f0eee2011-05-26 01:26:41 +0100363 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 return thread_local_.interrupt_flags_ & DEBUGCOMMAND;
365}
366
367
368void StackGuard::DebugCommand() {
369 if (FLAG_debugger_auto_break) {
Steve Block44f0eee2011-05-26 01:26:41 +0100370 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 thread_local_.interrupt_flags_ |= DEBUGCOMMAND;
Steve Block6ded16b2010-05-10 14:33:55 +0100372 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 }
374}
375#endif
376
377void StackGuard::Continue(InterruptFlag after_what) {
Steve Block44f0eee2011-05-26 01:26:41 +0100378 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000379 thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what);
Steve Block6ded16b2010-05-10 14:33:55 +0100380 if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 reset_limits(access);
382 }
383}
384
385
Steve Blocka7e24c12009-10-30 11:49:00 +0000386char* StackGuard::ArchiveStackGuard(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +0100387 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
389 ThreadLocal blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100390
391 // Set the stack limits using the old thread_local_.
392 // TODO(isolates): This was the old semantics of constructing a ThreadLocal
393 // (as the ctor called SetStackLimits, which looked at the
394 // current thread_local_ from StackGuard)-- but is this
395 // really what was intended?
396 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 thread_local_ = blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100398
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 return to + sizeof(ThreadLocal);
400}
401
402
403char* StackGuard::RestoreStackGuard(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +0100404 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
Steve Block44f0eee2011-05-26 01:26:41 +0100406 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 return from + sizeof(ThreadLocal);
408}
409
410
Steve Blocka7e24c12009-10-30 11:49:00 +0000411void StackGuard::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +0100412 Isolate::CurrentPerIsolateThreadData()->set_stack_limit(
413 thread_local_.real_climit_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000414}
415
416
417void StackGuard::ThreadLocal::Clear() {
Steve Blockd0582a62009-12-15 09:54:21 +0000418 real_jslimit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000419 jslimit_ = kIllegalLimit;
Steve Blockd0582a62009-12-15 09:54:21 +0000420 real_climit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000421 climit_ = kIllegalLimit;
422 nesting_ = 0;
423 postpone_interrupts_nesting_ = 0;
424 interrupt_flags_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000425}
426
427
Steve Block44f0eee2011-05-26 01:26:41 +0100428bool StackGuard::ThreadLocal::Initialize() {
429 bool should_set_stack_limits = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000430 if (real_climit_ == kIllegalLimit) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 // Takes the address of the limit variable in order to find out where
432 // the top of stack is right now.
Steve Block1e0659c2011-05-24 12:43:12 +0100433 const uintptr_t kLimitSize = FLAG_stack_size * KB;
Steve Block3ce2e202009-11-05 08:53:23 +0000434 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize;
435 ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize);
Steve Blockd0582a62009-12-15 09:54:21 +0000436 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437 jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000438 real_climit_ = limit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 climit_ = limit;
Steve Block44f0eee2011-05-26 01:26:41 +0100440 should_set_stack_limits = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 }
442 nesting_ = 0;
443 postpone_interrupts_nesting_ = 0;
444 interrupt_flags_ = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100445 return should_set_stack_limits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000446}
447
448
449void StackGuard::ClearThread(const ExecutionAccess& lock) {
450 thread_local_.Clear();
Steve Block44f0eee2011-05-26 01:26:41 +0100451 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000452}
453
454
455void StackGuard::InitThread(const ExecutionAccess& lock) {
Steve Block44f0eee2011-05-26 01:26:41 +0100456 if (thread_local_.Initialize()) isolate_->heap()->SetStackLimits();
457 uintptr_t stored_limit =
458 Isolate::CurrentPerIsolateThreadData()->stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 // You should hold the ExecutionAccess lock when you call this.
Steve Block44f0eee2011-05-26 01:26:41 +0100460 if (stored_limit != 0) {
461 StackGuard::SetStackLimit(stored_limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 }
463}
464
465
466// --- C a l l s t o n a t i v e s ---
467
Steve Block44f0eee2011-05-26 01:26:41 +0100468#define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \
469 do { \
470 Object** args[argc] = argv; \
471 ASSERT(has_pending_exception != NULL); \
472 return Call(Isolate::Current()->name##_fun(), \
473 Isolate::Current()->js_builtins_object(), argc, args, \
474 has_pending_exception); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 } while (false)
476
477
478Handle<Object> Execution::ToBoolean(Handle<Object> obj) {
479 // See the similar code in runtime.js:ToBoolean.
480 if (obj->IsBoolean()) return obj;
481 bool result = true;
482 if (obj->IsString()) {
483 result = Handle<String>::cast(obj)->length() != 0;
484 } else if (obj->IsNull() || obj->IsUndefined()) {
485 result = false;
486 } else if (obj->IsNumber()) {
487 double value = obj->Number();
488 result = !((value == 0) || isnan(value));
489 }
Steve Block44f0eee2011-05-26 01:26:41 +0100490 return Handle<Object>(HEAP->ToBoolean(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000491}
492
493
494Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) {
495 RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc);
496}
497
498
499Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) {
500 RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc);
501}
502
503
504Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) {
505 RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc);
506}
507
508
509Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) {
510 if (obj->IsJSObject()) return obj;
511 RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc);
512}
513
514
515Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) {
516 RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc);
517}
518
519
520Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) {
521 RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc);
522}
523
524
525Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) {
526 RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc);
527}
528
529
530Handle<Object> Execution::NewDate(double time, bool* exc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100531 Handle<Object> time_obj = FACTORY->NewNumber(time);
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc);
533}
534
535
536#undef RETURN_NATIVE_CALL
537
538
Ben Murdochf87a2032010-10-22 12:50:53 +0100539Handle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern,
540 Handle<String> flags,
541 bool* exc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100542 Handle<JSFunction> function = Handle<JSFunction>(
543 pattern->GetIsolate()->global_context()->regexp_function());
Ben Murdochf87a2032010-10-22 12:50:53 +0100544 Handle<Object> re_obj = RegExpImpl::CreateRegExpLiteral(
Steve Block44f0eee2011-05-26 01:26:41 +0100545 function, pattern, flags, exc);
Ben Murdochf87a2032010-10-22 12:50:53 +0100546 if (*exc) return Handle<JSRegExp>();
547 return Handle<JSRegExp>::cast(re_obj);
548}
549
550
Steve Blocka7e24c12009-10-30 11:49:00 +0000551Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
552 int int_index = static_cast<int>(index);
553 if (int_index < 0 || int_index >= string->length()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100554 return FACTORY->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000555 }
556
557 Handle<Object> char_at =
Steve Block44f0eee2011-05-26 01:26:41 +0100558 GetProperty(Isolate::Current()->js_builtins_object(),
559 FACTORY->char_at_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000560 if (!char_at->IsJSFunction()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100561 return FACTORY->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000562 }
563
564 bool caught_exception;
Steve Block44f0eee2011-05-26 01:26:41 +0100565 Handle<Object> index_object = FACTORY->NewNumberFromInt(int_index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 Object** index_arg[] = { index_object.location() };
567 Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
568 string,
569 ARRAY_SIZE(index_arg),
570 index_arg,
571 &caught_exception);
572 if (caught_exception) {
Steve Block44f0eee2011-05-26 01:26:41 +0100573 return FACTORY->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 }
575 return result;
576}
577
578
579Handle<JSFunction> Execution::InstantiateFunction(
580 Handle<FunctionTemplateInfo> data, bool* exc) {
581 // Fast case: see if the function has already been instantiated
582 int serial_number = Smi::cast(data->serial_number())->value();
Steve Block44f0eee2011-05-26 01:26:41 +0100583 Object* elm =
584 Isolate::Current()->global_context()->function_cache()->
585 GetElementNoExceptionThrown(serial_number);
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
587 // The function has not yet been instantiated in this context; do it.
588 Object** args[1] = { Handle<Object>::cast(data).location() };
589 Handle<Object> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100590 Call(Isolate::Current()->instantiate_fun(),
591 Isolate::Current()->js_builtins_object(), 1, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000592 if (*exc) return Handle<JSFunction>::null();
593 return Handle<JSFunction>::cast(result);
594}
595
596
597Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
598 bool* exc) {
599 if (data->property_list()->IsUndefined() &&
600 !data->constructor()->IsUndefined()) {
601 // Initialization to make gcc happy.
602 Object* result = NULL;
603 {
604 HandleScope scope;
605 Handle<FunctionTemplateInfo> cons_template =
606 Handle<FunctionTemplateInfo>(
607 FunctionTemplateInfo::cast(data->constructor()));
608 Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
609 if (*exc) return Handle<JSObject>::null();
610 Handle<Object> value = New(cons, 0, NULL, exc);
611 if (*exc) return Handle<JSObject>::null();
612 result = *value;
613 }
614 ASSERT(!*exc);
615 return Handle<JSObject>(JSObject::cast(result));
616 } else {
617 Object** args[1] = { Handle<Object>::cast(data).location() };
618 Handle<Object> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100619 Call(Isolate::Current()->instantiate_fun(),
620 Isolate::Current()->js_builtins_object(), 1, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 if (*exc) return Handle<JSObject>::null();
622 return Handle<JSObject>::cast(result);
623 }
624}
625
626
627void Execution::ConfigureInstance(Handle<Object> instance,
628 Handle<Object> instance_template,
629 bool* exc) {
630 Object** args[2] = { instance.location(), instance_template.location() };
Steve Block44f0eee2011-05-26 01:26:41 +0100631 Execution::Call(Isolate::Current()->configure_instance_fun(),
632 Isolate::Current()->js_builtins_object(), 2, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000633}
634
635
636Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
637 Handle<JSFunction> fun,
638 Handle<Object> pos,
639 Handle<Object> is_global) {
640 const int argc = 4;
641 Object** args[argc] = { recv.location(),
642 Handle<Object>::cast(fun).location(),
643 pos.location(),
644 is_global.location() };
645 bool caught_exception = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100646 Handle<Object> result =
647 TryCall(Isolate::Current()->get_stack_trace_line_fun(),
648 Isolate::Current()->js_builtins_object(), argc, args,
649 &caught_exception);
650 if (caught_exception || !result->IsString()) return FACTORY->empty_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 return Handle<String>::cast(result);
652}
653
654
655static Object* RuntimePreempt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100656 Isolate* isolate = Isolate::Current();
657
Steve Blocka7e24c12009-10-30 11:49:00 +0000658 // Clear the preempt request flag.
Steve Block44f0eee2011-05-26 01:26:41 +0100659 isolate->stack_guard()->Continue(PREEMPT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000660
661 ContextSwitcher::PreemptionReceived();
662
663#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100664 if (isolate->debug()->InDebugger()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000665 // If currently in the debugger don't do any actual preemption but record
666 // that preemption occoured while in the debugger.
Steve Block44f0eee2011-05-26 01:26:41 +0100667 isolate->debug()->PreemptionWhileInDebugger();
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 } else {
669 // Perform preemption.
670 v8::Unlocker unlocker;
671 Thread::YieldCPU();
672 }
673#else
Steve Block44f0eee2011-05-26 01:26:41 +0100674 { // NOLINT
675 // Perform preemption.
676 v8::Unlocker unlocker;
677 Thread::YieldCPU();
678 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000679#endif
680
Steve Block44f0eee2011-05-26 01:26:41 +0100681 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000682}
683
684
685#ifdef ENABLE_DEBUGGER_SUPPORT
686Object* Execution::DebugBreakHelper() {
Steve Block44f0eee2011-05-26 01:26:41 +0100687 Isolate* isolate = Isolate::Current();
688
Steve Blocka7e24c12009-10-30 11:49:00 +0000689 // Just continue if breaks are disabled.
Steve Block44f0eee2011-05-26 01:26:41 +0100690 if (isolate->debug()->disable_break()) {
691 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000692 }
693
Leon Clarkee46be812010-01-19 14:06:41 +0000694 // Ignore debug break during bootstrapping.
Steve Block44f0eee2011-05-26 01:26:41 +0100695 if (isolate->bootstrapper()->IsActive()) {
696 return isolate->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +0000697 }
698
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 {
700 JavaScriptFrameIterator it;
701 ASSERT(!it.done());
702 Object* fun = it.frame()->function();
703 if (fun && fun->IsJSFunction()) {
704 // Don't stop in builtin functions.
705 if (JSFunction::cast(fun)->IsBuiltin()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100706 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000707 }
708 GlobalObject* global = JSFunction::cast(fun)->context()->global();
709 // Don't stop in debugger functions.
Steve Block44f0eee2011-05-26 01:26:41 +0100710 if (isolate->debug()->IsDebugGlobal(global)) {
711 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 }
713 }
714 }
715
716 // Collect the break state before clearing the flags.
717 bool debug_command_only =
Steve Block44f0eee2011-05-26 01:26:41 +0100718 isolate->stack_guard()->IsDebugCommand() &&
719 !isolate->stack_guard()->IsDebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000720
Leon Clarkee46be812010-01-19 14:06:41 +0000721 // Clear the debug break request flag.
Steve Block44f0eee2011-05-26 01:26:41 +0100722 isolate->stack_guard()->Continue(DEBUGBREAK);
Leon Clarkee46be812010-01-19 14:06:41 +0000723
724 ProcessDebugMesssages(debug_command_only);
725
726 // Return to continue execution.
Steve Block44f0eee2011-05-26 01:26:41 +0100727 return isolate->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +0000728}
729
730void Execution::ProcessDebugMesssages(bool debug_command_only) {
731 // Clear the debug command request flag.
Steve Block44f0eee2011-05-26 01:26:41 +0100732 Isolate::Current()->stack_guard()->Continue(DEBUGCOMMAND);
Steve Blocka7e24c12009-10-30 11:49:00 +0000733
734 HandleScope scope;
735 // Enter the debugger. Just continue if we fail to enter the debugger.
736 EnterDebugger debugger;
737 if (debugger.FailedToEnter()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000738 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 }
740
741 // Notify the debug event listeners. Indicate auto continue if the break was
742 // a debug command break.
Steve Block44f0eee2011-05-26 01:26:41 +0100743 Isolate::Current()->debugger()->OnDebugBreak(FACTORY->undefined_value(),
744 debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000745}
Leon Clarkee46be812010-01-19 14:06:41 +0000746
747
Steve Blocka7e24c12009-10-30 11:49:00 +0000748#endif
749
John Reck59135872010-11-02 12:39:01 -0700750MaybeObject* Execution::HandleStackGuardInterrupt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100751 Isolate* isolate = Isolate::Current();
752 StackGuard* stack_guard = isolate->stack_guard();
753 isolate->counters()->stack_interrupts()->Increment();
754 if (stack_guard->IsRuntimeProfilerTick()) {
755 isolate->counters()->runtime_profiler_ticks()->Increment();
756 stack_guard->Continue(RUNTIME_PROFILER_TICK);
757 isolate->runtime_profiler()->OptimizeNow();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100758 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000759#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100760 if (stack_guard->IsDebugBreak() || stack_guard->IsDebugCommand()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 DebugBreakHelper();
762 }
763#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100764 if (stack_guard->IsPreempted()) RuntimePreempt();
765 if (stack_guard->IsTerminateExecution()) {
766 stack_guard->Continue(TERMINATE);
767 return isolate->TerminateExecution();
Steve Blocka7e24c12009-10-30 11:49:00 +0000768 }
Steve Block44f0eee2011-05-26 01:26:41 +0100769 if (stack_guard->IsInterrupted()) {
770 stack_guard->Continue(INTERRUPT);
771 return isolate->StackOverflow();
Steve Blocka7e24c12009-10-30 11:49:00 +0000772 }
Steve Block44f0eee2011-05-26 01:26:41 +0100773 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000774}
775
Steve Blocka7e24c12009-10-30 11:49:00 +0000776} } // namespace v8::internal