• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008, 2009 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef JSPropertyNameIterator_h
30 #define JSPropertyNameIterator_h
31 
32 #include "JSObject.h"
33 #include "JSString.h"
34 #include "PropertyNameArray.h"
35 
36 namespace JSC {
37 
38     class Identifier;
39     class JSObject;
40 
41     class JSPropertyNameIterator : public JSCell {
42     public:
43         static JSPropertyNameIterator* create(ExecState*, JSValue);
44 
45         virtual ~JSPropertyNameIterator();
46 
47         virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
48         virtual bool getPrimitiveNumber(ExecState*, double&, JSValue&);
49         virtual bool toBoolean(ExecState*) const;
50         virtual double toNumber(ExecState*) const;
51         virtual UString toString(ExecState*) const;
52         virtual JSObject* toObject(ExecState*) const;
53 
54         virtual void markChildren(MarkStack&);
55 
56         JSValue next(ExecState*);
57         void invalidate();
58 
createStructure(JSValue prototype)59         static PassRefPtr<Structure> createStructure(JSValue prototype)
60         {
61             return Structure::create(prototype, TypeInfo(CompoundType));
62         }
63     private:
64         JSPropertyNameIterator(ExecState*);
65         JSPropertyNameIterator(ExecState*, JSObject*, PassRefPtr<PropertyNameArrayData> propertyNameArrayData);
66 
67         JSObject* m_object;
68         RefPtr<PropertyNameArrayData> m_data;
69         PropertyNameArrayData::const_iterator m_position;
70         PropertyNameArrayData::const_iterator m_end;
71     };
72 
JSPropertyNameIterator(ExecState * exec)73 inline JSPropertyNameIterator::JSPropertyNameIterator(ExecState* exec)
74     : JSCell(exec->globalData().propertyNameIteratorStructure.get())
75     , m_object(0)
76     , m_position(0)
77     , m_end(0)
78 {
79 }
80 
JSPropertyNameIterator(ExecState * exec,JSObject * object,PassRefPtr<PropertyNameArrayData> propertyNameArrayData)81 inline JSPropertyNameIterator::JSPropertyNameIterator(ExecState* exec, JSObject* object, PassRefPtr<PropertyNameArrayData> propertyNameArrayData)
82     : JSCell(exec->globalData().propertyNameIteratorStructure.get())
83     , m_object(object)
84     , m_data(propertyNameArrayData)
85     , m_position(m_data->begin())
86     , m_end(m_data->end())
87 {
88 }
89 
create(ExecState * exec,JSValue v)90 inline JSPropertyNameIterator* JSPropertyNameIterator::create(ExecState* exec, JSValue v)
91 {
92     if (v.isUndefinedOrNull())
93         return new (exec) JSPropertyNameIterator(exec);
94 
95     JSObject* o = v.toObject(exec);
96     PropertyNameArray propertyNames(exec);
97     o->getPropertyNames(exec, propertyNames);
98     return new (exec) JSPropertyNameIterator(exec, o, propertyNames.releaseData());
99 }
100 
next(ExecState * exec)101 inline JSValue JSPropertyNameIterator::next(ExecState* exec)
102 {
103     if (m_position == m_end)
104         return JSValue();
105 
106     if (m_data->cachedStructure() == m_object->structure() && m_data->cachedPrototypeChain() == m_object->structure()->prototypeChain(exec))
107         return jsOwnedString(exec, (*m_position++).ustring());
108 
109     do {
110         if (m_object->hasProperty(exec, *m_position))
111             return jsOwnedString(exec, (*m_position++).ustring());
112         m_position++;
113     } while (m_position != m_end);
114 
115     return JSValue();
116 }
117 
118 } // namespace JSC
119 
120 #endif // JSPropertyNameIterator_h
121