]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/qt/qt_runtime.h
JavaScriptCore-466.1.6.tar.gz
[apple/javascriptcore.git] / bindings / qt / qt_runtime.h
1 /*
2 * Copyright (C) 2006 Trolltech ASA
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19
20 #ifndef BINDINGS_QT_RUNTIME_H_
21 #define BINDINGS_QT_RUNTIME_H_
22
23 #include "runtime.h"
24 #include "runtime_method.h"
25 #include "protect.h"
26
27 #include <qbytearray.h>
28 #include <qmetaobject.h>
29 #include <qpointer.h>
30
31 namespace KJS {
32 namespace Bindings {
33
34 class QtInstance;
35
36 class QtField : public Field {
37 public:
38
39 typedef enum {
40 MetaProperty,
41 DynamicProperty,
42 ChildObject
43 } QtFieldType;
44
45 QtField(const QMetaProperty &p)
46 : m_type(MetaProperty), m_property(p)
47 {}
48
49 QtField(const QByteArray &b)
50 : m_type(DynamicProperty), m_dynamicProperty(b)
51 {}
52
53 QtField(QObject *child)
54 : m_type(ChildObject), m_childObject(child)
55 {}
56
57 virtual JSValue* valueFromInstance(ExecState*, const Instance*) const;
58 virtual void setValueToInstance(ExecState*, const Instance*, JSValue*) const;
59 virtual const char* name() const;
60 QtFieldType fieldType() const {return m_type;}
61 private:
62 QtFieldType m_type;
63 QByteArray m_dynamicProperty;
64 QMetaProperty m_property;
65 QPointer<QObject> m_childObject;
66 };
67
68
69 class QtMethod : public Method
70 {
71 public:
72 QtMethod(const QMetaObject *mo, int i, const QByteArray &ident, int numParameters)
73 : m_metaObject(mo),
74 m_index(i),
75 m_identifier(ident),
76 m_nParams(numParameters)
77 { }
78
79 virtual const char* name() const { return m_identifier.constData(); }
80 virtual int numParameters() const { return m_nParams; }
81
82 private:
83 friend class QtInstance;
84 const QMetaObject *m_metaObject;
85 int m_index;
86 QByteArray m_identifier;
87 int m_nParams;
88 };
89
90
91 template <typename T> class QtArray : public Array
92 {
93 public:
94 QtArray(QList<T> list, QMetaType::Type type, PassRefPtr<RootObject>);
95 virtual ~QtArray();
96
97 RootObject* rootObject() const;
98
99 virtual void setValueAt(ExecState *exec, unsigned int index, JSValue *aValue) const;
100 virtual JSValue *valueAt(ExecState *exec, unsigned int index) const;
101 virtual unsigned int getLength() const {return m_length;}
102
103 private:
104 mutable QList<T> m_list; // setValueAt is const!
105 unsigned int m_length;
106 QMetaType::Type m_type;
107 };
108
109 // Based on RuntimeMethod
110
111 // Extra data classes (to avoid the CELL_SIZE limit on JS objects)
112
113 class QtRuntimeMethodData {
114 public:
115 virtual ~QtRuntimeMethodData();
116 RefPtr<QtInstance> m_instance;
117 };
118
119 class QtRuntimeConnectionMethod;
120 class QtRuntimeMetaMethodData : public QtRuntimeMethodData {
121 public:
122 ~QtRuntimeMetaMethodData();
123 QByteArray m_signature;
124 bool m_allowPrivate;
125 int m_index;
126 QtRuntimeConnectionMethod *m_connect;
127 QtRuntimeConnectionMethod *m_disconnect;
128 };
129
130 class QtRuntimeConnectionMethodData : public QtRuntimeMethodData {
131 public:
132 ~QtRuntimeConnectionMethodData();
133 QByteArray m_signature;
134 int m_index;
135 bool m_isConnect;
136 };
137
138 // Common base class (doesn't really do anything interesting)
139 class QtRuntimeMethod : public InternalFunctionImp
140 {
141 public:
142 virtual ~QtRuntimeMethod();
143
144 virtual CodeType codeType() const;
145 virtual Completion execute(ExecState *exec);
146
147 protected:
148 QtRuntimeMethodData *d_func() const {return d_ptr;}
149 QtRuntimeMethod(QtRuntimeMethodData *dd, ExecState *exec, const Identifier &n, PassRefPtr<QtInstance> inst);
150 QtRuntimeMethodData *d_ptr;
151 };
152
153 class QtRuntimeMetaMethod : public QtRuntimeMethod
154 {
155 public:
156 QtRuntimeMetaMethod(ExecState *exec, const Identifier &n, PassRefPtr<QtInstance> inst, int index, const QByteArray& signature, bool allowPrivate);
157
158 virtual bool getOwnPropertySlot(ExecState *, const Identifier&, PropertySlot&);
159 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
160
161 virtual void mark();
162
163 protected:
164 QtRuntimeMetaMethodData* d_func() const {return reinterpret_cast<QtRuntimeMetaMethodData*>(d_ptr);}
165
166 private:
167 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier&, const PropertySlot&);
168 static JSValue *connectGetter(ExecState *, JSObject *, const Identifier&, const PropertySlot&);
169 static JSValue *disconnectGetter(ExecState *, JSObject *, const Identifier&, const PropertySlot&);
170 };
171
172 class QtConnectionObject;
173 class QtRuntimeConnectionMethod : public QtRuntimeMethod
174 {
175 public:
176 QtRuntimeConnectionMethod(ExecState *exec, const Identifier &n, bool isConnect, PassRefPtr<QtInstance> inst, int index, const QByteArray& signature );
177
178 virtual bool getOwnPropertySlot(ExecState *, const Identifier&, PropertySlot&);
179 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
180
181 protected:
182 QtRuntimeConnectionMethodData* d_func() const {return reinterpret_cast<QtRuntimeConnectionMethodData*>(d_ptr);}
183
184 private:
185 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier&, const PropertySlot&);
186 static QMultiMap<QObject *, QtConnectionObject *> connections;
187 friend class QtConnectionObject;
188 };
189
190 class QtConnectionObject: public QObject
191 {
192 public:
193 QtConnectionObject(PassRefPtr<QtInstance> instance, int signalIndex, JSObject* thisObject, JSObject* funcObject);
194 ~QtConnectionObject();
195
196 static const QMetaObject staticMetaObject;
197 virtual const QMetaObject *metaObject() const;
198 virtual void *qt_metacast(const char *);
199 virtual int qt_metacall(QMetaObject::Call, int, void **argv);
200
201 bool match(QObject *sender, int signalIndex, JSObject* thisObject, JSObject *funcObject);
202
203 // actual slot:
204 void execute(void **argv);
205
206 private:
207 RefPtr<QtInstance> m_instance;
208 int m_signalIndex;
209 QObject* m_originalObject; // only used as a key, not dereferenced
210 ProtectedPtr<JSObject> m_thisObject;
211 ProtectedPtr<JSObject> m_funcObject;
212 };
213
214 } // namespace Bindings
215 } // namespace KJS
216
217 #endif