KJS

property_map.h
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2004, 2005, 2006, 2007 Apple Computer, Inc.
4  * Copyright (C) 2007 Christopher E. Hyde <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef KJS_PROPERTY_MAP_H_
24 #define KJS_PROPERTY_MAP_H_
25 
26 #include "identifier.h"
27 #include <wtf/OwnArrayPtr.h>
28 
29 namespace KJS
30 {
31 
32 class PropertyNameArray;
33 class JSValue;
34 
35 class SavedProperty;
36 
37 struct PropertyMapHashTable;
38 
39 // ECMA 262-3 8.6.1
40 // Property attributes
41 enum Attribute { None = 0,
42  ReadOnly = 1 << 1, // property can be only read, not written
43  DontEnum = 1 << 2, // property doesn't appear in (for .. in ..)
44  DontDelete = 1 << 3, // property can't be deleted
45  Internal = 1 << 4, // an internal property, set to bypass checks
46  Function = 1 << 5, // property is a function - only used by static hashtables
47  GetterSetter = 1 << 6, // property is a getter or setter
48  DontMark = 1 << 7
49  }; // used in locals arrays only --- indicates that the slot
50 // does not contain a value, and hence should not be marked.
51 
52 /*
53 * Saved Properties
54 */
55 class SavedProperties
56 {
57  friend class PropertyMap;
58 public:
59  SavedProperties();
60  ~SavedProperties();
61 
62 private:
63  int _count;
64  OwnArrayPtr<SavedProperty> _properties;
65 };
66 
67 /*
68 * A hashtable entry for the @ref PropertyMap.
69 */
70 struct PropertyMapHashTableEntry {
71  PropertyMapHashTableEntry() : key(nullptr) { }
72  UString::Rep *key;
73  JSValue *value;
74  int attributes;
75  int index;
76 };
77 
81 class KJS_EXPORT PropertyMap
82 {
83  friend class JSObject;
84 public:
85  enum PropertyMode {
86  ExcludeDontEnumProperties,
87  IncludeDontEnumProperties
88  };
89 
90  PropertyMap();
91  ~PropertyMap();
92 
93  void clear();
94 
95  void put(const Identifier &name, JSValue *value, int attributes, bool roCheck = false);
96  void remove(const Identifier &name);
97  JSValue *get(const Identifier &name) const;
98  JSValue *get(const Identifier &name, unsigned &attributes) const;
99  JSValue **getLocation(const Identifier &name);
100 
101  // Returns a location where this property can be set, if it
102  // exists, is writeable, and not a setter.
103  // Warning: this pointer may become invalid after any further modifications
104  JSValue **getWriteLocation(const Identifier &name);
105 
106  void mark() const;
107  static inline bool checkEnumerable(unsigned int attr, PropertyMode mode)
108  {
109  return (!(attr & DontEnum) || mode == PropertyMap::IncludeDontEnumProperties);
110  }
111  void getPropertyNames(PropertyNameArray &, PropertyMode mode = ExcludeDontEnumProperties) const;
112 
113  void save(SavedProperties &) const;
114  void restore(const SavedProperties &p);
115 
116  bool isEmpty() const;
117 
118  bool hasGetterSetterProperties() const
119  {
120  return m_getterSetterFlag;
121  }
122  void setHasGetterSetterProperties(bool f)
123  {
124  m_getterSetterFlag = f;
125  }
126 
127  void setExtensible(bool extensible)
128  {
129  m_extensible = extensible;
130  }
131  bool isExtensible() const
132  {
133  return m_extensible;
134  }
135 
136  // This /computes/ whether the table has getters or setters, while the above is
137  // used to cache the result. In other words, one usually does
138  // setHasGetterSetterProperties(containsGettersOrSetters()) whenever
139  // there is a reason to believe that the result has changed
140  bool containsGettersOrSetters() const;
141 private:
142  static bool keysMatch(const UString::Rep *, const UString::Rep *);
143  void expand();
144  void rehash();
145  void rehash(int newTableSize);
146  void createTable();
147 
148  void insert(UString::Rep *, JSValue *value, int attributes, int index);
149 
150  void checkConsistency();
151 
152  typedef PropertyMapHashTableEntry Entry;
153  typedef PropertyMapHashTable Table;
154 
155  UString::Rep *m_singleEntryKey;
156  union {
157  JSValue *singleEntryValue;
158  Table *table;
159  } m_u;
160 
161  short m_singleEntryAttributes;
162 
163  bool m_getterSetterFlag : 1;
164  bool m_usingTable : 1;
165  bool m_extensible : 1;
166 
167  // We also stick some of the object's
168  // bitflags here. Kind of ugly, but saves memory...
169  bool m_objLocalInjected : 1;
170 };
171 
172 inline PropertyMap::PropertyMap() :
173  m_singleEntryKey(nullptr),
174  m_getterSetterFlag(false),
175  m_usingTable(false),
176  m_extensible(true),
177  m_objLocalInjected(false)
178 {}
179 } // namespace
180 
181 #endif // _KJS_PROPERTY_MAP_H_
JSValue is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects in...
Definition: value.h:58
Represents an Identifier for a Javascript object.
Definition: identifier.h:36
Javascript Property Map.
Definition: property_map.h:81
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jan 14 2021 23:02:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.