File indexing completed on 2024-05-12 15:43:32

0001 /*
0002  *  This file is part of the KDE libraries
0003  *  Copyright (C) 2005 Apple Computer, Inc.
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  *
0020  */
0021 
0022 #ifndef KJS_PROPERTY_SLOT_H
0023 #define KJS_PROPERTY_SLOT_H
0024 
0025 #include <assert.h>
0026 #include "identifier.h"
0027 #include "value.h"
0028 
0029 namespace KJS
0030 {
0031 
0032 struct HashEntry;
0033 class ExecState;
0034 class JSObject;
0035 
0036 class KJS_EXPORT PropertySlot
0037 {
0038 public:
0039     typedef JSValue *(*GetValueFunc)(ExecState *, JSObject *originalObject, const Identifier &, const PropertySlot &);
0040     typedef JSValue *(*GetValueNumberFunc)(ExecState *, JSObject *originalObject, unsigned, const PropertySlot &);
0041 
0042     JSValue *getValue(ExecState *exec, JSObject *originalObject, const Identifier &propertyName) const
0043     {
0044         switch (m_getType) {
0045         case ValueType:
0046             return m_data.jsValue;
0047         case ValueSlotType:
0048             return *m_data.valueSlot;
0049         case StringFunction:
0050         default:
0051             return m_func.m_getValue(exec, originalObject, propertyName, *this);
0052         }
0053     }
0054 
0055     JSValue *getValue(ExecState *exec, JSObject *originalObject, unsigned propertyName) const
0056     {
0057         switch (m_getType) {
0058         case ValueType:
0059             return m_data.jsValue;
0060         case ValueSlotType:
0061             return *m_data.valueSlot;
0062         case NumberFunction:
0063             return m_func.m_getValueNumber(exec, originalObject, propertyName, *this);
0064         case StringFunction:
0065         default:
0066             return m_func.m_getValue(exec, originalObject, Identifier::from(propertyName), *this);
0067         }
0068     }
0069 
0070     void setValueSlot(JSObject *slotBase, JSValue **valueSlot)
0071     {
0072         m_slotBase = slotBase;
0073         m_data.valueSlot = valueSlot;
0074         m_getType = ValueSlotType;
0075     }
0076 
0077     void setValue(JSObject *slotBase, JSValue *value)
0078     {
0079         m_slotBase = slotBase;
0080         m_data.jsValue = value;
0081         m_getType = ValueType;
0082     }
0083 
0084     void setStaticEntry(JSObject *slotBase, const HashEntry *staticEntry, GetValueFunc getValue)
0085     {
0086         assert(getValue);
0087         m_slotBase = slotBase;
0088         m_data.staticEntry = staticEntry;
0089         m_func.m_getValue = getValue;
0090         m_getType = StringFunction;
0091     }
0092 
0093     void setCustom(JSObject *slotBase, GetValueFunc getValue)
0094     {
0095         assert(getValue);
0096         m_slotBase = slotBase;
0097         m_func.m_getValue = getValue;
0098         m_getType = StringFunction;
0099     }
0100 
0101     void setCustomIndex(JSObject *slotBase, unsigned index, GetValueFunc getValue)
0102     {
0103         assert(getValue);
0104         m_slotBase = slotBase;
0105         m_data.index = index;
0106         m_func.m_getValue = getValue;
0107         m_getType = StringFunction;
0108     }
0109 
0110     void setCustomIndex(JSObject *slotBase, unsigned index, GetValueNumberFunc getValue)
0111     {
0112         assert(getValue);
0113         m_slotBase = slotBase;
0114         m_data.index = index;
0115         m_func.m_getValueNumber = getValue;
0116         m_getType = NumberFunction;
0117     }
0118 
0119     void setCustomValue(JSObject *slotBase, void *value, GetValueFunc getValue)
0120     {
0121         assert(getValue);
0122         m_slotBase = slotBase;
0123         m_data.value = value;
0124         m_func.m_getValue = getValue;
0125         m_getType = StringFunction;
0126     }
0127 
0128     void setGetterSlot(JSObject *slotBase, JSObject *getterFunc)
0129     {
0130         m_func.m_getValue = functionGetter;
0131         m_slotBase = slotBase;
0132         m_data.getterFunc = getterFunc;
0133         m_getType = StringFunction;
0134     }
0135 
0136     void setUndefined(JSObject *slotBase)
0137     {
0138         m_slotBase = slotBase;
0139         m_func.m_getValue = undefinedGetter;
0140         m_getType = StringFunction;
0141     }
0142 
0143     JSObject *slotBase() const
0144     {
0145         return m_slotBase;
0146     }
0147 
0148     const HashEntry *staticEntry() const
0149     {
0150         return m_data.staticEntry;
0151     }
0152     unsigned index() const
0153     {
0154         return m_data.index;
0155     }
0156     void    *customValue() const
0157     {
0158         return m_data.value;
0159     }
0160 
0161 private:
0162     static JSValue *undefinedGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
0163     static JSValue *functionGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
0164 
0165     union {
0166         GetValueFunc m_getValue;
0167         GetValueNumberFunc m_getValueNumber;
0168     } m_func;
0169 
0170     JSObject *m_slotBase;
0171     union {
0172         JSObject *getterFunc;
0173         JSValue **valueSlot;
0174         const HashEntry *staticEntry;
0175         unsigned index;
0176         void    *value;
0177         JSValue *jsValue;
0178     } m_data;
0179 
0180     enum GetType {
0181         ValueType,
0182         ValueSlotType,
0183         StringFunction,
0184         NumberFunction
0185     };
0186     GetType m_getType;
0187 };
0188 
0189 }
0190 
0191 #endif // KJS_PROPERTY_SLOT_H