[ES6] "super" and "this" should be lexically bound inside an arrow function and shoul...
[WebKit-https.git] / Source / JavaScriptCore / runtime / Executable.cpp
1 /*
2  * Copyright (C) 2009, 2010, 2013, 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "Executable.h"
28
29 #include "BatchedTransitionOptimizer.h"
30 #include "CodeBlock.h"
31 #include "DFGDriver.h"
32 #include "JIT.h"
33 #include "JSCInlines.h"
34 #include "JSWASMModule.h"
35 #include "LLIntEntrypoint.h"
36 #include "Parser.h"
37 #include "ProfilerDatabase.h"
38 #include "TypeProfiler.h"
39 #include "WASMFunctionParser.h"
40 #include <wtf/CommaPrinter.h>
41 #include <wtf/Vector.h>
42 #include <wtf/text/StringBuilder.h>
43
44 namespace JSC {
45
46 const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, CREATE_METHOD_TABLE(ExecutableBase) };
47
48 void ExecutableBase::destroy(JSCell* cell)
49 {
50     static_cast<ExecutableBase*>(cell)->ExecutableBase::~ExecutableBase();
51 }
52
53 void ExecutableBase::clearCode()
54 {
55 #if ENABLE(JIT)
56     m_jitCodeForCall = nullptr;
57     m_jitCodeForConstruct = nullptr;
58     m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
59     m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
60 #endif
61     m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
62     m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
63
64     if (classInfo() == FunctionExecutable::info()) {
65         FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
66         executable->m_codeBlockForCall.clear();
67         executable->m_codeBlockForConstruct.clear();
68         return;
69     }
70
71     if (classInfo() == EvalExecutable::info()) {
72         EvalExecutable* executable = jsCast<EvalExecutable*>(this);
73         executable->m_evalCodeBlock.clear();
74         executable->m_unlinkedEvalCodeBlock.clear();
75         return;
76     }
77     
78     if (classInfo() == ProgramExecutable::info()) {
79         ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
80         executable->m_programCodeBlock.clear();
81         executable->m_unlinkedProgramCodeBlock.clear();
82         return;
83     }
84
85     if (classInfo() == ModuleProgramExecutable::info()) {
86         ModuleProgramExecutable* executable = jsCast<ModuleProgramExecutable*>(this);
87         executable->m_moduleProgramCodeBlock.clear();
88         executable->m_unlinkedModuleProgramCodeBlock.clear();
89         executable->m_moduleEnvironmentSymbolTable.clear();
90         return;
91     }
92     
93 #if ENABLE(WEBASSEMBLY)
94     if (classInfo() == WebAssemblyExecutable::info()) {
95         WebAssemblyExecutable* executable = jsCast<WebAssemblyExecutable*>(this);
96         executable->m_codeBlockForCall.clear();
97         return;
98     }
99 #endif
100
101     ASSERT(classInfo() == NativeExecutable::info());
102 }
103
104 #if ENABLE(DFG_JIT)
105 Intrinsic ExecutableBase::intrinsic() const
106 {
107     if (const NativeExecutable* nativeExecutable = jsDynamicCast<const NativeExecutable*>(this))
108         return nativeExecutable->intrinsic();
109     return NoIntrinsic;
110 }
111 #else
112 Intrinsic ExecutableBase::intrinsic() const
113 {
114     return NoIntrinsic;
115 }
116 #endif
117
118 const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, CREATE_METHOD_TABLE(NativeExecutable) };
119
120 void NativeExecutable::destroy(JSCell* cell)
121 {
122     static_cast<NativeExecutable*>(cell)->NativeExecutable::~NativeExecutable();
123 }
124
125 #if ENABLE(DFG_JIT)
126 Intrinsic NativeExecutable::intrinsic() const
127 {
128     return m_intrinsic;
129 }
130 #endif
131
132 const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, CREATE_METHOD_TABLE(ScriptExecutable) };
133
134 ScriptExecutable::ScriptExecutable(Structure* structure, VM& vm, const SourceCode& source, bool isInStrictContext, bool isInDerivedConstructorContext, bool isInArrowFunctionContext)
135     : ExecutableBase(vm, structure, NUM_PARAMETERS_NOT_COMPILED)
136     , m_source(source)
137     , m_features(isInStrictContext ? StrictModeFeature : 0)
138     , m_hasCapturedVariables(false)
139     , m_neverInline(false)
140     , m_didTryToEnterInLoop(false)
141     , m_isDerivedConstructorContext(isInDerivedConstructorContext)
142     , m_isArrowFunctionContext(isInArrowFunctionContext)
143     , m_overrideLineNumber(-1)
144     , m_firstLine(-1)
145     , m_lastLine(-1)
146     , m_startColumn(UINT_MAX)
147     , m_endColumn(UINT_MAX)
148     , m_typeProfilingStartOffset(UINT_MAX)
149     , m_typeProfilingEndOffset(UINT_MAX)
150 {
151 }
152
153 void ScriptExecutable::destroy(JSCell* cell)
154 {
155     static_cast<ScriptExecutable*>(cell)->ScriptExecutable::~ScriptExecutable();
156 }
157
158 void ScriptExecutable::installCode(CodeBlock* codeBlock)
159 {
160     installCode(*codeBlock->vm(), codeBlock, codeBlock->codeType(), codeBlock->specializationKind());
161 }
162
163 void ScriptExecutable::installCode(VM& vm, CodeBlock* genericCodeBlock, CodeType codeType, CodeSpecializationKind kind)
164 {
165     ASSERT(vm.heap.isDeferred());
166     
167     CodeBlock* oldCodeBlock = nullptr;
168     
169     switch (codeType) {
170     case GlobalCode: {
171         ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
172         ProgramCodeBlock* codeBlock = static_cast<ProgramCodeBlock*>(genericCodeBlock);
173         
174         ASSERT(kind == CodeForCall);
175         
176         oldCodeBlock = executable->m_programCodeBlock.get();
177         executable->m_programCodeBlock.setMayBeNull(vm, this, codeBlock);
178         break;
179     }
180
181     case ModuleCode: {
182         ModuleProgramExecutable* executable = jsCast<ModuleProgramExecutable*>(this);
183         ModuleProgramCodeBlock* codeBlock = static_cast<ModuleProgramCodeBlock*>(genericCodeBlock);
184
185         ASSERT(kind == CodeForCall);
186
187         oldCodeBlock = executable->m_moduleProgramCodeBlock.get();
188         executable->m_moduleProgramCodeBlock.setMayBeNull(vm, this, codeBlock);
189         break;
190     }
191
192     case EvalCode: {
193         EvalExecutable* executable = jsCast<EvalExecutable*>(this);
194         EvalCodeBlock* codeBlock = static_cast<EvalCodeBlock*>(genericCodeBlock);
195         
196         ASSERT(kind == CodeForCall);
197         
198         oldCodeBlock = executable->m_evalCodeBlock.get();
199         executable->m_evalCodeBlock.setMayBeNull(vm, this, codeBlock);
200         break;
201     }
202         
203     case FunctionCode: {
204         FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
205         FunctionCodeBlock* codeBlock = static_cast<FunctionCodeBlock*>(genericCodeBlock);
206         
207         switch (kind) {
208         case CodeForCall:
209             oldCodeBlock = executable->m_codeBlockForCall.get();
210             executable->m_codeBlockForCall.setMayBeNull(vm, this, codeBlock);
211             break;
212         case CodeForConstruct:
213             oldCodeBlock = executable->m_codeBlockForConstruct.get();
214             executable->m_codeBlockForConstruct.setMayBeNull(vm, this, codeBlock);
215             break;
216         }
217         break;
218     }
219     }
220
221     switch (kind) {
222     case CodeForCall:
223         m_jitCodeForCall = genericCodeBlock ? genericCodeBlock->jitCode() : nullptr;
224         m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
225         m_numParametersForCall = genericCodeBlock ? genericCodeBlock->numParameters() : NUM_PARAMETERS_NOT_COMPILED;
226         break;
227     case CodeForConstruct:
228         m_jitCodeForConstruct = genericCodeBlock ? genericCodeBlock->jitCode() : nullptr;
229         m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
230         m_numParametersForConstruct = genericCodeBlock ? genericCodeBlock->numParameters() : NUM_PARAMETERS_NOT_COMPILED;
231         break;
232     }
233
234     if (genericCodeBlock) {
235         RELEASE_ASSERT(genericCodeBlock->ownerExecutable() == this);
236         RELEASE_ASSERT(JITCode::isExecutableScript(genericCodeBlock->jitType()));
237         
238         if (Options::verboseOSR())
239             dataLog("Installing ", *genericCodeBlock, "\n");
240         
241         if (vm.m_perBytecodeProfiler)
242             vm.m_perBytecodeProfiler->ensureBytecodesFor(genericCodeBlock);
243         
244         if (Debugger* debugger = genericCodeBlock->globalObject()->debugger())
245             debugger->registerCodeBlock(genericCodeBlock);
246     }
247
248     if (oldCodeBlock)
249         oldCodeBlock->unlinkIncomingCalls();
250
251     vm.heap.writeBarrier(this);
252 }
253
254 CodeBlock* ScriptExecutable::newCodeBlockFor(
255     CodeSpecializationKind kind, JSFunction* function, JSScope* scope, JSObject*& exception)
256 {
257     VM* vm = scope->vm();
258
259     ASSERT(vm->heap.isDeferred());
260     ASSERT(startColumn() != UINT_MAX);
261     ASSERT(endColumn() != UINT_MAX);
262
263     if (classInfo() == EvalExecutable::info()) {
264         EvalExecutable* executable = jsCast<EvalExecutable*>(this);
265         RELEASE_ASSERT(kind == CodeForCall);
266         RELEASE_ASSERT(!executable->m_evalCodeBlock);
267         RELEASE_ASSERT(!function);
268         return EvalCodeBlock::create(vm,
269             executable, executable->m_unlinkedEvalCodeBlock.get(), scope,
270             executable->source().provider());
271     }
272     
273     if (classInfo() == ProgramExecutable::info()) {
274         ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
275         RELEASE_ASSERT(kind == CodeForCall);
276         RELEASE_ASSERT(!executable->m_programCodeBlock);
277         RELEASE_ASSERT(!function);
278         return ProgramCodeBlock::create(vm,
279             executable, executable->m_unlinkedProgramCodeBlock.get(), scope,
280             executable->source().provider(), executable->source().startColumn());
281     }
282
283     if (classInfo() == ModuleProgramExecutable::info()) {
284         ModuleProgramExecutable* executable = jsCast<ModuleProgramExecutable*>(this);
285         RELEASE_ASSERT(kind == CodeForCall);
286         RELEASE_ASSERT(!executable->m_moduleProgramCodeBlock);
287         RELEASE_ASSERT(!function);
288         return ModuleProgramCodeBlock::create(vm,
289             executable, executable->m_unlinkedModuleProgramCodeBlock.get(), scope,
290             executable->source().provider(), executable->source().startColumn());
291     }
292
293     RELEASE_ASSERT(classInfo() == FunctionExecutable::info());
294     RELEASE_ASSERT(function);
295     FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
296     RELEASE_ASSERT(!executable->codeBlockFor(kind));
297     JSGlobalObject* globalObject = scope->globalObject();
298     ParserError error;
299     DebuggerMode debuggerMode = globalObject->hasDebugger() ? DebuggerOn : DebuggerOff;
300     ProfilerMode profilerMode = globalObject->hasProfiler() ? ProfilerOn : ProfilerOff;
301     UnlinkedFunctionCodeBlock* unlinkedCodeBlock = 
302         executable->m_unlinkedExecutable->unlinkedCodeBlockFor(
303             *vm, executable->m_source, kind, debuggerMode, profilerMode, error, 
304             executable->parseMode());
305     recordParse(
306         executable->m_unlinkedExecutable->features(), 
307         executable->m_unlinkedExecutable->hasCapturedVariables(), firstLine(), 
308         lastLine(), startColumn(), endColumn()); 
309     if (!unlinkedCodeBlock) {
310         exception = vm->throwException(
311             globalObject->globalExec(),
312             error.toErrorObject(globalObject, executable->m_source));
313         return nullptr;
314     }
315
316     SourceProvider* provider = executable->source().provider();
317     unsigned sourceOffset = executable->source().startOffset();
318     unsigned startColumn = executable->source().startColumn();
319
320     return FunctionCodeBlock::create(vm,
321         executable, unlinkedCodeBlock, scope, provider, sourceOffset, startColumn);
322 }
323
324 CodeBlock* ScriptExecutable::newReplacementCodeBlockFor(
325     CodeSpecializationKind kind)
326 {
327     if (classInfo() == EvalExecutable::info()) {
328         RELEASE_ASSERT(kind == CodeForCall);
329         EvalExecutable* executable = jsCast<EvalExecutable*>(this);
330         EvalCodeBlock* baseline = static_cast<EvalCodeBlock*>(
331             executable->m_evalCodeBlock->baselineVersion());
332         EvalCodeBlock* result = EvalCodeBlock::create(vm(),
333             CodeBlock::CopyParsedBlock, *baseline);
334         result->setAlternative(*vm(), baseline);
335         return result;
336     }
337     
338     if (classInfo() == ProgramExecutable::info()) {
339         RELEASE_ASSERT(kind == CodeForCall);
340         ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
341         ProgramCodeBlock* baseline = static_cast<ProgramCodeBlock*>(
342             executable->m_programCodeBlock->baselineVersion());
343         ProgramCodeBlock* result = ProgramCodeBlock::create(vm(),
344             CodeBlock::CopyParsedBlock, *baseline);
345         result->setAlternative(*vm(), baseline);
346         return result;
347     }
348
349     if (classInfo() == ModuleProgramExecutable::info()) {
350         RELEASE_ASSERT(kind == CodeForCall);
351         ModuleProgramExecutable* executable = jsCast<ModuleProgramExecutable*>(this);
352         ModuleProgramCodeBlock* baseline = static_cast<ModuleProgramCodeBlock*>(
353             executable->m_moduleProgramCodeBlock->baselineVersion());
354         ModuleProgramCodeBlock* result = ModuleProgramCodeBlock::create(vm(),
355             CodeBlock::CopyParsedBlock, *baseline);
356         result->setAlternative(*vm(), baseline);
357         return result;
358     }
359
360     RELEASE_ASSERT(classInfo() == FunctionExecutable::info());
361     FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
362     FunctionCodeBlock* baseline = static_cast<FunctionCodeBlock*>(
363         executable->codeBlockFor(kind)->baselineVersion());
364     FunctionCodeBlock* result = FunctionCodeBlock::create(vm(),
365         CodeBlock::CopyParsedBlock, *baseline);
366     result->setAlternative(*vm(), baseline);
367     return result;
368 }
369
370 static void setupLLInt(VM& vm, CodeBlock* codeBlock)
371 {
372     LLInt::setEntrypoint(vm, codeBlock);
373 }
374
375 static void setupJIT(VM& vm, CodeBlock* codeBlock)
376 {
377 #if ENABLE(JIT)
378     CompilationResult result = JIT::compile(&vm, codeBlock, JITCompilationMustSucceed);
379     RELEASE_ASSERT(result == CompilationSuccessful);
380 #else
381     UNUSED_PARAM(vm);
382     UNUSED_PARAM(codeBlock);
383     UNREACHABLE_FOR_PLATFORM();
384 #endif
385 }
386
387 JSObject* ScriptExecutable::prepareForExecutionImpl(
388     ExecState* exec, JSFunction* function, JSScope* scope, CodeSpecializationKind kind)
389 {
390     VM& vm = exec->vm();
391     DeferGC deferGC(vm.heap);
392
393     if (vm.getAndClearFailNextNewCodeBlock())
394         return createError(exec->callerFrame(), ASCIILiteral("Forced Failure"));
395
396     JSObject* exception = 0;
397     CodeBlock* codeBlock = newCodeBlockFor(kind, function, scope, exception);
398     if (!codeBlock) {
399         RELEASE_ASSERT(exception);
400         return exception;
401     }
402     
403     if (Options::validateBytecode())
404         codeBlock->validate();
405     
406     if (Options::useLLInt())
407         setupLLInt(vm, codeBlock);
408     else
409         setupJIT(vm, codeBlock);
410     
411     installCode(*codeBlock->vm(), codeBlock, codeBlock->codeType(), codeBlock->specializationKind());
412     return 0;
413 }
414
415 const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(EvalExecutable) };
416
417 EvalExecutable* EvalExecutable::create(ExecState* exec, const SourceCode& source, bool isInStrictContext, ThisTDZMode thisTDZMode, bool isDerivedConstructorContext, bool isArrowFunctionContext, const VariableEnvironment* variablesUnderTDZ)
418 {
419     JSGlobalObject* globalObject = exec->lexicalGlobalObject();
420     if (!globalObject->evalEnabled()) {
421         exec->vm().throwException(exec, createEvalError(exec, globalObject->evalDisabledErrorMessage()));
422         return 0;
423     }
424
425     EvalExecutable* executable = new (NotNull, allocateCell<EvalExecutable>(*exec->heap())) EvalExecutable(exec, source, isInStrictContext, isDerivedConstructorContext, isArrowFunctionContext);
426     executable->finishCreation(exec->vm());
427
428     UnlinkedEvalCodeBlock* unlinkedEvalCode = globalObject->createEvalCodeBlock(exec, executable, thisTDZMode, isArrowFunctionContext, variablesUnderTDZ);
429     if (!unlinkedEvalCode)
430         return 0;
431
432     executable->m_unlinkedEvalCodeBlock.set(exec->vm(), executable, unlinkedEvalCode);
433
434     return executable;
435 }
436
437 EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext, bool isDerivedConstructorContext, bool isArrowFunctionContext)
438     : ScriptExecutable(exec->vm().evalExecutableStructure.get(), exec->vm(), source, inStrictContext, isDerivedConstructorContext, isArrowFunctionContext)
439 {
440 }
441
442 void EvalExecutable::destroy(JSCell* cell)
443 {
444     static_cast<EvalExecutable*>(cell)->EvalExecutable::~EvalExecutable();
445 }
446
447 const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(ProgramExecutable) };
448
449 ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
450     : ScriptExecutable(exec->vm().programExecutableStructure.get(), exec->vm(), source, false, false, false)
451 {
452     m_typeProfilingStartOffset = 0;
453     m_typeProfilingEndOffset = source.length() - 1;
454     if (exec->vm().typeProfiler() || exec->vm().controlFlowProfiler())
455         exec->vm().functionHasExecutedCache()->insertUnexecutedRange(sourceID(), m_typeProfilingStartOffset, m_typeProfilingEndOffset);
456 }
457
458 void ProgramExecutable::destroy(JSCell* cell)
459 {
460     static_cast<ProgramExecutable*>(cell)->ProgramExecutable::~ProgramExecutable();
461 }
462
463 const ClassInfo ModuleProgramExecutable::s_info = { "ModuleProgramExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(ModuleProgramExecutable) };
464
465 ModuleProgramExecutable::ModuleProgramExecutable(ExecState* exec, const SourceCode& source)
466     : ScriptExecutable(exec->vm().moduleProgramExecutableStructure.get(), exec->vm(), source, false, false, false)
467 {
468     m_typeProfilingStartOffset = 0;
469     m_typeProfilingEndOffset = source.length() - 1;
470     if (exec->vm().typeProfiler() || exec->vm().controlFlowProfiler())
471         exec->vm().functionHasExecutedCache()->insertUnexecutedRange(sourceID(), m_typeProfilingStartOffset, m_typeProfilingEndOffset);
472 }
473
474 ModuleProgramExecutable* ModuleProgramExecutable::create(ExecState* exec, const SourceCode& source)
475 {
476     JSGlobalObject* globalObject = exec->lexicalGlobalObject();
477     ModuleProgramExecutable* executable = new (NotNull, allocateCell<ModuleProgramExecutable>(*exec->heap())) ModuleProgramExecutable(exec, source);
478     executable->finishCreation(exec->vm());
479
480     UnlinkedModuleProgramCodeBlock* unlinkedModuleProgramCode = globalObject->createModuleProgramCodeBlock(exec, executable);
481     if (!unlinkedModuleProgramCode)
482         return nullptr;
483     executable->m_unlinkedModuleProgramCodeBlock.set(exec->vm(), executable, unlinkedModuleProgramCode);
484
485     executable->m_moduleEnvironmentSymbolTable.set(exec->vm(), executable, jsCast<SymbolTable*>(unlinkedModuleProgramCode->constantRegister(unlinkedModuleProgramCode->moduleEnvironmentSymbolTableConstantRegisterOffset()).get())->cloneScopePart(exec->vm()));
486
487     return executable;
488 }
489
490 void ModuleProgramExecutable::destroy(JSCell* cell)
491 {
492     static_cast<ModuleProgramExecutable*>(cell)->ModuleProgramExecutable::~ModuleProgramExecutable();
493 }
494
495 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
496
497 FunctionExecutable::FunctionExecutable(VM& vm, const SourceCode& source, UnlinkedFunctionExecutable* unlinkedExecutable, unsigned firstLine, unsigned lastLine, unsigned startColumn, unsigned endColumn)
498     : ScriptExecutable(vm.functionExecutableStructure.get(), vm, source, unlinkedExecutable->isInStrictContext(), unlinkedExecutable->isDerivedConstructorContext(), false)
499     , m_unlinkedExecutable(vm, this, unlinkedExecutable)
500 {
501     RELEASE_ASSERT(!source.isNull());
502     ASSERT(source.length());
503     m_firstLine = firstLine;
504     m_lastLine = lastLine;
505     ASSERT(startColumn != UINT_MAX);
506     ASSERT(endColumn != UINT_MAX);
507     m_startColumn = startColumn;
508     m_endColumn = endColumn;
509     m_parametersStartOffset = unlinkedExecutable->parametersStartOffset();
510     m_typeProfilingStartOffset = unlinkedExecutable->typeProfilingStartOffset();
511     m_typeProfilingEndOffset = unlinkedExecutable->typeProfilingEndOffset();
512 }
513
514 void FunctionExecutable::finishCreation(VM& vm)
515 {
516     Base::finishCreation(vm);
517     m_singletonFunction.set(vm, this, InferredValue::create(vm));
518 }
519
520 void FunctionExecutable::destroy(JSCell* cell)
521 {
522     static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
523 }
524
525 inline const char* samplingDescription(JITCode::JITType jitType)
526 {
527     switch (jitType) {
528     case JITCode::InterpreterThunk:
529         return "Interpreter Compilation (TOTAL)";
530     case JITCode::BaselineJIT:
531         return "Baseline Compilation (TOTAL)";
532     case JITCode::DFGJIT:
533         return "DFG Compilation (TOTAL)";
534     case JITCode::FTLJIT:
535         return "FTL Compilation (TOTAL)";
536     default:
537         RELEASE_ASSERT_NOT_REACHED();
538         return 0;
539     }
540 }
541
542 void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
543 {
544     EvalExecutable* thisObject = jsCast<EvalExecutable*>(cell);
545     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
546     ScriptExecutable::visitChildren(thisObject, visitor);
547     visitor.append(&thisObject->m_unlinkedEvalCodeBlock);
548     if (thisObject->m_evalCodeBlock)
549         thisObject->m_evalCodeBlock->visitWeakly(visitor);
550 }
551
552 JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
553 {
554     ParserError error;
555     VM* vm = &exec->vm();
556     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
557     std::unique_ptr<ProgramNode> programNode = parse<ProgramNode>(
558         vm, m_source, Identifier(), JSParserBuiltinMode::NotBuiltin, 
559         JSParserStrictMode::NotStrict, SourceParseMode::ProgramMode, SuperBinding::NotNeeded, error);
560     if (programNode)
561         return 0;
562     ASSERT(error.isValid());
563     return error.toErrorObject(lexicalGlobalObject, m_source);
564 }
565
566 JSObject* ProgramExecutable::initializeGlobalProperties(VM& vm, CallFrame* callFrame, JSScope* scope)
567 {
568     RELEASE_ASSERT(scope);
569     JSGlobalObject* globalObject = scope->globalObject();
570     RELEASE_ASSERT(globalObject);
571     ASSERT(&globalObject->vm() == &vm);
572
573     JSObject* exception = 0;
574     UnlinkedProgramCodeBlock* unlinkedCodeBlock = globalObject->createProgramCodeBlock(callFrame, this, &exception);
575     if (exception)
576         return exception;
577
578     JSGlobalLexicalEnvironment* globalLexicalEnvironment = globalObject->globalLexicalEnvironment();
579     const VariableEnvironment& variableDeclarations = unlinkedCodeBlock->variableDeclarations();
580     const VariableEnvironment& lexicalDeclarations = unlinkedCodeBlock->lexicalDeclarations();
581     // The ES6 spec says that no vars/global properties/let/const can be duplicated in the global scope.
582     // This carried out section 15.1.8 of the ES6 spec: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-globaldeclarationinstantiation
583     {
584         ExecState* exec = globalObject->globalExec();
585         // Check for intersection of "var" and "let"/"const"/"class"
586         for (auto& entry : lexicalDeclarations) {
587             if (variableDeclarations.contains(entry.key))
588                 return createSyntaxError(exec, makeString("Can't create duplicate variable: '", String(entry.key.get()), "'"));
589         }
590
591         // Check if any new "let"/"const"/"class" will shadow any pre-existing global property names, or "var"/"let"/"const" variables.
592         // It's an error to introduce a shadow.
593         for (auto& entry : lexicalDeclarations) {
594             if (globalObject->hasProperty(exec, entry.key.get()))
595                 return createSyntaxError(exec, makeString("Can't create duplicate variable that shadows a global property: '", String(entry.key.get()), "'"));
596
597             if (globalLexicalEnvironment->hasProperty(exec, entry.key.get()))
598                 return createSyntaxError(exec, makeString("Can't create duplicate variable: '", String(entry.key.get()), "'"));
599         }
600
601         // Check if any new "var"s will shadow any previous "let"/"const"/"class" names.
602         // It's an error to introduce a shadow.
603         if (!globalLexicalEnvironment->isEmpty()) {
604             for (auto& entry : variableDeclarations) {
605                 if (globalLexicalEnvironment->hasProperty(exec, entry.key.get()))
606                     return createSyntaxError(exec, makeString("Can't create duplicate variable: '", String(entry.key.get()), "'"));
607             }
608         }
609     }
610
611
612     m_unlinkedProgramCodeBlock.set(vm, this, unlinkedCodeBlock);
613
614     BatchedTransitionOptimizer optimizer(vm, globalObject);
615
616     for (size_t i = 0, numberOfFunctions = unlinkedCodeBlock->numberOfFunctionDecls(); i < numberOfFunctions; ++i) {
617         UnlinkedFunctionExecutable* unlinkedFunctionExecutable = unlinkedCodeBlock->functionDecl(i);
618         ASSERT(!unlinkedFunctionExecutable->name().isEmpty());
619         globalObject->addFunction(callFrame, unlinkedFunctionExecutable->name());
620         if (vm.typeProfiler() || vm.controlFlowProfiler()) {
621             vm.functionHasExecutedCache()->insertUnexecutedRange(sourceID(), 
622                 unlinkedFunctionExecutable->typeProfilingStartOffset(), 
623                 unlinkedFunctionExecutable->typeProfilingEndOffset());
624         }
625     }
626
627     for (auto& entry : variableDeclarations) {
628         ASSERT(entry.value.isVar());
629         globalObject->addVar(callFrame, Identifier::fromUid(&vm, entry.key.get()));
630     }
631
632     {
633         JSGlobalLexicalEnvironment* globalLexicalEnvironment = jsCast<JSGlobalLexicalEnvironment*>(globalObject->globalScope());
634         SymbolTable* symbolTable = globalLexicalEnvironment->symbolTable();
635         ConcurrentJITLocker locker(symbolTable->m_lock);
636         for (auto& entry : lexicalDeclarations) {
637             ScopeOffset offset = symbolTable->takeNextScopeOffset(locker);
638             SymbolTableEntry newEntry(VarOffset(offset), entry.value.isConst() ? ReadOnly : 0);
639             newEntry.prepareToWatch();
640             symbolTable->add(locker, entry.key.get(), newEntry);
641             
642             ScopeOffset offsetForAssert = globalLexicalEnvironment->addVariables(1, jsTDZValue());
643             RELEASE_ASSERT(offsetForAssert == offset);
644         }
645     }
646     return 0;
647 }
648
649 void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
650 {
651     ProgramExecutable* thisObject = jsCast<ProgramExecutable*>(cell);
652     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
653     ScriptExecutable::visitChildren(thisObject, visitor);
654     visitor.append(&thisObject->m_unlinkedProgramCodeBlock);
655     if (thisObject->m_programCodeBlock)
656         thisObject->m_programCodeBlock->visitWeakly(visitor);
657 }
658
659 void ModuleProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
660 {
661     ModuleProgramExecutable* thisObject = jsCast<ModuleProgramExecutable*>(cell);
662     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
663     ScriptExecutable::visitChildren(thisObject, visitor);
664     visitor.append(&thisObject->m_unlinkedModuleProgramCodeBlock);
665     visitor.append(&thisObject->m_moduleEnvironmentSymbolTable);
666     if (thisObject->m_moduleProgramCodeBlock)
667         thisObject->m_moduleProgramCodeBlock->visitWeakly(visitor);
668 }
669
670 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
671 {
672     FunctionCodeBlock* result;
673     if (kind == CodeForCall)
674         result = m_codeBlockForCall.get();
675     else {
676         RELEASE_ASSERT(kind == CodeForConstruct);
677         result = m_codeBlockForConstruct.get();
678     }
679     if (!result)
680         return 0;
681     return static_cast<FunctionCodeBlock*>(result->baselineAlternative());
682 }
683
684 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
685 {
686     FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
687     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
688     ScriptExecutable::visitChildren(thisObject, visitor);
689     if (thisObject->m_codeBlockForCall)
690         thisObject->m_codeBlockForCall->visitWeakly(visitor);
691     if (thisObject->m_codeBlockForConstruct)
692         thisObject->m_codeBlockForConstruct->visitWeakly(visitor);
693     visitor.append(&thisObject->m_unlinkedExecutable);
694     visitor.append(&thisObject->m_singletonFunction);
695 }
696
697 FunctionExecutable* FunctionExecutable::fromGlobalCode(
698     const Identifier& name, ExecState& exec, const SourceCode& source, 
699     JSObject*& exception, int overrideLineNumber)
700 {
701     UnlinkedFunctionExecutable* unlinkedExecutable = 
702         UnlinkedFunctionExecutable::fromGlobalCode(
703             name, exec, source, exception, overrideLineNumber);
704     if (!unlinkedExecutable)
705         return nullptr;
706
707     return unlinkedExecutable->link(exec.vm(), source, overrideLineNumber);
708 }
709
710 #if ENABLE(WEBASSEMBLY)
711 const ClassInfo WebAssemblyExecutable::s_info = { "WebAssemblyExecutable", &ExecutableBase::s_info, 0, CREATE_METHOD_TABLE(WebAssemblyExecutable) };
712
713 WebAssemblyExecutable::WebAssemblyExecutable(VM& vm, const SourceCode& source, JSWASMModule* module, unsigned functionIndex)
714     : ExecutableBase(vm, vm.webAssemblyExecutableStructure.get(), NUM_PARAMETERS_NOT_COMPILED)
715     , m_source(source)
716     , m_module(vm, this, module)
717     , m_functionIndex(functionIndex)
718 {
719 }
720
721 void WebAssemblyExecutable::destroy(JSCell* cell)
722 {
723     static_cast<WebAssemblyExecutable*>(cell)->WebAssemblyExecutable::~WebAssemblyExecutable();
724 }
725
726 void WebAssemblyExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
727 {
728     WebAssemblyExecutable* thisObject = jsCast<WebAssemblyExecutable*>(cell);
729     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
730     ExecutableBase::visitChildren(thisObject, visitor);
731     if (thisObject->m_codeBlockForCall)
732         thisObject->m_codeBlockForCall->visitWeakly(visitor);
733     visitor.append(&thisObject->m_module);
734 }
735
736 void WebAssemblyExecutable::prepareForExecution(ExecState* exec)
737 {
738     if (hasJITCodeForCall())
739         return;
740
741     VM& vm = exec->vm();
742     DeferGC deferGC(vm.heap);
743
744     WebAssemblyCodeBlock* codeBlock = WebAssemblyCodeBlock::create(&vm,
745         this, exec->lexicalGlobalObject());
746
747     WASMFunctionParser::compile(vm, codeBlock, m_module.get(), m_source, m_functionIndex);
748
749     m_jitCodeForCall = codeBlock->jitCode();
750     m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
751     m_numParametersForCall = codeBlock->numParameters();
752
753     m_codeBlockForCall.set(vm, this, codeBlock);
754
755     Heap::heap(this)->writeBarrier(this);
756 }
757 #endif
758
759 void ExecutableBase::dump(PrintStream& out) const
760 {
761     ExecutableBase* realThis = const_cast<ExecutableBase*>(this);
762     
763     if (classInfo() == NativeExecutable::info()) {
764         NativeExecutable* native = jsCast<NativeExecutable*>(realThis);
765         out.print("NativeExecutable:", RawPointer(bitwise_cast<void*>(native->function())), "/", RawPointer(bitwise_cast<void*>(native->constructor())));
766         return;
767     }
768     
769     if (classInfo() == EvalExecutable::info()) {
770         EvalExecutable* eval = jsCast<EvalExecutable*>(realThis);
771         if (CodeBlock* codeBlock = eval->codeBlock())
772             out.print(*codeBlock);
773         else
774             out.print("EvalExecutable w/o CodeBlock");
775         return;
776     }
777     
778     if (classInfo() == ProgramExecutable::info()) {
779         ProgramExecutable* eval = jsCast<ProgramExecutable*>(realThis);
780         if (CodeBlock* codeBlock = eval->codeBlock())
781             out.print(*codeBlock);
782         else
783             out.print("ProgramExecutable w/o CodeBlock");
784         return;
785     }
786
787     if (classInfo() == ModuleProgramExecutable::info()) {
788         ModuleProgramExecutable* executable = jsCast<ModuleProgramExecutable*>(realThis);
789         if (CodeBlock* codeBlock = executable->codeBlock())
790             out.print(*codeBlock);
791         else
792             out.print("ModuleProgramExecutable w/o CodeBlock");
793         return;
794     }
795     
796     FunctionExecutable* function = jsCast<FunctionExecutable*>(realThis);
797     if (!function->eitherCodeBlock())
798         out.print("FunctionExecutable w/o CodeBlock");
799     else {
800         CommaPrinter comma("/");
801         if (function->codeBlockForCall())
802             out.print(comma, *function->codeBlockForCall());
803         if (function->codeBlockForConstruct())
804             out.print(comma, *function->codeBlockForConstruct());
805     }
806 }
807
808 CodeBlockHash ExecutableBase::hashFor(CodeSpecializationKind kind) const
809 {
810     if (this->classInfo() == NativeExecutable::info())
811         return jsCast<const NativeExecutable*>(this)->hashFor(kind);
812     
813     return jsCast<const ScriptExecutable*>(this)->hashFor(kind);
814 }
815
816 CodeBlockHash NativeExecutable::hashFor(CodeSpecializationKind kind) const
817 {
818     if (kind == CodeForCall)
819         return CodeBlockHash(static_cast<unsigned>(bitwise_cast<size_t>(m_function)));
820     
821     RELEASE_ASSERT(kind == CodeForConstruct);
822     return CodeBlockHash(static_cast<unsigned>(bitwise_cast<size_t>(m_constructor)));
823 }
824
825 CodeBlockHash ScriptExecutable::hashFor(CodeSpecializationKind kind) const
826 {
827     return CodeBlockHash(source(), kind);
828 }
829
830 }