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