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