]>
Commit | Line | Data |
---|---|---|
14957cd0 A |
1 | /* |
2 | * Copyright (C) 2010 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 | #ifndef Local_h | |
27 | #define Local_h | |
28 | ||
29 | #include "Handle.h" | |
30 | #include "JSGlobalData.h" | |
31 | ||
32 | /* | |
33 | A strongly referenced handle whose lifetime is temporary, limited to a given | |
34 | LocalScope. Use Locals for local values on the stack. It is an error to | |
35 | create a Local outside of any LocalScope. | |
36 | */ | |
37 | ||
38 | namespace JSC { | |
39 | ||
40 | template <typename T> class Local : public Handle<T> { | |
41 | friend class LocalScope; | |
42 | using Handle<T>::slot; | |
43 | ||
44 | public: | |
45 | typedef typename Handle<T>::ExternalType ExternalType; | |
46 | ||
47 | Local(JSGlobalData&, ExternalType = ExternalType()); | |
48 | Local(JSGlobalData&, Handle<T>); | |
49 | Local(const Local<T>&); // Adopting constructor. Used to return a Local to a calling function. | |
50 | ||
51 | Local& operator=(ExternalType); | |
52 | Local& operator=(Handle<T>); | |
53 | ||
54 | private: | |
55 | Local(HandleSlot, ExternalType); // Used by LocalScope::release() to move a Local to a containing scope. | |
56 | void set(ExternalType); | |
57 | }; | |
58 | ||
59 | template <typename T> inline Local<T>::Local(JSGlobalData& globalData, ExternalType value) | |
6fe7ccc8 | 60 | : Handle<T>(globalData.heap.handleStack()->push()) |
14957cd0 A |
61 | { |
62 | set(value); | |
63 | } | |
64 | ||
65 | template <typename T> inline Local<T>::Local(JSGlobalData& globalData, Handle<T> other) | |
6fe7ccc8 | 66 | : Handle<T>(globalData.heap.handleStack()->push()) |
14957cd0 A |
67 | { |
68 | set(other.get()); | |
69 | } | |
70 | ||
71 | template <typename T> inline Local<T>::Local(const Local<T>& other) | |
72 | : Handle<T>(other.slot()) | |
73 | { | |
74 | const_cast<Local<T>&>(other).setSlot(0); // Prevent accidental sharing. | |
75 | } | |
76 | ||
77 | template <typename T> inline Local<T>::Local(HandleSlot slot, ExternalType value) | |
78 | : Handle<T>(slot, value) | |
79 | { | |
80 | } | |
81 | ||
82 | template <typename T> inline Local<T>& Local<T>::operator=(ExternalType value) | |
83 | { | |
84 | set(value); | |
85 | return *this; | |
86 | } | |
87 | ||
88 | template <typename T> inline Local<T>& Local<T>::operator=(Handle<T> other) | |
89 | { | |
90 | set(other.get()); | |
91 | return *this; | |
92 | } | |
93 | ||
94 | template <typename T> inline void Local<T>::set(ExternalType externalType) | |
95 | { | |
96 | ASSERT(slot()); | |
14957cd0 A |
97 | *slot() = externalType; |
98 | } | |
99 | ||
100 | ||
101 | template <typename T, unsigned inlineCapacity = 0> class LocalStack { | |
102 | typedef typename Handle<T>::ExternalType ExternalType; | |
103 | public: | |
104 | LocalStack(JSGlobalData& globalData) | |
6fe7ccc8 | 105 | : m_globalData(globalData) |
14957cd0 A |
106 | , m_count(0) |
107 | { | |
108 | } | |
109 | ||
110 | ExternalType peek() const | |
111 | { | |
112 | ASSERT(m_count > 0); | |
113 | return m_stack[m_count - 1].get(); | |
114 | } | |
115 | ||
116 | ExternalType pop() | |
117 | { | |
118 | ASSERT(m_count > 0); | |
119 | return m_stack[--m_count].get(); | |
120 | } | |
121 | ||
122 | void push(ExternalType value) | |
123 | { | |
124 | if (m_count == m_stack.size()) | |
6fe7ccc8 | 125 | m_stack.append(Local<T>(m_globalData, value)); |
14957cd0 A |
126 | else |
127 | m_stack[m_count] = value; | |
128 | m_count++; | |
129 | } | |
130 | ||
131 | bool isEmpty() const { return !m_count; } | |
132 | unsigned size() const { return m_count; } | |
133 | ||
134 | private: | |
6fe7ccc8 | 135 | JSGlobalData& m_globalData; |
14957cd0 A |
136 | Vector<Local<T>, inlineCapacity> m_stack; |
137 | unsigned m_count; | |
138 | }; | |
139 | ||
140 | } | |
141 | ||
142 | namespace WTF { | |
143 | ||
144 | template<typename T> struct VectorTraits<JSC::Local<T> > : SimpleClassVectorTraits { | |
145 | static const bool needsDestruction = false; | |
146 | static const bool canInitializeWithMemset = false; | |
147 | static const bool canCompareWithMemcmp = false; | |
148 | }; | |
149 | ||
150 | } | |
151 | ||
152 | #endif |