2 * Copyright (C) 2009-2018 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.
28 #include "BatchedTransitionOptimizer.h"
29 #include "CodeBlock.h"
31 #include "FunctionCodeBlock.h"
32 #include "FunctionOverrides.h"
34 #include "JSCInlines.h"
35 #include "LLIntEntrypoint.h"
37 #include "TypeProfiler.h"
38 #include "VMInlines.h"
39 #include <wtf/CommaPrinter.h>
43 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(FunctionExecutable) };
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)
49 RELEASE_ASSERT(!source.isNull());
50 ASSERT(source.length());
52 new (&m_singletonFunction) WriteBarrier<InferredValue>();
54 m_singletonFunctionState = ClearWatchpoint;
57 void FunctionExecutable::finishCreation(VM& vm)
59 Base::finishCreation(vm);
61 m_singletonFunction.set(vm, this, InferredValue::create(vm));
64 void FunctionExecutable::destroy(JSCell* cell)
66 static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
69 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
71 ExecutableToCodeBlockEdge* edge;
72 if (kind == CodeForCall)
73 edge = m_codeBlockForCall.get();
75 RELEASE_ASSERT(kind == CodeForConstruct);
76 edge = m_codeBlockForConstruct.get();
80 return static_cast<FunctionCodeBlock*>(edge->codeBlock()->baselineAlternative());
83 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
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);
92 visitor.append(thisObject->m_singletonFunction);
93 visitor.append(thisObject->m_cachedPolyProtoStructure);
96 FunctionExecutable* FunctionExecutable::fromGlobalCode(
97 const Identifier& name, ExecState& exec, const SourceCode& source,
98 JSObject*& exception, int overrideLineNumber, Optional<int> functionConstructorParametersEndPosition)
100 UnlinkedFunctionExecutable* unlinkedExecutable =
101 UnlinkedFunctionExecutable::fromGlobalCode(
102 name, exec, source, exception, overrideLineNumber, functionConstructorParametersEndPosition);
103 if (!unlinkedExecutable)
106 return unlinkedExecutable->link(exec.vm(), source, overrideLineNumber);
109 FunctionExecutable::RareData& FunctionExecutable::ensureRareDataSlow()
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);
122 void FunctionExecutable::overrideInfo(const FunctionOverrideInfo& overrideInfo)
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;