75b3091cf81c7296fe2fafe9a9676536299782fc
[WebKit-https.git] / Source / JavaScriptCore / runtime / Executable.h
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 #ifndef Executable_h
27 #define Executable_h
28
29 #include "ArityCheckMode.h"
30 #include "CallData.h"
31 #include "CodeBlockHash.h"
32 #include "CodeSpecializationKind.h"
33 #include "CompilationResult.h"
34 #include "ExecutableInfo.h"
35 #include "HandlerInfo.h"
36 #include "InferredValue.h"
37 #include "JITCode.h"
38 #include "JSGlobalObject.h"
39 #include "SamplingTool.h"
40 #include "SourceCode.h"
41 #include "TypeSet.h"
42 #include "UnlinkedCodeBlock.h"
43 #include "UnlinkedFunctionExecutable.h"
44
45 namespace JSC {
46
47 class CodeBlock;
48 class Debugger;
49 class EvalCodeBlock;
50 class FunctionCodeBlock;
51 class JSScope;
52 class JSWASMModule;
53 class LLIntOffsetsExtractor;
54 class ProgramCodeBlock;
55 class ModuleProgramCodeBlock;
56 class JSScope;
57 class WebAssemblyCodeBlock;
58 class ModuleProgramCodeBlock;
59 class JSModuleRecord;
60 class JSScope;
61
62 enum CompilationKind { FirstCompilation, OptimizingCompilation };
63
64 inline bool isCall(CodeSpecializationKind kind)
65 {
66     if (kind == CodeForCall)
67         return true;
68     ASSERT(kind == CodeForConstruct);
69     return false;
70 }
71
72 class ExecutableBase : public JSCell {
73     friend class JIT;
74
75 protected:
76     static const int NUM_PARAMETERS_IS_HOST = 0;
77     static const int NUM_PARAMETERS_NOT_COMPILED = -1;
78
79     ExecutableBase(VM& vm, Structure* structure, int numParameters)
80         : JSCell(vm, structure)
81         , m_numParametersForCall(numParameters)
82         , m_numParametersForConstruct(numParameters)
83     {
84     }
85
86     void finishCreation(VM& vm)
87     {
88         Base::finishCreation(vm);
89     }
90
91 public:
92     typedef JSCell Base;
93     static const unsigned StructureFlags = Base::StructureFlags;
94
95     static const bool needsDestruction = true;
96     static void destroy(JSCell*);
97         
98     CodeBlockHash hashFor(CodeSpecializationKind) const;
99
100     bool isEvalExecutable() const
101     {
102         return type() == EvalExecutableType;
103     }
104     bool isFunctionExecutable() const
105     {
106         return type() == FunctionExecutableType;
107     }
108     bool isProgramExecutable() const
109     {
110         return type() == ProgramExecutableType;
111     }
112     bool isModuleProgramExecutable()
113     {
114         return type() == ModuleProgramExecutableType;
115     }
116
117
118     bool isHostFunction() const
119     {
120         ASSERT((m_numParametersForCall == NUM_PARAMETERS_IS_HOST) == (m_numParametersForConstruct == NUM_PARAMETERS_IS_HOST));
121         return m_numParametersForCall == NUM_PARAMETERS_IS_HOST;
122     }
123
124 #if ENABLE(WEBASSEMBLY)
125     bool isWebAssemblyExecutable() const
126     {
127         return type() == WebAssemblyExecutableType;
128     }
129 #endif
130
131     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) { return Structure::create(vm, globalObject, proto, TypeInfo(CellType, StructureFlags), info()); }
132         
133     void clearCode();
134
135     DECLARE_EXPORT_INFO;
136
137 protected:
138     int m_numParametersForCall;
139     int m_numParametersForConstruct;
140
141 public:
142     PassRefPtr<JITCode> generatedJITCodeForCall()
143     {
144         ASSERT(m_jitCodeForCall);
145         return m_jitCodeForCall;
146     }
147
148     PassRefPtr<JITCode> generatedJITCodeForConstruct()
149     {
150         ASSERT(m_jitCodeForConstruct);
151         return m_jitCodeForConstruct;
152     }
153         
154     PassRefPtr<JITCode> generatedJITCodeFor(CodeSpecializationKind kind)
155     {
156         if (kind == CodeForCall)
157             return generatedJITCodeForCall();
158         ASSERT(kind == CodeForConstruct);
159         return generatedJITCodeForConstruct();
160     }
161     
162     MacroAssemblerCodePtr entrypointFor(CodeSpecializationKind kind, ArityCheckMode arity)
163     {
164         // Check if we have a cached result. We only have it for arity check because we use the
165         // no-arity entrypoint in non-virtual calls, which will "cache" this value directly in
166         // machine code.
167         if (arity == MustCheckArity) {
168             switch (kind) {
169             case CodeForCall:
170                 if (MacroAssemblerCodePtr result = m_jitCodeForCallWithArityCheck)
171                     return result;
172                 break;
173             case CodeForConstruct:
174                 if (MacroAssemblerCodePtr result = m_jitCodeForConstructWithArityCheck)
175                     return result;
176                 break;
177             }
178         }
179         MacroAssemblerCodePtr result =
180             generatedJITCodeFor(kind)->addressForCall(arity);
181         if (arity == MustCheckArity) {
182             // Cache the result; this is necessary for the JIT's virtual call optimizations.
183             switch (kind) {
184             case CodeForCall:
185                 m_jitCodeForCallWithArityCheck = result;
186                 break;
187             case CodeForConstruct:
188                 m_jitCodeForConstructWithArityCheck = result;
189                 break;
190             }
191         }
192         return result;
193     }
194
195     static ptrdiff_t offsetOfJITCodeWithArityCheckFor(
196         CodeSpecializationKind kind)
197     {
198         switch (kind) {
199         case CodeForCall:
200             return OBJECT_OFFSETOF(ExecutableBase, m_jitCodeForCallWithArityCheck);
201         case CodeForConstruct:
202             return OBJECT_OFFSETOF(ExecutableBase, m_jitCodeForConstructWithArityCheck);
203         }
204         RELEASE_ASSERT_NOT_REACHED();
205         return 0;
206     }
207     
208     static ptrdiff_t offsetOfNumParametersFor(CodeSpecializationKind kind)
209     {
210         if (kind == CodeForCall)
211             return OBJECT_OFFSETOF(ExecutableBase, m_numParametersForCall);
212         ASSERT(kind == CodeForConstruct);
213         return OBJECT_OFFSETOF(ExecutableBase, m_numParametersForConstruct);
214     }
215
216     bool hasJITCodeForCall() const
217     {
218         return m_numParametersForCall >= 0;
219     }
220         
221     bool hasJITCodeForConstruct() const
222     {
223         return m_numParametersForConstruct >= 0;
224     }
225         
226     bool hasJITCodeFor(CodeSpecializationKind kind) const
227     {
228         if (kind == CodeForCall)
229             return hasJITCodeForCall();
230         ASSERT(kind == CodeForConstruct);
231         return hasJITCodeForConstruct();
232     }
233
234     // Intrinsics are only for calls, currently.
235     Intrinsic intrinsic() const;
236         
237     Intrinsic intrinsicFor(CodeSpecializationKind kind) const
238     {
239         if (isCall(kind))
240             return intrinsic();
241         return NoIntrinsic;
242     }
243     
244     void dump(PrintStream&) const;
245         
246 protected:
247     RefPtr<JITCode> m_jitCodeForCall;
248     RefPtr<JITCode> m_jitCodeForConstruct;
249     MacroAssemblerCodePtr m_jitCodeForCallWithArityCheck;
250     MacroAssemblerCodePtr m_jitCodeForConstructWithArityCheck;
251 };
252
253 class NativeExecutable final : public ExecutableBase {
254     friend class JIT;
255     friend class LLIntOffsetsExtractor;
256 public:
257     typedef ExecutableBase Base;
258     static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
259
260     static NativeExecutable* create(VM& vm, PassRefPtr<JITCode> callThunk, NativeFunction function, PassRefPtr<JITCode> constructThunk, NativeFunction constructor, Intrinsic intrinsic)
261     {
262         NativeExecutable* executable;
263         executable = new (NotNull, allocateCell<NativeExecutable>(vm.heap)) NativeExecutable(vm, function, constructor);
264         executable->finishCreation(vm, callThunk, constructThunk, intrinsic);
265         return executable;
266     }
267
268     static void destroy(JSCell*);
269
270     CodeBlockHash hashFor(CodeSpecializationKind) const;
271
272     NativeFunction function() { return m_function; }
273     NativeFunction constructor() { return m_constructor; }
274         
275     NativeFunction nativeFunctionFor(CodeSpecializationKind kind)
276     {
277         if (kind == CodeForCall)
278             return function();
279         ASSERT(kind == CodeForConstruct);
280         return constructor();
281     }
282         
283     static ptrdiff_t offsetOfNativeFunctionFor(CodeSpecializationKind kind)
284     {
285         if (kind == CodeForCall)
286             return OBJECT_OFFSETOF(NativeExecutable, m_function);
287         ASSERT(kind == CodeForConstruct);
288         return OBJECT_OFFSETOF(NativeExecutable, m_constructor);
289     }
290
291     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) { return Structure::create(vm, globalObject, proto, TypeInfo(CellType, StructureFlags), info()); }
292         
293     DECLARE_INFO;
294
295     Intrinsic intrinsic() const;
296
297 protected:
298     void finishCreation(VM& vm, PassRefPtr<JITCode> callThunk, PassRefPtr<JITCode> constructThunk, Intrinsic intrinsic)
299     {
300         Base::finishCreation(vm);
301         m_jitCodeForCall = callThunk;
302         m_jitCodeForConstruct = constructThunk;
303         m_intrinsic = intrinsic;
304     }
305
306 private:
307     friend class ExecutableBase;
308
309     NativeExecutable(VM& vm, NativeFunction function, NativeFunction constructor)
310         : ExecutableBase(vm, vm.nativeExecutableStructure.get(), NUM_PARAMETERS_IS_HOST)
311         , m_function(function)
312         , m_constructor(constructor)
313     {
314     }
315
316     NativeFunction m_function;
317     NativeFunction m_constructor;
318         
319     Intrinsic m_intrinsic;
320 };
321
322 class ScriptExecutable : public ExecutableBase {
323 public:
324     typedef ExecutableBase Base;
325     static const unsigned StructureFlags = Base::StructureFlags;
326
327     static void destroy(JSCell*);
328         
329     CodeBlockHash hashFor(CodeSpecializationKind) const;
330
331     const SourceCode& source() const { return m_source; }
332     intptr_t sourceID() const { return m_source.providerID(); }
333     const String& sourceURL() const { return m_source.provider()->url(); }
334     int firstLine() const { return m_firstLine; }
335     void setOverrideLineNumber(int overrideLineNumber) { m_overrideLineNumber = overrideLineNumber; }
336     bool hasOverrideLineNumber() const { return m_overrideLineNumber != -1; }
337     int overrideLineNumber() const { return m_overrideLineNumber; }
338     int lastLine() const { return m_lastLine; }
339     unsigned startColumn() const { return m_startColumn; }
340     unsigned endColumn() const { return m_endColumn; }
341     unsigned typeProfilingStartOffset() const { return m_typeProfilingStartOffset; }
342     unsigned typeProfilingEndOffset() const { return m_typeProfilingEndOffset; }
343
344     bool usesEval() const { return m_features & EvalFeature; }
345     bool usesArguments() const { return m_features & ArgumentsFeature; }
346     bool needsActivation() const { return m_hasCapturedVariables || m_features & (EvalFeature | WithFeature); }
347     bool isStrictMode() const { return m_features & StrictModeFeature; }
348     ECMAMode ecmaMode() const { return isStrictMode() ? StrictMode : NotStrictMode; }
349         
350     void setNeverInline(bool value) { m_neverInline = value; }
351     void setNeverOptimize(bool value) { m_neverOptimize = value; }
352     void setDidTryToEnterInLoop(bool value) { m_didTryToEnterInLoop = value; }
353     bool neverInline() const { return m_neverInline; }
354     bool neverOptimize() const { return m_neverOptimize; }
355     bool didTryToEnterInLoop() const { return m_didTryToEnterInLoop; }
356     bool isInliningCandidate() const { return !neverInline(); }
357     bool isOkToOptimize() const { return !neverOptimize(); }
358     
359     bool* addressOfDidTryToEnterInLoop() { return &m_didTryToEnterInLoop; }
360
361     CodeFeatures features() const { return m_features; }
362         
363     DECLARE_EXPORT_INFO;
364
365     void recordParse(CodeFeatures features, bool hasCapturedVariables, int firstLine, int lastLine, unsigned startColumn, unsigned endColumn)
366     {
367         m_features = features;
368         m_hasCapturedVariables = hasCapturedVariables;
369         m_firstLine = firstLine;
370         m_lastLine = lastLine;
371         ASSERT(startColumn != UINT_MAX);
372         m_startColumn = startColumn;
373         ASSERT(endColumn != UINT_MAX);
374         m_endColumn = endColumn;
375     }
376
377     void installCode(CodeBlock*);
378     void installCode(VM&, CodeBlock*, CodeType, CodeSpecializationKind);
379     CodeBlock* newCodeBlockFor(CodeSpecializationKind, JSFunction*, JSScope*, JSObject*& exception);
380     CodeBlock* newReplacementCodeBlockFor(CodeSpecializationKind);
381     
382     JSObject* prepareForExecution(ExecState* exec, JSFunction* function, JSScope* scope, CodeSpecializationKind kind)
383     {
384         if (hasJITCodeFor(kind))
385             return 0;
386         return prepareForExecutionImpl(exec, function, scope, kind);
387     }
388
389     template <typename Functor> void forEachCodeBlock(Functor&&);
390
391 private:
392     friend class ExecutableBase;
393     JSObject* prepareForExecutionImpl(ExecState*, JSFunction*, JSScope*, CodeSpecializationKind);
394
395 protected:
396     ScriptExecutable(Structure* structure, VM& vm, const SourceCode& source, bool isInStrictContext);
397
398     void finishCreation(VM& vm)
399     {
400         Base::finishCreation(vm);
401         vm.heap.addExecutable(this); // Balanced by Heap::deleteUnmarkedCompiledCode().
402
403 #if ENABLE(CODEBLOCK_SAMPLING)
404         if (SamplingTool* sampler = vm.interpreter->sampler())
405             sampler->notifyOfScope(vm, this);
406 #endif
407     }
408
409     SourceCode m_source;
410     CodeFeatures m_features;
411     bool m_hasCapturedVariables;
412     bool m_neverInline;
413     bool m_neverOptimize { false };
414     bool m_didTryToEnterInLoop;
415     int m_overrideLineNumber;
416     int m_firstLine;
417     int m_lastLine;
418     unsigned m_startColumn;
419     unsigned m_endColumn;
420     unsigned m_typeProfilingStartOffset;
421     unsigned m_typeProfilingEndOffset;
422 };
423
424 class EvalExecutable final : public ScriptExecutable {
425     friend class LLIntOffsetsExtractor;
426 public:
427     typedef ScriptExecutable Base;
428     static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
429
430     static void destroy(JSCell*);
431
432     EvalCodeBlock* codeBlock()
433     {
434         return m_evalCodeBlock.get();
435     }
436
437     static EvalExecutable* create(ExecState*, const SourceCode&, bool isInStrictContext, ThisTDZMode, const VariableEnvironment*);
438
439     PassRefPtr<JITCode> generatedJITCode()
440     {
441         return generatedJITCodeForCall();
442     }
443
444     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
445     {
446         return Structure::create(vm, globalObject, proto, TypeInfo(EvalExecutableType, StructureFlags), info());
447     }
448         
449     DECLARE_INFO;
450
451     ExecutableInfo executableInfo() const { return ExecutableInfo(needsActivation(), usesEval(), isStrictMode(), false, false, ConstructorKind::None, GeneratorThisMode::NonEmpty, SuperBinding::NotNeeded, SourceParseMode::ProgramMode); }
452
453     unsigned numVariables() { return m_unlinkedEvalCodeBlock->numVariables(); }
454     unsigned numberOfFunctionDecls() { return m_unlinkedEvalCodeBlock->numberOfFunctionDecls(); }
455
456 private:
457     friend class ExecutableBase;
458     friend class ScriptExecutable;
459     EvalExecutable(ExecState*, const SourceCode&, bool);
460
461     static void visitChildren(JSCell*, SlotVisitor&);
462
463     WriteBarrier<EvalCodeBlock> m_evalCodeBlock;
464     WriteBarrier<UnlinkedEvalCodeBlock> m_unlinkedEvalCodeBlock;
465 };
466
467 class ProgramExecutable final : public ScriptExecutable {
468     friend class LLIntOffsetsExtractor;
469 public:
470     typedef ScriptExecutable Base;
471     static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
472
473     static ProgramExecutable* create(ExecState* exec, const SourceCode& source)
474     {
475         ProgramExecutable* executable = new (NotNull, allocateCell<ProgramExecutable>(*exec->heap())) ProgramExecutable(exec, source);
476         executable->finishCreation(exec->vm());
477         return executable;
478     }
479
480
481     JSObject* initializeGlobalProperties(VM&, CallFrame*, JSScope*);
482
483     static void destroy(JSCell*);
484
485     ProgramCodeBlock* codeBlock()
486     {
487         return m_programCodeBlock.get();
488     }
489
490     JSObject* checkSyntax(ExecState*);
491
492     PassRefPtr<JITCode> generatedJITCode()
493     {
494         return generatedJITCodeForCall();
495     }
496         
497     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
498     {
499         return Structure::create(vm, globalObject, proto, TypeInfo(ProgramExecutableType, StructureFlags), info());
500     }
501         
502     DECLARE_INFO;
503
504     ExecutableInfo executableInfo() const { return ExecutableInfo(needsActivation(), usesEval(), isStrictMode(), false, false, ConstructorKind::None, GeneratorThisMode::NonEmpty, SuperBinding::NotNeeded, SourceParseMode::ProgramMode); }
505
506 private:
507     friend class ExecutableBase;
508     friend class ScriptExecutable;
509
510     ProgramExecutable(ExecState*, const SourceCode&);
511
512     static void visitChildren(JSCell*, SlotVisitor&);
513
514     WriteBarrier<UnlinkedProgramCodeBlock> m_unlinkedProgramCodeBlock;
515     WriteBarrier<ProgramCodeBlock> m_programCodeBlock;
516 };
517
518 class ModuleProgramExecutable final : public ScriptExecutable {
519     friend class LLIntOffsetsExtractor;
520 public:
521     typedef ScriptExecutable Base;
522     static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
523
524     static ModuleProgramExecutable* create(ExecState*, const SourceCode&);
525
526     static void destroy(JSCell*);
527
528     ModuleProgramCodeBlock* codeBlock()
529     {
530         return m_moduleProgramCodeBlock.get();
531     }
532
533     PassRefPtr<JITCode> generatedJITCode()
534     {
535         return generatedJITCodeForCall();
536     }
537
538     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
539     {
540         return Structure::create(vm, globalObject, proto, TypeInfo(ModuleProgramExecutableType, StructureFlags), info());
541     }
542
543     DECLARE_INFO;
544
545     ExecutableInfo executableInfo() const { return ExecutableInfo(needsActivation(), usesEval(), isStrictMode(), false, false, ConstructorKind::None, GeneratorThisMode::NonEmpty, SuperBinding::NotNeeded, SourceParseMode::ModuleEvaluateMode); }
546     UnlinkedModuleProgramCodeBlock* unlinkedModuleProgramCodeBlock() { return m_unlinkedModuleProgramCodeBlock.get(); }
547
548     SymbolTable* moduleEnvironmentSymbolTable() { return m_moduleEnvironmentSymbolTable.get(); }
549
550 private:
551     friend class ExecutableBase;
552     friend class ScriptExecutable;
553
554     ModuleProgramExecutable(ExecState*, const SourceCode&);
555
556     static void visitChildren(JSCell*, SlotVisitor&);
557
558     WriteBarrier<UnlinkedModuleProgramCodeBlock> m_unlinkedModuleProgramCodeBlock;
559     WriteBarrier<SymbolTable> m_moduleEnvironmentSymbolTable;
560     WriteBarrier<ModuleProgramCodeBlock> m_moduleProgramCodeBlock;
561 };
562
563 class FunctionExecutable final : public ScriptExecutable {
564     friend class JIT;
565     friend class LLIntOffsetsExtractor;
566 public:
567     typedef ScriptExecutable Base;
568     static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
569
570     static FunctionExecutable* create(
571         VM& vm, const SourceCode& source, UnlinkedFunctionExecutable* unlinkedExecutable, 
572         unsigned firstLine, unsigned lastLine, unsigned startColumn, unsigned endColumn)
573     {
574         FunctionExecutable* executable = new (NotNull, allocateCell<FunctionExecutable>(vm.heap)) FunctionExecutable(vm, source, unlinkedExecutable, firstLine, lastLine, startColumn, endColumn);
575         executable->finishCreation(vm);
576         return executable;
577     }
578     static FunctionExecutable* fromGlobalCode(
579         const Identifier& name, ExecState&, const SourceCode&, 
580         JSObject*& exception, int overrideLineNumber);
581
582     static void destroy(JSCell*);
583         
584     UnlinkedFunctionExecutable* unlinkedExecutable() const
585     {
586         return m_unlinkedExecutable.get();
587     }
588
589     // Returns either call or construct bytecode. This can be appropriate
590     // for answering questions that that don't vary between call and construct --
591     // for example, argumentsRegister().
592     FunctionCodeBlock* eitherCodeBlock()
593     {
594         if (m_codeBlockForCall)
595             return m_codeBlockForCall.get();
596         return m_codeBlockForConstruct.get();
597     }
598         
599     bool isGeneratedForCall() const
600     {
601         return !!m_codeBlockForCall;
602     }
603
604     FunctionCodeBlock* codeBlockForCall()
605     {
606         return m_codeBlockForCall.get();
607     }
608
609     bool isGeneratedForConstruct() const
610     {
611         return m_codeBlockForConstruct.get();
612     }
613
614     FunctionCodeBlock* codeBlockForConstruct()
615     {
616         return m_codeBlockForConstruct.get();
617     }
618         
619     bool isGeneratedFor(CodeSpecializationKind kind)
620     {
621         if (kind == CodeForCall)
622             return isGeneratedForCall();
623         ASSERT(kind == CodeForConstruct);
624         return isGeneratedForConstruct();
625     }
626         
627     FunctionCodeBlock* codeBlockFor(CodeSpecializationKind kind)
628     {
629         if (kind == CodeForCall)
630             return codeBlockForCall();
631         ASSERT(kind == CodeForConstruct);
632         return codeBlockForConstruct();
633     }
634
635     FunctionCodeBlock* baselineCodeBlockFor(CodeSpecializationKind);
636         
637     FunctionCodeBlock* profiledCodeBlockFor(CodeSpecializationKind kind)
638     {
639         return baselineCodeBlockFor(kind);
640     }
641
642     RefPtr<TypeSet> returnStatementTypeSet() 
643     {
644         if (!m_returnStatementTypeSet)
645             m_returnStatementTypeSet = TypeSet::create();
646
647         return m_returnStatementTypeSet;
648     }
649         
650     FunctionMode functionMode() { return m_unlinkedExecutable->functionMode(); }
651     bool isBuiltinFunction() const { return m_unlinkedExecutable->isBuiltinFunction(); }
652     ConstructAbility constructAbility() const { return m_unlinkedExecutable->constructAbility(); }
653     bool isClassConstructorFunction() const { return m_unlinkedExecutable->isClassConstructorFunction(); }
654     const Identifier& name() { return m_unlinkedExecutable->name(); }
655     const Identifier& inferredName() { return m_unlinkedExecutable->inferredName(); }
656     JSString* nameValue() const { return m_unlinkedExecutable->nameValue(); }
657     size_t parameterCount() const { return m_unlinkedExecutable->parameterCount(); } // Excluding 'this'!
658     SourceParseMode parseMode() const { return m_unlinkedExecutable->parseMode(); }
659     bool isArrowFunction() const { return parseMode() == SourceParseMode::ArrowFunctionMode; }
660
661     static void visitChildren(JSCell*, SlotVisitor&);
662     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
663     {
664         return Structure::create(vm, globalObject, proto, TypeInfo(FunctionExecutableType, StructureFlags), info());
665     }
666
667     unsigned parametersStartOffset() const { return m_parametersStartOffset; }
668
669     void overrideParameterAndTypeProfilingStartEndOffsets(unsigned parametersStartOffset, unsigned typeProfilingStartOffset, unsigned typeProfilingEndOffset)
670     {
671         m_parametersStartOffset = parametersStartOffset;
672         m_typeProfilingStartOffset = typeProfilingStartOffset;
673         m_typeProfilingEndOffset = typeProfilingEndOffset;
674     }
675
676     DECLARE_INFO;
677
678     InferredValue* singletonFunction() { return m_singletonFunction.get(); }
679
680 private:
681     friend class ExecutableBase;
682     FunctionExecutable(
683         VM&, const SourceCode&, UnlinkedFunctionExecutable*, unsigned firstLine, 
684         unsigned lastLine, unsigned startColumn, unsigned endColumn);
685     
686     void finishCreation(VM&);
687
688     friend class ScriptExecutable;
689     
690     WriteBarrier<UnlinkedFunctionExecutable> m_unlinkedExecutable;
691     WriteBarrier<FunctionCodeBlock> m_codeBlockForCall;
692     WriteBarrier<FunctionCodeBlock> m_codeBlockForConstruct;
693     RefPtr<TypeSet> m_returnStatementTypeSet;
694     unsigned m_parametersStartOffset;
695     WriteBarrier<InferredValue> m_singletonFunction;
696 };
697
698 #if ENABLE(WEBASSEMBLY)
699 class WebAssemblyExecutable final : public ExecutableBase {
700 public:
701     typedef ExecutableBase Base;
702     static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
703
704     static WebAssemblyExecutable* create(VM& vm, const SourceCode& source, JSWASMModule* module, unsigned functionIndex)
705     {
706         WebAssemblyExecutable* executable = new (NotNull, allocateCell<WebAssemblyExecutable>(vm.heap)) WebAssemblyExecutable(vm, source, module, functionIndex);
707         executable->finishCreation(vm);
708         return executable;
709     }
710
711     static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
712     {
713         return Structure::create(vm, globalObject, proto, TypeInfo(WebAssemblyExecutableType, StructureFlags), info());
714     }
715
716     static void destroy(JSCell*);
717
718     DECLARE_INFO;
719
720     void prepareForExecution(ExecState*);
721
722     WebAssemblyCodeBlock* codeBlockForCall()
723     {
724         return m_codeBlockForCall.get();
725     }
726
727 private:
728     friend class ExecutableBase;
729     WebAssemblyExecutable(VM&, const SourceCode&, JSWASMModule*, unsigned functionIndex);
730
731     static void visitChildren(JSCell*, SlotVisitor&);
732
733     SourceCode m_source;
734     WriteBarrier<JSWASMModule> m_module;
735     unsigned m_functionIndex;
736
737     WriteBarrier<WebAssemblyCodeBlock> m_codeBlockForCall;
738 };
739 #endif
740
741 } // namespace JSC
742
743 #endif // Executable_h