Get rid of HeapRootVisitor and make SlotVisitor less painful to use
[WebKit-https.git] / Source / JavaScriptCore / bytecode / UnlinkedFunctionExecutable.cpp
1 /*
2  * Copyright (C) 2012-2013, 2015-2016 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 "UnlinkedFunctionExecutable.h"
28
29 #include "BytecodeGenerator.h"
30 #include "ClassInfo.h"
31 #include "CodeCache.h"
32 #include "Debugger.h"
33 #include "ExecutableInfo.h"
34 #include "FunctionOverrides.h"
35 #include "JSCInlines.h"
36 #include "Parser.h"
37 #include "SourceProvider.h"
38 #include "Structure.h"
39 #include "UnlinkedFunctionCodeBlock.h"
40
41 namespace JSC {
42
43 static_assert(sizeof(UnlinkedFunctionExecutable) <= 256, "UnlinkedFunctionExecutable should fit in a 256-byte cell.");
44
45 const ClassInfo UnlinkedFunctionExecutable::s_info = { "UnlinkedFunctionExecutable", 0, 0, CREATE_METHOD_TABLE(UnlinkedFunctionExecutable) };
46
47 static UnlinkedFunctionCodeBlock* generateUnlinkedFunctionCodeBlock(
48     VM& vm, UnlinkedFunctionExecutable* executable, const SourceCode& source,
49     CodeSpecializationKind kind, DebuggerMode debuggerMode,
50     UnlinkedFunctionKind functionKind, ParserError& error, SourceParseMode parseMode)
51 {
52     JSParserBuiltinMode builtinMode = executable->isBuiltinFunction() ? JSParserBuiltinMode::Builtin : JSParserBuiltinMode::NotBuiltin;
53     JSParserStrictMode strictMode = executable->isInStrictContext() ? JSParserStrictMode::Strict : JSParserStrictMode::NotStrict;
54     JSParserScriptMode scriptMode = executable->scriptMode();
55     ASSERT(isFunctionParseMode(executable->parseMode()));
56     std::unique_ptr<FunctionNode> function = parse<FunctionNode>(
57         &vm, source, executable->name(), builtinMode, strictMode, scriptMode, executable->parseMode(), executable->superBinding(), error, nullptr);
58
59     if (!function) {
60         ASSERT(error.isValid());
61         return nullptr;
62     }
63
64     function->finishParsing(executable->name(), executable->functionMode());
65     executable->recordParse(function->features(), function->hasCapturedVariables());
66
67     bool isClassContext = executable->superBinding() == SuperBinding::Needed;
68
69     UnlinkedFunctionCodeBlock* result = UnlinkedFunctionCodeBlock::create(&vm, FunctionCode, ExecutableInfo(function->usesEval(), function->isStrictMode(), kind == CodeForConstruct, functionKind == UnlinkedBuiltinFunction, executable->constructorKind(), scriptMode, executable->superBinding(), parseMode, executable->derivedContextType(), false, isClassContext, EvalContextType::FunctionEvalContext), debuggerMode);
70
71     error = BytecodeGenerator::generate(vm, function.get(), result, debuggerMode, executable->parentScopeTDZVariables());
72
73     if (error.isValid())
74         return nullptr;
75     return result;
76 }
77
78 UnlinkedFunctionExecutable::UnlinkedFunctionExecutable(VM* vm, Structure* structure, const SourceCode& parentSource, SourceCode&& parentSourceOverride, FunctionMetadataNode* node, UnlinkedFunctionKind kind, ConstructAbility constructAbility, JSParserScriptMode scriptMode, VariableEnvironment& parentScopeTDZVariables, DerivedContextType derivedContextType)
79     : Base(*vm, structure)
80     , m_firstLineOffset(node->firstLine() - parentSource.firstLine().oneBasedInt())
81     , m_lineCount(node->lastLine() - node->firstLine())
82     , m_unlinkedFunctionNameStart(node->functionNameStart() - parentSource.startOffset())
83     , m_unlinkedBodyStartColumn(node->startColumn())
84     , m_unlinkedBodyEndColumn(m_lineCount ? node->endColumn() : node->endColumn() - node->startColumn())
85     , m_startOffset(node->source().startOffset() - parentSource.startOffset())
86     , m_sourceLength(node->source().length())
87     , m_parametersStartOffset(node->parametersStart())
88     , m_typeProfilingStartOffset(node->functionKeywordStart())
89     , m_typeProfilingEndOffset(node->startStartOffset() + node->source().length() - 1)
90     , m_parameterCount(node->parameterCount())
91     , m_functionLength(node->functionLength())
92     , m_features(0)
93     , m_sourceParseMode(node->parseMode())
94     , m_isInStrictContext(node->isInStrictContext())
95     , m_hasCapturedVariables(false)
96     , m_isBuiltinFunction(kind == UnlinkedBuiltinFunction)
97     , m_constructAbility(static_cast<unsigned>(constructAbility))
98     , m_constructorKind(static_cast<unsigned>(node->constructorKind()))
99     , m_functionMode(static_cast<unsigned>(node->functionMode()))
100     , m_scriptMode(static_cast<unsigned>(scriptMode))
101     , m_superBinding(static_cast<unsigned>(node->superBinding()))
102     , m_derivedContextType(static_cast<unsigned>(derivedContextType))
103     , m_name(node->ident())
104     , m_ecmaName(node->ecmaName())
105     , m_inferredName(node->inferredName())
106     , m_parentSourceOverride(WTFMove(parentSourceOverride))
107     , m_classSource(node->classSource())
108 {
109     // Make sure these bitfields are adequately wide.
110     ASSERT(m_constructAbility == static_cast<unsigned>(constructAbility));
111     ASSERT(m_constructorKind == static_cast<unsigned>(node->constructorKind()));
112     ASSERT(m_functionMode == static_cast<unsigned>(node->functionMode()));
113     ASSERT(m_scriptMode == static_cast<unsigned>(scriptMode));
114     ASSERT(m_superBinding == static_cast<unsigned>(node->superBinding()));
115     ASSERT(m_derivedContextType == static_cast<unsigned>(derivedContextType));
116
117     m_parentScopeTDZVariables.swap(parentScopeTDZVariables);
118 }
119
120 void UnlinkedFunctionExecutable::destroy(JSCell* cell)
121 {
122     jsCast<UnlinkedFunctionExecutable*>(cell)->~UnlinkedFunctionExecutable();
123 }
124
125 void UnlinkedFunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
126 {
127     UnlinkedFunctionExecutable* thisObject = jsCast<UnlinkedFunctionExecutable*>(cell);
128     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
129     Base::visitChildren(thisObject, visitor);
130     visitor.append(thisObject->m_unlinkedCodeBlockForCall);
131     visitor.append(thisObject->m_unlinkedCodeBlockForConstruct);
132 }
133
134 FunctionExecutable* UnlinkedFunctionExecutable::link(VM& vm, const SourceCode& passedParentSource, std::optional<int> overrideLineNumber, Intrinsic intrinsic)
135 {
136     const SourceCode& parentSource = m_parentSourceOverride.isNull() ? passedParentSource : m_parentSourceOverride;
137     unsigned firstLine = parentSource.firstLine().oneBasedInt() + m_firstLineOffset;
138     unsigned startOffset = parentSource.startOffset() + m_startOffset;
139     unsigned lineCount = m_lineCount;
140
141     unsigned startColumn = linkedStartColumn(parentSource.startColumn().oneBasedInt());
142     unsigned endColumn = linkedEndColumn(startColumn);
143
144     SourceCode source(parentSource.provider(), startOffset, startOffset + m_sourceLength, firstLine, startColumn);
145     FunctionOverrides::OverrideInfo overrideInfo;
146     bool hasFunctionOverride = false;
147
148     if (UNLIKELY(Options::functionOverrides())) {
149         hasFunctionOverride = FunctionOverrides::initializeOverrideFor(source, overrideInfo);
150         if (UNLIKELY(hasFunctionOverride)) {
151             firstLine = overrideInfo.firstLine;
152             lineCount = overrideInfo.lineCount;
153             startColumn = overrideInfo.startColumn;
154             endColumn = overrideInfo.endColumn;
155             source = overrideInfo.sourceCode;
156         }
157     }
158
159     FunctionExecutable* result = FunctionExecutable::create(vm, source, this, firstLine + lineCount, endColumn, intrinsic);
160     if (overrideLineNumber)
161         result->setOverrideLineNumber(*overrideLineNumber);
162
163     if (UNLIKELY(hasFunctionOverride)) {
164         result->overrideParameterAndTypeProfilingStartEndOffsets(
165             overrideInfo.parametersStartOffset,
166             overrideInfo.typeProfilingStartOffset,
167             overrideInfo.typeProfilingEndOffset);
168     }
169
170     return result;
171 }
172
173 UnlinkedFunctionExecutable* UnlinkedFunctionExecutable::fromGlobalCode(
174     const Identifier& name, ExecState& exec, const SourceCode& source, 
175     JSObject*& exception, int overrideLineNumber)
176 {
177     ParserError error;
178     VM& vm = exec.vm();
179     auto& globalObject = *exec.lexicalGlobalObject();
180     CodeCache* codeCache = vm.codeCache();
181     DebuggerMode debuggerMode = globalObject.hasInteractiveDebugger() ? DebuggerOn : DebuggerOff;
182     UnlinkedFunctionExecutable* executable = codeCache->getUnlinkedGlobalFunctionExecutable(vm, name, source, debuggerMode, error);
183
184     if (globalObject.hasDebugger())
185         globalObject.debugger()->sourceParsed(&exec, source.provider(), error.line(), error.message());
186
187     if (error.isValid()) {
188         exception = error.toErrorObject(&globalObject, source, overrideLineNumber);
189         return nullptr;
190     }
191
192     return executable;
193 }
194
195 UnlinkedFunctionCodeBlock* UnlinkedFunctionExecutable::unlinkedCodeBlockFor(
196     VM& vm, const SourceCode& source, CodeSpecializationKind specializationKind, 
197     DebuggerMode debuggerMode, ParserError& error, SourceParseMode parseMode)
198 {
199     switch (specializationKind) {
200     case CodeForCall:
201         if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForCall.get())
202             return codeBlock;
203         break;
204     case CodeForConstruct:
205         if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForConstruct.get())
206             return codeBlock;
207         break;
208     }
209
210     UnlinkedFunctionCodeBlock* result = generateUnlinkedFunctionCodeBlock(
211         vm, this, source, specializationKind, debuggerMode, 
212         isBuiltinFunction() ? UnlinkedBuiltinFunction : UnlinkedNormalFunction, 
213         error, parseMode);
214     
215     if (error.isValid())
216         return nullptr;
217
218     switch (specializationKind) {
219     case CodeForCall:
220         m_unlinkedCodeBlockForCall.set(vm, this, result);
221         break;
222     case CodeForConstruct:
223         m_unlinkedCodeBlockForConstruct.set(vm, this, result);
224         break;
225     }
226     return result;
227 }
228
229 void UnlinkedFunctionExecutable::setInvalidTypeProfilingOffsets()
230 {
231     m_typeProfilingStartOffset = std::numeric_limits<unsigned>::max();
232     m_typeProfilingEndOffset = std::numeric_limits<unsigned>::max();
233 }
234
235 } // namespace JSC