2e85f4764713b22169ca7e0d271fe1ebd18ff6d1
[WebKit-https.git] / Source / JavaScriptCore / runtime / FunctionExecutable.cpp
1 /*
2  * Copyright (C) 2009-2018 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
28 #include "BatchedTransitionOptimizer.h"
29 #include "CodeBlock.h"
30 #include "Debugger.h"
31 #include "FunctionCodeBlock.h"
32 #include "FunctionOverrides.h"
33 #include "JIT.h"
34 #include "JSCInlines.h"
35 #include "LLIntEntrypoint.h"
36 #include "Parser.h"
37 #include "TypeProfiler.h"
38 #include "VMInlines.h"
39 #include <wtf/CommaPrinter.h>
40
41 namespace JSC {
42
43 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(FunctionExecutable) };
44
45 FunctionExecutable::FunctionExecutable(VM& vm, const SourceCode& source, UnlinkedFunctionExecutable* unlinkedExecutable, Intrinsic intrinsic)
46     : ScriptExecutable(vm.functionExecutableStructure.get(), vm, source, unlinkedExecutable->isInStrictContext(), unlinkedExecutable->derivedContextType(), false, EvalContextType::None, intrinsic)
47     , m_unlinkedExecutable(vm, this, unlinkedExecutable)
48 {
49     RELEASE_ASSERT(!source.isNull());
50     ASSERT(source.length());
51     if (VM::canUseJIT())
52         new (&m_singletonFunction) WriteBarrier<InferredValue>();
53     else
54         m_singletonFunctionState = ClearWatchpoint;
55 }
56
57 void FunctionExecutable::finishCreation(VM& vm)
58 {
59     Base::finishCreation(vm);
60     if (VM::canUseJIT())
61         m_singletonFunction.set(vm, this, InferredValue::create(vm));
62 }
63
64 void FunctionExecutable::destroy(JSCell* cell)
65 {
66     static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
67 }
68
69 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
70 {
71     ExecutableToCodeBlockEdge* edge;
72     if (kind == CodeForCall)
73         edge = m_codeBlockForCall.get();
74     else {
75         RELEASE_ASSERT(kind == CodeForConstruct);
76         edge = m_codeBlockForConstruct.get();
77     }
78     if (!edge)
79         return 0;
80     return static_cast<FunctionCodeBlock*>(edge->codeBlock()->baselineAlternative());
81 }
82
83 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
84 {
85     FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
86     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
87     Base::visitChildren(thisObject, visitor);
88     visitor.append(thisObject->m_codeBlockForCall);
89     visitor.append(thisObject->m_codeBlockForConstruct);
90     visitor.append(thisObject->m_unlinkedExecutable);
91     if (VM::canUseJIT())
92         visitor.append(thisObject->m_singletonFunction);
93     visitor.append(thisObject->m_cachedPolyProtoStructure);
94 }
95
96 FunctionExecutable* FunctionExecutable::fromGlobalCode(
97     const Identifier& name, ExecState& exec, const SourceCode& source, 
98     JSObject*& exception, int overrideLineNumber, Optional<int> functionConstructorParametersEndPosition)
99 {
100     UnlinkedFunctionExecutable* unlinkedExecutable = 
101         UnlinkedFunctionExecutable::fromGlobalCode(
102             name, exec, source, exception, overrideLineNumber, functionConstructorParametersEndPosition);
103     if (!unlinkedExecutable)
104         return nullptr;
105
106     return unlinkedExecutable->link(exec.vm(), source, overrideLineNumber);
107 }
108
109 FunctionExecutable::RareData& FunctionExecutable::ensureRareDataSlow()
110 {
111     ASSERT(!m_rareData);
112     auto rareData = std::make_unique<RareData>();
113     rareData->m_lineCount = lineCount();
114     rareData->m_endColumn = endColumn();
115     rareData->m_parametersStartOffset = parametersStartOffset();
116     rareData->m_typeProfilingStartOffset = typeProfilingStartOffset();
117     rareData->m_typeProfilingEndOffset = typeProfilingEndOffset();
118     m_rareData = WTFMove(rareData);
119     return *m_rareData;
120 }
121
122 void FunctionExecutable::overrideInfo(const FunctionOverrideInfo& overrideInfo)
123 {
124     auto& rareData = ensureRareData();
125     m_source = overrideInfo.sourceCode;
126     rareData.m_lineCount = overrideInfo.lineCount;
127     rareData.m_endColumn = overrideInfo.endColumn;
128     rareData.m_parametersStartOffset = overrideInfo.parametersStartOffset;
129     rareData.m_typeProfilingStartOffset = overrideInfo.typeProfilingStartOffset;
130     rareData.m_typeProfilingEndOffset = overrideInfo.typeProfilingEndOffset;
131 }
132
133 } // namespace JSC