LazyClassStructure.h
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 void setConstructor(JSObject* constructor); 53 54 VM& vm; 55 JSGlobalObject* global; 56 LazyClassStructure& classStructure; 57 const StructureInitializer& structureInit; 58 59 // It's expected that you set these using the set methods above. 60 JSObject* prototype { nullptr }; 61 Structure* structure { nullptr }; 62 JSObject* constructor { nullptr }; 63 }; 64 65 LazyClassStructure() 66 { 67 } 68 69 template<typename Callback> 70 void initLater(const Callback&); 71 72 Structure* get(const JSGlobalObject* global) const 73 { 74 ASSERT(!isCompilationThread()); 75 return m_structure.get(global); 76 } 77 78 JSObject* prototype(const JSGlobalObject* global) const 79 { 80 ASSERT(!isCompilationThread()); 81 return get(global)->storedPrototypeObject(); 82 } 83 84 // Almost as an afterthought, we also support getting the original constructor. This turns 85 // out to be important for ES6 support. 86 JSObject* constructor(const JSGlobalObject* global) const 87 { 88 ASSERT(!isCompilationThread()); 89 m_structure.get(global); 90 return m_constructor.get(); 91 } 92 93 Structure* getConcurrently() const 94 { 95 return m_structure.getConcurrently(); 96 } 97 98 JSObject* constructorConcurrently() const 99 { 100 return m_constructor.get(); 101 } 102 103 // 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. 104 Structure* getInitializedOnMainThread(const JSGlobalObject* global) const 105 { 106 return m_structure.getInitializedOnMainThread(global); 107 } 108 109 JSObject* prototypeInitializedOnMainThread(const JSGlobalObject* global) const 110 { 111 return getInitializedOnMainThread(global)->storedPrototypeObject(); 112 } 113 114 JSObject* constructorInitializedOnMainThread(const JSGlobalObject* global) const 115 { 116 m_structure.getInitializedOnMainThread(global); 117 return m_constructor.get(); 118 } 119 120 void visit(SlotVisitor&); 121 122 void dump(PrintStream&) const; 123 124 private: 125 LazyProperty<JSGlobalObject, Structure> m_structure; 126 WriteBarrier<JSObject> m_constructor; 127 }; 128 129 } // namespace JSC