2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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.
40 template <typename T
> class Local
: public Handle
<T
> {
41 friend class LocalScope
;
42 using Handle
<T
>::slot
;
45 typedef typename Handle
<T
>::ExternalType ExternalType
;
47 Local(VM
&, ExternalType
= ExternalType());
48 Local(VM
&, Handle
<T
>);
49 Local(const Local
<T
>&); // Adopting constructor. Used to return a Local to a calling function.
51 Local
& operator=(ExternalType
);
52 Local
& operator=(Handle
<T
>);
55 Local(HandleSlot
, ExternalType
); // Used by LocalScope::release() to move a Local to a containing scope.
56 void set(ExternalType
);
59 template <typename T
> inline Local
<T
>::Local(VM
& vm
, ExternalType value
)
60 : Handle
<T
>(vm
.heap
.handleStack()->push())
65 template <typename T
> inline Local
<T
>::Local(VM
& vm
, Handle
<T
> other
)
66 : Handle
<T
>(vm
.heap
.handleStack()->push())
71 template <typename T
> inline Local
<T
>::Local(const Local
<T
>& other
)
72 : Handle
<T
>(other
.slot())
74 const_cast<Local
<T
>&>(other
).setSlot(0); // Prevent accidental sharing.
77 template <typename T
> inline Local
<T
>::Local(HandleSlot slot
, ExternalType value
)
78 : Handle
<T
>(slot
, value
)
82 template <typename T
> inline Local
<T
>& Local
<T
>::operator=(ExternalType value
)
88 template <typename T
> inline Local
<T
>& Local
<T
>::operator=(Handle
<T
> other
)
94 template <typename T
> inline void Local
<T
>::set(ExternalType externalType
)
97 *slot() = externalType
;
101 template <typename T
, unsigned inlineCapacity
= 0> class LocalStack
{
102 typedef typename Handle
<T
>::ExternalType ExternalType
;
110 ExternalType
peek() const
113 return m_stack
[m_count
- 1].get();
119 return m_stack
[--m_count
].get();
122 void push(ExternalType value
)
124 if (m_count
== m_stack
.size())
125 m_stack
.append(Local
<T
>(m_vm
, value
));
127 m_stack
[m_count
] = value
;
131 bool isEmpty() const { return !m_count
; }
132 unsigned size() const { return m_count
; }
136 Vector
<Local
<T
>, inlineCapacity
> m_stack
;
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;