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