blob: eb26438fe87af6a1d264b3b9ad32ad0efab6e8e8 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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"
Ben Murdoch8b112d22011-06-08 16:22:53 +010034#include "codegen.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());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100202 Isolate* isolate = Isolate::Current();
203 Factory* factory = isolate->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000204
205 // If you return a function from here, it will be called when an
206 // attempt is made to call the given object as a function.
207
208 // Regular expressions can be called as functions in both Firefox
209 // and Safari so we allow it too.
210 if (object->IsJSRegExp()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100211 Handle<String> exec = factory->exec_symbol();
John Reck59135872010-11-02 12:39:01 -0700212 // TODO(lrn): Bug 617. We should use the default function here, not the
213 // one on the RegExp object.
214 Object* exec_function;
215 { MaybeObject* maybe_exec_function = object->GetProperty(*exec);
216 // This can lose an exception, but the alternative is to put a failure
217 // object in a handle, which is not GC safe.
218 if (!maybe_exec_function->ToObject(&exec_function)) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100219 return factory->undefined_value();
John Reck59135872010-11-02 12:39:01 -0700220 }
221 }
222 return Handle<Object>(exec_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 }
224
225 // Objects created through the API can have an instance-call handler
226 // that should be used when calling the object as a function.
227 if (object->IsHeapObject() &&
228 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
229 return Handle<JSFunction>(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100230 isolate->global_context()->call_as_function_delegate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 }
232
Ben Murdoch8b112d22011-06-08 16:22:53 +0100233 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000234}
235
236
237Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) {
238 ASSERT(!object->IsJSFunction());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100239 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000240
241 // If you return a function from here, it will be called when an
242 // attempt is made to call the given object as a constructor.
243
244 // Objects created through the API can have an instance-call handler
245 // that should be used when calling the object as a function.
246 if (object->IsHeapObject() &&
247 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
248 return Handle<JSFunction>(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100249 isolate->global_context()->call_as_constructor_delegate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 }
251
Ben Murdoch8b112d22011-06-08 16:22:53 +0100252 return isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000253}
254
255
Steve Blocka7e24c12009-10-30 11:49:00 +0000256bool StackGuard::IsStackOverflow() {
Steve Block44f0eee2011-05-26 01:26:41 +0100257 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 return (thread_local_.jslimit_ != kInterruptLimit &&
259 thread_local_.climit_ != kInterruptLimit);
260}
261
262
263void StackGuard::EnableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100264 ExecutionAccess access(isolate_);
Steve Block6ded16b2010-05-10 14:33:55 +0100265 if (has_pending_interrupts(access)) {
266 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 }
268}
269
270
271void StackGuard::SetStackLimit(uintptr_t limit) {
Steve Block44f0eee2011-05-26 01:26:41 +0100272 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000273 // If the current limits are special (eg due to a pending interrupt) then
274 // leave them alone.
275 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000276 if (thread_local_.jslimit_ == thread_local_.real_jslimit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 thread_local_.jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 }
Steve Blockd0582a62009-12-15 09:54:21 +0000279 if (thread_local_.climit_ == thread_local_.real_climit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 thread_local_.climit_ = limit;
281 }
Steve Blockd0582a62009-12-15 09:54:21 +0000282 thread_local_.real_climit_ = limit;
283 thread_local_.real_jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000284}
285
286
287void StackGuard::DisableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100288 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 reset_limits(access);
290}
291
292
Steve Blocka7e24c12009-10-30 11:49:00 +0000293bool StackGuard::IsInterrupted() {
Steve Block44f0eee2011-05-26 01:26:41 +0100294 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 return thread_local_.interrupt_flags_ & INTERRUPT;
296}
297
298
299void StackGuard::Interrupt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100300 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000301 thread_local_.interrupt_flags_ |= INTERRUPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100302 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000303}
304
305
306bool StackGuard::IsPreempted() {
Steve Block44f0eee2011-05-26 01:26:41 +0100307 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 return thread_local_.interrupt_flags_ & PREEMPT;
309}
310
311
312void StackGuard::Preempt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100313 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000314 thread_local_.interrupt_flags_ |= PREEMPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100315 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000316}
317
318
319bool StackGuard::IsTerminateExecution() {
Steve Block44f0eee2011-05-26 01:26:41 +0100320 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 return thread_local_.interrupt_flags_ & TERMINATE;
322}
323
324
325void StackGuard::TerminateExecution() {
Steve Block44f0eee2011-05-26 01:26:41 +0100326 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 thread_local_.interrupt_flags_ |= TERMINATE;
Steve Block6ded16b2010-05-10 14:33:55 +0100328 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000329}
330
331
Ben Murdochb0fe1622011-05-05 13:52:32 +0100332bool StackGuard::IsRuntimeProfilerTick() {
Steve Block44f0eee2011-05-26 01:26:41 +0100333 ExecutionAccess access(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100334 return thread_local_.interrupt_flags_ & RUNTIME_PROFILER_TICK;
335}
336
337
338void StackGuard::RequestRuntimeProfilerTick() {
339 // Ignore calls if we're not optimizing or if we can't get the lock.
Steve Block44f0eee2011-05-26 01:26:41 +0100340 if (FLAG_opt && ExecutionAccess::TryLock(isolate_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100341 thread_local_.interrupt_flags_ |= RUNTIME_PROFILER_TICK;
342 if (thread_local_.postpone_interrupts_nesting_ == 0) {
343 thread_local_.jslimit_ = thread_local_.climit_ = kInterruptLimit;
Steve Block44f0eee2011-05-26 01:26:41 +0100344 isolate_->heap()->SetStackLimits();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100345 }
Steve Block44f0eee2011-05-26 01:26:41 +0100346 ExecutionAccess::Unlock(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100347 }
348}
349
350
Steve Blocka7e24c12009-10-30 11:49:00 +0000351#ifdef ENABLE_DEBUGGER_SUPPORT
352bool StackGuard::IsDebugBreak() {
Steve Block44f0eee2011-05-26 01:26:41 +0100353 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 return thread_local_.interrupt_flags_ & DEBUGBREAK;
355}
356
357
358void StackGuard::DebugBreak() {
Steve Block44f0eee2011-05-26 01:26:41 +0100359 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 thread_local_.interrupt_flags_ |= DEBUGBREAK;
Steve Block6ded16b2010-05-10 14:33:55 +0100361 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000362}
363
364
365bool StackGuard::IsDebugCommand() {
Steve Block44f0eee2011-05-26 01:26:41 +0100366 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 return thread_local_.interrupt_flags_ & DEBUGCOMMAND;
368}
369
370
371void StackGuard::DebugCommand() {
372 if (FLAG_debugger_auto_break) {
Steve Block44f0eee2011-05-26 01:26:41 +0100373 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 thread_local_.interrupt_flags_ |= DEBUGCOMMAND;
Steve Block6ded16b2010-05-10 14:33:55 +0100375 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000376 }
377}
378#endif
379
380void StackGuard::Continue(InterruptFlag after_what) {
Steve Block44f0eee2011-05-26 01:26:41 +0100381 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what);
Steve Block6ded16b2010-05-10 14:33:55 +0100383 if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 reset_limits(access);
385 }
386}
387
388
Steve Blocka7e24c12009-10-30 11:49:00 +0000389char* StackGuard::ArchiveStackGuard(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +0100390 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
392 ThreadLocal blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100393
394 // Set the stack limits using the old thread_local_.
395 // TODO(isolates): This was the old semantics of constructing a ThreadLocal
396 // (as the ctor called SetStackLimits, which looked at the
397 // current thread_local_ from StackGuard)-- but is this
398 // really what was intended?
399 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 thread_local_ = blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100401
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 return to + sizeof(ThreadLocal);
403}
404
405
406char* StackGuard::RestoreStackGuard(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +0100407 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
Steve Block44f0eee2011-05-26 01:26:41 +0100409 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000410 return from + sizeof(ThreadLocal);
411}
412
413
Steve Blocka7e24c12009-10-30 11:49:00 +0000414void StackGuard::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +0100415 Isolate::CurrentPerIsolateThreadData()->set_stack_limit(
416 thread_local_.real_climit_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000417}
418
419
420void StackGuard::ThreadLocal::Clear() {
Steve Blockd0582a62009-12-15 09:54:21 +0000421 real_jslimit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000422 jslimit_ = kIllegalLimit;
Steve Blockd0582a62009-12-15 09:54:21 +0000423 real_climit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000424 climit_ = kIllegalLimit;
425 nesting_ = 0;
426 postpone_interrupts_nesting_ = 0;
427 interrupt_flags_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000428}
429
430
Steve Block44f0eee2011-05-26 01:26:41 +0100431bool StackGuard::ThreadLocal::Initialize() {
432 bool should_set_stack_limits = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000433 if (real_climit_ == kIllegalLimit) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 // Takes the address of the limit variable in order to find out where
435 // the top of stack is right now.
Steve Block1e0659c2011-05-24 12:43:12 +0100436 const uintptr_t kLimitSize = FLAG_stack_size * KB;
Steve Block3ce2e202009-11-05 08:53:23 +0000437 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize;
438 ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize);
Steve Blockd0582a62009-12-15 09:54:21 +0000439 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000440 jslimit_ = SimulatorStack::JsLimitFromCLimit(limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000441 real_climit_ = limit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 climit_ = limit;
Steve Block44f0eee2011-05-26 01:26:41 +0100443 should_set_stack_limits = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 }
445 nesting_ = 0;
446 postpone_interrupts_nesting_ = 0;
447 interrupt_flags_ = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100448 return should_set_stack_limits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000449}
450
451
452void StackGuard::ClearThread(const ExecutionAccess& lock) {
453 thread_local_.Clear();
Steve Block44f0eee2011-05-26 01:26:41 +0100454 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000455}
456
457
458void StackGuard::InitThread(const ExecutionAccess& lock) {
Steve Block44f0eee2011-05-26 01:26:41 +0100459 if (thread_local_.Initialize()) isolate_->heap()->SetStackLimits();
460 uintptr_t stored_limit =
461 Isolate::CurrentPerIsolateThreadData()->stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 // You should hold the ExecutionAccess lock when you call this.
Steve Block44f0eee2011-05-26 01:26:41 +0100463 if (stored_limit != 0) {
464 StackGuard::SetStackLimit(stored_limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 }
466}
467
468
469// --- C a l l s t o n a t i v e s ---
470
Steve Block44f0eee2011-05-26 01:26:41 +0100471#define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \
472 do { \
Ben Murdoch8b112d22011-06-08 16:22:53 +0100473 Isolate* isolate = Isolate::Current(); \
Steve Block44f0eee2011-05-26 01:26:41 +0100474 Object** args[argc] = argv; \
475 ASSERT(has_pending_exception != NULL); \
Ben Murdoch8b112d22011-06-08 16:22:53 +0100476 return Call(isolate->name##_fun(), \
477 isolate->js_builtins_object(), argc, args, \
Steve Block44f0eee2011-05-26 01:26:41 +0100478 has_pending_exception); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 } while (false)
480
481
482Handle<Object> Execution::ToBoolean(Handle<Object> obj) {
483 // See the similar code in runtime.js:ToBoolean.
484 if (obj->IsBoolean()) return obj;
485 bool result = true;
486 if (obj->IsString()) {
487 result = Handle<String>::cast(obj)->length() != 0;
488 } else if (obj->IsNull() || obj->IsUndefined()) {
489 result = false;
490 } else if (obj->IsNumber()) {
491 double value = obj->Number();
492 result = !((value == 0) || isnan(value));
493 }
Steve Block44f0eee2011-05-26 01:26:41 +0100494 return Handle<Object>(HEAP->ToBoolean(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000495}
496
497
498Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) {
499 RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc);
500}
501
502
503Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) {
504 RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc);
505}
506
507
508Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) {
509 RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc);
510}
511
512
513Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) {
514 if (obj->IsJSObject()) return obj;
515 RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc);
516}
517
518
519Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) {
520 RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc);
521}
522
523
524Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) {
525 RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc);
526}
527
528
529Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) {
530 RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc);
531}
532
533
534Handle<Object> Execution::NewDate(double time, bool* exc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100535 Handle<Object> time_obj = FACTORY->NewNumber(time);
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc);
537}
538
539
540#undef RETURN_NATIVE_CALL
541
542
Ben Murdochf87a2032010-10-22 12:50:53 +0100543Handle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern,
544 Handle<String> flags,
545 bool* exc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100546 Handle<JSFunction> function = Handle<JSFunction>(
547 pattern->GetIsolate()->global_context()->regexp_function());
Ben Murdochf87a2032010-10-22 12:50:53 +0100548 Handle<Object> re_obj = RegExpImpl::CreateRegExpLiteral(
Steve Block44f0eee2011-05-26 01:26:41 +0100549 function, pattern, flags, exc);
Ben Murdochf87a2032010-10-22 12:50:53 +0100550 if (*exc) return Handle<JSRegExp>();
551 return Handle<JSRegExp>::cast(re_obj);
552}
553
554
Steve Blocka7e24c12009-10-30 11:49:00 +0000555Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100556 Isolate* isolate = string->GetIsolate();
557 Factory* factory = isolate->factory();
558
Steve Blocka7e24c12009-10-30 11:49:00 +0000559 int int_index = static_cast<int>(index);
560 if (int_index < 0 || int_index >= string->length()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100561 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000562 }
563
564 Handle<Object> char_at =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100565 GetProperty(isolate->js_builtins_object(),
566 factory->char_at_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000567 if (!char_at->IsJSFunction()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100568 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000569 }
570
571 bool caught_exception;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100572 Handle<Object> index_object = factory->NewNumberFromInt(int_index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000573 Object** index_arg[] = { index_object.location() };
574 Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
575 string,
576 ARRAY_SIZE(index_arg),
577 index_arg,
578 &caught_exception);
579 if (caught_exception) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100580 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 }
582 return result;
583}
584
585
586Handle<JSFunction> Execution::InstantiateFunction(
587 Handle<FunctionTemplateInfo> data, bool* exc) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100588 Isolate* isolate = data->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000589 // Fast case: see if the function has already been instantiated
590 int serial_number = Smi::cast(data->serial_number())->value();
Steve Block44f0eee2011-05-26 01:26:41 +0100591 Object* elm =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100592 isolate->global_context()->function_cache()->
Steve Block44f0eee2011-05-26 01:26:41 +0100593 GetElementNoExceptionThrown(serial_number);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
595 // The function has not yet been instantiated in this context; do it.
596 Object** args[1] = { Handle<Object>::cast(data).location() };
597 Handle<Object> result =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100598 Call(isolate->instantiate_fun(),
599 isolate->js_builtins_object(), 1, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000600 if (*exc) return Handle<JSFunction>::null();
601 return Handle<JSFunction>::cast(result);
602}
603
604
605Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
606 bool* exc) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100607 Isolate* isolate = data->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000608 if (data->property_list()->IsUndefined() &&
609 !data->constructor()->IsUndefined()) {
610 // Initialization to make gcc happy.
611 Object* result = NULL;
612 {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100613 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000614 Handle<FunctionTemplateInfo> cons_template =
615 Handle<FunctionTemplateInfo>(
616 FunctionTemplateInfo::cast(data->constructor()));
617 Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
618 if (*exc) return Handle<JSObject>::null();
619 Handle<Object> value = New(cons, 0, NULL, exc);
620 if (*exc) return Handle<JSObject>::null();
621 result = *value;
622 }
623 ASSERT(!*exc);
624 return Handle<JSObject>(JSObject::cast(result));
625 } else {
626 Object** args[1] = { Handle<Object>::cast(data).location() };
627 Handle<Object> result =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100628 Call(isolate->instantiate_fun(),
629 isolate->js_builtins_object(), 1, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 if (*exc) return Handle<JSObject>::null();
631 return Handle<JSObject>::cast(result);
632 }
633}
634
635
636void Execution::ConfigureInstance(Handle<Object> instance,
637 Handle<Object> instance_template,
638 bool* exc) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100639 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000640 Object** args[2] = { instance.location(), instance_template.location() };
Ben Murdoch8b112d22011-06-08 16:22:53 +0100641 Execution::Call(isolate->configure_instance_fun(),
642 isolate->js_builtins_object(), 2, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000643}
644
645
646Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
647 Handle<JSFunction> fun,
648 Handle<Object> pos,
649 Handle<Object> is_global) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100650 Isolate* isolate = fun->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 const int argc = 4;
652 Object** args[argc] = { recv.location(),
653 Handle<Object>::cast(fun).location(),
654 pos.location(),
655 is_global.location() };
656 bool caught_exception = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100657 Handle<Object> result =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100658 TryCall(isolate->get_stack_trace_line_fun(),
659 isolate->js_builtins_object(), argc, args,
Steve Block44f0eee2011-05-26 01:26:41 +0100660 &caught_exception);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100661 if (caught_exception || !result->IsString()) {
662 return isolate->factory()->empty_symbol();
663 }
664
Steve Blocka7e24c12009-10-30 11:49:00 +0000665 return Handle<String>::cast(result);
666}
667
668
669static Object* RuntimePreempt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100670 Isolate* isolate = Isolate::Current();
671
Steve Blocka7e24c12009-10-30 11:49:00 +0000672 // Clear the preempt request flag.
Steve Block44f0eee2011-05-26 01:26:41 +0100673 isolate->stack_guard()->Continue(PREEMPT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000674
675 ContextSwitcher::PreemptionReceived();
676
677#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100678 if (isolate->debug()->InDebugger()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 // If currently in the debugger don't do any actual preemption but record
680 // that preemption occoured while in the debugger.
Steve Block44f0eee2011-05-26 01:26:41 +0100681 isolate->debug()->PreemptionWhileInDebugger();
Steve Blocka7e24c12009-10-30 11:49:00 +0000682 } else {
683 // Perform preemption.
684 v8::Unlocker unlocker;
685 Thread::YieldCPU();
686 }
687#else
Steve Block44f0eee2011-05-26 01:26:41 +0100688 { // NOLINT
689 // Perform preemption.
690 v8::Unlocker unlocker;
691 Thread::YieldCPU();
692 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000693#endif
694
Steve Block44f0eee2011-05-26 01:26:41 +0100695 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000696}
697
698
699#ifdef ENABLE_DEBUGGER_SUPPORT
700Object* Execution::DebugBreakHelper() {
Steve Block44f0eee2011-05-26 01:26:41 +0100701 Isolate* isolate = Isolate::Current();
702
Steve Blocka7e24c12009-10-30 11:49:00 +0000703 // Just continue if breaks are disabled.
Steve Block44f0eee2011-05-26 01:26:41 +0100704 if (isolate->debug()->disable_break()) {
705 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000706 }
707
Leon Clarkee46be812010-01-19 14:06:41 +0000708 // Ignore debug break during bootstrapping.
Steve Block44f0eee2011-05-26 01:26:41 +0100709 if (isolate->bootstrapper()->IsActive()) {
710 return isolate->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +0000711 }
712
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100714 JavaScriptFrameIterator it(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000715 ASSERT(!it.done());
716 Object* fun = it.frame()->function();
717 if (fun && fun->IsJSFunction()) {
718 // Don't stop in builtin functions.
719 if (JSFunction::cast(fun)->IsBuiltin()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100720 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000721 }
722 GlobalObject* global = JSFunction::cast(fun)->context()->global();
723 // Don't stop in debugger functions.
Steve Block44f0eee2011-05-26 01:26:41 +0100724 if (isolate->debug()->IsDebugGlobal(global)) {
725 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000726 }
727 }
728 }
729
730 // Collect the break state before clearing the flags.
731 bool debug_command_only =
Steve Block44f0eee2011-05-26 01:26:41 +0100732 isolate->stack_guard()->IsDebugCommand() &&
733 !isolate->stack_guard()->IsDebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000734
Leon Clarkee46be812010-01-19 14:06:41 +0000735 // Clear the debug break request flag.
Steve Block44f0eee2011-05-26 01:26:41 +0100736 isolate->stack_guard()->Continue(DEBUGBREAK);
Leon Clarkee46be812010-01-19 14:06:41 +0000737
738 ProcessDebugMesssages(debug_command_only);
739
740 // Return to continue execution.
Steve Block44f0eee2011-05-26 01:26:41 +0100741 return isolate->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +0000742}
743
744void Execution::ProcessDebugMesssages(bool debug_command_only) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100745 Isolate* isolate = Isolate::Current();
Leon Clarkee46be812010-01-19 14:06:41 +0000746 // Clear the debug command request flag.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100747 isolate->stack_guard()->Continue(DEBUGCOMMAND);
Steve Blocka7e24c12009-10-30 11:49:00 +0000748
Ben Murdoch8b112d22011-06-08 16:22:53 +0100749 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000750 // Enter the debugger. Just continue if we fail to enter the debugger.
751 EnterDebugger debugger;
752 if (debugger.FailedToEnter()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000753 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000754 }
755
756 // Notify the debug event listeners. Indicate auto continue if the break was
757 // a debug command break.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100758 isolate->debugger()->OnDebugBreak(isolate->factory()->undefined_value(),
759 debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000760}
Leon Clarkee46be812010-01-19 14:06:41 +0000761
762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763#endif
764
John Reck59135872010-11-02 12:39:01 -0700765MaybeObject* Execution::HandleStackGuardInterrupt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100766 Isolate* isolate = Isolate::Current();
767 StackGuard* stack_guard = isolate->stack_guard();
768 isolate->counters()->stack_interrupts()->Increment();
769 if (stack_guard->IsRuntimeProfilerTick()) {
770 isolate->counters()->runtime_profiler_ticks()->Increment();
771 stack_guard->Continue(RUNTIME_PROFILER_TICK);
772 isolate->runtime_profiler()->OptimizeNow();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100773 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000774#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100775 if (stack_guard->IsDebugBreak() || stack_guard->IsDebugCommand()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000776 DebugBreakHelper();
777 }
778#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100779 if (stack_guard->IsPreempted()) RuntimePreempt();
780 if (stack_guard->IsTerminateExecution()) {
781 stack_guard->Continue(TERMINATE);
782 return isolate->TerminateExecution();
Steve Blocka7e24c12009-10-30 11:49:00 +0000783 }
Steve Block44f0eee2011-05-26 01:26:41 +0100784 if (stack_guard->IsInterrupted()) {
785 stack_guard->Continue(INTERRUPT);
786 return isolate->StackOverflow();
Steve Blocka7e24c12009-10-30 11:49:00 +0000787 }
Steve Block44f0eee2011-05-26 01:26:41 +0100788 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000789}
790
Steve Blocka7e24c12009-10-30 11:49:00 +0000791} } // namespace v8::internal