Unreviewed, rolling out r161033 and r161074.
[WebKit-https.git] / Source / JavaScriptCore / interpreter / Interpreter.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "Interpreter.h"
32
33 #include "Arguments.h"
34 #include "BatchedTransitionOptimizer.h"
35 #include "CallFrame.h"
36 #include "CallFrameClosure.h"
37 #include "CallFrameInlines.h"
38 #include "CodeBlock.h"
39 #include "Heap.h"
40 #include "Debugger.h"
41 #include "DebuggerCallFrame.h"
42 #include "ErrorInstance.h"
43 #include "EvalCodeCache.h"
44 #include "ExceptionHelpers.h"
45 #include "GetterSetter.h"
46 #include "JSActivation.h"
47 #include "JSArray.h"
48 #include "JSBoundFunction.h"
49 #include "JSNameScope.h"
50 #include "JSNotAnObject.h"
51 #include "JSPropertyNameIterator.h"
52 #include "JSStackInlines.h"
53 #include "JSString.h"
54 #include "JSWithScope.h"
55 #include "LLIntCLoop.h"
56 #include "LLIntThunks.h"
57 #include "LegacyProfiler.h"
58 #include "LiteralParser.h"
59 #include "NameInstance.h"
60 #include "ObjectPrototype.h"
61 #include "Operations.h"
62 #include "Parser.h"
63 #include "ProtoCallFrame.h"
64 #include "RegExpObject.h"
65 #include "RegExpPrototype.h"
66 #include "Register.h"
67 #include "SamplingTool.h"
68 #include "StackVisitor.h"
69 #include "StrictEvalActivation.h"
70 #include "StrongInlines.h"
71 #include "VMEntryScope.h"
72 #include "VirtualRegister.h"
73
74 #include <limits.h>
75 #include <stdio.h>
76 #include <wtf/StackStats.h>
77 #include <wtf/StringPrintStream.h>
78 #include <wtf/Threading.h>
79 #include <wtf/WTFThreadData.h>
80 #include <wtf/text/StringBuilder.h>
81
82 #if ENABLE(JIT)
83 #include "JIT.h"
84 #endif
85
86 #define WTF_USE_GCC_COMPUTED_GOTO_WORKAROUND (ENABLE(LLINT) && !defined(__llvm__))
87
88 using namespace std;
89
90 namespace JSC {
91
92 Interpreter::ErrorHandlingMode::ErrorHandlingMode(ExecState *exec)
93     : m_interpreter(*exec->interpreter())
94 {
95     if (!m_interpreter.m_errorHandlingModeReentry)
96         m_interpreter.stack().enableErrorStackReserve();
97     m_interpreter.m_errorHandlingModeReentry++;
98 }
99
100 Interpreter::ErrorHandlingMode::~ErrorHandlingMode()
101 {
102     m_interpreter.m_errorHandlingModeReentry--;
103     ASSERT(m_interpreter.m_errorHandlingModeReentry >= 0);
104     if (!m_interpreter.m_errorHandlingModeReentry)
105         m_interpreter.stack().disableErrorStackReserve();
106 }
107
108 JSValue eval(CallFrame* callFrame)
109 {
110     if (!callFrame->argumentCount())
111         return jsUndefined();
112
113     JSValue program = callFrame->argument(0);
114     if (!program.isString())
115         return program;
116     
117     TopCallFrameSetter topCallFrame(callFrame->vm(), callFrame);
118     String programSource = asString(program)->value(callFrame);
119     if (callFrame->hadException())
120         return JSValue();
121     
122     CallFrame* callerFrame = callFrame->callerFrame();
123     CodeBlock* callerCodeBlock = callerFrame->codeBlock();
124     JSScope* callerScopeChain = callerFrame->scope();
125     EvalExecutable* eval = callerCodeBlock->evalCodeCache().tryGet(callerCodeBlock->isStrictMode(), programSource, callerScopeChain);
126
127     if (!eval) {
128         if (!callerCodeBlock->isStrictMode()) {
129             // FIXME: We can use the preparser in strict mode, we just need additional logic
130             // to prevent duplicates.
131             if (programSource.is8Bit()) {
132                 LiteralParser<LChar> preparser(callFrame, programSource.characters8(), programSource.length(), NonStrictJSON);
133                 if (JSValue parsedObject = preparser.tryLiteralParse())
134                     return parsedObject;
135             } else {
136                 LiteralParser<UChar> preparser(callFrame, programSource.characters16(), programSource.length(), NonStrictJSON);
137                 if (JSValue parsedObject = preparser.tryLiteralParse())
138                     return parsedObject;                
139             }
140         }
141         
142         // If the literal parser bailed, it should not have thrown exceptions.
143         ASSERT(!callFrame->vm().exception());
144
145         eval = callerCodeBlock->evalCodeCache().getSlow(callFrame, callerCodeBlock->ownerExecutable(), callerCodeBlock->isStrictMode(), programSource, callerScopeChain);
146         if (!eval)
147             return jsUndefined();
148     }
149
150     JSValue thisValue = callerFrame->thisValue();
151     Interpreter* interpreter = callFrame->vm().interpreter;
152     return interpreter->execute(eval, callFrame, thisValue, callerScopeChain);
153 }
154
155 CallFrame* sizeAndAllocFrameForVarargs(CallFrame* callFrame, JSStack* stack, JSValue arguments, int firstFreeRegister)
156 {
157     if (!arguments) { // f.apply(x, arguments), with arguments unmodified.
158         unsigned argumentCountIncludingThis = callFrame->argumentCountIncludingThis();
159         CallFrame* newCallFrame = CallFrame::create(callFrame->registers() + firstFreeRegister - argumentCountIncludingThis - JSStack::CallFrameHeaderSize - 1);
160         if (argumentCountIncludingThis > Arguments::MaxArguments + 1 || !stack->grow(newCallFrame->registers())) {
161             callFrame->vm().throwException(callFrame, createStackOverflowError(callFrame));
162             return 0;
163         }
164         return newCallFrame;
165     }
166
167     if (arguments.isUndefinedOrNull()) {
168         CallFrame* newCallFrame = CallFrame::create(callFrame->registers() + firstFreeRegister - 1 - JSStack::CallFrameHeaderSize - 1);
169         if (!stack->grow(newCallFrame->registers())) {
170             callFrame->vm().throwException(callFrame, createStackOverflowError(callFrame));
171             return 0;
172         }
173         return newCallFrame;
174     }
175
176     if (!arguments.isObject()) {
177         callFrame->vm().throwException(callFrame, createInvalidParameterError(callFrame, "Function.prototype.apply", arguments));
178         return 0;
179     }
180
181     if (asObject(arguments)->classInfo() == Arguments::info()) {
182         Arguments* argsObject = asArguments(arguments);
183         unsigned argCount = argsObject->length(callFrame);
184         CallFrame* newCallFrame = CallFrame::create(callFrame->registers() + firstFreeRegister - CallFrame::offsetFor(argCount + 1));
185         if (argCount > Arguments::MaxArguments || !stack->grow(newCallFrame->registers())) {
186             callFrame->vm().throwException(callFrame, createStackOverflowError(callFrame));
187             return 0;
188         }
189         return newCallFrame;
190     }
191
192     if (isJSArray(arguments)) {
193         JSArray* array = asArray(arguments);
194         unsigned argCount = array->length();
195         CallFrame* newCallFrame = CallFrame::create(callFrame->registers() + firstFreeRegister - CallFrame::offsetFor(argCount + 1));
196         if (argCount > Arguments::MaxArguments || !stack->grow(newCallFrame->registers())) {
197             callFrame->vm().throwException(callFrame, createStackOverflowError(callFrame));
198             return 0;
199         }
200         return newCallFrame;
201     }
202
203     JSObject* argObject = asObject(arguments);
204     unsigned argCount = argObject->get(callFrame, callFrame->propertyNames().length).toUInt32(callFrame);
205     CallFrame* newCallFrame = CallFrame::create(callFrame->registers() + firstFreeRegister - CallFrame::offsetFor(argCount + 1));
206     if (argCount > Arguments::MaxArguments || !stack->grow(newCallFrame->registers())) {
207         callFrame->vm().throwException(callFrame,  createStackOverflowError(callFrame));
208         return 0;
209     }
210     return newCallFrame;
211 }
212
213 void loadVarargs(CallFrame* callFrame, CallFrame* newCallFrame, JSValue thisValue, JSValue arguments)
214 {
215     if (!arguments) { // f.apply(x, arguments), with arguments unmodified.
216         unsigned argumentCountIncludingThis = callFrame->argumentCountIncludingThis();
217
218         newCallFrame->setArgumentCountIncludingThis(argumentCountIncludingThis);
219         newCallFrame->setThisValue(thisValue);
220         for (size_t i = 0; i < callFrame->argumentCount(); ++i)
221             newCallFrame->setArgument(i, callFrame->argumentAfterCapture(i));
222         return;
223     }
224     
225     if (arguments.isUndefinedOrNull()) {
226         newCallFrame->setArgumentCountIncludingThis(1);
227         newCallFrame->setThisValue(thisValue);
228         return;
229     }
230     
231     if (asObject(arguments)->classInfo() == Arguments::info()) {
232         Arguments* argsObject = asArguments(arguments);
233         unsigned argCount = argsObject->length(callFrame);
234         newCallFrame->setArgumentCountIncludingThis(argCount + 1);
235         newCallFrame->setThisValue(thisValue);
236         argsObject->copyToArguments(callFrame, newCallFrame, argCount);
237         return;
238     }
239     
240     if (isJSArray(arguments)) {
241         JSArray* array = asArray(arguments);
242         unsigned argCount = array->length();
243         newCallFrame->setArgumentCountIncludingThis(argCount + 1);
244         newCallFrame->setThisValue(thisValue);
245         array->copyToArguments(callFrame, newCallFrame, argCount);
246         return;
247     }
248     
249     JSObject* argObject = asObject(arguments);
250     unsigned argCount = argObject->get(callFrame, callFrame->propertyNames().length).toUInt32(callFrame);
251     newCallFrame->setArgumentCountIncludingThis(argCount + 1);
252     newCallFrame->setThisValue(thisValue);
253     for (size_t i = 0; i < argCount; ++i) {
254         newCallFrame->setArgument(i, asObject(arguments)->get(callFrame, i));
255         if (UNLIKELY(callFrame->vm().exception()))
256             return;
257     }
258 }
259
260 Interpreter::Interpreter(VM& vm)
261     : m_sampleEntryDepth(0)
262     , m_vm(vm)
263     , m_stack(vm)
264     , m_errorHandlingModeReentry(0)
265 #if !ASSERT_DISABLED
266     , m_initialized(false)
267 #endif
268 {
269 }
270
271 Interpreter::~Interpreter()
272 {
273 }
274
275 void Interpreter::initialize(bool canUseJIT)
276 {
277     UNUSED_PARAM(canUseJIT);
278
279 #if ENABLE(COMPUTED_GOTO_OPCODES) && ENABLE(LLINT)
280     m_opcodeTable = LLInt::opcodeMap();
281     for (int i = 0; i < numOpcodeIDs; ++i)
282         m_opcodeIDTable.add(m_opcodeTable[i], static_cast<OpcodeID>(i));
283 #endif
284
285 #if !ASSERT_DISABLED
286     m_initialized = true;
287 #endif
288
289 #if ENABLE(OPCODE_SAMPLING)
290     enableSampler();
291 #endif
292 }
293
294 #ifdef NDEBUG
295
296 void Interpreter::dumpCallFrame(CallFrame*)
297 {
298 }
299
300 #else
301
302 void Interpreter::dumpCallFrame(CallFrame* callFrame)
303 {
304     callFrame->codeBlock()->dumpBytecode();
305     dumpRegisters(callFrame);
306 }
307
308 class DumpRegisterFunctor {
309 public:
310     DumpRegisterFunctor(const Register*& it)
311         : m_hasSkippedFirstFrame(false)
312         , m_it(it)
313     {
314     }
315
316     StackVisitor::Status operator()(StackVisitor& visitor)
317     {
318         if (!m_hasSkippedFirstFrame) {
319             m_hasSkippedFirstFrame = true;
320             return StackVisitor::Continue;
321         }
322
323         unsigned line = 0;
324         unsigned unusedColumn = 0;
325         visitor->computeLineAndColumn(line, unusedColumn);
326         dataLogF("[ReturnVPC]                | %10p | %d (line %d)\n", m_it, visitor->bytecodeOffset(), line);
327         --m_it;
328         return StackVisitor::Done;
329     }
330
331 private:
332     bool m_hasSkippedFirstFrame;
333     const Register*& m_it;
334 };
335
336 void Interpreter::dumpRegisters(CallFrame* callFrame)
337 {
338     dataLogF("Register frame: \n\n");
339     dataLogF("-----------------------------------------------------------------------------\n");
340     dataLogF("            use            |   address  |                value               \n");
341     dataLogF("-----------------------------------------------------------------------------\n");
342
343     CodeBlock* codeBlock = callFrame->codeBlock();
344     const Register* it;
345     const Register* end;
346
347     it = callFrame->registers() + JSStack::ThisArgument + callFrame->argumentCount();
348     end = callFrame->registers() + JSStack::ThisArgument - 1;
349     while (it > end) {
350         JSValue v = it->jsValue();
351         int registerNumber = it - callFrame->registers();
352         String name = codeBlock->nameForRegister(VirtualRegister(registerNumber));
353         dataLogF("[r% 3d %14s]      | %10p | %-16s 0x%lld \n", registerNumber, name.ascii().data(), it, toCString(v).data(), (long long)JSValue::encode(v));
354         --it;
355     }
356     
357     dataLogF("-----------------------------------------------------------------------------\n");
358     dataLogF("[ArgumentCount]            | %10p | %lu \n", it, (unsigned long) callFrame->argumentCount());
359     --it;
360     dataLogF("[CallerFrame]              | %10p | %p \n", it, callFrame->callerFrame());
361     --it;
362     dataLogF("[Callee]                   | %10p | %p \n", it, callFrame->callee());
363     --it;
364     dataLogF("[ScopeChain]               | %10p | %p \n", it, callFrame->scope());
365     --it;
366 #if ENABLE(JIT)
367     AbstractPC pc = callFrame->abstractReturnPC(callFrame->vm());
368     if (pc.hasJITReturnAddress())
369         dataLogF("[ReturnJITPC]              | %10p | %p \n", it, pc.jitReturnAddress().value());
370 #endif
371
372     DumpRegisterFunctor functor(it);
373     callFrame->iterate(functor);
374
375     dataLogF("[CodeBlock]                | %10p | %p \n", it, callFrame->codeBlock());
376     --it;
377     dataLogF("-----------------------------------------------------------------------------\n");
378
379     end = it - codeBlock->m_numVars;
380     if (it != end) {
381         do {
382             JSValue v = it->jsValue();
383             int registerNumber = it - callFrame->registers();
384             String name = codeBlock->nameForRegister(VirtualRegister(registerNumber));
385             dataLogF("[r% 3d %14s]      | %10p | %-16s 0x%lld \n", registerNumber, name.ascii().data(), it, toCString(v).data(), (long long)JSValue::encode(v));
386             --it;
387         } while (it != end);
388     }
389     dataLogF("-----------------------------------------------------------------------------\n");
390
391     end = it - codeBlock->m_numCalleeRegisters + codeBlock->m_numVars;
392     if (it != end) {
393         do {
394             JSValue v = (*it).jsValue();
395             int registerNumber = it - callFrame->registers();
396             dataLogF("[r% 3d]                     | %10p | %-16s 0x%lld \n", registerNumber, it, toCString(v).data(), (long long)JSValue::encode(v));
397             --it;
398         } while (it != end);
399     }
400     dataLogF("-----------------------------------------------------------------------------\n");
401 }
402
403 #endif
404
405 bool Interpreter::isOpcode(Opcode opcode)
406 {
407 #if ENABLE(COMPUTED_GOTO_OPCODES)
408 #if !ENABLE(LLINT)
409     return static_cast<OpcodeID>(bitwise_cast<uintptr_t>(opcode)) <= op_end;
410 #else
411     return opcode != HashTraits<Opcode>::emptyValue()
412         && !HashTraits<Opcode>::isDeletedValue(opcode)
413         && m_opcodeIDTable.contains(opcode);
414 #endif
415 #else
416     return opcode >= 0 && opcode <= op_end;
417 #endif
418 }
419
420 static bool unwindCallFrame(StackVisitor& visitor)
421 {
422     CallFrame* callFrame = visitor->callFrame();
423     CodeBlock* codeBlock = visitor->codeBlock();
424     CodeBlock* oldCodeBlock = codeBlock;
425     JSScope* scope = callFrame->scope();
426
427     if (Debugger* debugger = callFrame->vmEntryGlobalObject()->debugger()) {
428         if (callFrame->callee())
429             debugger->returnEvent(callFrame);
430         else
431             debugger->didExecuteProgram(callFrame);
432     }
433
434     JSValue activation;
435     if (oldCodeBlock->codeType() == FunctionCode && oldCodeBlock->needsActivation()) {
436 #if ENABLE(DFG_JIT)
437         RELEASE_ASSERT(!visitor->isInlinedFrame());
438 #endif
439         activation = callFrame->uncheckedR(oldCodeBlock->activationRegister().offset()).jsValue();
440         if (activation)
441             jsCast<JSActivation*>(activation)->tearOff(*scope->vm());
442     }
443
444     if (oldCodeBlock->codeType() == FunctionCode && oldCodeBlock->usesArguments()) {
445         if (Arguments* arguments = visitor->existingArguments()) {
446             if (activation)
447                 arguments->didTearOffActivation(callFrame, jsCast<JSActivation*>(activation));
448 #if ENABLE(DFG_JIT)
449             else if (visitor->isInlinedFrame())
450                 arguments->tearOff(callFrame, visitor->inlineCallFrame());
451 #endif
452             else
453                 arguments->tearOff(callFrame);
454         }
455     }
456
457     CallFrame* callerFrame = callFrame->callerFrame();
458     if (callerFrame->isVMEntrySentinel()) {
459         callFrame->vm().topCallFrame = callerFrame->vmEntrySentinelCallerFrame();
460         return false;
461     }
462     return true;
463 }
464
465 static StackFrameCodeType getStackFrameCodeType(StackVisitor& visitor)
466 {
467     switch (visitor->codeType()) {
468     case StackVisitor::Frame::Eval:
469         return StackFrameEvalCode;
470     case StackVisitor::Frame::Function:
471         return StackFrameFunctionCode;
472     case StackVisitor::Frame::Global:
473         return StackFrameGlobalCode;
474     case StackVisitor::Frame::Native:
475         ASSERT_NOT_REACHED();
476         return StackFrameNativeCode;
477     }
478     RELEASE_ASSERT_NOT_REACHED();
479     return StackFrameGlobalCode;
480 }
481
482 void StackFrame::computeLineAndColumn(unsigned& line, unsigned& column)
483 {
484     if (!codeBlock) {
485         line = 0;
486         column = 0;
487         return;
488     }
489
490     int divot = 0;
491     int unusedStartOffset = 0;
492     int unusedEndOffset = 0;
493     unsigned divotLine = 0;
494     unsigned divotColumn = 0;
495     expressionInfo(divot, unusedStartOffset, unusedEndOffset, divotLine, divotColumn);
496
497     line = divotLine + lineOffset;
498     column = divotColumn + (divotLine ? 1 : firstLineColumnOffset);
499 }
500
501 void StackFrame::expressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column)
502 {
503     codeBlock->expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column);
504     divot += characterOffset;
505 }
506
507 String StackFrame::toString(CallFrame* callFrame)
508 {
509     StringBuilder traceBuild;
510     String functionName = friendlyFunctionName(callFrame);
511     String sourceURL = friendlySourceURL();
512     traceBuild.append(functionName);
513     if (!sourceURL.isEmpty()) {
514         if (!functionName.isEmpty())
515             traceBuild.append('@');
516         traceBuild.append(sourceURL);
517         if (codeType != StackFrameNativeCode) {
518             unsigned line;
519             unsigned column;
520             computeLineAndColumn(line, column);
521
522             traceBuild.append(':');
523             traceBuild.appendNumber(line);
524             traceBuild.append(':');
525             traceBuild.appendNumber(column);
526         }
527     }
528     return traceBuild.toString().impl();
529 }
530
531 class GetStackTraceFunctor {
532 public:
533     GetStackTraceFunctor(VM& vm, Vector<StackFrame>& results, size_t remainingCapacity)
534         : m_vm(vm)
535         , m_results(results)
536         , m_remainingCapacityForFrameCapture(remainingCapacity)
537     {
538     }
539
540     StackVisitor::Status operator()(StackVisitor& visitor)
541     {
542         VM& vm = m_vm;
543         if (m_remainingCapacityForFrameCapture) {
544             if (visitor->isJSFrame()) {
545                 CodeBlock* codeBlock = visitor->codeBlock();
546                 StackFrame s = {
547                     Strong<JSObject>(vm, visitor->callee()),
548                     getStackFrameCodeType(visitor),
549                     Strong<ExecutableBase>(vm, codeBlock->ownerExecutable()),
550                     Strong<UnlinkedCodeBlock>(vm, codeBlock->unlinkedCodeBlock()),
551                     codeBlock->source(),
552                     codeBlock->ownerExecutable()->lineNo(),
553                     codeBlock->firstLineColumnOffset(),
554                     codeBlock->sourceOffset(),
555                     visitor->bytecodeOffset(),
556                     visitor->sourceURL()
557                 };
558                 m_results.append(s);
559             } else {
560                 StackFrame s = { Strong<JSObject>(vm, visitor->callee()), StackFrameNativeCode, Strong<ExecutableBase>(), Strong<UnlinkedCodeBlock>(), 0, 0, 0, 0, 0, String()};
561                 m_results.append(s);
562             }
563     
564             m_remainingCapacityForFrameCapture--;
565             return StackVisitor::Continue;
566         }
567         return StackVisitor::Done;
568     }
569
570 private:
571     VM& m_vm;
572     Vector<StackFrame>& m_results;
573     size_t m_remainingCapacityForFrameCapture;
574 };
575
576 void Interpreter::getStackTrace(Vector<StackFrame>& results, size_t maxStackSize)
577 {
578     VM& vm = m_vm;
579     ASSERT(!vm.topCallFrame->isVMEntrySentinel());
580     CallFrame* callFrame = vm.topCallFrame;
581     if (!callFrame)
582         return;
583
584     GetStackTraceFunctor functor(vm, results, maxStackSize);
585     callFrame->iterate(functor);
586 }
587
588 JSString* Interpreter::stackTraceAsString(ExecState* exec, Vector<StackFrame> stackTrace)
589 {
590     // FIXME: JSStringJoiner could be more efficient than StringBuilder here.
591     StringBuilder builder;
592     for (unsigned i = 0; i < stackTrace.size(); i++) {
593         builder.append(String(stackTrace[i].toString(exec)));
594         if (i != stackTrace.size() - 1)
595             builder.append('\n');
596     }
597     return jsString(&exec->vm(), builder.toString());
598 }
599
600 class GetExceptionHandlerFunctor {
601 public:
602     GetExceptionHandlerFunctor()
603         : m_handler(0)
604     {
605     }
606
607     HandlerInfo* handler() { return m_handler; }
608
609     StackVisitor::Status operator()(StackVisitor& visitor)
610     {
611         CodeBlock* codeBlock = visitor->codeBlock();
612         if (!codeBlock)
613             return StackVisitor::Continue;
614
615         unsigned bytecodeOffset = visitor->bytecodeOffset();
616         m_handler = codeBlock->handlerForBytecodeOffset(bytecodeOffset);
617         if (m_handler)
618             return StackVisitor::Done;
619
620         return StackVisitor::Continue;
621     }
622
623 private:
624     HandlerInfo* m_handler;
625 };
626
627 class UnwindFunctor {
628 public:
629     UnwindFunctor(CallFrame*& callFrame, bool isTermination, CodeBlock*& codeBlock, HandlerInfo*& handler)
630         : m_callFrame(callFrame)
631         , m_isTermination(isTermination)
632         , m_codeBlock(codeBlock)
633         , m_handler(handler)
634     {
635     }
636
637     StackVisitor::Status operator()(StackVisitor& visitor)
638     {
639         VM& vm = m_callFrame->vm();
640         m_callFrame = visitor->callFrame();
641         m_codeBlock = visitor->codeBlock();
642         unsigned bytecodeOffset = visitor->bytecodeOffset();
643
644         if (m_isTermination || !(m_handler = m_codeBlock->handlerForBytecodeOffset(bytecodeOffset))) {
645             if (!unwindCallFrame(visitor)) {
646                 if (LegacyProfiler* profiler = vm.enabledProfiler())
647                     profiler->exceptionUnwind(m_callFrame);
648                 return StackVisitor::Done;
649             }
650         } else
651             return StackVisitor::Done;
652
653         return StackVisitor::Continue;
654     }
655
656 private:
657     CallFrame*& m_callFrame;
658     bool m_isTermination;
659     CodeBlock*& m_codeBlock;
660     HandlerInfo*& m_handler;
661 };
662
663 NEVER_INLINE HandlerInfo* Interpreter::unwind(CallFrame*& callFrame, JSValue& exceptionValue)
664 {
665     CodeBlock* codeBlock = callFrame->codeBlock();
666     bool isTermination = false;
667
668     ASSERT(!exceptionValue.isEmpty());
669     ASSERT(!exceptionValue.isCell() || exceptionValue.asCell());
670     // This shouldn't be possible (hence the assertions), but we're already in the slowest of
671     // slow cases, so let's harden against it anyway to be safe.
672     if (exceptionValue.isEmpty() || (exceptionValue.isCell() && !exceptionValue.asCell()))
673         exceptionValue = jsNull();
674
675     if (exceptionValue.isObject()) {
676         isTermination = isTerminatedExecutionException(asObject(exceptionValue));
677     }
678
679     ASSERT(callFrame->vm().exceptionStack().size());
680     ASSERT(!exceptionValue.isObject() || asObject(exceptionValue)->hasProperty(callFrame, callFrame->vm().propertyNames->stack));
681
682     Debugger* debugger = callFrame->vmEntryGlobalObject()->debugger();
683     if (debugger && debugger->needsExceptionCallbacks()) {
684         // We need to clear the exception and the exception stack here in order to see if a new exception happens.
685         // Afterwards, the values are put back to continue processing this error.
686         ClearExceptionScope scope(&callFrame->vm());
687         // This code assumes that if the debugger is enabled then there is no inlining.
688         // If that assumption turns out to be false then we'll ignore the inlined call
689         // frames.
690         // https://bugs.webkit.org/show_bug.cgi?id=121754
691
692         bool hasHandler;
693         if (isTermination)
694             hasHandler = false;
695         else {
696             GetExceptionHandlerFunctor functor;
697             callFrame->iterate(functor);
698             hasHandler = !!functor.handler();
699         }
700
701         debugger->exception(callFrame, exceptionValue, hasHandler);
702     }
703
704     // Calculate an exception handler vPC, unwinding call frames as necessary.
705     HandlerInfo* handler = 0;
706     VM& vm = callFrame->vm();
707     ASSERT(callFrame == vm.topCallFrame);
708     UnwindFunctor functor(callFrame, isTermination, codeBlock, handler);
709     callFrame->iterate(functor);
710     if (!handler)
711         return 0;
712
713     if (LegacyProfiler* profiler = vm.enabledProfiler())
714         profiler->exceptionUnwind(callFrame);
715
716     // Unwind the scope chain within the exception handler's call frame.
717     int targetScopeDepth = handler->scopeDepth;
718     if (codeBlock->needsActivation() && callFrame->uncheckedR(codeBlock->activationRegister().offset()).jsValue())
719         ++targetScopeDepth;
720
721     JSScope* scope = callFrame->scope();
722     int scopeDelta = scope->depth() - targetScopeDepth;
723     RELEASE_ASSERT(scopeDelta >= 0);
724
725     while (scopeDelta--)
726         scope = scope->next();
727     callFrame->setScope(scope);
728
729     return handler;
730 }
731
732 static inline JSValue checkedReturn(JSValue returnValue)
733 {
734     ASSERT(returnValue);
735     return returnValue;
736 }
737
738 static inline JSObject* checkedReturn(JSObject* returnValue)
739 {
740     ASSERT(returnValue);
741     return returnValue;
742 }
743
744 class SamplingScope {
745 public:
746     SamplingScope(Interpreter* interpreter)
747         : m_interpreter(interpreter)
748     {
749         interpreter->startSampling();
750     }
751     ~SamplingScope()
752     {
753         m_interpreter->stopSampling();
754     }
755 private:
756     Interpreter* m_interpreter;
757 };
758
759 JSValue Interpreter::execute(ProgramExecutable* program, CallFrame* callFrame, JSObject* thisObj)
760 {
761     SamplingScope samplingScope(this);
762     
763     JSScope* scope = callFrame->scope();
764     VM& vm = *scope->vm();
765
766     ASSERT(!vm.exception());
767     ASSERT(!vm.isCollectorBusy());
768     if (vm.isCollectorBusy())
769         return jsNull();
770
771     VMEntryScope entryScope(vm, scope->globalObject());
772     if (!vm.isSafeToRecurse())
773         return checkedReturn(throwStackOverflowError(callFrame));
774
775     // First check if the "program" is actually just a JSON object. If so,
776     // we'll handle the JSON object here. Else, we'll handle real JS code
777     // below at failedJSONP.
778
779     Vector<JSONPData> JSONPData;
780     bool parseResult;
781     const String programSource = program->source().toString();
782     if (programSource.isNull())
783         return jsUndefined();
784     if (programSource.is8Bit()) {
785         LiteralParser<LChar> literalParser(callFrame, programSource.characters8(), programSource.length(), JSONP);
786         parseResult = literalParser.tryJSONPParse(JSONPData, scope->globalObject()->globalObjectMethodTable()->supportsRichSourceInfo(scope->globalObject()));
787     } else {
788         LiteralParser<UChar> literalParser(callFrame, programSource.characters16(), programSource.length(), JSONP);
789         parseResult = literalParser.tryJSONPParse(JSONPData, scope->globalObject()->globalObjectMethodTable()->supportsRichSourceInfo(scope->globalObject()));
790     }
791
792     if (parseResult) {
793         JSGlobalObject* globalObject = scope->globalObject();
794         JSValue result;
795         for (unsigned entry = 0; entry < JSONPData.size(); entry++) {
796             Vector<JSONPPathEntry> JSONPPath;
797             JSONPPath.swap(JSONPData[entry].m_path);
798             JSValue JSONPValue = JSONPData[entry].m_value.get();
799             if (JSONPPath.size() == 1 && JSONPPath[0].m_type == JSONPPathEntryTypeDeclare) {
800                 globalObject->addVar(callFrame, JSONPPath[0].m_pathEntryName);
801                 PutPropertySlot slot;
802                 globalObject->methodTable()->put(globalObject, callFrame, JSONPPath[0].m_pathEntryName, JSONPValue, slot);
803                 result = jsUndefined();
804                 continue;
805             }
806             JSValue baseObject(globalObject);
807             for (unsigned i = 0; i < JSONPPath.size() - 1; i++) {
808                 ASSERT(JSONPPath[i].m_type != JSONPPathEntryTypeDeclare);
809                 switch (JSONPPath[i].m_type) {
810                 case JSONPPathEntryTypeDot: {
811                     if (i == 0) {
812                         PropertySlot slot(globalObject);
813                         if (!globalObject->getPropertySlot(callFrame, JSONPPath[i].m_pathEntryName, slot)) {
814                             if (entry)
815                                 return callFrame->vm().throwException(callFrame, createUndefinedVariableError(globalObject->globalExec(), JSONPPath[i].m_pathEntryName));
816                             goto failedJSONP;
817                         }
818                         baseObject = slot.getValue(callFrame, JSONPPath[i].m_pathEntryName);
819                     } else
820                         baseObject = baseObject.get(callFrame, JSONPPath[i].m_pathEntryName);
821                     if (callFrame->hadException())
822                         return jsUndefined();
823                     continue;
824                 }
825                 case JSONPPathEntryTypeLookup: {
826                     baseObject = baseObject.get(callFrame, JSONPPath[i].m_pathIndex);
827                     if (callFrame->hadException())
828                         return jsUndefined();
829                     continue;
830                 }
831                 default:
832                     RELEASE_ASSERT_NOT_REACHED();
833                     return jsUndefined();
834                 }
835             }
836             PutPropertySlot slot;
837             switch (JSONPPath.last().m_type) {
838             case JSONPPathEntryTypeCall: {
839                 JSValue function = baseObject.get(callFrame, JSONPPath.last().m_pathEntryName);
840                 if (callFrame->hadException())
841                     return jsUndefined();
842                 CallData callData;
843                 CallType callType = getCallData(function, callData);
844                 if (callType == CallTypeNone)
845                     return callFrame->vm().throwException(callFrame, createNotAFunctionError(callFrame, function));
846                 MarkedArgumentBuffer jsonArg;
847                 jsonArg.append(JSONPValue);
848                 JSValue thisValue = JSONPPath.size() == 1 ? jsUndefined(): baseObject;
849                 JSONPValue = JSC::call(callFrame, function, callType, callData, thisValue, jsonArg);
850                 if (callFrame->hadException())
851                     return jsUndefined();
852                 break;
853             }
854             case JSONPPathEntryTypeDot: {
855                 baseObject.put(callFrame, JSONPPath.last().m_pathEntryName, JSONPValue, slot);
856                 if (callFrame->hadException())
857                     return jsUndefined();
858                 break;
859             }
860             case JSONPPathEntryTypeLookup: {
861                 baseObject.putByIndex(callFrame, JSONPPath.last().m_pathIndex, JSONPValue, slot.isStrictMode());
862                 if (callFrame->hadException())
863                     return jsUndefined();
864                 break;
865             }
866             default:
867                 RELEASE_ASSERT_NOT_REACHED();
868                     return jsUndefined();
869             }
870             result = JSONPValue;
871         }
872         return result;
873     }
874 failedJSONP:
875     // If we get here, then we have already proven that the script is not a JSON
876     // object.
877
878     // Compile source to bytecode if necessary:
879     if (JSObject* error = program->initializeGlobalProperties(vm, callFrame, scope))
880         return checkedReturn(callFrame->vm().throwException(callFrame, error));
881
882     if (JSObject* error = program->prepareForExecution(callFrame, scope, CodeForCall))
883         return checkedReturn(callFrame->vm().throwException(callFrame, error));
884
885     ProgramCodeBlock* codeBlock = program->codeBlock();
886
887     if (UNLIKELY(vm.watchdog.didFire(callFrame)))
888         return throwTerminatedExecutionException(callFrame);
889
890     ASSERT(codeBlock->numParameters() == 1); // 1 parameter for 'this'.
891
892     if (UNLIKELY(!m_stack.entryCheck(codeBlock, 1)))
893         return checkedReturn(throwStackOverflowError(callFrame));
894
895     ProtoCallFrame protoCallFrame;
896     protoCallFrame.init(codeBlock, scope, 0, thisObj, 1);
897
898     if (LegacyProfiler* profiler = vm.enabledProfiler())
899         profiler->willExecute(callFrame, program->sourceURL(), program->lineNo());
900
901     // Execute the code:
902     JSValue result;
903     {
904         SamplingTool::CallRecord callRecord(m_sampler.get());
905         Watchdog::Scope watchdogScope(vm.watchdog);
906
907         result = program->generatedJITCode()->execute(&vm, &protoCallFrame, m_stack.getTopOfStack());
908     }
909
910     if (LegacyProfiler* profiler = vm.enabledProfiler())
911         profiler->didExecute(callFrame, program->sourceURL(), program->lineNo());
912
913     return checkedReturn(result);
914 }
915
916 JSValue Interpreter::executeCall(CallFrame* callFrame, JSObject* function, CallType callType, const CallData& callData, JSValue thisValue, const ArgList& args)
917 {
918     VM& vm = callFrame->vm();
919     ASSERT(!callFrame->hadException());
920     ASSERT(!vm.isCollectorBusy());
921     if (vm.isCollectorBusy())
922         return jsNull();
923
924     bool isJSCall = (callType == CallTypeJS);
925     JSScope* scope;
926     CodeBlock* newCodeBlock;
927     size_t argsCount = 1 + args.size(); // implicit "this" parameter
928
929     if (isJSCall)
930         scope = callData.js.scope;
931     else {
932         ASSERT(callType == CallTypeHost);
933         scope = callFrame->scope();
934     }
935
936     VMEntryScope entryScope(vm, scope->globalObject());
937     if (!vm.isSafeToRecurse())
938         return checkedReturn(throwStackOverflowError(callFrame));
939
940     if (isJSCall) {
941         // Compile the callee:
942         JSObject* compileError = callData.js.functionExecutable->prepareForExecution(callFrame, scope, CodeForCall);
943         if (UNLIKELY(!!compileError)) {
944             return checkedReturn(callFrame->vm().throwException(callFrame, compileError));
945         }
946         newCodeBlock = callData.js.functionExecutable->codeBlockForCall();
947         ASSERT(!!newCodeBlock);
948         newCodeBlock->m_shouldAlwaysBeInlined = false;
949     } else
950         newCodeBlock = 0;
951
952     if (UNLIKELY(vm.watchdog.didFire(callFrame)))
953         return throwTerminatedExecutionException(callFrame);
954
955     if (UNLIKELY(!m_stack.entryCheck(newCodeBlock, argsCount)))
956         return checkedReturn(throwStackOverflowError(callFrame));
957
958     ProtoCallFrame protoCallFrame;
959     protoCallFrame.init(newCodeBlock, scope, function, thisValue, argsCount, args.data());
960
961     if (LegacyProfiler* profiler = vm.enabledProfiler())
962         profiler->willExecute(callFrame, function);
963
964     JSValue result;
965     {
966         SamplingTool::CallRecord callRecord(m_sampler.get(), !isJSCall);
967         Watchdog::Scope watchdogScope(vm.watchdog);
968
969         // Execute the code:
970         if (isJSCall)
971             result = callData.js.functionExecutable->generatedJITCodeForCall()->execute(&vm, &protoCallFrame, m_stack.getTopOfStack());
972         else
973             result = JSValue::decode(callToNativeFunction(reinterpret_cast<void*>(callData.native.function), &vm.topCallFrame, &protoCallFrame, m_stack.getTopOfStack()));
974     }
975
976     if (LegacyProfiler* profiler = vm.enabledProfiler())
977         profiler->didExecute(callFrame, function);
978
979     return checkedReturn(result);
980 }
981
982 JSObject* Interpreter::executeConstruct(CallFrame* callFrame, JSObject* constructor, ConstructType constructType, const ConstructData& constructData, const ArgList& args)
983 {
984     VM& vm = callFrame->vm();
985     ASSERT(!callFrame->hadException());
986     ASSERT(!vm.isCollectorBusy());
987     // We throw in this case because we have to return something "valid" but we're
988     // already in an invalid state.
989     if (vm.isCollectorBusy())
990         return checkedReturn(throwStackOverflowError(callFrame));
991
992     bool isJSConstruct = (constructType == ConstructTypeJS);
993     JSScope* scope;
994     CodeBlock* newCodeBlock;
995     size_t argsCount = 1 + args.size(); // implicit "this" parameter
996
997     if (isJSConstruct)
998         scope = constructData.js.scope;
999     else {
1000         ASSERT(constructType == ConstructTypeHost);
1001         scope = callFrame->scope();
1002     }
1003
1004     VMEntryScope entryScope(vm, scope->globalObject());
1005     if (!vm.isSafeToRecurse())
1006         return checkedReturn(throwStackOverflowError(callFrame));
1007
1008     if (isJSConstruct) {
1009         // Compile the callee:
1010         JSObject* compileError = constructData.js.functionExecutable->prepareForExecution(callFrame, scope, CodeForConstruct);
1011         if (UNLIKELY(!!compileError)) {
1012             return checkedReturn(callFrame->vm().throwException(callFrame, compileError));
1013         }
1014         newCodeBlock = constructData.js.functionExecutable->codeBlockForConstruct();
1015         ASSERT(!!newCodeBlock);
1016         newCodeBlock->m_shouldAlwaysBeInlined = false;
1017     } else
1018         newCodeBlock = 0;
1019
1020     if (UNLIKELY(vm.watchdog.didFire(callFrame)))
1021         return throwTerminatedExecutionException(callFrame);
1022
1023     if (UNLIKELY(!m_stack.entryCheck(newCodeBlock, argsCount)))
1024         return checkedReturn(throwStackOverflowError(callFrame));
1025
1026     ProtoCallFrame protoCallFrame;
1027     protoCallFrame.init(newCodeBlock, scope, constructor, jsUndefined(), argsCount, args.data());
1028
1029     if (LegacyProfiler* profiler = vm.enabledProfiler())
1030         profiler->willExecute(callFrame, constructor);
1031
1032     JSValue result;
1033     {
1034         SamplingTool::CallRecord callRecord(m_sampler.get(), !isJSConstruct);
1035         Watchdog::Scope watchdogScope(vm.watchdog);
1036
1037         // Execute the code.
1038         if (isJSConstruct)
1039             result = constructData.js.functionExecutable->generatedJITCodeForConstruct()->execute(&vm, &protoCallFrame, m_stack.getTopOfStack());
1040         else {
1041             result = JSValue::decode(callToNativeFunction(reinterpret_cast<void*>(constructData.native.function), &vm.topCallFrame, &protoCallFrame, m_stack.getTopOfStack()));
1042
1043             if (!callFrame->hadException())
1044                 RELEASE_ASSERT(result.isObject());
1045         }
1046     }
1047
1048     if (LegacyProfiler* profiler = vm.enabledProfiler())
1049         profiler->didExecute(callFrame, constructor);
1050
1051     if (callFrame->hadException())
1052         return 0;
1053     ASSERT(result.isObject());
1054     return checkedReturn(asObject(result));
1055 }
1056
1057 CallFrameClosure Interpreter::prepareForRepeatCall(FunctionExecutable* functionExecutable, CallFrame* callFrame, ProtoCallFrame* protoCallFrame, JSFunction* function, int argumentCountIncludingThis, JSScope* scope, JSValue* args)
1058 {
1059     VM& vm = *scope->vm();
1060     ASSERT(!vm.exception());
1061     
1062     if (vm.isCollectorBusy())
1063         return CallFrameClosure();
1064
1065     // Compile the callee:
1066     JSObject* error = functionExecutable->prepareForExecution(callFrame, scope, CodeForCall);
1067     if (error) {
1068         callFrame->vm().throwException(callFrame, error);
1069         return CallFrameClosure();
1070     }
1071     CodeBlock* newCodeBlock = functionExecutable->codeBlockForCall();
1072     newCodeBlock->m_shouldAlwaysBeInlined = false;
1073
1074     size_t argsCount = argumentCountIncludingThis;
1075
1076     if (UNLIKELY(!m_stack.entryCheck(newCodeBlock, argsCount))) {
1077         throwStackOverflowError(callFrame);
1078         return CallFrameClosure();
1079     }
1080
1081     protoCallFrame->init(newCodeBlock, scope, function, jsUndefined(), argsCount, args);
1082     // Return the successful closure:
1083     CallFrameClosure result = { callFrame, protoCallFrame, function, functionExecutable, &vm, scope, newCodeBlock->numParameters(), argumentCountIncludingThis };
1084     return result;
1085 }
1086
1087 JSValue Interpreter::execute(CallFrameClosure& closure) 
1088 {
1089     VM& vm = *closure.vm;
1090     SamplingScope samplingScope(this);
1091     
1092     ASSERT(!vm.isCollectorBusy());
1093     if (vm.isCollectorBusy())
1094         return jsNull();
1095
1096     StackStats::CheckPoint stackCheckPoint;
1097     closure.resetCallFrame();
1098
1099     if (LegacyProfiler* profiler = vm.enabledProfiler())
1100         profiler->willExecute(closure.oldCallFrame, closure.function);
1101
1102     if (UNLIKELY(vm.watchdog.didFire(closure.oldCallFrame)))
1103         return throwTerminatedExecutionException(closure.oldCallFrame);
1104
1105     // Execute the code:
1106     JSValue result;
1107     {
1108         SamplingTool::CallRecord callRecord(m_sampler.get());
1109         Watchdog::Scope watchdogScope(vm.watchdog);
1110
1111         result = closure.functionExecutable->generatedJITCodeForCall()->execute(&vm, closure.protoCallFrame, m_stack.getTopOfStack());
1112     }
1113
1114     if (LegacyProfiler* profiler = vm.enabledProfiler())
1115         profiler->didExecute(closure.oldCallFrame, closure.function);
1116
1117     return checkedReturn(result);
1118 }
1119
1120 JSValue Interpreter::execute(EvalExecutable* eval, CallFrame* callFrame, JSValue thisValue, JSScope* scope)
1121 {
1122     VM& vm = *scope->vm();
1123     SamplingScope samplingScope(this);
1124     
1125     ASSERT(scope->vm() == &callFrame->vm());
1126     ASSERT(!vm.exception());
1127     ASSERT(!vm.isCollectorBusy());
1128     if (vm.isCollectorBusy())
1129         return jsNull();
1130
1131     VMEntryScope entryScope(vm, scope->globalObject());
1132     if (!vm.isSafeToRecurse())
1133         return checkedReturn(throwStackOverflowError(callFrame));        
1134
1135     unsigned numVariables = eval->numVariables();
1136     int numFunctions = eval->numberOfFunctionDecls();
1137
1138     JSScope* variableObject;
1139     if ((numVariables || numFunctions) && eval->isStrictMode()) {
1140         scope = StrictEvalActivation::create(callFrame);
1141         variableObject = scope;
1142     } else {
1143         for (JSScope* node = scope; ; node = node->next()) {
1144             RELEASE_ASSERT(node);
1145             if (node->isVariableObject() && !node->isNameScopeObject()) {
1146                 variableObject = node;
1147                 break;
1148             }
1149         }
1150     }
1151
1152     JSObject* compileError = eval->prepareForExecution(callFrame, scope, CodeForCall);
1153     if (UNLIKELY(!!compileError))
1154         return checkedReturn(callFrame->vm().throwException(callFrame, compileError));
1155     EvalCodeBlock* codeBlock = eval->codeBlock();
1156
1157     if (numVariables || numFunctions) {
1158         BatchedTransitionOptimizer optimizer(vm, variableObject);
1159         if (variableObject->next())
1160             variableObject->globalObject()->varInjectionWatchpoint()->fireAll();
1161
1162         for (unsigned i = 0; i < numVariables; ++i) {
1163             const Identifier& ident = codeBlock->variable(i);
1164             if (!variableObject->hasProperty(callFrame, ident)) {
1165                 PutPropertySlot slot;
1166                 variableObject->methodTable()->put(variableObject, callFrame, ident, jsUndefined(), slot);
1167             }
1168         }
1169
1170         for (int i = 0; i < numFunctions; ++i) {
1171             FunctionExecutable* function = codeBlock->functionDecl(i);
1172             PutPropertySlot slot;
1173             variableObject->methodTable()->put(variableObject, callFrame, function->name(), JSFunction::create(vm, function, scope), slot);
1174         }
1175     }
1176
1177     if (UNLIKELY(vm.watchdog.didFire(callFrame)))
1178         return throwTerminatedExecutionException(callFrame);
1179
1180     ASSERT(codeBlock->numParameters() == 1); // 1 parameter for 'this'.
1181
1182     if (UNLIKELY(!m_stack.entryCheck(codeBlock, 1)))
1183         return checkedReturn(throwStackOverflowError(callFrame));
1184
1185     ProtoCallFrame protoCallFrame;
1186     protoCallFrame.init(codeBlock, scope, 0, thisValue, 1);
1187
1188     if (LegacyProfiler* profiler = vm.enabledProfiler())
1189         profiler->willExecute(callFrame, eval->sourceURL(), eval->lineNo());
1190
1191     // Execute the code:
1192     JSValue result;
1193     {
1194         SamplingTool::CallRecord callRecord(m_sampler.get());
1195         Watchdog::Scope watchdogScope(vm.watchdog);
1196
1197         result = eval->generatedJITCode()->execute(&vm, &protoCallFrame, m_stack.getTopOfStack());
1198     }
1199
1200     if (LegacyProfiler* profiler = vm.enabledProfiler())
1201         profiler->didExecute(callFrame, eval->sourceURL(), eval->lineNo());
1202
1203     return checkedReturn(result);
1204 }
1205
1206 NEVER_INLINE void Interpreter::debug(CallFrame* callFrame, DebugHookID debugHookID)
1207 {
1208     Debugger* debugger = callFrame->vmEntryGlobalObject()->debugger();
1209     if (!debugger || !debugger->needsOpDebugCallbacks())
1210         return;
1211
1212     switch (debugHookID) {
1213         case DidEnterCallFrame:
1214             debugger->callEvent(callFrame);
1215             return;
1216         case WillLeaveCallFrame:
1217             debugger->returnEvent(callFrame);
1218             return;
1219         case WillExecuteStatement:
1220             debugger->atStatement(callFrame);
1221             return;
1222         case WillExecuteProgram:
1223             debugger->willExecuteProgram(callFrame);
1224             return;
1225         case DidExecuteProgram:
1226             debugger->didExecuteProgram(callFrame);
1227             return;
1228         case DidReachBreakpoint:
1229             debugger->didReachBreakpoint(callFrame);
1230             return;
1231     }
1232 }    
1233
1234 void Interpreter::enableSampler()
1235 {
1236 #if ENABLE(OPCODE_SAMPLING)
1237     if (!m_sampler) {
1238         m_sampler = adoptPtr(new SamplingTool(this));
1239         m_sampler->setup();
1240     }
1241 #endif
1242 }
1243 void Interpreter::dumpSampleData(ExecState* exec)
1244 {
1245 #if ENABLE(OPCODE_SAMPLING)
1246     if (m_sampler)
1247         m_sampler->dump(exec);
1248 #else
1249     UNUSED_PARAM(exec);
1250 #endif
1251 }
1252 void Interpreter::startSampling()
1253 {
1254 #if ENABLE(SAMPLING_THREAD)
1255     if (!m_sampleEntryDepth)
1256         SamplingThread::start();
1257
1258     m_sampleEntryDepth++;
1259 #endif
1260 }
1261 void Interpreter::stopSampling()
1262 {
1263 #if ENABLE(SAMPLING_THREAD)
1264     m_sampleEntryDepth--;
1265     if (!m_sampleEntryDepth)
1266         SamplingThread::stop();
1267 #endif
1268 }
1269
1270 } // namespace JSC