2 * Copyright (C) 2012, 2013, 2015 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "UnlinkedFunctionExecutable.h"
29 #include "BytecodeGenerator.h"
30 #include "ClassInfo.h"
31 #include "CodeCache.h"
32 #include "Executable.h"
33 #include "ExecutableInfo.h"
34 #include "FunctionOverrides.h"
35 #include "JSCInlines.h"
38 #include "SourceProvider.h"
39 #include "Structure.h"
40 #include "SymbolTable.h"
41 #include "UnlinkedInstructionStream.h"
42 #include <wtf/DataLog.h>
46 static_assert(sizeof(UnlinkedFunctionExecutable) <= 256, "UnlinkedFunctionExecutable should fit in a 256-byte cell.");
48 const ClassInfo UnlinkedFunctionExecutable::s_info = { "UnlinkedFunctionExecutable", 0, 0, CREATE_METHOD_TABLE(UnlinkedFunctionExecutable) };
50 static UnlinkedFunctionCodeBlock* generateUnlinkedFunctionCodeBlock(
51 VM& vm, UnlinkedFunctionExecutable* executable, const SourceCode& source,
52 CodeSpecializationKind kind, DebuggerMode debuggerMode, ProfilerMode profilerMode,
53 UnlinkedFunctionKind functionKind, ParserError& error, SourceParseMode parseMode)
55 JSParserBuiltinMode builtinMode = executable->isBuiltinFunction() ? JSParserBuiltinMode::Builtin : JSParserBuiltinMode::NotBuiltin;
56 JSParserStrictMode strictMode = executable->isInStrictContext() ? JSParserStrictMode::Strict : JSParserStrictMode::NotStrict;
57 ASSERT(isFunctionParseMode(executable->parseMode()));
58 std::unique_ptr<FunctionNode> function = parse<FunctionNode>(
59 &vm, source, executable->name(), builtinMode, strictMode, executable->parseMode(), executable->superBinding(), error, nullptr);
62 ASSERT(error.isValid());
66 function->finishParsing(executable->name(), executable->functionMode());
67 executable->recordParse(function->features(), function->hasCapturedVariables());
69 UnlinkedFunctionCodeBlock* result = UnlinkedFunctionCodeBlock::create(&vm, FunctionCode,
70 ExecutableInfo(function->needsActivation(), function->usesEval(), function->isStrictMode(), kind == CodeForConstruct, functionKind == UnlinkedBuiltinFunction, executable->constructorKind(), executable->superBinding(), parseMode, executable->isDerivedConstructorContext(), false));
72 auto generator(std::make_unique<BytecodeGenerator>(vm, function.get(), result, debuggerMode, profilerMode, executable->parentScopeTDZVariables()));
73 error = generator->generate();
79 UnlinkedFunctionExecutable::UnlinkedFunctionExecutable(VM* vm, Structure* structure, const SourceCode& source, RefPtr<SourceProvider>&& sourceOverride, FunctionMetadataNode* node, UnlinkedFunctionKind kind, ConstructAbility constructAbility, VariableEnvironment& parentScopeTDZVariables, bool isDerivedConstructorContext)
80 : Base(*vm, structure)
81 , m_name(node->ident())
82 , m_inferredName(node->inferredName())
83 , m_sourceOverride(WTF::move(sourceOverride))
84 , m_firstLineOffset(node->firstLine() - source.firstLine())
85 , m_lineCount(node->lastLine() - node->firstLine())
86 , m_unlinkedFunctionNameStart(node->functionNameStart() - source.startOffset())
87 , m_unlinkedBodyStartColumn(node->startColumn())
88 , m_unlinkedBodyEndColumn(m_lineCount ? node->endColumn() : node->endColumn() - node->startColumn())
89 , m_startOffset(node->source().startOffset() - source.startOffset())
90 , m_sourceLength(node->source().length())
91 , m_parametersStartOffset(node->parametersStart())
92 , m_typeProfilingStartOffset(node->functionKeywordStart())
93 , m_typeProfilingEndOffset(node->startStartOffset() + node->source().length() - 1)
94 , m_parameterCount(node->parameterCount())
95 , m_parseMode(node->parseMode())
97 , m_isInStrictContext(node->isInStrictContext())
98 , m_hasCapturedVariables(false)
99 , m_isBuiltinFunction(kind == UnlinkedBuiltinFunction)
100 , m_constructAbility(static_cast<unsigned>(constructAbility))
101 , m_constructorKind(static_cast<unsigned>(node->constructorKind()))
102 , m_functionMode(node->functionMode())
103 , m_superBinding(static_cast<unsigned>(node->superBinding()))
104 , m_isDerivedConstructorContext(isDerivedConstructorContext)
106 ASSERT(m_constructorKind == static_cast<unsigned>(node->constructorKind()));
107 m_parentScopeTDZVariables.swap(parentScopeTDZVariables);
110 void UnlinkedFunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
112 UnlinkedFunctionExecutable* thisObject = jsCast<UnlinkedFunctionExecutable*>(cell);
113 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
114 Base::visitChildren(thisObject, visitor);
115 visitor.append(&thisObject->m_unlinkedCodeBlockForCall);
116 visitor.append(&thisObject->m_unlinkedCodeBlockForConstruct);
117 visitor.append(&thisObject->m_nameValue);
120 FunctionExecutable* UnlinkedFunctionExecutable::link(VM& vm, const SourceCode& ownerSource, int overrideLineNumber)
122 SourceCode source = m_sourceOverride ? SourceCode(m_sourceOverride) : ownerSource;
123 unsigned firstLine = source.firstLine() + m_firstLineOffset;
124 unsigned startOffset = source.startOffset() + m_startOffset;
125 unsigned lineCount = m_lineCount;
127 // Adjust to one-based indexing.
128 bool startColumnIsOnFirstSourceLine = !m_firstLineOffset;
129 unsigned startColumn = m_unlinkedBodyStartColumn + (startColumnIsOnFirstSourceLine ? source.startColumn() : 1);
130 bool endColumnIsOnStartLine = !lineCount;
131 unsigned endColumn = m_unlinkedBodyEndColumn + (endColumnIsOnStartLine ? startColumn : 1);
133 SourceCode code(source.provider(), startOffset, startOffset + m_sourceLength, firstLine, startColumn);
134 FunctionOverrides::OverrideInfo overrideInfo;
135 bool hasFunctionOverride = false;
137 if (UNLIKELY(Options::functionOverrides())) {
138 hasFunctionOverride = FunctionOverrides::initializeOverrideFor(code, overrideInfo);
139 if (hasFunctionOverride) {
140 firstLine = overrideInfo.firstLine;
141 lineCount = overrideInfo.lineCount;
142 startColumn = overrideInfo.startColumn;
143 endColumn = overrideInfo.endColumn;
144 code = overrideInfo.sourceCode;
148 FunctionExecutable* result = FunctionExecutable::create(vm, code, this, firstLine, firstLine + lineCount, startColumn, endColumn);
149 if (overrideLineNumber != -1)
150 result->setOverrideLineNumber(overrideLineNumber);
152 if (UNLIKELY(hasFunctionOverride)) {
153 result->overrideParameterAndTypeProfilingStartEndOffsets(
154 overrideInfo.parametersStartOffset,
155 overrideInfo.typeProfilingStartOffset,
156 overrideInfo.typeProfilingEndOffset);
162 UnlinkedFunctionExecutable* UnlinkedFunctionExecutable::fromGlobalCode(
163 const Identifier& name, ExecState& exec, const SourceCode& source,
164 JSObject*& exception, int overrideLineNumber)
168 CodeCache* codeCache = vm.codeCache();
169 UnlinkedFunctionExecutable* executable = codeCache->getFunctionExecutableFromGlobalCode(vm, name, source, error);
171 auto& globalObject = *exec.lexicalGlobalObject();
172 if (globalObject.hasDebugger())
173 globalObject.debugger()->sourceParsed(&exec, source.provider(), error.line(), error.message());
175 if (error.isValid()) {
176 exception = error.toErrorObject(&globalObject, source, overrideLineNumber);
183 UnlinkedFunctionCodeBlock* UnlinkedFunctionExecutable::unlinkedCodeBlockFor(
184 VM& vm, const SourceCode& source, CodeSpecializationKind specializationKind,
185 DebuggerMode debuggerMode, ProfilerMode profilerMode, ParserError& error, SourceParseMode parseMode)
187 switch (specializationKind) {
189 if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForCall.get())
192 case CodeForConstruct:
193 if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForConstruct.get())
198 UnlinkedFunctionCodeBlock* result = generateUnlinkedFunctionCodeBlock(
199 vm, this, source, specializationKind, debuggerMode, profilerMode,
200 isBuiltinFunction() ? UnlinkedBuiltinFunction : UnlinkedNormalFunction,
206 switch (specializationKind) {
208 m_unlinkedCodeBlockForCall.set(vm, this, result);
210 case CodeForConstruct:
211 m_unlinkedCodeBlockForConstruct.set(vm, this, result);
217 void UnlinkedFunctionExecutable::setInvalidTypeProfilingOffsets()
219 m_typeProfilingStartOffset = std::numeric_limits<unsigned>::max();
220 m_typeProfilingEndOffset = std::numeric_limits<unsigned>::max();