blob: e84ab9e8ed430e4e4fb4779b4166669c4551730a [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()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000135 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
Ben Murdoch257744e2011-11-30 15:57:28 +0000148Handle<Object> Execution::Call(Handle<Object> callable,
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 Handle<Object> receiver,
150 int argc,
151 Object*** args,
152 bool* pending_exception) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000153 if (!callable->IsJSFunction()) {
154 callable = TryGetFunctionDelegate(callable, pending_exception);
155 if (*pending_exception) return callable;
156 }
157 Handle<JSFunction> func = Handle<JSFunction>::cast(callable);
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 return Invoke(false, func, receiver, argc, args, pending_exception);
159}
160
161
162Handle<Object> Execution::New(Handle<JSFunction> func, int argc,
163 Object*** args, bool* pending_exception) {
Steve Block44f0eee2011-05-26 01:26:41 +0100164 return Invoke(true, func, Isolate::Current()->global(), argc, args,
165 pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +0000166}
167
168
169Handle<Object> Execution::TryCall(Handle<JSFunction> func,
170 Handle<Object> receiver,
171 int argc,
172 Object*** args,
173 bool* caught_exception) {
174 // Enter a try-block while executing the JavaScript code. To avoid
175 // duplicate error printing it must be non-verbose. Also, to avoid
176 // creating message objects during stack overflow we shouldn't
177 // capture messages.
178 v8::TryCatch catcher;
179 catcher.SetVerbose(false);
180 catcher.SetCaptureMessage(false);
181
182 Handle<Object> result = Invoke(false, func, receiver, argc, args,
183 caught_exception);
184
185 if (*caught_exception) {
186 ASSERT(catcher.HasCaught());
Steve Block44f0eee2011-05-26 01:26:41 +0100187 Isolate* isolate = Isolate::Current();
188 ASSERT(isolate->has_pending_exception());
189 ASSERT(isolate->external_caught_exception());
190 if (isolate->pending_exception() ==
191 isolate->heap()->termination_exception()) {
192 result = isolate->factory()->termination_exception();
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 } else {
194 result = v8::Utils::OpenHandle(*catcher.Exception());
195 }
Steve Block44f0eee2011-05-26 01:26:41 +0100196 isolate->OptionalRescheduleException(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 }
198
Steve Block44f0eee2011-05-26 01:26:41 +0100199 ASSERT(!Isolate::Current()->has_pending_exception());
200 ASSERT(!Isolate::Current()->external_caught_exception());
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 return result;
202}
203
204
205Handle<Object> Execution::GetFunctionDelegate(Handle<Object> object) {
206 ASSERT(!object->IsJSFunction());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100207 Isolate* isolate = Isolate::Current();
208 Factory* factory = isolate->factory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000209
210 // If you return a function from here, it will be called when an
211 // attempt is made to call the given object as a function.
212
213 // Regular expressions can be called as functions in both Firefox
214 // and Safari so we allow it too.
215 if (object->IsJSRegExp()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100216 Handle<String> exec = factory->exec_symbol();
John Reck59135872010-11-02 12:39:01 -0700217 // TODO(lrn): Bug 617. We should use the default function here, not the
218 // one on the RegExp object.
219 Object* exec_function;
220 { MaybeObject* maybe_exec_function = object->GetProperty(*exec);
221 // This can lose an exception, but the alternative is to put a failure
222 // object in a handle, which is not GC safe.
223 if (!maybe_exec_function->ToObject(&exec_function)) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100224 return factory->undefined_value();
John Reck59135872010-11-02 12:39:01 -0700225 }
226 }
227 return Handle<Object>(exec_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 }
229
230 // Objects created through the API can have an instance-call handler
231 // that should be used when calling the object as a function.
232 if (object->IsHeapObject() &&
233 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
234 return Handle<JSFunction>(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100235 isolate->global_context()->call_as_function_delegate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 }
237
Ben Murdoch8b112d22011-06-08 16:22:53 +0100238 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000239}
240
241
Ben Murdoch257744e2011-11-30 15:57:28 +0000242Handle<Object> Execution::TryGetFunctionDelegate(Handle<Object> object,
243 bool* has_pending_exception) {
244 ASSERT(!object->IsJSFunction());
245 Isolate* isolate = Isolate::Current();
246
247 // Objects created through the API can have an instance-call handler
248 // that should be used when calling the object as a function.
249 if (object->IsHeapObject() &&
250 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
251 return Handle<JSFunction>(
252 isolate->global_context()->call_as_function_delegate());
253 }
254
255 // If the Object doesn't have an instance-call handler we should
256 // throw a non-callable exception.
257 i::Handle<i::Object> error_obj = isolate->factory()->NewTypeError(
258 "called_non_callable", i::HandleVector<i::Object>(&object, 1));
259 isolate->Throw(*error_obj);
260 *has_pending_exception = true;
261
262 return isolate->factory()->undefined_value();
263}
264
265
Steve Blocka7e24c12009-10-30 11:49:00 +0000266Handle<Object> Execution::GetConstructorDelegate(Handle<Object> object) {
267 ASSERT(!object->IsJSFunction());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100268 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000269
270 // If you return a function from here, it will be called when an
271 // attempt is made to call the given object as a constructor.
272
273 // Objects created through the API can have an instance-call handler
274 // that should be used when calling the object as a function.
275 if (object->IsHeapObject() &&
276 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
277 return Handle<JSFunction>(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100278 isolate->global_context()->call_as_constructor_delegate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 }
280
Ben Murdoch8b112d22011-06-08 16:22:53 +0100281 return isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000282}
283
284
Ben Murdoch257744e2011-11-30 15:57:28 +0000285Handle<Object> Execution::TryGetConstructorDelegate(
286 Handle<Object> object,
287 bool* has_pending_exception) {
288 ASSERT(!object->IsJSFunction());
289 Isolate* isolate = Isolate::Current();
290
291 // If you return a function from here, it will be called when an
292 // attempt is made to call the given object as a constructor.
293
294 // Objects created through the API can have an instance-call handler
295 // that should be used when calling the object as a function.
296 if (object->IsHeapObject() &&
297 HeapObject::cast(*object)->map()->has_instance_call_handler()) {
298 return Handle<JSFunction>(
299 isolate->global_context()->call_as_constructor_delegate());
300 }
301
302 // If the Object doesn't have an instance-call handler we should
303 // throw a non-callable exception.
304 i::Handle<i::Object> error_obj = isolate->factory()->NewTypeError(
305 "called_non_callable", i::HandleVector<i::Object>(&object, 1));
306 isolate->Throw(*error_obj);
307 *has_pending_exception = true;
308
309 return isolate->factory()->undefined_value();
310}
311
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313bool StackGuard::IsStackOverflow() {
Steve Block44f0eee2011-05-26 01:26:41 +0100314 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 return (thread_local_.jslimit_ != kInterruptLimit &&
316 thread_local_.climit_ != kInterruptLimit);
317}
318
319
320void StackGuard::EnableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100321 ExecutionAccess access(isolate_);
Steve Block6ded16b2010-05-10 14:33:55 +0100322 if (has_pending_interrupts(access)) {
323 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 }
325}
326
327
328void StackGuard::SetStackLimit(uintptr_t limit) {
Steve Block44f0eee2011-05-26 01:26:41 +0100329 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000330 // If the current limits are special (eg due to a pending interrupt) then
331 // leave them alone.
Ben Murdoch257744e2011-11-30 15:57:28 +0000332 uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000333 if (thread_local_.jslimit_ == thread_local_.real_jslimit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 thread_local_.jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 }
Steve Blockd0582a62009-12-15 09:54:21 +0000336 if (thread_local_.climit_ == thread_local_.real_climit_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 thread_local_.climit_ = limit;
338 }
Steve Blockd0582a62009-12-15 09:54:21 +0000339 thread_local_.real_climit_ = limit;
340 thread_local_.real_jslimit_ = jslimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000341}
342
343
344void StackGuard::DisableInterrupts() {
Steve Block44f0eee2011-05-26 01:26:41 +0100345 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 reset_limits(access);
347}
348
349
Steve Blocka7e24c12009-10-30 11:49:00 +0000350bool StackGuard::IsInterrupted() {
Steve Block44f0eee2011-05-26 01:26:41 +0100351 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000352 return thread_local_.interrupt_flags_ & INTERRUPT;
353}
354
355
356void StackGuard::Interrupt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100357 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000358 thread_local_.interrupt_flags_ |= INTERRUPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100359 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000360}
361
362
363bool StackGuard::IsPreempted() {
Steve Block44f0eee2011-05-26 01:26:41 +0100364 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 return thread_local_.interrupt_flags_ & PREEMPT;
366}
367
368
369void StackGuard::Preempt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100370 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 thread_local_.interrupt_flags_ |= PREEMPT;
Steve Block6ded16b2010-05-10 14:33:55 +0100372 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000373}
374
375
376bool StackGuard::IsTerminateExecution() {
Steve Block44f0eee2011-05-26 01:26:41 +0100377 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 return thread_local_.interrupt_flags_ & TERMINATE;
379}
380
381
382void StackGuard::TerminateExecution() {
Steve Block44f0eee2011-05-26 01:26:41 +0100383 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 thread_local_.interrupt_flags_ |= TERMINATE;
Steve Block6ded16b2010-05-10 14:33:55 +0100385 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000386}
387
388
Ben Murdochb0fe1622011-05-05 13:52:32 +0100389bool StackGuard::IsRuntimeProfilerTick() {
Steve Block44f0eee2011-05-26 01:26:41 +0100390 ExecutionAccess access(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100391 return thread_local_.interrupt_flags_ & RUNTIME_PROFILER_TICK;
392}
393
394
395void StackGuard::RequestRuntimeProfilerTick() {
396 // Ignore calls if we're not optimizing or if we can't get the lock.
Steve Block44f0eee2011-05-26 01:26:41 +0100397 if (FLAG_opt && ExecutionAccess::TryLock(isolate_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100398 thread_local_.interrupt_flags_ |= RUNTIME_PROFILER_TICK;
399 if (thread_local_.postpone_interrupts_nesting_ == 0) {
400 thread_local_.jslimit_ = thread_local_.climit_ = kInterruptLimit;
Steve Block44f0eee2011-05-26 01:26:41 +0100401 isolate_->heap()->SetStackLimits();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100402 }
Steve Block44f0eee2011-05-26 01:26:41 +0100403 ExecutionAccess::Unlock(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100404 }
405}
406
407
Steve Blocka7e24c12009-10-30 11:49:00 +0000408#ifdef ENABLE_DEBUGGER_SUPPORT
409bool StackGuard::IsDebugBreak() {
Steve Block44f0eee2011-05-26 01:26:41 +0100410 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 return thread_local_.interrupt_flags_ & DEBUGBREAK;
412}
413
414
415void StackGuard::DebugBreak() {
Steve Block44f0eee2011-05-26 01:26:41 +0100416 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 thread_local_.interrupt_flags_ |= DEBUGBREAK;
Steve Block6ded16b2010-05-10 14:33:55 +0100418 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000419}
420
421
422bool StackGuard::IsDebugCommand() {
Steve Block44f0eee2011-05-26 01:26:41 +0100423 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000424 return thread_local_.interrupt_flags_ & DEBUGCOMMAND;
425}
426
427
428void StackGuard::DebugCommand() {
429 if (FLAG_debugger_auto_break) {
Steve Block44f0eee2011-05-26 01:26:41 +0100430 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000431 thread_local_.interrupt_flags_ |= DEBUGCOMMAND;
Steve Block6ded16b2010-05-10 14:33:55 +0100432 set_interrupt_limits(access);
Steve Blocka7e24c12009-10-30 11:49:00 +0000433 }
434}
435#endif
436
437void StackGuard::Continue(InterruptFlag after_what) {
Steve Block44f0eee2011-05-26 01:26:41 +0100438 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 thread_local_.interrupt_flags_ &= ~static_cast<int>(after_what);
Steve Block6ded16b2010-05-10 14:33:55 +0100440 if (!should_postpone_interrupts(access) && !has_pending_interrupts(access)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 reset_limits(access);
442 }
443}
444
445
Steve Blocka7e24c12009-10-30 11:49:00 +0000446char* StackGuard::ArchiveStackGuard(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +0100447 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 memcpy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
449 ThreadLocal blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100450
451 // Set the stack limits using the old thread_local_.
452 // TODO(isolates): This was the old semantics of constructing a ThreadLocal
453 // (as the ctor called SetStackLimits, which looked at the
454 // current thread_local_ from StackGuard)-- but is this
455 // really what was intended?
456 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000457 thread_local_ = blank;
Steve Block44f0eee2011-05-26 01:26:41 +0100458
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 return to + sizeof(ThreadLocal);
460}
461
462
463char* StackGuard::RestoreStackGuard(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +0100464 ExecutionAccess access(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 memcpy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
Steve Block44f0eee2011-05-26 01:26:41 +0100466 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 return from + sizeof(ThreadLocal);
468}
469
470
Steve Blocka7e24c12009-10-30 11:49:00 +0000471void StackGuard::FreeThreadResources() {
Steve Block44f0eee2011-05-26 01:26:41 +0100472 Isolate::CurrentPerIsolateThreadData()->set_stack_limit(
473 thread_local_.real_climit_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000474}
475
476
477void StackGuard::ThreadLocal::Clear() {
Steve Blockd0582a62009-12-15 09:54:21 +0000478 real_jslimit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 jslimit_ = kIllegalLimit;
Steve Blockd0582a62009-12-15 09:54:21 +0000480 real_climit_ = kIllegalLimit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 climit_ = kIllegalLimit;
482 nesting_ = 0;
483 postpone_interrupts_nesting_ = 0;
484 interrupt_flags_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000485}
486
487
Ben Murdoch257744e2011-11-30 15:57:28 +0000488bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100489 bool should_set_stack_limits = false;
Steve Blockd0582a62009-12-15 09:54:21 +0000490 if (real_climit_ == kIllegalLimit) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 // Takes the address of the limit variable in order to find out where
492 // the top of stack is right now.
Steve Block1e0659c2011-05-24 12:43:12 +0100493 const uintptr_t kLimitSize = FLAG_stack_size * KB;
Steve Block3ce2e202009-11-05 08:53:23 +0000494 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit) - kLimitSize;
495 ASSERT(reinterpret_cast<uintptr_t>(&limit) > kLimitSize);
Ben Murdoch257744e2011-11-30 15:57:28 +0000496 real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
497 jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000498 real_climit_ = limit;
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 climit_ = limit;
Steve Block44f0eee2011-05-26 01:26:41 +0100500 should_set_stack_limits = true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 }
502 nesting_ = 0;
503 postpone_interrupts_nesting_ = 0;
504 interrupt_flags_ = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100505 return should_set_stack_limits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000506}
507
508
509void StackGuard::ClearThread(const ExecutionAccess& lock) {
510 thread_local_.Clear();
Steve Block44f0eee2011-05-26 01:26:41 +0100511 isolate_->heap()->SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000512}
513
514
515void StackGuard::InitThread(const ExecutionAccess& lock) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000516 if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits();
517 Isolate::PerIsolateThreadData* per_thread =
518 isolate_->FindOrAllocatePerThreadDataForThisThread();
519 uintptr_t stored_limit = per_thread->stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 // You should hold the ExecutionAccess lock when you call this.
Steve Block44f0eee2011-05-26 01:26:41 +0100521 if (stored_limit != 0) {
522 StackGuard::SetStackLimit(stored_limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 }
524}
525
526
527// --- C a l l s t o n a t i v e s ---
528
Steve Block44f0eee2011-05-26 01:26:41 +0100529#define RETURN_NATIVE_CALL(name, argc, argv, has_pending_exception) \
530 do { \
Ben Murdoch8b112d22011-06-08 16:22:53 +0100531 Isolate* isolate = Isolate::Current(); \
Steve Block44f0eee2011-05-26 01:26:41 +0100532 Object** args[argc] = argv; \
533 ASSERT(has_pending_exception != NULL); \
Ben Murdoch8b112d22011-06-08 16:22:53 +0100534 return Call(isolate->name##_fun(), \
535 isolate->js_builtins_object(), argc, args, \
Steve Block44f0eee2011-05-26 01:26:41 +0100536 has_pending_exception); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 } while (false)
538
539
540Handle<Object> Execution::ToBoolean(Handle<Object> obj) {
541 // See the similar code in runtime.js:ToBoolean.
542 if (obj->IsBoolean()) return obj;
543 bool result = true;
544 if (obj->IsString()) {
545 result = Handle<String>::cast(obj)->length() != 0;
546 } else if (obj->IsNull() || obj->IsUndefined()) {
547 result = false;
548 } else if (obj->IsNumber()) {
549 double value = obj->Number();
550 result = !((value == 0) || isnan(value));
551 }
Steve Block44f0eee2011-05-26 01:26:41 +0100552 return Handle<Object>(HEAP->ToBoolean(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000553}
554
555
556Handle<Object> Execution::ToNumber(Handle<Object> obj, bool* exc) {
557 RETURN_NATIVE_CALL(to_number, 1, { obj.location() }, exc);
558}
559
560
561Handle<Object> Execution::ToString(Handle<Object> obj, bool* exc) {
562 RETURN_NATIVE_CALL(to_string, 1, { obj.location() }, exc);
563}
564
565
566Handle<Object> Execution::ToDetailString(Handle<Object> obj, bool* exc) {
567 RETURN_NATIVE_CALL(to_detail_string, 1, { obj.location() }, exc);
568}
569
570
571Handle<Object> Execution::ToObject(Handle<Object> obj, bool* exc) {
572 if (obj->IsJSObject()) return obj;
573 RETURN_NATIVE_CALL(to_object, 1, { obj.location() }, exc);
574}
575
576
577Handle<Object> Execution::ToInteger(Handle<Object> obj, bool* exc) {
578 RETURN_NATIVE_CALL(to_integer, 1, { obj.location() }, exc);
579}
580
581
582Handle<Object> Execution::ToUint32(Handle<Object> obj, bool* exc) {
583 RETURN_NATIVE_CALL(to_uint32, 1, { obj.location() }, exc);
584}
585
586
587Handle<Object> Execution::ToInt32(Handle<Object> obj, bool* exc) {
588 RETURN_NATIVE_CALL(to_int32, 1, { obj.location() }, exc);
589}
590
591
592Handle<Object> Execution::NewDate(double time, bool* exc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100593 Handle<Object> time_obj = FACTORY->NewNumber(time);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 RETURN_NATIVE_CALL(create_date, 1, { time_obj.location() }, exc);
595}
596
597
598#undef RETURN_NATIVE_CALL
599
600
Ben Murdochf87a2032010-10-22 12:50:53 +0100601Handle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern,
602 Handle<String> flags,
603 bool* exc) {
Steve Block44f0eee2011-05-26 01:26:41 +0100604 Handle<JSFunction> function = Handle<JSFunction>(
605 pattern->GetIsolate()->global_context()->regexp_function());
Ben Murdochf87a2032010-10-22 12:50:53 +0100606 Handle<Object> re_obj = RegExpImpl::CreateRegExpLiteral(
Steve Block44f0eee2011-05-26 01:26:41 +0100607 function, pattern, flags, exc);
Ben Murdochf87a2032010-10-22 12:50:53 +0100608 if (*exc) return Handle<JSRegExp>();
609 return Handle<JSRegExp>::cast(re_obj);
610}
611
612
Steve Blocka7e24c12009-10-30 11:49:00 +0000613Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100614 Isolate* isolate = string->GetIsolate();
615 Factory* factory = isolate->factory();
616
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 int int_index = static_cast<int>(index);
618 if (int_index < 0 || int_index >= string->length()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100619 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000620 }
621
622 Handle<Object> char_at =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100623 GetProperty(isolate->js_builtins_object(),
624 factory->char_at_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 if (!char_at->IsJSFunction()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100626 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000627 }
628
629 bool caught_exception;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100630 Handle<Object> index_object = factory->NewNumberFromInt(int_index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000631 Object** index_arg[] = { index_object.location() };
632 Handle<Object> result = TryCall(Handle<JSFunction>::cast(char_at),
633 string,
634 ARRAY_SIZE(index_arg),
635 index_arg,
636 &caught_exception);
637 if (caught_exception) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100638 return factory->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000639 }
640 return result;
641}
642
643
644Handle<JSFunction> Execution::InstantiateFunction(
645 Handle<FunctionTemplateInfo> data, bool* exc) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100646 Isolate* isolate = data->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 // Fast case: see if the function has already been instantiated
648 int serial_number = Smi::cast(data->serial_number())->value();
Steve Block44f0eee2011-05-26 01:26:41 +0100649 Object* elm =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100650 isolate->global_context()->function_cache()->
Steve Block44f0eee2011-05-26 01:26:41 +0100651 GetElementNoExceptionThrown(serial_number);
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 if (elm->IsJSFunction()) return Handle<JSFunction>(JSFunction::cast(elm));
653 // The function has not yet been instantiated in this context; do it.
654 Object** args[1] = { Handle<Object>::cast(data).location() };
655 Handle<Object> result =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100656 Call(isolate->instantiate_fun(),
657 isolate->js_builtins_object(), 1, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000658 if (*exc) return Handle<JSFunction>::null();
659 return Handle<JSFunction>::cast(result);
660}
661
662
663Handle<JSObject> Execution::InstantiateObject(Handle<ObjectTemplateInfo> data,
664 bool* exc) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100665 Isolate* isolate = data->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000666 if (data->property_list()->IsUndefined() &&
667 !data->constructor()->IsUndefined()) {
668 // Initialization to make gcc happy.
669 Object* result = NULL;
670 {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100671 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000672 Handle<FunctionTemplateInfo> cons_template =
673 Handle<FunctionTemplateInfo>(
674 FunctionTemplateInfo::cast(data->constructor()));
675 Handle<JSFunction> cons = InstantiateFunction(cons_template, exc);
676 if (*exc) return Handle<JSObject>::null();
677 Handle<Object> value = New(cons, 0, NULL, exc);
678 if (*exc) return Handle<JSObject>::null();
679 result = *value;
680 }
681 ASSERT(!*exc);
682 return Handle<JSObject>(JSObject::cast(result));
683 } else {
684 Object** args[1] = { Handle<Object>::cast(data).location() };
685 Handle<Object> result =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100686 Call(isolate->instantiate_fun(),
687 isolate->js_builtins_object(), 1, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 if (*exc) return Handle<JSObject>::null();
689 return Handle<JSObject>::cast(result);
690 }
691}
692
693
694void Execution::ConfigureInstance(Handle<Object> instance,
695 Handle<Object> instance_template,
696 bool* exc) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100697 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 Object** args[2] = { instance.location(), instance_template.location() };
Ben Murdoch8b112d22011-06-08 16:22:53 +0100699 Execution::Call(isolate->configure_instance_fun(),
700 isolate->js_builtins_object(), 2, args, exc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000701}
702
703
704Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
705 Handle<JSFunction> fun,
706 Handle<Object> pos,
707 Handle<Object> is_global) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100708 Isolate* isolate = fun->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000709 const int argc = 4;
710 Object** args[argc] = { recv.location(),
711 Handle<Object>::cast(fun).location(),
712 pos.location(),
713 is_global.location() };
714 bool caught_exception = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100715 Handle<Object> result =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100716 TryCall(isolate->get_stack_trace_line_fun(),
717 isolate->js_builtins_object(), argc, args,
Steve Block44f0eee2011-05-26 01:26:41 +0100718 &caught_exception);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100719 if (caught_exception || !result->IsString()) {
720 return isolate->factory()->empty_symbol();
721 }
722
Steve Blocka7e24c12009-10-30 11:49:00 +0000723 return Handle<String>::cast(result);
724}
725
726
727static Object* RuntimePreempt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100728 Isolate* isolate = Isolate::Current();
729
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 // Clear the preempt request flag.
Steve Block44f0eee2011-05-26 01:26:41 +0100731 isolate->stack_guard()->Continue(PREEMPT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000732
733 ContextSwitcher::PreemptionReceived();
734
735#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100736 if (isolate->debug()->InDebugger()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000737 // If currently in the debugger don't do any actual preemption but record
738 // that preemption occoured while in the debugger.
Steve Block44f0eee2011-05-26 01:26:41 +0100739 isolate->debug()->PreemptionWhileInDebugger();
Steve Blocka7e24c12009-10-30 11:49:00 +0000740 } else {
741 // Perform preemption.
Ben Murdoch257744e2011-11-30 15:57:28 +0000742 v8::Unlocker unlocker(reinterpret_cast<v8::Isolate*>(isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 Thread::YieldCPU();
744 }
745#else
Steve Block44f0eee2011-05-26 01:26:41 +0100746 { // NOLINT
747 // Perform preemption.
Ben Murdoch257744e2011-11-30 15:57:28 +0000748 v8::Unlocker unlocker(reinterpret_cast<v8::Isolate*>(isolate));
Steve Block44f0eee2011-05-26 01:26:41 +0100749 Thread::YieldCPU();
750 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000751#endif
752
Steve Block44f0eee2011-05-26 01:26:41 +0100753 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000754}
755
756
757#ifdef ENABLE_DEBUGGER_SUPPORT
758Object* Execution::DebugBreakHelper() {
Steve Block44f0eee2011-05-26 01:26:41 +0100759 Isolate* isolate = Isolate::Current();
760
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 // Just continue if breaks are disabled.
Steve Block44f0eee2011-05-26 01:26:41 +0100762 if (isolate->debug()->disable_break()) {
763 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000764 }
765
Leon Clarkee46be812010-01-19 14:06:41 +0000766 // Ignore debug break during bootstrapping.
Steve Block44f0eee2011-05-26 01:26:41 +0100767 if (isolate->bootstrapper()->IsActive()) {
768 return isolate->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +0000769 }
770
Steve Blocka7e24c12009-10-30 11:49:00 +0000771 {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100772 JavaScriptFrameIterator it(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 ASSERT(!it.done());
774 Object* fun = it.frame()->function();
775 if (fun && fun->IsJSFunction()) {
776 // Don't stop in builtin functions.
777 if (JSFunction::cast(fun)->IsBuiltin()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100778 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000779 }
780 GlobalObject* global = JSFunction::cast(fun)->context()->global();
781 // Don't stop in debugger functions.
Steve Block44f0eee2011-05-26 01:26:41 +0100782 if (isolate->debug()->IsDebugGlobal(global)) {
783 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000784 }
785 }
786 }
787
788 // Collect the break state before clearing the flags.
789 bool debug_command_only =
Steve Block44f0eee2011-05-26 01:26:41 +0100790 isolate->stack_guard()->IsDebugCommand() &&
791 !isolate->stack_guard()->IsDebugBreak();
Steve Blocka7e24c12009-10-30 11:49:00 +0000792
Leon Clarkee46be812010-01-19 14:06:41 +0000793 // Clear the debug break request flag.
Steve Block44f0eee2011-05-26 01:26:41 +0100794 isolate->stack_guard()->Continue(DEBUGBREAK);
Leon Clarkee46be812010-01-19 14:06:41 +0000795
796 ProcessDebugMesssages(debug_command_only);
797
798 // Return to continue execution.
Steve Block44f0eee2011-05-26 01:26:41 +0100799 return isolate->heap()->undefined_value();
Leon Clarkee46be812010-01-19 14:06:41 +0000800}
801
802void Execution::ProcessDebugMesssages(bool debug_command_only) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100803 Isolate* isolate = Isolate::Current();
Leon Clarkee46be812010-01-19 14:06:41 +0000804 // Clear the debug command request flag.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100805 isolate->stack_guard()->Continue(DEBUGCOMMAND);
Steve Blocka7e24c12009-10-30 11:49:00 +0000806
Ben Murdoch8b112d22011-06-08 16:22:53 +0100807 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000808 // Enter the debugger. Just continue if we fail to enter the debugger.
809 EnterDebugger debugger;
810 if (debugger.FailedToEnter()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000811 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 }
813
814 // Notify the debug event listeners. Indicate auto continue if the break was
815 // a debug command break.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100816 isolate->debugger()->OnDebugBreak(isolate->factory()->undefined_value(),
817 debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000818}
Leon Clarkee46be812010-01-19 14:06:41 +0000819
820
Steve Blocka7e24c12009-10-30 11:49:00 +0000821#endif
822
John Reck59135872010-11-02 12:39:01 -0700823MaybeObject* Execution::HandleStackGuardInterrupt() {
Steve Block44f0eee2011-05-26 01:26:41 +0100824 Isolate* isolate = Isolate::Current();
825 StackGuard* stack_guard = isolate->stack_guard();
826 isolate->counters()->stack_interrupts()->Increment();
827 if (stack_guard->IsRuntimeProfilerTick()) {
828 isolate->counters()->runtime_profiler_ticks()->Increment();
829 stack_guard->Continue(RUNTIME_PROFILER_TICK);
830 isolate->runtime_profiler()->OptimizeNow();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100831 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000832#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100833 if (stack_guard->IsDebugBreak() || stack_guard->IsDebugCommand()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000834 DebugBreakHelper();
835 }
836#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100837 if (stack_guard->IsPreempted()) RuntimePreempt();
838 if (stack_guard->IsTerminateExecution()) {
839 stack_guard->Continue(TERMINATE);
840 return isolate->TerminateExecution();
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 }
Steve Block44f0eee2011-05-26 01:26:41 +0100842 if (stack_guard->IsInterrupted()) {
843 stack_guard->Continue(INTERRUPT);
844 return isolate->StackOverflow();
Steve Blocka7e24c12009-10-30 11:49:00 +0000845 }
Steve Block44f0eee2011-05-26 01:26:41 +0100846 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000847}
848
Steve Blocka7e24c12009-10-30 11:49:00 +0000849} } // namespace v8::internal