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
41namespace JSC {
42
43const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(FunctionExecutable) };
44
45FunctionExecutable::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}
52
53void FunctionExecutable::finishCreation(VM& vm, ScriptExecutable* topLevelExecutable)
54{
55 Base::finishCreation(vm);
56 m_topLevelExecutable.set(vm, this, topLevelExecutable ? topLevelExecutable : this);
57}
58
59void FunctionExecutable::destroy(JSCell* cell)
60{
61 static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
62}
63
64FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
65{
66 ExecutableToCodeBlockEdge* edge;
67 if (kind == CodeForCall)
68 edge = m_codeBlockForCall.get();
69 else {
70 RELEASE_ASSERT(kind == CodeForConstruct);
71 edge = m_codeBlockForConstruct.get();
72 }
73 if (!edge)
74 return 0;
75 return static_cast<FunctionCodeBlock*>(edge->codeBlock()->baselineAlternative());
76}
77
78void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
79{
80 FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
81 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
82 Base::visitChildren(thisObject, visitor);
83 visitor.append(thisObject->m_topLevelExecutable);
84 visitor.append(thisObject->m_codeBlockForCall);
85 visitor.append(thisObject->m_codeBlockForConstruct);
86 visitor.append(thisObject->m_unlinkedExecutable);
87 if (RareData* rareData = thisObject->m_rareData.get()) {
88 visitor.append(rareData->m_cachedPolyProtoStructure);
89 if (TemplateObjectMap* map = rareData->m_templateObjectMap.get()) {
90 auto locker = holdLock(thisObject->cellLock());
91 for (auto& entry : *map)
92 visitor.append(entry.value);
93 }
94 }
95}
96
97FunctionExecutable* FunctionExecutable::fromGlobalCode(
98 const Identifier& name, JSGlobalObject* globalObject, const SourceCode& source,
99 JSObject*& exception, int overrideLineNumber, Optional<int> functionConstructorParametersEndPosition)
100{
101 UnlinkedFunctionExecutable* unlinkedExecutable =
102 UnlinkedFunctionExecutable::fromGlobalCode(
103 name, globalObject, source, exception, overrideLineNumber, functionConstructorParametersEndPosition);
104 if (!unlinkedExecutable)
105 return nullptr;
106
107 return unlinkedExecutable->link(globalObject->vm(), nullptr, source, overrideLineNumber);
108}
109
110FunctionExecutable::RareData& FunctionExecutable::ensureRareDataSlow()
111{
112 ASSERT(!m_rareData);
113 auto rareData = makeUnique<RareData>();
114 rareData->m_lineCount = lineCount();
115 rareData->m_endColumn = endColumn();
116 rareData->m_parametersStartOffset = parametersStartOffset();
117 rareData->m_typeProfilingStartOffset = typeProfilingStartOffset();
118 rareData->m_typeProfilingEndOffset = typeProfilingEndOffset();
119 WTF::storeStoreFence();
120 m_rareData = WTFMove(rareData);
121 return *m_rareData;
122}
123
124void FunctionExecutable::overrideInfo(const FunctionOverrideInfo& overrideInfo)
125{
126 auto& rareData = ensureRareData();
127 m_source = overrideInfo.sourceCode;
128 rareData.m_lineCount = overrideInfo.lineCount;
129 rareData.m_endColumn = overrideInfo.endColumn;
130 rareData.m_parametersStartOffset = overrideInfo.parametersStartOffset;
131 rareData.m_typeProfilingStartOffset = overrideInfo.typeProfilingStartOffset;
132 rareData.m_typeProfilingEndOffset = overrideInfo.typeProfilingEndOffset;
133}
134
135auto FunctionExecutable::ensureTemplateObjectMap(VM&) -> TemplateObjectMap&
136{
137 RareData& rareData = ensureRareData();
138 return ensureTemplateObjectMapImpl(rareData.m_templateObjectMap);
139}
140
141} // namespace JSC
142