/
plastiqmetaobject.cpp
471 lines (365 loc) · 12.6 KB
/
plastiqmetaobject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "plastiqmetaobject.h"
#include "plastiqobject.h"
#include "plastiqmethod.h"
#include "pqengine_global.h"
#include "phpqt5.h"
#include <QHashIterator>
const char *PlastiQMetaObject::className() const
{
return d.className;
}
int PlastiQMetaObject::methodId(const QByteArray &signature, PlastiQMethod::Access filter) const
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
PQDBGLPUP(QString("PlastiQMetaObject::d.className = %1").arg(d.className));
PQDBGLPUP(QString("signature: %1").arg(signature.constData()));
#endif
const PlastiQMetaObject *pqmo = this;
int mid = -1;
int offset = 0;
while(mid < 0 && pqmo) {
PlastiQMethod method = pqmo->d.pq_methods->value(signature, PlastiQMethod());
if(filter == PlastiQMethod::None || filter == method.access) {
mid = method.index;
}
else {
mid = -1;
}
if(mid < 0) {
offset += pqmo->d.pq_methods->size();
}
pqmo = pqmo->d.superdata;
}
PQDBG_LVL_DONE();
return mid >= 0 ? mid + offset : -1;
}
int PlastiQMetaObject::signalId(const QByteArray &signature) const
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
PQDBGLPUP(QString("PlastiQMetaObject::d.className = %1").arg(d.className));
PQDBGLPUP(QString("signature: %1").arg(signature.constData()));
#endif
const PlastiQMetaObject *pqmo = this;
int sid = -1;
int offset = 0;
while(sid < 0 && pqmo) {
sid = pqmo->d.pq_signals->value(signature, PlastiQMethod()).index;
if(sid < 0) {
offset += pqmo->d.pq_signals->size();
}
pqmo = pqmo->d.superdata;
}
PQDBG_LVL_DONE();
return sid >= 0 ? sid + offset : -1;
}
int PlastiQMetaObject::constructorId(const QByteArray &signature) const
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
PQDBGLPUP(QString("PlastiQMetaObject::d.className = %1").arg(d.className));
PQDBGLPUP(QString("signature: %1").arg(signature.constData()));
#endif
const PlastiQMetaObject *pqmo = this;
int cid = -1;
int offset = 0;
// FIXME нужно ли????????
while(cid < 0 && pqmo) {
cid = pqmo->d.pq_constructors->value(signature, PlastiQMethod()).index;
if(cid < -1) {
offset += pqmo->d.pq_constructors->size();
}
pqmo = pqmo->d.superdata;
}
cid = cid >= 0 ? cid + offset : -1;
PQDBGLPUP(QString("constructorId: %1").arg(cid));
PQDBG_LVL_DONE();
return cid;
}
int PlastiQMetaObject::virtualMethodIndex(const QByteArray &methodName, quint32 argc) const
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
#endif
const PlastiQMetaObject *pqmo = this;
while (pqmo) {
PQDBGLPUP(QString("test in: %1").arg(pqmo->className()));
QHashIterator<QByteArray, PlastiQMethod> i(*pqmo->d.pq_methods);
while (i.hasNext()) {
i.next();
const PlastiQMethod &m = i.value();
if (m.name == methodName) {
const quint32 _argc = m.argTypes.isEmpty() ? 0 : quint32(m.argTypes.split(',').size());
if (_argc != argc) {
PQDBG_LVL_DONE();
return false;
}
switch (m.access) {
case PlastiQMethod::Virtual:
case PlastiQMethod::VirtualProtected:
case PlastiQMethod::VirtualPublic:
PQDBG_LVL_DONE();
return m.index;
default:
continue;
}
}
}
pqmo = pqmo->d.superdata;
}
PQDBG_LVL_DONE();
return -1;
}
QObject* PlastiQMetaObject::toQObject(PlastiQObject *object)
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
#endif
QObject *qobject = Q_NULLPTR;
if(!object || !object->plastiq_data()) {
PQDBG_LVL_DONE();
return Q_NULLPTR;
}
PQDBGLPUP(QString("objectType: %1").arg(object->plastiq_objectType()));
switch(object->plastiq_objectType()) {
case PlastiQ::IsQObject:
qobject = (QObject*) object->plastiq_data();
break;
case PlastiQ::IsQWidget:
case PlastiQ::IsQWindow: {
PMOGStack stack = new PMOGStackItem[1];
stack[0].s_voidpp = reinterpret_cast<void**>(&qobject);
object->plastiq_metaObject()->d.static_metacall(object, PlastiQMetaObject::ToQObject, 0, stack);
PQDBGLPUP(QString("casted ptr: %1").arg(reinterpret_cast<quint64>(qobject)));
delete [] stack;
stack = Q_NULLPTR;
} break;
default: qobject = Q_NULLPTR;
}
PQDBG_LVL_DONE();
return qobject;
}
//void PlastiQMetaObject::selfDtor(PlastiQObject *object)
//{
//#ifdef PQDEBUG
// PQDBG_LVL_START(__FUNCTION__);
//#endif
// if(!object || !object->data()) {
// PQDBG_LVL_DONE();
// return;
// }
// object->metaObject()->d.static_metacall(object, PlastiQMetaObject::Dtor, 0, Q_NULLPTR);
// PQDBG_LVL_DONE();
//}
bool PlastiQMetaObject::haveParent(PlastiQObject *object)
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
#endif
if(!object || !object->plastiq_data()) {
PQDBG_LVL_DONE();
return false;
}
/*
bool haveParent = false;
PlastiQ::ObjectType objectType = *(object->metaObject()->d.objectType);
switch(objectType) {
case PlastiQ::IsQObject:
case PlastiQ::IsQWidget:
case PlastiQ::IsQWindow: {
PMOGStack stack = new PMOGStackItem[1];
invokeMethod(object, "parent()", stack);
QObject *qobject = reinterpret_cast<QObject*>(stack[0].s_voidp);
if(qobject != Q_NULLPTR) {
haveParent = true;
}
delete [] stack;
} break;
case PlastiQ::IsQtObject: {
PMOGStack stack = new PMOGStackItem[1];
object->metaObject()->d.static_metacall(object, PlastiQMetaObject::HaveParent, -1, stack);
haveParent = stack->s_bool;
delete [] stack;
}
default: ;
}
*/
PMOGStack stack = new PMOGStackItem[1];
object->plastiq_metaObject()->d.static_metacall(object, PlastiQMetaObject::HaveParent, -1, stack);
bool haveParent = stack[0].s_bool;
delete [] stack;
PQDBGLPUP(QStringLiteral("%1::haveParent: %2").arg(object->plastiq_metaObject()->className()).arg(haveParent));
PQDBG_LVL_DONE();
return haveParent;
}
bool PlastiQMetaObject::invokeMethod(PlastiQObject *object, const QByteArray &signature, const PMOGStack &stack)
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
PQDBGLPUP(QString("object->metaObject()->d.className = %1").arg(object->plastiq_metaObject()->d.className));
#endif
if(!object || !object->plastiq_data()) {
PQDBG_LVL_DONE();
return false;
}
int mid = object->plastiq_metaObject()->methodId(signature);
PQDBGLPUP(QString("signature: %1, methodId: %2").arg(signature.constData()).arg(mid));
object->plastiq_metaObject()->d.static_metacall(object, PlastiQMetaObject::InvokeMethod, mid, stack);
PQDBG_LVL_DONE();
return true;
}
bool PlastiQMetaObject::connect(PQObjectWrapper *sender, const QByteArray &signalSignature, PQObjectWrapper *receiver, const QByteArray &slot)
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
#endif
int signalId = sender->object->plastiq_metaObject()->signalId(signalSignature);
if(signalId >= 0) {
PQDBGLPUP(QString("signalId: %1").arg(signalId));
PMOGStack stack = new PMOGStackItem[10];
stack[1].s_voidp = reinterpret_cast<void*>(sender);
stack[2].s_voidp = reinterpret_cast<void*>(receiver);
stack[3].s_bytearray = slot;
sender->object->plastiq_metaObject()->d.static_metacall(sender->object, PlastiQMetaObject::CreateConnection, signalId, stack);
delete [] stack;
}
#ifdef PQDEBUG
else {
PQDBGLPUP(QString("signalId: %1").arg(signalId));
}
#endif
PQDBG_LVL_DONE();
return signalId >= 0;
}
PlastiQObject *PlastiQMetaObject::newInstance(const QByteArray &signature, const PMOGStack &stack)
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
PQDBGLPUP(QString("PlastiQMetaObject::d.className = %1").arg(d.className));
#endif
int cid = constructorId(signature);
PlastiQObject *object = Q_NULLPTR;
stack[0].s_voidpp = reinterpret_cast<void**>(&object);
if(cid < 0) {
PQDBG_LVL_DONE();
return object;
}
static_metacall(PlastiQMetaObject::CreateInstance, cid, stack);
PQDBG_LVL_DONE();
return object;
}
PlastiQObject *PlastiQMetaObject::createInstanceFromData(void *data)
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
PQDBGLPUP(QString("objectId: %1").arg(reinterpret_cast<quint64>(data)));
#endif
PMOGStack stack = new PMOGStackItem[2];
PlastiQObject *object = Q_NULLPTR;
stack[0].s_voidpp = reinterpret_cast<void**>(&object);
stack[1].s_voidp = data;
static_metacall(PlastiQMetaObject::CreateDataInstance, 0, stack);
delete [] stack;
PQDBG_LVL_DONE();
return object;
}
bool PlastiQMetaObject::static_metacall(PlastiQMetaObject::Call call, int id, const PMOGStack &stack)
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
#endif
if(!d.static_metacall) {
PQDBG_LVL_DONE();
return false;
}
PQDBGLPUP(QString("d.static_metacall id %1").arg(id));
d.static_metacall(Q_NULLPTR, call, id, stack);
PQDBG_LVL_DONE();
return true;
}
const QList<PlastiQCandidateMethod> PlastiQMetaObject::candidates(const QByteArray &name, int argc, PlastiQMethod::Type type, PlastiQMethod::Access filter, bool ignoreCase) const
{
#ifdef PQDEBUG
PQDBG_LVL_START(__FUNCTION__);
PQDBGLPUP(QString("PlastiQMetaObject::d.className = %1").arg(d.className));
PQDBGLPUP(QString("find candidates for `%1`").arg(name.constData()));
#endif
QList<PlastiQCandidateMethod> candidateList;
const PlastiQMetaObject *m = this;
int offset = 0;
bool maxArgs = argc == -1;
QByteArray lowerName = name.toLower();
do {
QHashIterator<QByteArray, PlastiQMethod> *i;
const QHash<QByteArray, PlastiQMethod> *hash;
switch(type) {
case PlastiQMethod::Method:
i = new QHashIterator<QByteArray, PlastiQMethod>(*(m->d.pq_methods));
hash = m->d.pq_methods;
break;
case PlastiQMethod::Constructor:
i = new QHashIterator<QByteArray, PlastiQMethod>(*(m->d.pq_constructors));
hash = m->d.pq_constructors;
break;
case PlastiQMethod::Signal:
i = new QHashIterator<QByteArray, PlastiQMethod>(*(m->d.pq_signals));
hash = m->d.pq_signals;
break;
default:
PQDBG_LVL_DONE();
return candidateList;
}
while(i->hasNext()) {
i->next();
if(ignoreCase) {
if(i->value().name.toLower() != lowerName) continue;
}
else {
if(i->value().name != name) continue;
}
int idx = i->key().indexOf("(");
QString methodSignature = i->key().mid(idx + 1, i->key().size() - idx - 2);
QStringList argTypes = methodSignature.split(",");
int candidateArgc = methodSignature.length() ? argTypes.size() : 0;
if(type == PlastiQMethod::Signal && maxArgs) {
if(argc < candidateArgc) {
argc = candidateArgc;
candidateList.clear();
candidateList.append({ i->value().name, argc, argTypes, i->value().index + offset });
continue;
}
}
else {
if(!maxArgs && candidateArgc != argc) {
continue;
}
}
if(filter == PlastiQMethod::None || filter == i->value().access) {
if(maxArgs) {
if(argc < candidateArgc) {
PQDBGLPUP(QString("reappend candidate: %1(%2)").arg(name.constData()).arg(methodSignature));
argc = candidateArgc;
candidateList.clear();
candidateList.append({ i->value().name, argc, argTypes, i->value().index + offset });
continue;
}
}
else {
PQDBGLPUP(QString("append candidate: %1(%2)").arg(name.constData()).arg(methodSignature));
candidateList.append({ i->value().name, argc, argTypes, i->value().index + offset });
}
}
}
if(type == PlastiQMethod::Constructor) {
delete i;
break;
}
else {
offset += hash->size();
delete i;
}
} while(m = m->d.superdata);
PQDBG_LVL_DONE();
return candidateList;
}