| 1 | // -*- c-basic-offset: 4 -*- |
| 2 | /* |
| 3 | * Copyright (C) 2005, 2007 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public License |
| 16 | * along with this library; see the file COPYING.LIB. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #ifndef KJS_PROPERTY_SLOT_H |
| 23 | #define KJS_PROPERTY_SLOT_H |
| 24 | |
| 25 | #include "identifier.h" |
| 26 | #include "value.h" |
| 27 | #include <wtf/Assertions.h> |
| 28 | |
| 29 | namespace KJS { |
| 30 | |
| 31 | class ExecState; |
| 32 | class JSObject; |
| 33 | |
| 34 | struct HashEntry; |
| 35 | |
| 36 | #define KJS_VALUE_SLOT_MARKER 0 |
| 37 | #define KJS_NUMERIC_PROPERTY_NAME_SLOT_MARKER reinterpret_cast<GetValueFunc>(1) |
| 38 | |
| 39 | class PropertySlot { |
| 40 | public: |
| 41 | typedef JSValue* (*GetValueFunc)(ExecState*, JSObject* originalObject, const Identifier&, const PropertySlot&); |
| 42 | typedef JSValue* (*GetValueNumericFunc)(ExecState*, JSObject* originalObject, unsigned index, const PropertySlot&); |
| 43 | |
| 44 | JSValue* getValue(ExecState* exec, JSObject* originalObject, const Identifier& propertyName) const |
| 45 | { |
| 46 | if (m_getValue == KJS_VALUE_SLOT_MARKER) |
| 47 | return *m_data.valueSlot; |
| 48 | ASSERT(m_getValue != KJS_NUMERIC_PROPERTY_NAME_SLOT_MARKER); |
| 49 | return m_getValue(exec, originalObject, propertyName, *this); |
| 50 | } |
| 51 | |
| 52 | JSValue* getValue(ExecState* exec, JSObject* originalObject, unsigned propertyName) const |
| 53 | { |
| 54 | if (m_getValue == KJS_VALUE_SLOT_MARKER) |
| 55 | return *m_data.valueSlot; |
| 56 | if (m_getValue == KJS_NUMERIC_PROPERTY_NAME_SLOT_MARKER) |
| 57 | return m_data.numericFunc(exec, originalObject, propertyName, *this); |
| 58 | return m_getValue(exec, originalObject, Identifier::from(propertyName), *this); |
| 59 | } |
| 60 | |
| 61 | void setValueSlot(JSObject* slotBase, JSValue** valueSlot) |
| 62 | { |
| 63 | m_getValue = KJS_VALUE_SLOT_MARKER; |
| 64 | m_slotBase = slotBase; |
| 65 | m_data.valueSlot = valueSlot; |
| 66 | } |
| 67 | |
| 68 | void setStaticEntry(JSObject* slotBase, const HashEntry* staticEntry, GetValueFunc getValue) |
| 69 | { |
| 70 | ASSERT(getValue); |
| 71 | m_getValue = getValue; |
| 72 | m_slotBase = slotBase; |
| 73 | m_data.staticEntry = staticEntry; |
| 74 | } |
| 75 | |
| 76 | void setCustom(JSObject* slotBase, GetValueFunc getValue) |
| 77 | { |
| 78 | ASSERT(getValue); |
| 79 | m_getValue = getValue; |
| 80 | m_slotBase = slotBase; |
| 81 | } |
| 82 | |
| 83 | void setCustomIndex(JSObject* slotBase, unsigned index, GetValueFunc getValue) |
| 84 | { |
| 85 | ASSERT(getValue); |
| 86 | m_getValue = getValue; |
| 87 | m_slotBase = slotBase; |
| 88 | m_data.index = index; |
| 89 | } |
| 90 | |
| 91 | void setCustomNumeric(JSObject* slotBase, GetValueNumericFunc getValue) |
| 92 | { |
| 93 | ASSERT(getValue); |
| 94 | m_slotBase = slotBase; |
| 95 | m_getValue = KJS_NUMERIC_PROPERTY_NAME_SLOT_MARKER; |
| 96 | m_data.numericFunc = getValue; |
| 97 | } |
| 98 | |
| 99 | void setGetterSlot(JSObject* slotBase, JSObject* getterFunc) |
| 100 | { |
| 101 | m_getValue = functionGetter; |
| 102 | m_slotBase = slotBase; |
| 103 | m_data.getterFunc = getterFunc; |
| 104 | } |
| 105 | |
| 106 | void setUndefined(JSObject *slotBase) |
| 107 | { |
| 108 | m_slotBase = slotBase; |
| 109 | m_getValue = undefinedGetter; |
| 110 | } |
| 111 | |
| 112 | void setUngettable(JSObject* slotBase) // Used to signal that you have a property, but trying to get it at this time is an error. |
| 113 | { |
| 114 | m_slotBase = slotBase; |
| 115 | m_getValue = ungettableGetter; |
| 116 | } |
| 117 | |
| 118 | JSObject* slotBase() const { return m_slotBase; } |
| 119 | |
| 120 | const HashEntry* staticEntry() const { return m_data.staticEntry; } |
| 121 | unsigned index() const { return m_data.index; } |
| 122 | |
| 123 | private: |
| 124 | static JSValue* undefinedGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); |
| 125 | static JSValue* ungettableGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); |
| 126 | static JSValue* functionGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); |
| 127 | |
| 128 | GetValueFunc m_getValue; |
| 129 | |
| 130 | JSObject* m_slotBase; |
| 131 | union { |
| 132 | JSObject* getterFunc; |
| 133 | JSValue** valueSlot; |
| 134 | const HashEntry* staticEntry; |
| 135 | unsigned index; |
| 136 | GetValueNumericFunc numericFunc; |
| 137 | } m_data; |
| 138 | }; |
| 139 | |
| 140 | } |
| 141 | |
| 142 | #endif // KJS_PROPERTY_SLOT_H |