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 | #pragma once |
27 | |
28 | #include "ExecutableToCodeBlockEdge.h" |
29 | #include "ScriptExecutable.h" |
30 | #include "SourceCode.h" |
31 | #include <wtf/Box.h> |
32 | #include <wtf/Markable.h> |
33 | |
34 | namespace JSC { |
35 | |
36 | struct FunctionOverrideInfo; |
37 | |
38 | class FunctionExecutable final : public ScriptExecutable { |
39 | friend class JIT; |
40 | friend class LLIntOffsetsExtractor; |
41 | public: |
42 | typedef ScriptExecutable Base; |
43 | static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; |
44 | |
45 | template<typename CellType, SubspaceAccess> |
46 | static IsoSubspace* subspaceFor(VM& vm) |
47 | { |
48 | return &vm.functionExecutableSpace.space; |
49 | } |
50 | |
51 | static FunctionExecutable* create(VM& vm, ScriptExecutable* topLevelExecutable, const SourceCode& source, UnlinkedFunctionExecutable* unlinkedExecutable, Intrinsic intrinsic) |
52 | { |
53 | FunctionExecutable* executable = new (NotNull, allocateCell<FunctionExecutable>(vm.heap)) FunctionExecutable(vm, source, unlinkedExecutable, intrinsic); |
54 | executable->finishCreation(vm, topLevelExecutable); |
55 | return executable; |
56 | } |
57 | static FunctionExecutable* fromGlobalCode( |
58 | const Identifier& name, ExecState&, const SourceCode&, |
59 | JSObject*& exception, int overrideLineNumber, Optional<int> functionConstructorParametersEndPosition); |
60 | |
61 | static void destroy(JSCell*); |
62 | |
63 | UnlinkedFunctionExecutable* unlinkedExecutable() const |
64 | { |
65 | return m_unlinkedExecutable.get(); |
66 | } |
67 | |
68 | // Returns either call or construct bytecode. This can be appropriate |
69 | // for answering questions that that don't vary between call and construct -- |
70 | // for example, argumentsRegister(). |
71 | FunctionCodeBlock* eitherCodeBlock() |
72 | { |
73 | ExecutableToCodeBlockEdge* edge; |
74 | if (m_codeBlockForCall) |
75 | edge = m_codeBlockForCall.get(); |
76 | else |
77 | edge = m_codeBlockForConstruct.get(); |
78 | return bitwise_cast<FunctionCodeBlock*>(ExecutableToCodeBlockEdge::unwrap(edge)); |
79 | } |
80 | |
81 | bool isGeneratedForCall() const |
82 | { |
83 | return !!m_codeBlockForCall; |
84 | } |
85 | |
86 | FunctionCodeBlock* codeBlockForCall() |
87 | { |
88 | return bitwise_cast<FunctionCodeBlock*>(ExecutableToCodeBlockEdge::unwrap(m_codeBlockForCall.get())); |
89 | } |
90 | |
91 | bool isGeneratedForConstruct() const |
92 | { |
93 | return !!m_codeBlockForConstruct; |
94 | } |
95 | |
96 | FunctionCodeBlock* codeBlockForConstruct() |
97 | { |
98 | return bitwise_cast<FunctionCodeBlock*>(ExecutableToCodeBlockEdge::unwrap(m_codeBlockForConstruct.get())); |
99 | } |
100 | |
101 | bool isGeneratedFor(CodeSpecializationKind kind) |
102 | { |
103 | if (kind == CodeForCall) |
104 | return isGeneratedForCall(); |
105 | ASSERT(kind == CodeForConstruct); |
106 | return isGeneratedForConstruct(); |
107 | } |
108 | |
109 | FunctionCodeBlock* codeBlockFor(CodeSpecializationKind kind) |
110 | { |
111 | if (kind == CodeForCall) |
112 | return codeBlockForCall(); |
113 | ASSERT(kind == CodeForConstruct); |
114 | return codeBlockForConstruct(); |
115 | } |
116 | |
117 | FunctionCodeBlock* baselineCodeBlockFor(CodeSpecializationKind); |
118 | |
119 | FunctionCodeBlock* profiledCodeBlockFor(CodeSpecializationKind kind) |
120 | { |
121 | return baselineCodeBlockFor(kind); |
122 | } |
123 | |
124 | RefPtr<TypeSet> returnStatementTypeSet() |
125 | { |
126 | RareData& rareData = ensureRareData(); |
127 | if (!rareData.m_returnStatementTypeSet) |
128 | rareData.m_returnStatementTypeSet = TypeSet::create(); |
129 | return rareData.m_returnStatementTypeSet; |
130 | } |
131 | |
132 | FunctionMode functionMode() { return m_unlinkedExecutable->functionMode(); } |
133 | bool isBuiltinFunction() const { return m_unlinkedExecutable->isBuiltinFunction(); } |
134 | ConstructAbility constructAbility() const { return m_unlinkedExecutable->constructAbility(); } |
135 | bool isClass() const { return m_unlinkedExecutable->isClass(); } |
136 | bool isArrowFunction() const { return parseMode() == SourceParseMode::ArrowFunctionMode; } |
137 | bool isGetter() const { return parseMode() == SourceParseMode::GetterMode; } |
138 | bool isSetter() const { return parseMode() == SourceParseMode::SetterMode; } |
139 | bool isGenerator() const { return isGeneratorParseMode(parseMode()); } |
140 | bool isAsyncGenerator() const { return isAsyncGeneratorParseMode(parseMode()); } |
141 | bool isMethod() const { return parseMode() == SourceParseMode::MethodMode; } |
142 | bool hasCallerAndArgumentsProperties() const |
143 | { |
144 | // Per https://tc39.github.io/ecma262/#sec-forbidden-extensions, only sloppy-mode non-builtin functions in old-style (pre-ES6) syntactic forms can contain |
145 | // "caller" and "arguments". |
146 | return !isStrictMode() && parseMode() == SourceParseMode::NormalFunctionMode && !isClassConstructorFunction(); |
147 | } |
148 | bool hasPrototypeProperty() const |
149 | { |
150 | return SourceParseModeSet( |
151 | SourceParseMode::NormalFunctionMode, |
152 | SourceParseMode::GeneratorBodyMode, |
153 | SourceParseMode::GeneratorWrapperFunctionMode, |
154 | SourceParseMode::GeneratorWrapperMethodMode, |
155 | SourceParseMode::AsyncGeneratorWrapperFunctionMode, |
156 | SourceParseMode::AsyncGeneratorWrapperMethodMode, |
157 | SourceParseMode::AsyncGeneratorBodyMode |
158 | ).contains(parseMode()) || isClass(); |
159 | } |
160 | DerivedContextType derivedContextType() const { return m_unlinkedExecutable->derivedContextType(); } |
161 | bool isClassConstructorFunction() const { return m_unlinkedExecutable->isClassConstructorFunction(); } |
162 | const Identifier& name() { return m_unlinkedExecutable->name(); } |
163 | const Identifier& ecmaName() { return m_unlinkedExecutable->ecmaName(); } |
164 | unsigned parameterCount() const { return m_unlinkedExecutable->parameterCount(); } // Excluding 'this'! |
165 | SourceParseMode parseMode() const { return m_unlinkedExecutable->parseMode(); } |
166 | JSParserScriptMode scriptMode() const { return m_unlinkedExecutable->scriptMode(); } |
167 | SourceCode classSource() const { return m_unlinkedExecutable->classSource(); } |
168 | |
169 | static void visitChildren(JSCell*, SlotVisitor&); |
170 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) |
171 | { |
172 | return Structure::create(vm, globalObject, proto, TypeInfo(FunctionExecutableType, StructureFlags), info()); |
173 | } |
174 | |
175 | void setOverrideLineNumber(int overrideLineNumber) |
176 | { |
177 | if (overrideLineNumber == -1) { |
178 | if (UNLIKELY(m_rareData)) |
179 | m_rareData->m_overrideLineNumber = WTF::nullopt; |
180 | return; |
181 | } |
182 | ensureRareData().m_overrideLineNumber = overrideLineNumber; |
183 | } |
184 | |
185 | Optional<int> overrideLineNumber() const |
186 | { |
187 | if (UNLIKELY(m_rareData)) |
188 | return m_rareData->m_overrideLineNumber; |
189 | return WTF::nullopt; |
190 | } |
191 | |
192 | int lineCount() const |
193 | { |
194 | if (UNLIKELY(m_rareData)) |
195 | return m_rareData->m_lineCount; |
196 | return m_unlinkedExecutable->lineCount(); |
197 | } |
198 | |
199 | int endColumn() const |
200 | { |
201 | if (UNLIKELY(m_rareData)) |
202 | return m_rareData->m_endColumn; |
203 | return m_unlinkedExecutable->linkedEndColumn(m_source.startColumn().oneBasedInt()); |
204 | } |
205 | |
206 | int firstLine() const |
207 | { |
208 | return source().firstLine().oneBasedInt(); |
209 | } |
210 | |
211 | int lastLine() const |
212 | { |
213 | return firstLine() + lineCount(); |
214 | } |
215 | |
216 | unsigned typeProfilingStartOffset(VM&) const |
217 | { |
218 | return typeProfilingStartOffset(); |
219 | } |
220 | |
221 | unsigned typeProfilingStartOffset() const |
222 | { |
223 | if (UNLIKELY(m_rareData)) |
224 | return m_rareData->m_typeProfilingStartOffset; |
225 | return m_unlinkedExecutable->typeProfilingStartOffset(); |
226 | } |
227 | |
228 | unsigned typeProfilingEndOffset(VM&) const |
229 | { |
230 | return typeProfilingEndOffset(); |
231 | } |
232 | |
233 | unsigned typeProfilingEndOffset() const |
234 | { |
235 | if (UNLIKELY(m_rareData)) |
236 | return m_rareData->m_typeProfilingEndOffset; |
237 | return m_unlinkedExecutable->typeProfilingEndOffset(); |
238 | } |
239 | |
240 | unsigned () const |
241 | { |
242 | if (UNLIKELY(m_rareData)) |
243 | return m_rareData->m_parametersStartOffset; |
244 | return m_unlinkedExecutable->parametersStartOffset(); |
245 | } |
246 | |
247 | void overrideInfo(const FunctionOverrideInfo&); |
248 | |
249 | DECLARE_INFO; |
250 | |
251 | InferredValue* singletonFunction() |
252 | { |
253 | if (VM::canUseJIT()) |
254 | return m_singletonFunction.get(); |
255 | return nullptr; |
256 | } |
257 | |
258 | void notifyCreation(VM& vm, JSValue value, const char* reason) |
259 | { |
260 | if (VM::canUseJIT()) { |
261 | singletonFunction()->notifyWrite(vm, value, reason); |
262 | return; |
263 | } |
264 | switch (m_singletonFunctionState) { |
265 | case ClearWatchpoint: |
266 | m_singletonFunctionState = IsWatched; |
267 | return; |
268 | case IsWatched: |
269 | m_singletonFunctionState = IsInvalidated; |
270 | return; |
271 | case IsInvalidated: |
272 | return; |
273 | } |
274 | } |
275 | |
276 | bool singletonFunctionHasBeenInvalidated() |
277 | { |
278 | if (VM::canUseJIT()) |
279 | return singletonFunction()->hasBeenInvalidated(); |
280 | return m_singletonFunctionState == IsInvalidated; |
281 | } |
282 | |
283 | // Cached poly proto structure for the result of constructing this executable. |
284 | Structure* cachedPolyProtoStructure() |
285 | { |
286 | if (UNLIKELY(m_rareData)) |
287 | return m_rareData->m_cachedPolyProtoStructure.get(); |
288 | return nullptr; |
289 | } |
290 | void setCachedPolyProtoStructure(VM& vm, Structure* structure) |
291 | { |
292 | ensureRareData().m_cachedPolyProtoStructure.set(vm, this, structure); |
293 | } |
294 | |
295 | InlineWatchpointSet& ensurePolyProtoWatchpoint() |
296 | { |
297 | if (!m_polyProtoWatchpoint) |
298 | m_polyProtoWatchpoint = Box<InlineWatchpointSet>::create(IsWatched); |
299 | return *m_polyProtoWatchpoint; |
300 | } |
301 | |
302 | Box<InlineWatchpointSet> sharedPolyProtoWatchpoint() const { return m_polyProtoWatchpoint; } |
303 | |
304 | ScriptExecutable* topLevelExecutable() const { return m_topLevelExecutable.get(); } |
305 | |
306 | TemplateObjectMap& ensureTemplateObjectMap(VM&); |
307 | |
308 | private: |
309 | friend class ExecutableBase; |
310 | FunctionExecutable(VM&, const SourceCode&, UnlinkedFunctionExecutable*, Intrinsic); |
311 | |
312 | void finishCreation(VM&, ScriptExecutable* topLevelExecutable); |
313 | |
314 | friend class ScriptExecutable; |
315 | |
316 | struct RareData { |
317 | WTF_MAKE_STRUCT_FAST_ALLOCATED; |
318 | RefPtr<TypeSet> m_returnStatementTypeSet; |
319 | unsigned m_lineCount; |
320 | unsigned m_endColumn; |
321 | Markable<int, IntegralMarkableTraits<int, -1>> m_overrideLineNumber; |
322 | unsigned { 0 }; |
323 | unsigned m_typeProfilingStartOffset { UINT_MAX }; |
324 | unsigned m_typeProfilingEndOffset { UINT_MAX }; |
325 | std::unique_ptr<TemplateObjectMap> m_templateObjectMap; |
326 | WriteBarrier<Structure> m_cachedPolyProtoStructure; |
327 | }; |
328 | |
329 | RareData& ensureRareData() |
330 | { |
331 | if (LIKELY(m_rareData)) |
332 | return *m_rareData; |
333 | return ensureRareDataSlow(); |
334 | } |
335 | RareData& ensureRareDataSlow(); |
336 | |
337 | // FIXME: We can merge rareData pointer and top-level executable pointer. First time, setting parent. |
338 | // If RareData is required, materialize RareData, swap it, and store top-level executable pointer inside RareData. |
339 | // https://bugs.webkit.org/show_bug.cgi?id=197625 |
340 | std::unique_ptr<RareData> m_rareData; |
341 | WriteBarrier<ScriptExecutable> m_topLevelExecutable; |
342 | WriteBarrier<UnlinkedFunctionExecutable> m_unlinkedExecutable; |
343 | WriteBarrier<ExecutableToCodeBlockEdge> m_codeBlockForCall; |
344 | WriteBarrier<ExecutableToCodeBlockEdge> m_codeBlockForConstruct; |
345 | union { |
346 | WriteBarrier<InferredValue> m_singletonFunction; |
347 | WatchpointState m_singletonFunctionState; |
348 | }; |
349 | Box<InlineWatchpointSet> m_polyProtoWatchpoint; |
350 | }; |
351 | |
352 | } // namespace JSC |
353 | |