CodeBlock.h shouldn't be included from everywhere
[WebKit-https.git] / Source / JavaScriptCore / interpreter / Interpreter.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2010, 2012-2015 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 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 "BatchedTransitionOptimizer.h"
34 #include "CallFrameClosure.h"
35 #include "ClonedArguments.h"
36 #include "CodeBlock.h"
37 #include "DirectArguments.h"
38 #include "Heap.h"
39 #include "Debugger.h"
40 #include "DebuggerCallFrame.h"
41 #include "ErrorInstance.h"
42 #include "EvalCodeCache.h"
43 #include "Exception.h"
44 #include "ExceptionHelpers.h"
45 #include "GetterSetter.h"
46 #include "JSArray.h"
47 #include "JSArrowFunction.h"
48 #include "JSBoundFunction.h"
49 #include "JSCInlines.h"
50 #include "JSLexicalEnvironment.h"
51 #include "JSModuleEnvironment.h"
52 #include "JSNotAnObject.h"
53 #include "JSStackInlines.h"
54 #include "JSString.h"
55 #include "JSWithScope.h"
56 #include "LLIntCLoop.h"
57 #include "LLIntThunks.h"
58 #include "LegacyProfiler.h"
59 #include "LiteralParser.h"
60 #include "ObjectPrototype.h"
61 #include "Parser.h"
62 #include "ProtoCallFrame.h"
63 #include "RegExpObject.h"
64 #include "RegExpPrototype.h"
65 #include "Register.h"
66 #include "SamplingTool.h"
67 #include "ScopedArguments.h"
68 #include "StackAlignment.h"
69 #include "StackVisitor.h"
70 #include "StrictEvalActivation.h"
71 #include "StrongInlines.h"
72 #include "Symbol.h"
73 #include "VMEntryScope.h"
74 #include "VMInlines.h"
75 #include "VirtualRegister.h"
76
77 #include <limits.h>
78 #include <stdio.h>
79 #include <wtf/StackStats.h>
80 #include <wtf/StdLibExtras.h>
81 #include <wtf/StringPrintStream.h>
82 #include <wtf/Threading.h>
83 #include <wtf/WTFThreadData.h>
84 #include <wtf/text/StringBuilder.h>
85
86 #if ENABLE(JIT)
87 #include "JIT.h"
88 #endif
89
90 using namespace std;
91
92 namespace JSC {
93
94 String StackFrame::friendlySourceURL() const
95 {
96     String traceLine;
97     
98     switch (codeType) {
99     case StackFrameEvalCode:
100     case StackFrameModuleCode:
101     case StackFrameFunctionCode:
102     case StackFrameGlobalCode:
103         if (!sourceURL.isEmpty())
104             traceLine = sourceURL.impl();
105         break;
106     case StackFrameNativeCode:
107         traceLine = "[native code]";
108         break;
109     }
110     return traceLine.isNull() ? emptyString() : traceLine;
111 }
112
113 String StackFrame::friendlyFunctionName(CallFrame* callFrame) const
114 {
115     String traceLine;
116     JSObject* stackFrameCallee = callee.get();
117
118     switch (codeType) {
119     case StackFrameEvalCode:
120         traceLine = "eval code";
121         break;
122     case StackFrameModuleCode:
123         traceLine = "module code";
124         break;
125     case StackFrameNativeCode:
126         if (callee)
127             traceLine = getCalculatedDisplayName(callFrame, stackFrameCallee).impl();
128         break;
129     case StackFrameFunctionCode:
130         traceLine = getCalculatedDisplayName(callFrame, stackFrameCallee).impl();
131         break;
132     case StackFrameGlobalCode:
133         traceLine = "global code";
134         break;
135     }
136     return traceLine.isNull() ? emptyString() : traceLine;
137 }
138
139 JSValue eval(CallFrame* callFrame)
140 {
141     if (!callFrame->argumentCount())
142         return jsUndefined();
143
144     JSValue program = callFrame->argument(0);
145     if (!program.isString())
146         return program;
147     
148     TopCallFrameSetter topCallFrame(callFrame->vm(), callFrame);
149     String programSource = asString(program)->value(callFrame);
150     if (callFrame->hadException())
151         return JSValue();
152     
153     CallFrame* callerFrame = callFrame->callerFrame();
154     CodeBlock* callerCodeBlock = callerFrame->codeBlock();
155     JSScope* callerScopeChain = callerFrame->uncheckedR(callerCodeBlock->scopeRegister().offset()).Register::scope();
156     EvalExecutable* eval = callerCodeBlock->evalCodeCache().tryGet(callerCodeBlock->isStrictMode(), programSource, callerScopeChain);
157
158     if (!eval) {
159         if (!callerCodeBlock->isStrictMode()) {
160             if (programSource.is8Bit()) {
161                 LiteralParser<LChar> preparser(callFrame, programSource.characters8(), programSource.length(), NonStrictJSON);
162                 if (JSValue parsedObject = preparser.tryLiteralParse())
163                     return parsedObject;
164             } else {
165                 LiteralParser<UChar> preparser(callFrame, programSource.characters16(), programSource.length(), NonStrictJSON);
166                 if (JSValue parsedObject = preparser.tryLiteralParse())
167                     return parsedObject;                
168             }
169         }
170         
171         // If the literal parser bailed, it should not have thrown exceptions.
172         ASSERT(!callFrame->vm().exception());
173
174         ThisTDZMode thisTDZMode = callerCodeBlock->unlinkedCodeBlock()->constructorKind() == ConstructorKind::Derived ? ThisTDZMode::AlwaysCheck : ThisTDZMode::CheckIfNeeded;
175         eval = callerCodeBlock->evalCodeCache().getSlow(callFrame, callerCodeBlock->ownerScriptExecutable(), callerCodeBlock->isStrictMode(), thisTDZMode, programSource, callerScopeChain);
176         if (!eval)
177             return jsUndefined();
178     }
179
180     JSValue thisValue = callerFrame->thisValue();
181     Interpreter* interpreter = callFrame->vm().interpreter;
182     return interpreter->execute(eval, callFrame, thisValue, callerScopeChain);
183 }
184
185 unsigned sizeOfVarargs(CallFrame* callFrame, JSValue arguments, uint32_t firstVarArgOffset)
186 {
187     if (UNLIKELY(!arguments.isCell())) {
188         if (arguments.isUndefinedOrNull())
189             return 0;
190         
191         callFrame->vm().throwException(callFrame, createInvalidFunctionApplyParameterError(callFrame, arguments));
192         return 0;
193     }
194     
195     JSCell* cell = arguments.asCell();
196     unsigned length;
197     switch (cell->type()) {
198     case DirectArgumentsType:
199         length = jsCast<DirectArguments*>(cell)->length(callFrame);
200         break;
201     case ScopedArgumentsType:
202         length =jsCast<ScopedArguments*>(cell)->length(callFrame);
203         break;
204     case StringType:
205         callFrame->vm().throwException(callFrame, createInvalidFunctionApplyParameterError(callFrame,  arguments));
206         return 0;
207     default:
208         ASSERT(arguments.isObject());
209         if (isJSArray(cell))
210             length = jsCast<JSArray*>(cell)->length();
211         else
212             length = jsCast<JSObject*>(cell)->get(callFrame, callFrame->propertyNames().length).toUInt32(callFrame);
213         break;
214     }
215     
216     if (length >= firstVarArgOffset)
217         length -= firstVarArgOffset;
218     else
219         length = 0;
220     
221     return length;
222 }
223
224 unsigned sizeFrameForVarargs(CallFrame* callFrame, JSStack* stack, JSValue arguments, unsigned numUsedStackSlots, uint32_t firstVarArgOffset)
225 {
226     unsigned length = sizeOfVarargs(callFrame, arguments, firstVarArgOffset);
227     
228     CallFrame* calleeFrame = calleeFrameForVarargs(callFrame, numUsedStackSlots, length + 1);
229     if (length > maxArguments || !stack->ensureCapacityFor(calleeFrame->registers())) {
230         throwStackOverflowError(callFrame);
231         return 0;
232     }
233     
234     return length;
235 }
236
237 void loadVarargs(CallFrame* callFrame, VirtualRegister firstElementDest, JSValue arguments, uint32_t offset, uint32_t length)
238 {
239     if (UNLIKELY(!arguments.isCell()))
240         return;
241     
242     JSCell* cell = arguments.asCell();
243     switch (cell->type()) {
244     case DirectArgumentsType:
245         jsCast<DirectArguments*>(cell)->copyToArguments(callFrame, firstElementDest, offset, length);
246         return;
247     case ScopedArgumentsType:
248         jsCast<ScopedArguments*>(cell)->copyToArguments(callFrame, firstElementDest, offset, length);
249         return;
250     default: {
251         ASSERT(arguments.isObject());
252         JSObject* object = jsCast<JSObject*>(cell);
253         if (isJSArray(object)) {
254             jsCast<JSArray*>(object)->copyToArguments(callFrame, firstElementDest, offset, length);
255             return;
256         }
257         unsigned i;
258         for (i = 0; i < length && object->canGetIndexQuickly(i + offset); ++i)
259             callFrame->r(firstElementDest + i) = object->getIndexQuickly(i + offset);
260         for (; i < length; ++i)
261             callFrame->r(firstElementDest + i) = object->get(callFrame, i + offset);
262         return;
263     } }
264 }
265
266 void setupVarargsFrame(CallFrame* callFrame, CallFrame* newCallFrame, JSValue arguments, uint32_t offset, uint32_t length)
267 {
268     VirtualRegister calleeFrameOffset(newCallFrame - callFrame);
269     
270     loadVarargs(
271         callFrame,
272         calleeFrameOffset + CallFrame::argumentOffset(0),
273         arguments, offset, length);
274     
275     newCallFrame->setArgumentCountIncludingThis(length + 1);
276 }
277
278 void setupVarargsFrameAndSetThis(CallFrame* callFrame, CallFrame* newCallFrame, JSValue thisValue, JSValue arguments, uint32_t firstVarArgOffset, uint32_t length)
279 {
280     setupVarargsFrame(callFrame, newCallFrame, arguments, firstVarArgOffset, length);
281     newCallFrame->setThisValue(thisValue);
282 }
283
284 Interpreter::Interpreter(VM& vm)
285     : m_sampleEntryDepth(0)
286     , m_vm(vm)
287     , m_stack(vm)
288     , m_errorHandlingModeReentry(0)
289 #if !ASSERT_DISABLED
290     , m_initialized(false)
291 #endif
292 {
293 }
294
295 Interpreter::~Interpreter()
296 {
297 }
298
299 void Interpreter::initialize()
300 {
301 #if ENABLE(COMPUTED_GOTO_OPCODES)
302     m_opcodeTable = LLInt::opcodeMap();
303     for (int i = 0; i < numOpcodeIDs; ++i)
304         m_opcodeIDTable.add(m_opcodeTable[i], static_cast<OpcodeID>(i));
305 #endif
306
307 #if !ASSERT_DISABLED
308     m_initialized = true;
309 #endif
310
311 #if ENABLE(OPCODE_SAMPLING)
312     enableSampler();
313 #endif
314 }
315
316 #ifdef NDEBUG
317
318 void Interpreter::dumpCallFrame(CallFrame*)
319 {
320 }
321
322 #else
323
324 void Interpreter::dumpCallFrame(CallFrame* callFrame)
325 {
326     callFrame->codeBlock()->dumpBytecode();
327     dumpRegisters(callFrame);
328 }
329
330 class DumpRegisterFunctor {
331 public:
332     DumpRegisterFunctor(const Register*& it)
333         : m_hasSkippedFirstFrame(false)
334         , m_it(it)
335     {
336     }
337
338     StackVisitor::Status operator()(StackVisitor& visitor)
339     {
340         if (!m_hasSkippedFirstFrame) {
341             m_hasSkippedFirstFrame = true;
342             return StackVisitor::Continue;
343         }
344
345         unsigned line = 0;
346         unsigned unusedColumn = 0;
347         visitor->computeLineAndColumn(line, unusedColumn);
348         dataLogF("[ReturnVPC]                | %10p | %d (line %d)\n", m_it, visitor->bytecodeOffset(), line);
349         --m_it;
350         return StackVisitor::Done;
351     }
352
353 private:
354     bool m_hasSkippedFirstFrame;
355     const Register*& m_it;
356 };
357
358 void Interpreter::dumpRegisters(CallFrame* callFrame)
359 {
360     dataLogF("Register frame: \n\n");
361     dataLogF("-----------------------------------------------------------------------------\n");
362     dataLogF("            use            |   address  |                value               \n");
363     dataLogF("-----------------------------------------------------------------------------\n");
364
365     CodeBlock* codeBlock = callFrame->codeBlock();
366     const Register* it;
367     const Register* end;
368
369     it = callFrame->registers() + JSStack::ThisArgument + callFrame->argumentCount();
370     end = callFrame->registers() + JSStack::ThisArgument - 1;
371     while (it > end) {
372         JSValue v = it->jsValue();
373         int registerNumber = it - callFrame->registers();
374         String name = codeBlock->nameForRegister(VirtualRegister(registerNumber));
375         dataLogF("[r% 3d %14s]      | %10p | %-16s 0x%lld \n", registerNumber, name.ascii().data(), it, toCString(v).data(), (long long)JSValue::encode(v));
376         --it;
377     }
378     
379     dataLogF("-----------------------------------------------------------------------------\n");
380     dataLogF("[ArgumentCount]            | %10p | %lu \n", it, (unsigned long) callFrame->argumentCount());
381     --it;
382     dataLogF("[CallerFrame]              | %10p | %p \n", it, callFrame->callerFrame());
383     --it;
384     dataLogF("[Callee]                   | %10p | %p \n", it, callFrame->callee());
385     --it;
386     // FIXME: Remove the next decrement when the ScopeChain slot is removed from the call header
387     --it;
388 #if ENABLE(JIT)
389     AbstractPC pc = callFrame->abstractReturnPC(callFrame->vm());
390     if (pc.hasJITReturnAddress())
391         dataLogF("[ReturnJITPC]              | %10p | %p \n", it, pc.jitReturnAddress().value());
392 #endif
393
394     DumpRegisterFunctor functor(it);
395     callFrame->iterate(functor);
396
397     dataLogF("[CodeBlock]                | %10p | %p \n", it, callFrame->codeBlock());
398     --it;
399     dataLogF("-----------------------------------------------------------------------------\n");
400
401     end = it - codeBlock->m_numVars;
402     if (it != end) {
403         do {
404             JSValue v = it->jsValue();
405             int registerNumber = it - callFrame->registers();
406             String name = codeBlock->nameForRegister(VirtualRegister(registerNumber));
407             dataLogF("[r% 3d %14s]      | %10p | %-16s 0x%lld \n", registerNumber, name.ascii().data(), it, toCString(v).data(), (long long)JSValue::encode(v));
408             --it;
409         } while (it != end);
410     }
411     dataLogF("-----------------------------------------------------------------------------\n");
412
413     end = it - codeBlock->m_numCalleeRegisters + codeBlock->m_numVars;
414     if (it != end) {
415         do {
416             JSValue v = (*it).jsValue();
417             int registerNumber = it - callFrame->registers();
418             dataLogF("[r% 3d]                     | %10p | %-16s 0x%lld \n", registerNumber, it, toCString(v).data(), (long long)JSValue::encode(v));
419             --it;
420         } while (it != end);
421     }
422     dataLogF("-----------------------------------------------------------------------------\n");
423 }
424
425 #endif
426
427 bool Interpreter::isOpcode(Opcode opcode)
428 {
429 #if ENABLE(COMPUTED_GOTO_OPCODES)
430     return opcode != HashTraits<Opcode>::emptyValue()
431         && !HashTraits<Opcode>::isDeletedValue(opcode)
432         && m_opcodeIDTable.contains(opcode);
433 #else
434     return opcode >= 0 && opcode <= op_end;
435 #endif
436 }
437
438 static StackFrameCodeType getStackFrameCodeType(StackVisitor& visitor)
439 {
440     switch (visitor->codeType()) {
441     case StackVisitor::Frame::Eval:
442         return StackFrameEvalCode;
443     case StackVisitor::Frame::Module:
444         return StackFrameModuleCode;
445     case StackVisitor::Frame::Function:
446         return StackFrameFunctionCode;
447     case StackVisitor::Frame::Global:
448         return StackFrameGlobalCode;
449     case StackVisitor::Frame::Native:
450         ASSERT_NOT_REACHED();
451         return StackFrameNativeCode;
452     }
453     RELEASE_ASSERT_NOT_REACHED();
454     return StackFrameGlobalCode;
455 }
456
457 void StackFrame::computeLineAndColumn(unsigned& line, unsigned& column)
458 {
459     if (!codeBlock) {
460         line = 0;
461         column = 0;
462         return;
463     }
464
465     int divot = 0;
466     int unusedStartOffset = 0;
467     int unusedEndOffset = 0;
468     unsigned divotLine = 0;
469     unsigned divotColumn = 0;
470     expressionInfo(divot, unusedStartOffset, unusedEndOffset, divotLine, divotColumn);
471
472     line = divotLine + lineOffset;
473     column = divotColumn + (divotLine ? 1 : firstLineColumnOffset);
474
475     if (executable->hasOverrideLineNumber())
476         line = executable->overrideLineNumber();
477 }
478
479 void StackFrame::expressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column)
480 {
481     codeBlock->expressionRangeForBytecodeOffset(bytecodeOffset, divot, startOffset, endOffset, line, column);
482     divot += characterOffset;
483 }
484
485 String StackFrame::toString(CallFrame* callFrame)
486 {
487     StringBuilder traceBuild;
488     String functionName = friendlyFunctionName(callFrame);
489     String sourceURL = friendlySourceURL();
490     traceBuild.append(functionName);
491     if (!sourceURL.isEmpty()) {
492         if (!functionName.isEmpty())
493             traceBuild.append('@');
494         traceBuild.append(sourceURL);
495         if (codeType != StackFrameNativeCode) {
496             unsigned line;
497             unsigned column;
498             computeLineAndColumn(line, column);
499
500             traceBuild.append(':');
501             traceBuild.appendNumber(line);
502             traceBuild.append(':');
503             traceBuild.appendNumber(column);
504         }
505     }
506     return traceBuild.toString().impl();
507 }
508
509 static inline bool isWebAssemblyExecutable(ExecutableBase* executable)
510 {
511 #if !ENABLE(WEBASSEMBLY)
512     UNUSED_PARAM(executable);
513     return false;
514 #else
515     return executable->isWebAssemblyExecutable();
516 #endif
517 }
518
519 class GetStackTraceFunctor {
520 public:
521     GetStackTraceFunctor(VM& vm, Vector<StackFrame>& results, size_t remainingCapacity)
522         : m_vm(vm)
523         , m_results(results)
524         , m_remainingCapacityForFrameCapture(remainingCapacity)
525     {
526     }
527
528     StackVisitor::Status operator()(StackVisitor& visitor)
529     {
530         VM& vm = m_vm;
531         if (m_remainingCapacityForFrameCapture) {
532             if (visitor->isJSFrame()
533                 && !isWebAssemblyExecutable(visitor->codeBlock()->ownerExecutable())
534                 && !visitor->codeBlock()->unlinkedCodeBlock()->isBuiltinFunction()) {
535                 CodeBlock* codeBlock = visitor->codeBlock();
536                 StackFrame s = {
537                     Strong<JSObject>(vm, visitor->callee()),
538                     getStackFrameCodeType(visitor),
539                     Strong<ScriptExecutable>(vm, codeBlock->ownerScriptExecutable()),
540                     Strong<UnlinkedCodeBlock>(vm, codeBlock->unlinkedCodeBlock()),
541                     codeBlock->source(),
542                     codeBlock->ownerScriptExecutable()->firstLine(),
543                     codeBlock->firstLineColumnOffset(),
544                     codeBlock->sourceOffset(),
545                     visitor->bytecodeOffset(),
546                     visitor->sourceURL()
547                 };
548                 m_results.append(s);
549             } else {
550                 StackFrame s = { Strong<JSObject>(vm, visitor->callee()), StackFrameNativeCode, Strong<ScriptExecutable>(), Strong<UnlinkedCodeBlock>(), 0, 0, 0, 0, 0, String()};
551                 m_results.append(s);
552             }
553     
554             m_remainingCapacityForFrameCapture--;
555             return StackVisitor::Continue;
556         }
557         return StackVisitor::Done;
558     }
559
560 private:
561     VM& m_vm;
562     Vector<StackFrame>& m_results;
563     size_t m_remainingCapacityForFrameCapture;
564 };
565
566 void Interpreter::getStackTrace(Vector<StackFrame>& results, size_t maxStackSize)
567 {
568     VM& vm = m_vm;
569     CallFrame* callFrame = vm.topCallFrame;
570     if (!callFrame)
571         return;
572
573     GetStackTraceFunctor functor(vm, results, maxStackSize);
574     callFrame->iterate(functor);
575 }
576
577 JSString* Interpreter::stackTraceAsString(ExecState* exec, Vector<StackFrame> stackTrace)
578 {
579     // FIXME: JSStringJoiner could be more efficient than StringBuilder here.
580     StringBuilder builder;
581     for (unsigned i = 0; i < stackTrace.size(); i++) {
582         builder.append(String(stackTrace[i].toString(exec)));
583         if (i != stackTrace.size() - 1)
584             builder.append('\n');
585     }
586     return jsString(&exec->vm(), builder.toString());
587 }
588
589 ALWAYS_INLINE static HandlerInfo* findExceptionHandler(StackVisitor& visitor, CodeBlock* codeBlock, CodeBlock::RequiredHandler requiredHandler)
590 {
591     ASSERT(codeBlock);
592 #if ENABLE(DFG_JIT)
593     ASSERT(!visitor->isInlinedFrame());
594 #endif
595
596     CallFrame* callFrame = visitor->callFrame();
597     unsigned exceptionHandlerIndex;
598     if (codeBlock->jitType() != JITCode::DFGJIT)
599         exceptionHandlerIndex = callFrame->bytecodeOffset();
600     else
601         exceptionHandlerIndex = callFrame->callSiteIndex().bits();
602
603     return codeBlock->handlerForIndex(exceptionHandlerIndex, requiredHandler);
604 }
605
606 class GetCatchHandlerFunctor {
607 public:
608     GetCatchHandlerFunctor()
609         : m_handler(0)
610     {
611     }
612
613     HandlerInfo* handler() { return m_handler; }
614
615     StackVisitor::Status operator()(StackVisitor& visitor)
616     {
617         visitor.unwindToMachineCodeBlockFrame();
618
619         CodeBlock* codeBlock = visitor->codeBlock();
620         if (!codeBlock)
621             return StackVisitor::Continue;
622
623         m_handler = findExceptionHandler(visitor, codeBlock, CodeBlock::RequiredHandler::CatchHandler);
624         if (m_handler)
625             return StackVisitor::Done;
626
627         return StackVisitor::Continue;
628     }
629
630 private:
631     HandlerInfo* m_handler;
632 };
633
634 ALWAYS_INLINE static void notifyDebuggerOfUnwinding(CallFrame* callFrame)
635 {
636     if (Debugger* debugger = callFrame->vmEntryGlobalObject()->debugger()) {
637         SuspendExceptionScope scope(&callFrame->vm());
638         if (jsDynamicCast<JSFunction*>(callFrame->callee()))
639             debugger->returnEvent(callFrame);
640         else
641             debugger->didExecuteProgram(callFrame);
642         ASSERT(!callFrame->hadException());
643     }
644 }
645
646 class UnwindFunctor {
647 public:
648     UnwindFunctor(CallFrame*& callFrame, bool isTermination, CodeBlock*& codeBlock, HandlerInfo*& handler)
649         : m_callFrame(callFrame)
650         , m_isTermination(isTermination)
651         , m_codeBlock(codeBlock)
652         , m_handler(handler)
653     {
654     }
655
656     StackVisitor::Status operator()(StackVisitor& visitor)
657     {
658         visitor.unwindToMachineCodeBlockFrame();
659         VM& vm = m_callFrame->vm();
660         m_callFrame = visitor->callFrame();
661         m_codeBlock = visitor->codeBlock();
662
663         m_handler = nullptr;
664         if (!m_isTermination) {
665             if (m_codeBlock && !isWebAssemblyExecutable(m_codeBlock->ownerExecutable()))
666                 m_handler = findExceptionHandler(visitor, m_codeBlock, CodeBlock::RequiredHandler::AnyHandler);
667         }
668
669         if (m_handler)
670             return StackVisitor::Done;
671
672         notifyDebuggerOfUnwinding(m_callFrame);
673
674         bool shouldStopUnwinding = visitor->callerIsVMEntryFrame();
675         if (shouldStopUnwinding) {
676             if (LegacyProfiler* profiler = vm.enabledProfiler())
677                 profiler->exceptionUnwind(m_callFrame);
678
679             copyCalleeSavesToVMCalleeSavesBuffer(visitor);
680
681             return StackVisitor::Done;
682         }
683
684         copyCalleeSavesToVMCalleeSavesBuffer(visitor);
685
686         return StackVisitor::Continue;
687     }
688
689 private:
690     void copyCalleeSavesToVMCalleeSavesBuffer(StackVisitor& visitor)
691     {
692 #if ENABLE(JIT) && NUMBER_OF_CALLEE_SAVES_REGISTERS > 0
693
694         if (!visitor->isJSFrame())
695             return;
696
697 #if ENABLE(DFG_JIT)
698         if (visitor->inlineCallFrame())
699             return;
700 #endif
701         RegisterAtOffsetList* currentCalleeSaves = m_codeBlock ? m_codeBlock->calleeSaveRegisters() : nullptr;
702
703         if (!currentCalleeSaves)
704             return;
705
706         VM& vm = m_callFrame->vm();
707         RegisterAtOffsetList* allCalleeSaves = vm.getAllCalleeSaveRegisterOffsets();
708         RegisterSet dontCopyRegisters = RegisterSet::stackRegisters();
709         intptr_t* frame = reinterpret_cast<intptr_t*>(m_callFrame->registers());
710
711         unsigned registerCount = currentCalleeSaves->size();
712         for (unsigned i = 0; i < registerCount; i++) {
713             RegisterAtOffset currentEntry = currentCalleeSaves->at(i);
714             if (dontCopyRegisters.get(currentEntry.reg()))
715                 continue;
716             RegisterAtOffset* vmCalleeSavesEntry = allCalleeSaves->find(currentEntry.reg());
717             
718             vm.calleeSaveRegistersBuffer[vmCalleeSavesEntry->offsetAsIndex()] = *(frame + currentEntry.offsetAsIndex());
719         }
720 #else
721         UNUSED_PARAM(visitor);
722 #endif
723     }
724
725     CallFrame*& m_callFrame;
726     bool m_isTermination;
727     CodeBlock*& m_codeBlock;
728     HandlerInfo*& m_handler;
729 };
730
731 NEVER_INLINE HandlerInfo* Interpreter::unwind(VM& vm, CallFrame*& callFrame, Exception* exception, UnwindStart unwindStart)
732 {
733     if (unwindStart == UnwindFromCallerFrame) {
734         if (callFrame->callerFrameOrVMEntryFrame() == vm.topVMEntryFrame)
735             return nullptr;
736
737         callFrame = callFrame->callerFrame();
738         vm.topCallFrame = callFrame;
739     }
740
741     CodeBlock* codeBlock = callFrame->codeBlock();
742
743     JSValue exceptionValue = exception->value();
744     ASSERT(!exceptionValue.isEmpty());
745     ASSERT(!exceptionValue.isCell() || exceptionValue.asCell());
746     // This shouldn't be possible (hence the assertions), but we're already in the slowest of
747     // slow cases, so let's harden against it anyway to be safe.
748     if (exceptionValue.isEmpty() || (exceptionValue.isCell() && !exceptionValue.asCell()))
749         exceptionValue = jsNull();
750
751     ASSERT(vm.exception() && vm.exception()->stack().size());
752
753     // Calculate an exception handler vPC, unwinding call frames as necessary.
754     HandlerInfo* handler = nullptr;
755     UnwindFunctor functor(callFrame, isTerminatedExecutionException(exception), codeBlock, handler);
756     callFrame->iterate(functor);
757     if (!handler)
758         return nullptr;
759
760     return handler;
761 }
762
763 void Interpreter::notifyDebuggerOfExceptionToBeThrown(CallFrame* callFrame, Exception* exception)
764 {
765     Debugger* debugger = callFrame->vmEntryGlobalObject()->debugger();
766     if (debugger && debugger->needsExceptionCallbacks() && !exception->didNotifyInspectorOfThrow()) {
767         // This code assumes that if the debugger is enabled then there is no inlining.
768         // If that assumption turns out to be false then we'll ignore the inlined call
769         // frames.
770         // https://bugs.webkit.org/show_bug.cgi?id=121754
771
772         bool hasCatchHandler;
773         bool isTermination = isTerminatedExecutionException(exception);
774         if (isTermination)
775             hasCatchHandler = false;
776         else {
777             GetCatchHandlerFunctor functor;
778             callFrame->iterate(functor);
779             HandlerInfo* handler = functor.handler();
780             ASSERT(!handler || handler->isCatchHandler());
781             hasCatchHandler = !!handler;
782         }
783
784         debugger->exception(callFrame, exception->value(), hasCatchHandler);
785     }
786     exception->setDidNotifyInspectorOfThrow();
787 }
788
789 static inline JSValue checkedReturn(JSValue returnValue)
790 {
791     ASSERT(returnValue);
792     return returnValue;
793 }
794
795 static inline JSObject* checkedReturn(JSObject* returnValue)
796 {
797     ASSERT(returnValue);
798     return returnValue;
799 }
800
801 class SamplingScope {
802 public:
803     SamplingScope(Interpreter* interpreter)
804         : m_interpreter(interpreter)
805     {
806         interpreter->startSampling();
807     }
808     ~SamplingScope()
809     {
810         m_interpreter->stopSampling();
811     }
812 private:
813     Interpreter* m_interpreter;
814 };
815
816 JSValue Interpreter::execute(ProgramExecutable* program, CallFrame* callFrame, JSObject* thisObj)
817 {
818     SamplingScope samplingScope(this);
819
820     JSScope* scope = thisObj->globalObject()->globalScope();
821     VM& vm = *scope->vm();
822
823     ASSERT(!vm.exception());
824     ASSERT(!vm.isCollectorBusy());
825     RELEASE_ASSERT(vm.currentThreadIsHoldingAPILock());
826     if (vm.isCollectorBusy())
827         return jsNull();
828
829     if (!vm.isSafeToRecurse())
830         return checkedReturn(throwStackOverflowError(callFrame));
831
832     // First check if the "program" is actually just a JSON object. If so,
833     // we'll handle the JSON object here. Else, we'll handle real JS code
834     // below at failedJSONP.
835
836     Vector<JSONPData> JSONPData;
837     bool parseResult;
838     const String programSource = program->source().toString();
839     if (programSource.isNull())
840         return jsUndefined();
841     if (programSource.is8Bit()) {
842         LiteralParser<LChar> literalParser(callFrame, programSource.characters8(), programSource.length(), JSONP);
843         parseResult = literalParser.tryJSONPParse(JSONPData, scope->globalObject()->globalObjectMethodTable()->supportsRichSourceInfo(scope->globalObject()));
844     } else {
845         LiteralParser<UChar> literalParser(callFrame, programSource.characters16(), programSource.length(), JSONP);
846         parseResult = literalParser.tryJSONPParse(JSONPData, scope->globalObject()->globalObjectMethodTable()->supportsRichSourceInfo(scope->globalObject()));
847     }
848
849     if (parseResult) {
850         JSGlobalObject* globalObject = scope->globalObject();
851         JSValue result;
852         for (unsigned entry = 0; entry < JSONPData.size(); entry++) {
853             Vector<JSONPPathEntry> JSONPPath;
854             JSONPPath.swap(JSONPData[entry].m_path);
855             JSValue JSONPValue = JSONPData[entry].m_value.get();
856             if (JSONPPath.size() == 1 && JSONPPath[0].m_type == JSONPPathEntryTypeDeclare) {
857                 globalObject->addVar(callFrame, JSONPPath[0].m_pathEntryName);
858                 PutPropertySlot slot(globalObject);
859                 globalObject->methodTable()->put(globalObject, callFrame, JSONPPath[0].m_pathEntryName, JSONPValue, slot);
860                 result = jsUndefined();
861                 continue;
862             }
863             JSValue baseObject(globalObject);
864             for (unsigned i = 0; i < JSONPPath.size() - 1; i++) {
865                 ASSERT(JSONPPath[i].m_type != JSONPPathEntryTypeDeclare);
866                 switch (JSONPPath[i].m_type) {
867                 case JSONPPathEntryTypeDot: {
868                     if (i == 0) {
869                         PropertySlot slot(globalObject);
870                         if (!globalObject->getPropertySlot(callFrame, JSONPPath[i].m_pathEntryName, slot)) {
871                             if (entry)
872                                 return callFrame->vm().throwException(callFrame, createUndefinedVariableError(callFrame, JSONPPath[i].m_pathEntryName));
873                             goto failedJSONP;
874                         }
875                         baseObject = slot.getValue(callFrame, JSONPPath[i].m_pathEntryName);
876                     } else
877                         baseObject = baseObject.get(callFrame, JSONPPath[i].m_pathEntryName);
878                     if (callFrame->hadException())
879                         return jsUndefined();
880                     continue;
881                 }
882                 case JSONPPathEntryTypeLookup: {
883                     baseObject = baseObject.get(callFrame, JSONPPath[i].m_pathIndex);
884                     if (callFrame->hadException())
885                         return jsUndefined();
886                     continue;
887                 }
888                 default:
889                     RELEASE_ASSERT_NOT_REACHED();
890                     return jsUndefined();
891                 }
892             }
893             PutPropertySlot slot(baseObject);
894             switch (JSONPPath.last().m_type) {
895             case JSONPPathEntryTypeCall: {
896                 JSValue function = baseObject.get(callFrame, JSONPPath.last().m_pathEntryName);
897                 if (callFrame->hadException())
898                     return jsUndefined();
899                 CallData callData;
900                 CallType callType = getCallData(function, callData);
901                 if (callType == CallTypeNone)
902                     return callFrame->vm().throwException(callFrame, createNotAFunctionError(callFrame, function));
903                 MarkedArgumentBuffer jsonArg;
904                 jsonArg.append(JSONPValue);
905                 JSValue thisValue = JSONPPath.size() == 1 ? jsUndefined(): baseObject;
906                 JSONPValue = JSC::call(callFrame, function, callType, callData, thisValue, jsonArg);
907                 if (callFrame->hadException())
908                     return jsUndefined();
909                 break;
910             }
911             case JSONPPathEntryTypeDot: {
912                 baseObject.put(callFrame, JSONPPath.last().m_pathEntryName, JSONPValue, slot);
913                 if (callFrame->hadException())
914                     return jsUndefined();
915                 break;
916             }
917             case JSONPPathEntryTypeLookup: {
918                 baseObject.putByIndex(callFrame, JSONPPath.last().m_pathIndex, JSONPValue, slot.isStrictMode());
919                 if (callFrame->hadException())
920                     return jsUndefined();
921                 break;
922             }
923             default:
924                 RELEASE_ASSERT_NOT_REACHED();
925                     return jsUndefined();
926             }
927             result = JSONPValue;
928         }
929         return result;
930     }
931 failedJSONP:
932     // If we get here, then we have already proven that the script is not a JSON
933     // object.
934
935     VMEntryScope entryScope(vm, scope->globalObject());
936
937     // Compile source to bytecode if necessary:
938     if (JSObject* error = program->initializeGlobalProperties(vm, callFrame, scope))
939         return checkedReturn(callFrame->vm().throwException(callFrame, error));
940
941     if (JSObject* error = program->prepareForExecution(callFrame, nullptr, scope, CodeForCall))
942         return checkedReturn(callFrame->vm().throwException(callFrame, error));
943
944     ProgramCodeBlock* codeBlock = program->codeBlock();
945
946     if (UNLIKELY(vm.shouldTriggerTermination(callFrame)))
947         return throwTerminatedExecutionException(callFrame);
948
949     ASSERT(codeBlock->numParameters() == 1); // 1 parameter for 'this'.
950
951     ProtoCallFrame protoCallFrame;
952     protoCallFrame.init(codeBlock, JSCallee::create(vm, scope->globalObject(), scope), thisObj, 1);
953
954     if (LegacyProfiler* profiler = vm.enabledProfiler())
955         profiler->willExecute(callFrame, program->sourceURL(), program->firstLine(), program->startColumn());
956
957     // Execute the code:
958     JSValue result;
959     {
960         SamplingTool::CallRecord callRecord(m_sampler.get());
961         result = program->generatedJITCode()->execute(&vm, &protoCallFrame);
962     }
963
964     if (LegacyProfiler* profiler = vm.enabledProfiler())
965         profiler->didExecute(callFrame, program->sourceURL(), program->firstLine(), program->startColumn());
966
967     return checkedReturn(result);
968 }
969
970 JSValue Interpreter::executeCall(CallFrame* callFrame, JSObject* function, CallType callType, const CallData& callData, JSValue thisValue, const ArgList& args)
971 {
972     VM& vm = callFrame->vm();
973     ASSERT(!callFrame->hadException());
974     ASSERT(!vm.isCollectorBusy());
975     if (vm.isCollectorBusy())
976         return jsNull();
977
978     bool isJSCall = (callType == CallTypeJS);
979     JSScope* scope = nullptr;
980     CodeBlock* newCodeBlock;
981     size_t argsCount = 1 + args.size(); // implicit "this" parameter
982
983     JSGlobalObject* globalObject;
984
985     if (isJSCall) {
986         scope = callData.js.scope;
987         globalObject = scope->globalObject();
988     } else {
989         ASSERT(callType == CallTypeHost);
990         globalObject = function->globalObject();
991     }
992
993     VMEntryScope entryScope(vm, globalObject);
994     if (!vm.isSafeToRecurse())
995         return checkedReturn(throwStackOverflowError(callFrame));
996
997     if (isJSCall) {
998         // Compile the callee:
999         JSObject* compileError = callData.js.functionExecutable->prepareForExecution(callFrame, jsCast<JSFunction*>(function), scope, CodeForCall);
1000         if (UNLIKELY(!!compileError)) {
1001             return checkedReturn(callFrame->vm().throwException(callFrame, compileError));
1002         }
1003         newCodeBlock = callData.js.functionExecutable->codeBlockForCall();
1004         ASSERT(!!newCodeBlock);
1005         newCodeBlock->m_shouldAlwaysBeInlined = false;
1006     } else
1007         newCodeBlock = 0;
1008
1009     if (UNLIKELY(vm.shouldTriggerTermination(callFrame)))
1010         return throwTerminatedExecutionException(callFrame);
1011
1012     ProtoCallFrame protoCallFrame;
1013     protoCallFrame.init(newCodeBlock, function, thisValue, argsCount, args.data());
1014
1015     if (LegacyProfiler* profiler = vm.enabledProfiler())
1016         profiler->willExecute(callFrame, function);
1017
1018     JSValue result;
1019     {
1020         SamplingTool::CallRecord callRecord(m_sampler.get(), !isJSCall);
1021
1022         // Execute the code:
1023         if (isJSCall)
1024             result = callData.js.functionExecutable->generatedJITCodeForCall()->execute(&vm, &protoCallFrame);
1025         else {
1026             result = JSValue::decode(vmEntryToNative(reinterpret_cast<void*>(callData.native.function), &vm, &protoCallFrame));
1027             if (callFrame->hadException())
1028                 result = jsNull();
1029         }
1030     }
1031
1032     if (LegacyProfiler* profiler = vm.enabledProfiler())
1033         profiler->didExecute(callFrame, function);
1034
1035     return checkedReturn(result);
1036 }
1037
1038 JSObject* Interpreter::executeConstruct(CallFrame* callFrame, JSObject* constructor, ConstructType constructType, const ConstructData& constructData, const ArgList& args, JSValue newTarget)
1039 {
1040     VM& vm = callFrame->vm();
1041     ASSERT(!callFrame->hadException());
1042     ASSERT(!vm.isCollectorBusy());
1043     // We throw in this case because we have to return something "valid" but we're
1044     // already in an invalid state.
1045     if (vm.isCollectorBusy())
1046         return checkedReturn(throwStackOverflowError(callFrame));
1047
1048     bool isJSConstruct = (constructType == ConstructTypeJS);
1049     JSScope* scope = nullptr;
1050     CodeBlock* newCodeBlock;
1051     size_t argsCount = 1 + args.size(); // implicit "this" parameter
1052
1053     JSGlobalObject* globalObject;
1054
1055     if (isJSConstruct) {
1056         scope = constructData.js.scope;
1057         globalObject = scope->globalObject();
1058     } else {
1059         ASSERT(constructType == ConstructTypeHost);
1060         globalObject = constructor->globalObject();
1061     }
1062
1063     VMEntryScope entryScope(vm, globalObject);
1064     if (!vm.isSafeToRecurse())
1065         return checkedReturn(throwStackOverflowError(callFrame));
1066
1067     if (isJSConstruct) {
1068         // Compile the callee:
1069         JSObject* compileError = constructData.js.functionExecutable->prepareForExecution(callFrame, jsCast<JSFunction*>(constructor), scope, CodeForConstruct);
1070         if (UNLIKELY(!!compileError)) {
1071             return checkedReturn(callFrame->vm().throwException(callFrame, compileError));
1072         }
1073         newCodeBlock = constructData.js.functionExecutable->codeBlockForConstruct();
1074         ASSERT(!!newCodeBlock);
1075         newCodeBlock->m_shouldAlwaysBeInlined = false;
1076     } else
1077         newCodeBlock = 0;
1078
1079     if (UNLIKELY(vm.shouldTriggerTermination(callFrame)))
1080         return throwTerminatedExecutionException(callFrame);
1081
1082     ProtoCallFrame protoCallFrame;
1083     protoCallFrame.init(newCodeBlock, constructor, newTarget, argsCount, args.data());
1084
1085     if (LegacyProfiler* profiler = vm.enabledProfiler())
1086         profiler->willExecute(callFrame, constructor);
1087
1088     JSValue result;
1089     {
1090         SamplingTool::CallRecord callRecord(m_sampler.get(), !isJSConstruct);
1091
1092         // Execute the code.
1093         if (isJSConstruct)
1094             result = constructData.js.functionExecutable->generatedJITCodeForConstruct()->execute(&vm, &protoCallFrame);
1095         else {
1096             result = JSValue::decode(vmEntryToNative(reinterpret_cast<void*>(constructData.native.function), &vm, &protoCallFrame));
1097
1098             if (!callFrame->hadException())
1099                 RELEASE_ASSERT(result.isObject());
1100         }
1101     }
1102
1103     if (LegacyProfiler* profiler = vm.enabledProfiler())
1104         profiler->didExecute(callFrame, constructor);
1105
1106     if (callFrame->hadException())
1107         return 0;
1108     ASSERT(result.isObject());
1109     return checkedReturn(asObject(result));
1110 }
1111
1112 CallFrameClosure Interpreter::prepareForRepeatCall(FunctionExecutable* functionExecutable, CallFrame* callFrame, ProtoCallFrame* protoCallFrame, JSFunction* function, int argumentCountIncludingThis, JSScope* scope, JSValue* args)
1113 {
1114     VM& vm = *scope->vm();
1115     ASSERT(!vm.exception());
1116     
1117     if (vm.isCollectorBusy())
1118         return CallFrameClosure();
1119
1120     // Compile the callee:
1121     JSObject* error = functionExecutable->prepareForExecution(callFrame, function, scope, CodeForCall);
1122     if (error) {
1123         callFrame->vm().throwException(callFrame, error);
1124         return CallFrameClosure();
1125     }
1126     CodeBlock* newCodeBlock = functionExecutable->codeBlockForCall();
1127     newCodeBlock->m_shouldAlwaysBeInlined = false;
1128
1129     size_t argsCount = argumentCountIncludingThis;
1130
1131     protoCallFrame->init(newCodeBlock, function, jsUndefined(), argsCount, args);
1132     // Return the successful closure:
1133     CallFrameClosure result = { callFrame, protoCallFrame, function, functionExecutable, &vm, scope, newCodeBlock->numParameters(), argumentCountIncludingThis };
1134     return result;
1135 }
1136
1137 JSValue Interpreter::execute(CallFrameClosure& closure) 
1138 {
1139     VM& vm = *closure.vm;
1140     SamplingScope samplingScope(this);
1141     
1142     ASSERT(!vm.isCollectorBusy());
1143     RELEASE_ASSERT(vm.currentThreadIsHoldingAPILock());
1144     if (vm.isCollectorBusy())
1145         return jsNull();
1146
1147     StackStats::CheckPoint stackCheckPoint;
1148
1149     if (LegacyProfiler* profiler = vm.enabledProfiler())
1150         profiler->willExecute(closure.oldCallFrame, closure.function);
1151
1152     if (UNLIKELY(vm.shouldTriggerTermination(closure.oldCallFrame)))
1153         return throwTerminatedExecutionException(closure.oldCallFrame);
1154
1155     // Execute the code:
1156     JSValue result;
1157     {
1158         SamplingTool::CallRecord callRecord(m_sampler.get());
1159         result = closure.functionExecutable->generatedJITCodeForCall()->execute(&vm, closure.protoCallFrame);
1160     }
1161
1162     if (LegacyProfiler* profiler = vm.enabledProfiler())
1163         profiler->didExecute(closure.oldCallFrame, closure.function);
1164
1165     return checkedReturn(result);
1166 }
1167
1168 JSValue Interpreter::execute(EvalExecutable* eval, CallFrame* callFrame, JSValue thisValue, JSScope* scope)
1169 {
1170     VM& vm = *scope->vm();
1171     SamplingScope samplingScope(this);
1172     
1173     ASSERT(scope->vm() == &callFrame->vm());
1174     ASSERT(!vm.exception());
1175     ASSERT(!vm.isCollectorBusy());
1176     RELEASE_ASSERT(vm.currentThreadIsHoldingAPILock());
1177     if (vm.isCollectorBusy())
1178         return jsNull();
1179
1180     VMEntryScope entryScope(vm, scope->globalObject());
1181     if (!vm.isSafeToRecurse())
1182         return checkedReturn(throwStackOverflowError(callFrame));        
1183
1184     unsigned numVariables = eval->numVariables();
1185     int numFunctions = eval->numberOfFunctionDecls();
1186
1187     JSScope* variableObject;
1188     if ((numVariables || numFunctions) && eval->isStrictMode()) {
1189         scope = StrictEvalActivation::create(callFrame, scope);
1190         variableObject = scope;
1191     } else {
1192         for (JSScope* node = scope; ; node = node->next()) {
1193             RELEASE_ASSERT(node);
1194             if (node->isGlobalObject()) {
1195                 variableObject = node;
1196                 break;
1197             } 
1198             if (JSLexicalEnvironment* lexicalEnvironment = jsDynamicCast<JSLexicalEnvironment*>(node)) {
1199                 if (lexicalEnvironment->symbolTable()->scopeType() == SymbolTable::ScopeType::VarScope) {
1200                     variableObject = node;
1201                     break;
1202                 }
1203             }
1204         }
1205     }
1206
1207     JSObject* compileError = eval->prepareForExecution(callFrame, nullptr, scope, CodeForCall);
1208     if (UNLIKELY(!!compileError))
1209         return checkedReturn(callFrame->vm().throwException(callFrame, compileError));
1210     EvalCodeBlock* codeBlock = eval->codeBlock();
1211
1212     // We can't declare a "var"/"function" that overwrites a global "let"/"const"/"class" in a sloppy-mode eval.
1213     if (variableObject->isGlobalObject() && !eval->isStrictMode() && (numVariables || numFunctions)) {
1214         JSGlobalLexicalEnvironment* globalLexicalEnvironment = jsCast<JSGlobalObject*>(variableObject)->globalLexicalEnvironment();
1215         for (unsigned i = 0; i < numVariables; ++i) {
1216             const Identifier& ident = codeBlock->variable(i);
1217             PropertySlot slot(globalLexicalEnvironment);
1218             if (JSGlobalLexicalEnvironment::getOwnPropertySlot(globalLexicalEnvironment, callFrame, ident, slot)) {
1219                 return checkedReturn(callFrame->vm().throwException(callFrame,
1220                     createTypeError(callFrame, makeString("Can't create duplicate global variable in eval: '", String(ident.impl()), "'"))));
1221             }
1222         }
1223
1224         for (int i = 0; i < numFunctions; ++i) {
1225             FunctionExecutable* function = codeBlock->functionDecl(i);
1226             PropertySlot slot(globalLexicalEnvironment);
1227             if (JSGlobalLexicalEnvironment::getOwnPropertySlot(globalLexicalEnvironment, callFrame, function->name(), slot)) {
1228                 return checkedReturn(callFrame->vm().throwException(callFrame,
1229                     createTypeError(callFrame, makeString("Can't create duplicate global variable in eval: '", String(function->name().impl()), "'"))));
1230             }
1231         }
1232     }
1233
1234     if (numVariables || numFunctions) {
1235         BatchedTransitionOptimizer optimizer(vm, variableObject);
1236         if (variableObject->next())
1237             variableObject->globalObject()->varInjectionWatchpoint()->fireAll("Executed eval, fired VarInjection watchpoint");
1238
1239         for (unsigned i = 0; i < numVariables; ++i) {
1240             const Identifier& ident = codeBlock->variable(i);
1241             if (!variableObject->hasProperty(callFrame, ident)) {
1242                 PutPropertySlot slot(variableObject);
1243                 variableObject->methodTable()->put(variableObject, callFrame, ident, jsUndefined(), slot);
1244             }
1245         }
1246
1247         for (int i = 0; i < numFunctions; ++i) {
1248             FunctionExecutable* function = codeBlock->functionDecl(i);
1249             PutPropertySlot slot(variableObject);
1250             variableObject->methodTable()->put(variableObject, callFrame, function->name(), JSFunction::create(vm, function, scope), slot);
1251         }
1252     }
1253
1254     if (UNLIKELY(vm.shouldTriggerTermination(callFrame)))
1255         return throwTerminatedExecutionException(callFrame);
1256
1257     ASSERT(codeBlock->numParameters() == 1); // 1 parameter for 'this'.
1258
1259     ProtoCallFrame protoCallFrame;
1260     protoCallFrame.init(codeBlock, JSCallee::create(vm, scope->globalObject(), scope), thisValue, 1);
1261
1262     if (LegacyProfiler* profiler = vm.enabledProfiler())
1263         profiler->willExecute(callFrame, eval->sourceURL(), eval->firstLine(), eval->startColumn());
1264
1265     // Execute the code:
1266     JSValue result;
1267     {
1268         SamplingTool::CallRecord callRecord(m_sampler.get());
1269         result = eval->generatedJITCode()->execute(&vm, &protoCallFrame);
1270     }
1271
1272     if (LegacyProfiler* profiler = vm.enabledProfiler())
1273         profiler->didExecute(callFrame, eval->sourceURL(), eval->firstLine(), eval->startColumn());
1274
1275     return checkedReturn(result);
1276 }
1277
1278 JSValue Interpreter::execute(ModuleProgramExecutable* executable, CallFrame* callFrame, JSModuleEnvironment* scope)
1279 {
1280     VM& vm = *scope->vm();
1281     SamplingScope samplingScope(this);
1282
1283     ASSERT(scope->vm() == &callFrame->vm());
1284     ASSERT(!vm.exception());
1285     ASSERT(!vm.isCollectorBusy());
1286     RELEASE_ASSERT(vm.currentThreadIsHoldingAPILock());
1287     if (vm.isCollectorBusy())
1288         return jsNull();
1289
1290     VMEntryScope entryScope(vm, scope->globalObject());
1291     if (!vm.isSafeToRecurse())
1292         return checkedReturn(throwStackOverflowError(callFrame));
1293
1294     JSObject* compileError = executable->prepareForExecution(callFrame, nullptr, scope, CodeForCall);
1295     if (UNLIKELY(!!compileError))
1296         return checkedReturn(callFrame->vm().throwException(callFrame, compileError));
1297     ModuleProgramCodeBlock* codeBlock = executable->codeBlock();
1298
1299     if (UNLIKELY(vm.watchdog && vm.watchdog->didFire(callFrame)))
1300         return throwTerminatedExecutionException(callFrame);
1301
1302     ASSERT(codeBlock->numParameters() == 1); // 1 parameter for 'this'.
1303
1304     // The |this| of the module is always `undefined`.
1305     // http://www.ecma-international.org/ecma-262/6.0/#sec-module-environment-records-hasthisbinding
1306     // http://www.ecma-international.org/ecma-262/6.0/#sec-module-environment-records-getthisbinding
1307     ProtoCallFrame protoCallFrame;
1308     protoCallFrame.init(codeBlock, JSCallee::create(vm, scope->globalObject(), scope), jsUndefined(), 1);
1309
1310     if (LegacyProfiler* profiler = vm.enabledProfiler())
1311         profiler->willExecute(callFrame, executable->sourceURL(), executable->firstLine(), executable->startColumn());
1312
1313     // Execute the code:
1314     JSValue result;
1315     {
1316         SamplingTool::CallRecord callRecord(m_sampler.get());
1317         result = executable->generatedJITCode()->execute(&vm, &protoCallFrame);
1318     }
1319
1320     if (LegacyProfiler* profiler = vm.enabledProfiler())
1321         profiler->didExecute(callFrame, executable->sourceURL(), executable->firstLine(), executable->startColumn());
1322
1323     return checkedReturn(result);
1324 }
1325
1326 NEVER_INLINE void Interpreter::debug(CallFrame* callFrame, DebugHookID debugHookID)
1327 {
1328     Debugger* debugger = callFrame->vmEntryGlobalObject()->debugger();
1329     if (!debugger)
1330         return;
1331
1332     ASSERT(callFrame->codeBlock()->hasDebuggerRequests());
1333     ASSERT(!callFrame->hadException());
1334
1335     switch (debugHookID) {
1336         case DidEnterCallFrame:
1337             debugger->callEvent(callFrame);
1338             break;
1339         case WillLeaveCallFrame:
1340             debugger->returnEvent(callFrame);
1341             break;
1342         case WillExecuteStatement:
1343             debugger->atStatement(callFrame);
1344             break;
1345         case WillExecuteProgram:
1346             debugger->willExecuteProgram(callFrame);
1347             break;
1348         case DidExecuteProgram:
1349             debugger->didExecuteProgram(callFrame);
1350             break;
1351         case DidReachBreakpoint:
1352             debugger->didReachBreakpoint(callFrame);
1353             break;
1354     }
1355     ASSERT(!callFrame->hadException());
1356 }    
1357
1358 void Interpreter::enableSampler()
1359 {
1360 #if ENABLE(OPCODE_SAMPLING)
1361     if (!m_sampler) {
1362         m_sampler = std::make_unique<SamplingTool>(this);
1363         m_sampler->setup();
1364     }
1365 #endif
1366 }
1367 void Interpreter::dumpSampleData(ExecState* exec)
1368 {
1369 #if ENABLE(OPCODE_SAMPLING)
1370     if (m_sampler)
1371         m_sampler->dump(exec);
1372 #else
1373     UNUSED_PARAM(exec);
1374 #endif
1375 }
1376 void Interpreter::startSampling()
1377 {
1378 #if ENABLE(SAMPLING_THREAD)
1379     if (!m_sampleEntryDepth)
1380         SamplingThread::start();
1381
1382     m_sampleEntryDepth++;
1383 #endif
1384 }
1385 void Interpreter::stopSampling()
1386 {
1387 #if ENABLE(SAMPLING_THREAD)
1388     m_sampleEntryDepth--;
1389     if (!m_sampleEntryDepth)
1390         SamplingThread::stop();
1391 #endif
1392 }
1393
1394 } // namespace JSC