1/*
2 * Copyright (C) 2015-2016 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#ifndef FunctionRareData_h
27#define FunctionRareData_h
28
29#include "InternalFunctionAllocationProfile.h"
30#include "JSCell.h"
31#include "ObjectAllocationProfile.h"
32#include "Watchpoint.h"
33
34namespace JSC {
35
36class JSGlobalObject;
37class LLIntOffsetsExtractor;
38namespace DFG {
39class SpeculativeJIT;
40class JITCompiler;
41}
42
43class FunctionRareData : public JSCell {
44 friend class JIT;
45 friend class DFG::SpeculativeJIT;
46 friend class DFG::JITCompiler;
47 friend class VM;
48
49public:
50 typedef JSCell Base;
51 static const unsigned StructureFlags = StructureIsImmortal | Base::StructureFlags;
52
53 static FunctionRareData* create(VM&);
54
55 static const bool needsDestruction = true;
56 static void destroy(JSCell*);
57
58 static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype);
59
60 static void visitChildren(JSCell*, SlotVisitor&);
61
62 DECLARE_INFO;
63
64 static inline ptrdiff_t offsetOfObjectAllocationProfile()
65 {
66 return OBJECT_OFFSETOF(FunctionRareData, m_objectAllocationProfile);
67 }
68
69 ObjectAllocationProfile* objectAllocationProfile()
70 {
71 return &m_objectAllocationProfile;
72 }
73
74 Structure* objectAllocationStructure() { return m_objectAllocationProfile.structure(); }
75
76 InlineWatchpointSet& allocationProfileWatchpointSet()
77 {
78 return m_objectAllocationProfileWatchpoint;
79 }
80
81 void clear(const char* reason);
82
83 void initializeObjectAllocationProfile(VM&, JSObject* prototype, size_t inlineCapacity);
84
85 bool isObjectAllocationProfileInitialized() { return !m_objectAllocationProfile.isNull(); }
86
87 Structure* internalFunctionAllocationStructure() { return m_internalFunctionAllocationProfile.structure(); }
88 Structure* createInternalFunctionAllocationStructureFromBase(VM& vm, JSObject* prototype, Structure* baseStructure)
89 {
90 return m_internalFunctionAllocationProfile.createAllocationStructureFromBase(vm, this, prototype, baseStructure);
91 }
92
93protected:
94 FunctionRareData(VM&);
95 ~FunctionRareData();
96
97private:
98
99 friend class LLIntOffsetsExtractor;
100
101 // Ideally, there would only be one allocation profile for subclassing but due to Reflect.construct we
102 // have two. There are some pros and cons in comparison to our current system to using the same profile
103 // for both JS constructors and subclasses of builtin constructors:
104 //
105 // 1) + Uses less memory.
106 // 2) + Conceptually simplier as there is only one profile.
107 // 3) - We would need a check in all JSFunction object creations (both with classes and without) that the
108 // new.target's profiled structure has a JSFinalObject ClassInfo. This is needed, for example, if we have
109 // `Reflect.construct(Array, args, myConstructor)` since myConstructor will be the new.target of Array
110 // the Array constructor will set the allocation profile of myConstructor to hold an Array structure
111 //
112 // We don't really care about 1) since this memory is rare and small in total. 2) is unfortunate but is
113 // probably outweighed by the cost of 3).
114 ObjectAllocationProfile m_objectAllocationProfile;
115 InlineWatchpointSet m_objectAllocationProfileWatchpoint;
116 InternalFunctionAllocationProfile m_internalFunctionAllocationProfile;
117};
118
119} // namespace JSC
120
121#endif // FunctionRareData_h
122