1 | /* |
2 | * Copyright (C) 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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "LazyProperty.h" |
29 | #include "Structure.h" |
30 | |
31 | namespace JSC { |
32 | |
33 | class JSGlobalObject; |
34 | class VM; |
35 | |
36 | class LazyClassStructure { |
37 | typedef LazyProperty<JSGlobalObject, Structure>::Initializer StructureInitializer; |
38 | |
39 | public: |
40 | struct Initializer { |
41 | Initializer(VM&, JSGlobalObject*, LazyClassStructure&, const StructureInitializer&); |
42 | |
43 | // This should be called first or not at all. |
44 | void setPrototype(JSObject* prototype); |
45 | |
46 | // If this is called after setPrototype() then it just sets the structure. If this is |
47 | // called first then it sets the prototype by extracting it from the structure. |
48 | void setStructure(Structure*); |
49 | |
50 | // Call this last. It's expected that the constructor is initialized to point to the |
51 | // prototype already. This will automatically set prototype.constructor=constructor. |
52 | // This will also stuff the constructor into the global object at the given property. |
53 | // Note that the variant that does not take a property name attempts to deduce it by |
54 | // casting constructor to either JSFunction or InternalFunction. Also, you can pass |
55 | // nullptr for the property name, in which case we don't assign the property to the |
56 | // global object. |
57 | void setConstructor(PropertyName, JSObject* constructor); |
58 | void setConstructor(JSObject* constructor); |
59 | |
60 | VM& vm; |
61 | JSGlobalObject* global; |
62 | LazyClassStructure& classStructure; |
63 | const StructureInitializer& structureInit; |
64 | |
65 | // It's expected that you set these using the set methods above. |
66 | JSObject* prototype { nullptr }; |
67 | Structure* structure { nullptr }; |
68 | JSObject* constructor { nullptr }; |
69 | }; |
70 | |
71 | LazyClassStructure() |
72 | { |
73 | } |
74 | |
75 | template<typename Callback> |
76 | void initLater(const Callback&); |
77 | |
78 | Structure* get(const JSGlobalObject* global) const |
79 | { |
80 | ASSERT(!isCompilationThread()); |
81 | return m_structure.get(global); |
82 | } |
83 | |
84 | JSObject* prototype(const JSGlobalObject* global) const |
85 | { |
86 | ASSERT(!isCompilationThread()); |
87 | return get(global)->storedPrototypeObject(); |
88 | } |
89 | |
90 | // Almost as an afterthought, we also support getting the original constructor. This turns |
91 | // out to be important for ES6 support. |
92 | JSObject* constructor(const JSGlobalObject* global) const |
93 | { |
94 | ASSERT(!isCompilationThread()); |
95 | m_structure.get(global); |
96 | return m_constructor.get(); |
97 | } |
98 | |
99 | Structure* getConcurrently() const |
100 | { |
101 | return m_structure.getConcurrently(); |
102 | } |
103 | |
104 | JSObject* constructorConcurrently() const |
105 | { |
106 | return m_constructor.get(); |
107 | } |
108 | |
109 | // Call this "InitializedOnMainThread" function if we would like to (1) get a value from a compiler thread which must be initialized on the main thread and (2) initialize a value if we are on the main thread. |
110 | Structure* getInitializedOnMainThread(const JSGlobalObject* global) const |
111 | { |
112 | return m_structure.getInitializedOnMainThread(global); |
113 | } |
114 | |
115 | JSObject* prototypeInitializedOnMainThread(const JSGlobalObject* global) const |
116 | { |
117 | return getInitializedOnMainThread(global)->storedPrototypeObject(); |
118 | } |
119 | |
120 | JSObject* constructorInitializedOnMainThread(const JSGlobalObject* global) const |
121 | { |
122 | m_structure.getInitializedOnMainThread(global); |
123 | return m_constructor.get(); |
124 | } |
125 | |
126 | void visit(SlotVisitor&); |
127 | |
128 | void dump(PrintStream&) const; |
129 | |
130 | private: |
131 | LazyProperty<JSGlobalObject, Structure> m_structure; |
132 | WriteBarrier<JSObject> m_constructor; |
133 | }; |
134 | |
135 | } // namespace JSC |
136 | |