summaryrefslogtreecommitdiff
path: root/src/script/api/qscriptqobject.cpp
blob: 9b8bbca7814a0a724cefeacefe5ac9ea40b3a687 (plain)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtScript module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL-ONLY$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qscriptconverter_p.h"
#include "qscriptisolate_p.h"
#include "qscriptengine.h"
#include "qscriptengine_p.h"
#include "qscriptqobject_p.h"
#include "qscriptv8objectwrapper_p.h"

#include <QtCore/qmetaobject.h>
#include <QtCore/qvarlengtharray.h>

#include <QtCore/qdebug.h>
#include "qscriptvalue_p.h"
#include "qscriptcontext_p.h"
#include "qscriptable_p.h"

#include "qscript_impl_p.h"
#include <v8.h>

QT_BEGIN_NAMESPACE

static inline bool methodNameEquals(const QMetaMethod &method,
                                    const char *signature, int nameLength)
{
    const char *otherSignature = method.signature();
    return !qstrncmp(otherSignature, signature, nameLength)
        && (otherSignature[nameLength] == '(');
}

static inline int methodNameLength(const char *signature)
{
    const char *s = signature;
    while (*s && (*s != '('))
        ++s;
    return s - signature;
}

static int indexOfMetaEnum(const QMetaObject *meta, const QByteArray &str)
{
    QByteArray scope;
    QByteArray name;
    int scopeIdx = str.lastIndexOf("::");
    if (scopeIdx != -1) {
        scope = str.left(scopeIdx);
        name = str.mid(scopeIdx + 2);
    } else {
        name = str;
    }
    for (int i = meta->enumeratorCount() - 1; i >= 0; --i) {
        QMetaEnum m = meta->enumerator(i);
        if ((m.name() == name) && (scope.isEmpty() || (m.scope() == scope)))
            return i;
    }
    return -1;
}

static v8::Handle<v8::Value> throwAmbiguousError(const QMetaObject *meta, const QByteArray &functionName, const QString &errorString)
{
    int nameLength = methodNameLength(functionName);
    QString string = errorString + QLatin1String(" %1(); candidates were");
    string = string.arg(QString::fromLatin1(functionName, nameLength));

    for (int index = 0; index < meta->methodCount(); ++index) {
        QMetaMethod method = meta->method(index);
        if (!methodNameEquals(method, functionName, nameLength))
            continue;
        string += QLatin1String("\n    ");
        string += QLatin1String(method.signature());
    }
    return v8::ThrowException(v8::Exception::TypeError(QScriptConverter::toString(string)));
}

// Generic implementation of Qt meta-method invocation.
// Uses QMetaType and friends to resolve types and convert arguments.
static v8::Handle<v8::Value> callQtMetaMethod(QScriptEnginePrivate *engine, QObject *qobject,
                                              const QMetaObject *meta, int methodIndex,
                                              const v8::Arguments& args)
{
    QMetaMethod method = meta->method(methodIndex);
    QList<QByteArray> parameterTypeNames = method.parameterTypes();

    if (args.Length() < parameterTypeNames.size()) {
        return throwAmbiguousError(meta, method.signature(), QString::fromLatin1("too few arguments in call to"));
    }

    const char *returnTypeName = method.typeName();
    int rtype = QMetaType::type(returnTypeName);
    QVarLengthArray<QVariant, 10> cppArgs(1 + parameterTypeNames.size());
    if (rtype > 0) {
        cppArgs[0] = QVariant(rtype, (void *)0);
    } else if (*returnTypeName) { //empty is not void*
        return v8::ThrowException(v8::Exception::TypeError(QScriptConverter::toString(
            QString::fromLatin1("cannot call %0(): unknown return type `%1' (register the type with qScriptRegisterMetaType())")
                .arg(QString::fromLatin1(method.signature(), methodNameLength(method.signature())),
                     QLatin1String(returnTypeName)))));
    }

    // Convert arguments to C++ types.
    for (int i = 0; i < parameterTypeNames.size() && !engine->hasUncaughtException(); ++i) {
        v8::Handle<v8::Value> actual = args[i];

        int targetType = QMetaType::type(parameterTypeNames.at(i));
        if (!targetType) {

            int enumIndex = indexOfMetaEnum(meta, parameterTypeNames.at(i));
            if (enumIndex != -1) {
                const QMetaEnum m(meta->enumerator(enumIndex));
                Q_ASSERT(m.isValid());
                if (actual->IsNumber()) {
                    int ival = actual->ToInt32()->Value();
                    if (m.valueToKey(ival) != 0) {
                        cppArgs[1+i] = ival;
                        continue;
                    }
                } else if (actual->IsString()) {
                    int ival = m.keyToValue(QScriptConverter::toString(actual->ToString()).toLatin1());
                    if (ival != -1) {
                        cppArgs[1+i] = ival;
                        continue;
                    }
                }
            }

            QVariant v(QMetaType::QObjectStar, (void *)0);
            if (engine->convertToNativeQObject(actual, parameterTypeNames.at(i), reinterpret_cast<void* *>(v.data()))) {
                cppArgs[1+i] = v;
                continue;
            }

            return v8::ThrowException(v8::Exception::TypeError(QScriptConverter::toString(
                QString::fromLatin1("cannot call %0(): argument %1 has unknown type `%2' (register the type with qScriptRegisterMetaType())")
                    .arg(QString::fromLatin1(method.signature(), methodNameLength(method.signature())),
                         QString::number(i+1), QLatin1String(parameterTypeNames.at(i))))));
        }


        QVariant v(targetType, (void *)0);
        if (engine->metaTypeFromJS(actual, targetType, v.data())) {
            cppArgs[1+i] = v;
            continue;
        }

        if(engine->isQtVariant(actual)) {
            QVariant &var = engine->variantValue(actual);
            if (var.userType() == targetType) {
                cppArgs[1+i] = var;
                continue;
            } else if (var.canConvert(QVariant::Type(targetType))) {
                v = var;
                v.convert(QVariant::Type(targetType));
                cppArgs[1+i] = v;
                continue;
            }
            QByteArray typeName = var.typeName();
            if (typeName.endsWith('*')
                && (QMetaType::type(typeName.left(typeName.size()-1)) == targetType)) {
                cppArgs[1+i] = QVariant(targetType, *reinterpret_cast<void* *>(var.data()));
                continue;
            }
        }

        return throwAmbiguousError(meta, method.signature(), QString::fromLatin1("incompatible type of argument(s) in call to"));
    }

    if (engine->hasUncaughtException()) {
        return engine->uncaughtException();
    }

    // Prepare void** array for metacall.
    QVarLengthArray<void *, 10> argv(cppArgs.size());
    void **argvData = argv.data();
    for (int i = 0; i < cppArgs.size(); ++i)
        argvData[i] = const_cast<void *>(cppArgs[i].constData());
    if (rtype <= 0)
        argvData[0] = 0;

    // Call the C++ method!
    QMetaObject::metacall(qobject, QMetaObject::InvokeMetaMethod, methodIndex, argvData);

    // Convert and return result.
    if (rtype <= 0)
        return v8::Undefined();
    return engine->metaTypeToJS(rtype, argvData[0]);
}

static int conversionDistance(QScriptEnginePrivate *engine, v8::Handle<v8::Value> actual, int targetType)
{
    if (actual->IsNumber()) {
        switch (targetType) {
        case QMetaType::Double:
            // perfect
            return 0;
        case QMetaType::Float:
            return 1;
        case QMetaType::LongLong:
        case QMetaType::ULongLong:
            return 2;
        case QMetaType::Long:
        case QMetaType::ULong:
            return 3;
        case QMetaType::Int:
        case QMetaType::UInt:
            return 4;
        case QMetaType::Short:
        case QMetaType::UShort:
            return 5;
        case QMetaType::Char:
        case QMetaType::UChar:
            return 6;
        default:
            return 10;
        }
    } else if (actual->IsString()) {
        switch (targetType) {
        case QMetaType::QString:
            // perfect
            return 0;
        default:
            return 10;
        }
    } else if (actual->IsBoolean()) {
        switch (targetType) {
        case QMetaType::Bool:
            // perfect
            return 0;
        default:
            return 10;
        }
    } else if (actual->IsDate()) {
        switch (targetType) {
        case QMetaType::QDateTime:
            return 0;
        case QMetaType::QDate:
            return 1;
        case QMetaType::QTime:
            return 2;
        default:
            return 10;
        }
    }
#if 0
    else if (actual->IsRegExp()) {
        switch (targetType) {
        case QMetaType::QRegExp:
            // perfect
            return 0;
        default:
            return 10;
        }
    }
#endif
    else if (engine->isQtVariant(actual)) {
        if ((targetType == QMetaType::QVariant)
            || (engine->variantFromJS(actual).userType() == targetType)) {
            // perfect
            return 0;
        }
        return 10;
    } else if (actual->IsArray()) {
        switch (targetType) {
        case QMetaType::QStringList:
        case QMetaType::QVariantList:
            return 5;
        default:
            return 10;
        }
    } else if (engine->isQtObject(actual)) {
        switch (targetType) {
        case QMetaType::QObjectStar:
        case QMetaType::QWidgetStar:
            // perfect
            return 0;
        default:
            return 10;
        }
    } else if (actual->IsNull()) {
        switch (targetType) {
        case QMetaType::VoidStar:
        case QMetaType::QObjectStar:
        case QMetaType::QWidgetStar:
            // perfect
            return 0;
        default:
            return 10;
        }
    }
    return 100;
}

static int resolveOverloadedQtMetaMethodCall(QScriptEnginePrivate *engine, const QMetaObject *meta, int initialIndex, const v8::Arguments& args)
{
    int bestIndex = -1;
    int bestDistance = INT_MAX;
    int nameLength = 0;
    const char *initialMethodSignature = 0;
    for (int index = initialIndex; index >= 0; --index) {
        QMetaMethod method = meta->method(index);
        if (index == initialIndex) {
            initialMethodSignature = method.signature();
            nameLength = methodNameLength(initialMethodSignature);
        } else {
            if (!methodNameEquals(method, initialMethodSignature, nameLength))
                continue;
        }

        QList<QByteArray> parameterTypeNames = method.parameterTypes();
        int parameterCount = parameterTypeNames.size();
        if (args.Length() != parameterCount)
            continue;

        int distance = 0;
        for (int i = 0; (distance < bestDistance) && (i < parameterCount); ++i) {
            int targetType = QMetaType::type(parameterTypeNames.at(i));
            Q_ASSERT(targetType != 0);
            distance += conversionDistance(engine, args[i], targetType);
        }

        if (distance == 0) {
            // Perfect match, no need to look further.
            return index;
        }

        if (distance < bestDistance) {
            bestIndex = index;
            bestDistance = distance;
        } else if (distance == bestDistance && bestIndex >= 0) {
            // it is possible that a virtual method is redeclared in a subclass,
            // in which case we want to ignore the superclass declaration
            if (qstrcmp(method.signature(), meta->method(bestIndex).signature())) {
                // different signature with the same distance, this is ambiguous;
                bestIndex = -1;
            }
        }
    }
    return bestIndex;
}


// Helper class to invoke QObject::{,dis}connectNotify() (they are protected).
class QtObjectNotifyCaller : public QObject
{
public:
    void callConnectNotify(const char *signal)
        { connectNotify(signal); }
    void callDisconnectNotify(const char *signal)
        { disconnectNotify(signal); }

    static void callConnectNotify(QObject *sender, int signalIndex)
    {
        QMetaMethod signal = sender->metaObject()->method(signalIndex);
        QByteArray signalString;
        signalString.append('2'); // signal code
        signalString.append(signal.signature());
        static_cast<QtObjectNotifyCaller*>(sender)->callConnectNotify(signalString);
    }

    static void callDisconnectNotify(QObject *sender, int signalIndex)
    {
        QMetaMethod signal = sender->metaObject()->method(signalIndex);
        QByteArray signalString;
        signalString.append('2'); // signal code
        signalString.append(signal.signature());
        static_cast<QtObjectNotifyCaller*>(sender)->callDisconnectNotify(signalString);
    }
};

// A C++ signal-to-JS handler connection.
//
// Acts as a middle-man; intercepts a C++ signal,
// and invokes a JS callback function.
//
class QScriptConnection : public QObject
{
public:
    QScriptConnection(QScriptSignalData *signal);
    ~QScriptConnection();

    bool connect(v8::Handle<v8::Object> receiver, v8::Handle<v8::Object> callback, Qt::ConnectionType type);
    bool disconnect();

    QScriptEnginePrivate *engine() const
    {
        return m_signal->engine;
    }
    v8::Handle<v8::Object> callback() const
    { return m_callback; }

    // This class implements qt_metacall() and friends manually; moc should
    // not process it. Qt Meta System doesn't allow to connect ta a slot with undefined arguments
    // as the type safety check should be always performed. This is a hack that allow to skip the check.
    // The Q_OBJECT macro must be manually expanded:
    static const QMetaObject staticMetaObject;
    Q_OBJECT_GETSTATICMETAOBJECT
    virtual const QMetaObject *metaObject() const;
    virtual void *qt_metacast(const char *);
    virtual int qt_metacall(QMetaObject::Call, int, void **);

    // Slot.
    void onSignal(void **);
    void deleteNow()
    {
        // This will be called if underlaying qobject instance is destroyed so there is no guarantee
        // that an isolate was setup correctly (delete can be called outside of public QtScript API).
        QScriptEnginePrivate *engine = this->engine();
        QScriptIsolate api(engine, QScriptIsolate::NotNullEngine);
        Q_ASSERT_X(engine == QScriptQObjectData::get(m_signal->object())->engine(),
                   Q_FUNC_INFO,
                   "Mismatch of QScriptEngines detected");
        delete this;
    }

private:
    QScriptSignalData *m_signal;
    v8::Persistent<v8::Object> m_callback;
    v8::Persistent<v8::Object> m_receiver;
};

QScriptSignalData::~QScriptSignalData()
{
    foreach (QScriptConnection *connection, m_connections) {
        delete connection;
    }
}

v8::Handle<v8::FunctionTemplate> QScriptSignalData::createFunctionTemplate(QScriptEnginePrivate* engine)
{
    v8::HandleScope handleScope;
    v8::Handle<v8::FunctionTemplate> funcTempl = v8::FunctionTemplate::New();
    funcTempl->SetClassName(v8::String::New("QtSignal"));

    v8::Handle<v8::ObjectTemplate> instTempl = funcTempl->InstanceTemplate();
    instTempl->SetCallAsFunctionHandler(QScriptV8ObjectWrapperHelper::callAsFunction<QScriptSignalData>);
    instTempl->SetInternalFieldCount(2); // QScriptSignalData* , and the 'self'

    v8::Handle<v8::ObjectTemplate> protoTempl = funcTempl->PrototypeTemplate();
    v8::Handle<v8::Signature> sig = v8::Signature::New(funcTempl);
    protoTempl->Set(v8::String::New("connect"), v8::FunctionTemplate::New(QtConnectCallback, v8::Handle<v8::Value>(), sig));
    protoTempl->Set(v8::String::New("disconnect"), v8::FunctionTemplate::New(QtDisconnectCallback, v8::Handle<v8::Value>(), sig));

    return handleScope.Close(funcTempl);
}

v8::Handle<v8::FunctionTemplate> QScriptMetaMethodData::createFunctionTemplate(QScriptEnginePrivate* engine)
{
    v8::HandleScope handleScope;
    v8::Handle<v8::FunctionTemplate> funcTempl = v8::FunctionTemplate::New();
    funcTempl->SetClassName(v8::String::New("QtMetaMethod"));

    v8::Handle<v8::ObjectTemplate> instTempl = funcTempl->InstanceTemplate();
    instTempl->SetCallAsFunctionHandler(QScriptV8ObjectWrapperHelper::callAsFunction<QScriptMetaMethodData>);
    instTempl->SetInternalFieldCount(2); // QScriptMetaMethodData* and the 'self'

    return handleScope.Close(funcTempl);
}


// Connects this signal to the given callback.
// receiver might be empty
// Returns undefined if the connection succeeded, otherwise throws an error.
v8::Handle<v8::Value> QScriptSignalData::connect(v8::Handle<v8::Object> receiver,
                                            v8::Handle<v8::Object> slot, Qt::ConnectionType type)
{
     if (QScriptMetaMethodData *cppSlot = QScriptMetaMethodData::safeGet(slot, engine)) {
         if (receiver.IsEmpty())
             receiver = cppSlot->object();
         QObject *recv = QScriptQObjectData::get(receiver)->cppObject(QScriptQObjectData::IgnoreException);
         if (recv) {
             QObject *sender = QScriptQObjectData::get(object())->cppObject();
             if (QMetaObject::connect(sender, index(), recv, cppSlot->index(), type))
                 return v8::Undefined();
             else
                 return v8::ThrowException(v8::Exception::Error(v8::String::New("QtSignal.connect(): failed to connect")));
         }
     }
     QScriptConnection *connection = new QScriptConnection(this);
     if (!connection->connect(receiver, slot, type)) {
         delete connection;
         return v8::ThrowException(v8::Exception::Error(v8::String::New("QtSignal.connect(): failed to connect")));
     }
     m_connections.append(connection);
     return v8::Undefined();
}

// Disconnect this signal from the given callback.
// Returns undefined if the disconnection succeeded, otherwise throws an error.
v8::Handle<v8::Value> QScriptSignalData::disconnect(v8::Handle<v8::Function> callback)
{
    for (int i = 0; i < m_connections.size(); ++i) {
        QScriptConnection *connection = m_connections.at(i);
        if (connection->callback()->StrictEquals(callback)) {
            if (!connection->disconnect())
                return v8::ThrowException(v8::Exception::Error(v8::String::New("QtSignal.disconnect(): failed to disconnect")));
            m_connections.removeAt(i);
            delete connection;
            return v8::Undefined();
        }
    }
    return v8::ThrowException(v8::Exception::Error(v8::String::New("QtSignal.disconnect(): function not connected to this signal")));
}

//Call the method
template <typename T,  v8::Persistent<v8::FunctionTemplate> QScriptEnginePrivate::*functionTemplate>
v8::Handle<v8::Value> QScriptGenericMetaMethodData<T, functionTemplate>::call()
{
    QScriptQObjectData *instance = QScriptQObjectData::get(object());
    v8::Local<v8::Value> error;
    QObject *qobject = instance->cppObject(&error);
    if (!qobject)
        return error;

    const QMetaObject *meta = qobject->metaObject();
    const v8::Arguments *args = this->engine->currentContext()->arguments;

    int methodIndex = m_info.index;
    if (m_info.overloaded)
        methodIndex = resolveOverloadedQtMetaMethodCall(this->engine, meta, methodIndex, *args);

    if (methodIndex < 0) { // Ambiguous call.
        return throwAmbiguousError(meta, meta->method(m_info.index).signature(), QString::fromLatin1("ambiguous call of overloaded function"));
    }

    QScriptEnginePrivate *oldEngine = 0;
    QScriptable *scriptable = instance->toQScriptable();
    if (scriptable)
        oldEngine = QScriptablePrivate::get(scriptable)->swapEngine(instance->engine());

    v8::Handle<v8::Value> result;
    if (m_info.voidvoid) {
        QMetaObject::metacall(qobject, QMetaObject::InvokeMetaMethod, methodIndex, 0);
    } else {
        result = callQtMetaMethod(this->engine, qobject, meta, methodIndex, *args);
    }

    if (scriptable)
        QScriptablePrivate::get(scriptable)->swapEngine(oldEngine);

    return result;
}

QScriptConnection::QScriptConnection(QScriptSignalData *signal)
    : m_signal(signal)
{
    Q_ASSERT(m_signal);
    Q_ASSERT(m_signal->engine);
}

QScriptConnection::~QScriptConnection()
{
    m_signal->unregisterQScriptConnection(this);
    m_callback.Dispose();
    m_receiver.Dispose();
}


// Connects to this connection's signal, and binds this connection to the
// given callback.
// Returns true if the connection succeeded, otherwise returns false.
bool QScriptConnection::connect(v8::Handle<v8::Object> receiver, v8::Handle<v8::Object> callback, Qt::ConnectionType type)
{
    Q_ASSERT(m_callback.IsEmpty());
    QScriptQObjectData *instance = QScriptQObjectData::get(m_signal->object());
    QObject *sender = instance->cppObject();
    bool ok;
    if (sender) {
        if (QMetaObject::connect(sender, m_signal->index(), this, staticMetaObject.methodOffset(), type)) {
            if (!receiver.IsEmpty() && m_signal->engine->isQtObject(receiver)) {
                instance = QScriptQObjectData::get(receiver);
                QObject *recv = instance->cppObject(QScriptQObjectData::IgnoreException);
                if (recv) {
                    // FIXME: we are connecting to qobjects, can we try to connect them directly?
                    QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(deleteNow()));
                }
            }

            QtObjectNotifyCaller::callConnectNotify(sender, m_signal->index());
            m_callback = v8::Persistent<v8::Object>::New(callback);
            m_receiver = v8::Persistent<v8::Object>::New(receiver);
            ok = true;
        } else
            ok = false;
    } else
        ok = false;
    return ok;
}

// Disconnects from this connection's signal, and unbinds the callback.
bool QScriptConnection::disconnect()
{
    Q_ASSERT(!m_callback.IsEmpty());
    QScriptQObjectData *instance = QScriptQObjectData::get(m_signal->object());
    QObject *sender = instance->cppObject();
    bool ok = sender && QMetaObject::disconnect(sender, m_signal->index(),
                                      this, staticMetaObject.methodOffset());
    if (ok) {
        QtObjectNotifyCaller::callDisconnectNotify(sender, m_signal->index());
        m_callback.Dispose();
        m_callback.Clear();
    }
    return ok;
}

// This slot is called when the C++ signal is emitted.
// It forwards the call to the JS callback.
void QScriptConnection::onSignal(void **argv)
{
    Q_ASSERT(!m_callback.IsEmpty());

    QScriptEnginePrivate *engine = this->engine();
    QScriptIsolate api(engine, QScriptIsolate::NotNullEngine);
    Q_ASSERT_X(engine == QScriptQObjectData::get(m_signal->object())->engine(),
               Q_FUNC_INFO,
               "Mismatch of QScriptEngines detected");
    v8::HandleScope handleScope;

    const QMetaObject *meta = sender()->metaObject();
    QMetaMethod method = meta->method(m_signal->index());


    QList<QByteArray> parameterTypes = method.parameterTypes();
    int argc = parameterTypes.count();

    // Convert arguments to JS.
    QVarLengthArray<v8::Handle<v8::Value>, 8> jsArgv(argc);
    for (int i = 0; i < argc; ++i) {
        v8::Handle<v8::Value> convertedArg;
        void *arg = argv[i + 1];
        QByteArray typeName = parameterTypes.at(i);
        int type = QMetaType::type(typeName);
        if (!type) {
            qWarning("Unable to handle unregistered datatype '%s' "
                     "when invoking signal handler for %s::%s",
                     typeName.constData(), meta->className(), method.signature());
            convertedArg = v8::Undefined();
        } else {
            convertedArg = engine->metaTypeToJS(type, arg);
        }
        jsArgv[i] = convertedArg;
    }

    v8::TryCatch tryCatch;
    v8::Handle<v8::Object> receiver = m_receiver;
    if (receiver.IsEmpty())
        receiver = v8::Context::GetCurrent()->Global();

    /*QScriptQObjectData *instance = QtInstanceData::safeGet(receiver);
    if (instance) {
        QObject *obj = instance->cppObject(QScriptQObjectData::IgnoreException);
        if (!obj)
            return;
    }*/
    m_callback->Call(receiver, argc, const_cast<v8::Handle<v8::Value>*>(jsArgv.constData()));

    if (tryCatch.HasCaught()) {
        v8::Local<v8::Value> result = tryCatch.Exception();
        engine->setException(result, tryCatch.Message());
        engine->emitSignalHandlerException();
    }
}

// moc-generated code.
// DO NOT EDIT!

static const uint qt_meta_data_QScriptConnection[] = {

 // content:
       5,       // revision
       0,       // classname
       0,    0, // classinfo
       2,   14, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       0,       // signalCount

 // slots: signature, parameters, type, tag, flags
      19,   18,   18,   18, 0x0a,
      30,   18,   18,   18, 0x0a,

       0        // eod
};

static const char qt_meta_stringdata_QScriptConnection[] = {
    "QScriptConnection\0\0onSignal()\0deleteNow()\0"
};

const QMetaObject QScriptConnection::staticMetaObject = {
    { &QObject::staticMetaObject, qt_meta_stringdata_QScriptConnection,
      qt_meta_data_QScriptConnection, 0 }
};

#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &QScriptConnection::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION

const QMetaObject *QScriptConnection::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}

void *QScriptConnection::qt_metacast(const char *_clname)
{
    if (!_clname) return 0;
    if (!strcmp(_clname, qt_meta_stringdata_QScriptConnection))
        return static_cast<void*>(const_cast< QScriptConnection*>(this));
    return QObject::qt_metacast(_clname);
}

int QScriptConnection::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: onSignal(_a); break;
        case 1: deleteNow(); break;
        default: ;
        }
        _id -= 2;
    }
    return _id;
}

#if 0
struct StringType
{
    typedef QString Type;

    static v8::Handle<v8::Value> toJS(const QString &s)
    { return qtStringToJS(s); }

    static QString fromJS(v8::Handle<v8::Value> v)
    { return qtStringFromJS(v->ToString()); }
};

struct DoubleType
{
    typedef double Type;

    static v8::Handle<v8::Value> toJS(double d)
    { return v8::Number::New(d); }

    static double fromJS(v8::Handle<v8::Value> v)
    { return v->ToNumber()->Value(); }
};

struct BoolType
{
    typedef bool Type;

    static v8::Handle<v8::Value> toJS(bool b)
    { return v8::Boolean::New(b); }

    static bool fromJS(v8::Handle<v8::Value> v)
    { return v->ToBoolean()->Value(); }
};

template <class Type>
static v8::Handle<v8::Value> QtMetaPropertyFastGetter(v8::Local<v8::String> /*property*/,
                                                      const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.Holder();
    QScriptQObjectData *data = QtInstanceData::get(self);

    QObject *qobject = data->cppObject();

    int propertyIndex = v8::Int32::Cast(*info.Data())->Value();

    typename Type::Type v;
    void *argv[] = { &v };

    QMetaObject::metacall(qobject, QMetaObject::ReadProperty, propertyIndex, argv);

    return Type::toJS(v);
}

template <class Type>
static void QtMetaPropertyFastSetter(v8::Local<v8::String> /*property*/,
                                     v8::Local<v8::Value> value,
                                     const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.Holder();
    QScriptQObjectData *data = QtInstanceData::get(self);

    QObject *qobject = data->cppObject();

    int propertyIndex = v8::Int32::Cast(*info.Data())->Value();

    typename Type::Type v = Type::fromJS(value);
    void *argv[] = { &v };

    QMetaObject::metacall(qobject, QMetaObject::WriteProperty, propertyIndex, argv);
}
#endif


// This callback implements meta-object-defined property reads for objects
// that don't inherit QScriptable.
// - info.Holder() is a QObject wrapper
// - info.Data() is the meta-property index
static v8::Handle<v8::Value> QtMetaPropertyGetter(v8::Local<v8::String> property,
                                                  const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.Holder();
    QScriptQObjectData *data = QScriptQObjectData::get(self);
    QScriptEnginePrivate *engine = data->engine();
    QScriptContextPrivate context(engine, &info);

    v8::Local<v8::Value> error;
    QObject *qobject = data->cppObject(&error);
    if (!qobject)
        return error;

    const QMetaObject *meta = qobject->metaObject();

    int propertyIndex = v8::Int32::Cast(*info.Data())->Value();

    QMetaProperty prop = meta->property(propertyIndex);
    if (!prop.isReadable())
        return v8::Undefined();

    QScriptEnginePrivate *oldEngine = 0;
    QScriptable *scriptable = data->toQScriptable();
    if (scriptable)
        oldEngine = QScriptablePrivate::get(scriptable)->swapEngine(data->engine());

    QVariant value = prop.read(qobject);

    if (scriptable)
        QScriptablePrivate::get(scriptable)->swapEngine(oldEngine);

    return engine->variantToJS(value);
}

// This callback implements meta-object-defined property writes for objects
// that don't inherit QScriptable.
// - info.Holder() is a QObject wrapper
// - info.Data() is the meta-property index
static void QtMetaPropertySetter(v8::Local<v8::String> /*property*/,
                                 v8::Local<v8::Value> value,
                                 const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.Holder(); // This?
    QScriptQObjectData *data = QScriptQObjectData::get(self);
    QScriptEnginePrivate *engine = data->engine();
    QScriptContextPrivate context(engine, &info);

    QObject *qobject = data->cppObject();
    if (!qobject)
        return;

    const QMetaObject *meta = qobject->metaObject();

    int propertyIndex = v8::Int32::Cast(*info.Data())->Value();

    QMetaProperty prop = meta->property(propertyIndex);
    Q_ASSERT(prop.isWritable());

    QVariant cppValue;

    if (prop.isEnumType() && value->IsString()
        && !engine->hasDemarshalFunction(prop.userType())) {
        // Give QMetaProperty::write() a chance to convert from
        // string to enum value.
        cppValue = QScriptConverter::toString(value->ToString());
    } else {
        int type = prop.userType();
        if (type == -1) { // -1 is a QVariant.
            cppValue = engine->variantFromJS(value);
        } else {
            cppValue = QVariant(type, (void *)0);
            if (!engine->metaTypeFromJS(value, type, cppValue.data())) {
                cppValue = engine->variantFromJS(value);
                if (cppValue.userType() != type) {
                    QByteArray typeName = cppValue.typeName();
                    if (typeName.endsWith('*') && (QMetaType::type(typeName.left(typeName.size()-1)) == type)) {
                        cppValue = QVariant(type, *reinterpret_cast<void* *>(cppValue.data()));
                    }
                }
            }
        }
    }

    QScriptEnginePrivate *oldEngine = 0;
    QScriptable *scriptable = data->toQScriptable();
    if (scriptable)
        oldEngine = QScriptablePrivate::get(scriptable)->swapEngine(data->engine());

    prop.write(qobject, cppValue);

    if (scriptable)
        QScriptablePrivate::get(scriptable)->swapEngine(oldEngine);
}

// This callback implements reading a presumably existing dynamic property.
// Unlike meta-object-defined properties, dynamic properties are specific to
// an instance (not tied to a class). Dynamic properties can be added,
// changed and removed at any time. If the dynamic property with the given
// name no longer exists, this accessor will be uninstalled.
v8::Handle<v8::Value> QtDynamicPropertyGetter(v8::Local<v8::String> property,
                                                     const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.Holder(); // This?
    QScriptQObjectData *data = QScriptQObjectData::get(self);
    QScriptEnginePrivate *engine = data->engine();
    QScriptContextPrivate context(engine, &info);

    v8::Local<v8::Value> error;
    QObject *qobject = data->cppObject(&error);
    if (!qobject)
        return error;

    QByteArray name = QScriptConverter::toString(property).toLatin1();
    QVariant value = qobject->property(name);
    if (!value.isValid()) {
        // The property no longer exists. Remove this accessor.
        self->ForceDelete(property);

        // Fallback to V8 to get our property again. Unless other
        // property is found on the prototype chain, we'll end up
        // in QObject interceptor again.
        return self->Get(property);
    }
    return engine->variantToJS(value);
}

// This callback implements writing a presumably existing dynamic property.
// If the dynamic property with the given name no longer exists, this accessor
// will be uninstalled.
void QtDynamicPropertySetter(v8::Local<v8::String> property,
                                    v8::Local<v8::Value> value,
                                    const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.Holder(); // This?
    QScriptQObjectData *data = QScriptQObjectData::get(self);
    QScriptEnginePrivate *engine = data->engine();
    QScriptContextPrivate context(engine, &info);

    QObject *qobject = data->cppObject();
    if (!qobject)
        return;

    QByteArray name = QScriptConverter::toString(property).toLatin1();
    if (qobject->dynamicPropertyNames().indexOf(name) == -1) {
        // The property no longer exists. Remove this accessor.
        self->ForceDelete(property);
        // Call normal logic for property writes.
        Q_UNIMPLEMENTED();
    }

    QVariant cppValue = engine->variantFromJS(value);
    qobject->setProperty(name, cppValue);
}

// This callback implements a fallback property getter for Qt wrapper objects.
// This is only called if the property is not a QMetaObject-defined property,
// Q_INVOKABLE method or slot. It handles signals (which are methods that must
// be bound to an object), and dynamic properties and child objects (since they
// are instance-specific, not defined by the QMetaObject).
v8::Handle<v8::Value> QtLazyPropertyGetter(v8::Local<v8::String> property,
                                                  const v8::AccessorInfo& info)
{
    QScriptEnginePrivate *engine = reinterpret_cast<QScriptEnginePrivate *>(v8::External::Unwrap(info.Data()));
    Q_ASSERT(engine);
    v8::Local<v8::Object> self = info.This();
    if (!engine->qobjectTemplate()->HasInstance(self))
        return v8::Handle<v8::Value>(); //the QObject prototype is being used on another object.
    QScriptQObjectData *data = QScriptQObjectData::get(self);
    Q_ASSERT(engine == data->engine());
    QScriptContextPrivate context(engine, &info);

    v8::Local<v8::Value> error;
    QObject *qobject = data->cppObject(&error);
    if (!qobject)
        return error;

    QByteArray name = QScriptConverter::toString(property).toLatin1();

    // Look up dynamic property.
    {
        int index = qobject->dynamicPropertyNames().indexOf(name);
        if (index != -1) {
            QVariant value = qobject->property(name);
            // Install accessor for this dynamic property.
            // Dynamic properties can be removed from the C++ object without
            // us knowing it (well, we could install an event filter, but
            // that seems overkill). The getter makes sure that the property
            // is still there the next time a read is attempted, and
            // uninstalls itself if not, so that we fall back to this
            // interceptor again.
            self->SetAccessor(property,
                              QtDynamicPropertyGetter,
                              QtDynamicPropertySetter);

            return engine->variantToJS(value);
        }
    }

    // Look up child.
    if (!(data->options() & QScriptEngine::ExcludeChildObjects)) {
        QList<QObject*> children = qobject->children();
        for (int index = 0; index < children.count(); ++index) {
            QObject *child = children.at(index);
            QString childName = child->objectName();
            if (childName == QString::fromLatin1(name)) {
                v8::Handle<v8::Value> result = engine->newQObject(child);
                //TODO: We could set the property, but then we should also make sure it goes away when the object is destroyed.
                //  and we should not hide the dynamic properties
                //self->Set(property, result, v8::PropertyAttribute(v8::DontEnum | v8::DontDelete));
                return result;
            }
        }
    }

    return v8::Handle<v8::Value>();
}

v8::Handle<v8::Value> QtLazyPropertySetter(v8::Local<v8::String> property,
                                                  v8::Local<v8::Value> value,
                                                  const v8::AccessorInfo& info)
{
    QScriptEnginePrivate *engine = reinterpret_cast<QScriptEnginePrivate *>(v8::External::Unwrap(info.Data()));
    Q_ASSERT(engine);
    v8::Local<v8::Object> self = info.This();
    if (!engine->qobjectTemplate()->HasInstance(self))
        return v8::Handle<v8::Value>(); //the QObject prototype is being used on another object.
    QScriptQObjectData *data = QScriptQObjectData::get(self);
    if (!(data->options() & QScriptEngine::AutoCreateDynamicProperties))
        return v8::Handle<v8::Value>();
    Q_ASSERT(engine == data->engine());

    v8::Local<v8::Value> error;
    QObject *qobject = data->cppObject(&error);
    if (!qobject)
        return error;

    QByteArray name = QScriptConverter::toString(property).toLatin1();
    qobject->setProperty(name, engine->variantFromJS(value));

    self->SetAccessor(property, QtDynamicPropertyGetter, QtDynamicPropertySetter);
    
    return value;
}

// This callback implements a catch-all property getter for QMetaObject wrapper objects.
v8::Handle<v8::Value> QtMetaObjectPropertyGetter(v8::Local<v8::String> property,
                                                 const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.Holder();
    QtMetaObjectData *data = QtMetaObjectData::get(self);
    QScriptEnginePrivate *engine = data->engine();
    QScriptContextPrivate context(engine, &info);

    const QMetaObject *meta = data->metaObject();
    QString propertyName = QScriptConverter::toString(property);

    if (propertyName == QLatin1String("prototype")) {
        v8::Handle<v8::Value> ctor = data->constructor();
        if (ctor->IsObject())
            return ctor->ToObject()->Get(property);
    }

    QByteArray name = propertyName.toLatin1();

    for (int i = 0; i < meta->enumeratorCount(); ++i) {
        QMetaEnum e = meta->enumerator(i);
        for (int j = 0; j < e.keyCount(); ++j) {
            const char *key = e.key(j);
            if (!qstrcmp(key, name.constData()))
                return v8::Int32::New(e.value(j));
        }
    }

    return v8::Handle<v8::Value>();
}

v8::Handle<v8::Array> QtMetaObjectEnumerator(const v8::AccessorInfo& info)
{
    v8::HandleScope handleScope;

    v8::Local<v8::Object> self = info.Holder();
    QtMetaObjectData *data = QtMetaObjectData::get(self);
    QScriptEnginePrivate *engine = data->engine();
    QScriptContextPrivate context(engine, &info);

    const QMetaObject *meta = data->metaObject();
    v8::Handle<v8::Array> names = v8::Array::New(0);
    int index = 0;
    for (int i = 0; i < meta->enumeratorCount(); ++i) {
        QMetaEnum e = meta->enumerator(i);
        for (int j = 0; j < e.keyCount(); ++j)
            names->Set(index++, v8::String::New(e.key(j)));
    }

    return handleScope.Close(names);
}

// This callback is called when the QMetaObject is invoked
v8::Handle<v8::Value> QtMetaObjectCallback(const v8::Arguments& args)
{
    v8::Local<v8::Object> self = args.Holder();
    QtMetaObjectData *data = QtMetaObjectData::get(self);
    QScriptEnginePrivate *engine = data->engine();
    QScriptContextPrivate context(engine, &args);

//    const QMetaObject *meta = data->metaObject();
//    qDebug() << Q_FUNC_INFO << meta->className();
//     if (!args.IsConstructCall())
//         return v8::Handle<v8::Value>();
//

    v8::Handle<v8::Value> ctor = data->constructor();
    if (ctor->IsFunction()) {
        QVarLengthArray<v8::Handle<v8::Value>, 8> newArgs;
        newArgs.reserve(args.Length());
        for (int i = 0; i < args.Length(); i++) {
            newArgs.append(args[i]);
        }
        return v8::Function::Cast(*ctor)->NewInstance(newArgs.count(), newArgs.data());
    }
    return v8::Handle<v8::Value>();
}

// This callback implements the connect() method of signal wrapper objects.
// The this-object is a QtSignal wrapper.
// If the connect succeeds, this function returns undefined; otherwise,
// an error is thrown.
// If the connect succeeds, the associated JS function will be invoked
// when the C++ object emits the signal.
v8::Handle<v8::Value> QScriptSignalData::QtConnectCallback(const v8::Arguments& args)
{
    v8::HandleScope handleScope;
    v8::Local<v8::Object> self = args.Holder();
    QScriptSignalData *data = QScriptSignalData::get(self);

    if (args.Length() == 0)
        return v8::ThrowException(v8::Exception::SyntaxError(v8::String::New("QtSignal.connect(): no arguments given")));

    if (data->resolveMode() == QScriptSignalData::ResolvedByName) {
        // ### Check if the signal is overloaded. If it is, throw an error,
        // since it's not possible to know which of the overloads we should connect to.
        // Can probably figure this out at class/instance construction time
    }

    v8::Handle<v8::Object> receiver;
    v8::Handle<v8::Object> slot;
    if (args.Length() == 1) {
        //simple function
        if (!args[0]->IsObject())
            return handleScope.Close(v8::ThrowException(v8::Exception::TypeError(v8::String::New("QtSignal.connect(): argument is not a function"))));
        slot = v8::Handle<v8::Object>(v8::Object::Cast(*args[0]));
    } else {
        receiver = v8::Handle<v8::Object>(v8::Object::Cast(*args[0]));
        v8::Local<v8::Value> arg1 = args[1];
        if (arg1->IsObject() && !arg1->IsString()) {
            slot = v8::Handle<v8::Object>(v8::Object::Cast(*arg1));
        } else if (!receiver.IsEmpty() && arg1->IsString()) {
            v8::Local<v8::String> propertyName = arg1->ToString();
            slot = v8::Handle<v8::Object>(v8::Object::Cast(*receiver->Get(propertyName)));
        }
    }

    if (slot.IsEmpty() || !slot->IsCallable()) {
        return handleScope.Close(v8::ThrowException(v8::Exception::TypeError(v8::String::New("QtSignal.connect(): target is not a function"))));
    }

    // Options:
    // 1) Connection manager per C++ object.
    // 2) Connection manager per Qt wrapper object.
    // 3) Connection manager per signal wrapper object.
    // 4) Connection object per connection.

    // disconnect() needs to be able to go introspect connections
    // of that signal only, for that wrapper only.

    // qDebug() << "connect" << data->object() << data->index();
    return handleScope.Close(data->connect(receiver, slot));
}

// This callback implements the disconnect() method of signal wrapper objects.
// The this-object is a QtSignal wrapper.
// If the disconnect succeeds, this function returns undefined; otherwise,
// an error is thrown.
v8::Handle<v8::Value> QScriptSignalData::QtDisconnectCallback(const v8::Arguments& args)
{
    v8::Local<v8::Object> self = args.Holder();
    QScriptSignalData *data = QScriptSignalData::get(self);

    if (args.Length() == 0)
        return v8::ThrowException(v8::Exception::SyntaxError(v8::String::New("QtSignal.disconnect(): no arguments given")));

    // ### Should be able to take any [[Callable]], but there is no v8 API for that.
    if (!args[0]->IsFunction())
        return v8::ThrowException(v8::Exception::TypeError(v8::String::New("QtSignal.disconnect(): argument is not a function")));

    return data->disconnect(v8::Handle<v8::Function>(v8::Function::Cast(*args[0])));
}

static v8::Handle<v8::Value> QtGetMetaMethod(v8::Local<v8::String> property, const v8::AccessorInfo& info)
{
    v8::Local<v8::Object> self = info.This();

    v8::Local<v8::Value> cached = self->GetHiddenValue(property);
    if (!cached.IsEmpty())
        return cached;

    v8::Local<v8::Array> dataArray = v8::Array::Cast(*info.Data());
    QScriptEnginePrivate *engine = static_cast<QScriptEnginePrivate *>(v8::External::Unwrap(dataArray->Get(0)));
    if (!engine->isQtObject(self)) {
        //If we are not called on a QObject (ie, the prototype is used in another object, we cannot do anything
        return v8::Handle<v8::Value>();
    }
    QScriptQObjectData *instance = QScriptQObjectData::get(self);
    Q_ASSERT(engine == instance->engine());
    //QScriptContextPrivate context(engine, &info);

    v8::Local<v8::Value> error;
    QObject *qobject = instance->cppObject(&error);
    if (!qobject)
        return error;

    const QMetaObject *meta = qobject->metaObject();
    QScriptMetaMethodInfo mmInfo;
    mmInfo.intData = v8::Uint32::Cast(*dataArray->Get(1))->Value();
    const QMetaMethod &method = meta->method(mmInfo.index);

    v8::Handle<v8::Object> result;
    if (method.methodType() == QMetaMethod::Signal) {
        QScriptSignalData *data = new QScriptSignalData(engine, self, mmInfo);
        result = QScriptSignalData::createInstance(data);
    } else {
        QScriptMetaMethodData *data = new QScriptMetaMethodData(engine, self, mmInfo);
        result = QScriptMetaMethodData::createInstance(data);
    }

    self->SetHiddenValue(property, result);
    //make a reference so QScriptGenericMetaMethodData::m_object is not garbage collected too early
    result->SetInternalField(1, self);
    return result;
}

static v8::Handle<v8::Value> findChildCallback(const v8::Arguments& args)
{
    v8::HandleScope handleScope;
    QScriptEnginePrivate *engine = reinterpret_cast<QScriptEnginePrivate *>(v8::External::Unwrap(args.Data()));
    Q_ASSERT(engine);
    v8::Local<v8::Object> self = args.This();
    if (!engine->qobjectTemplate()->HasInstance(self))
        return v8::Handle<v8::Value>(); //the QObject prototype is being used on another object.
        QScriptQObjectData *data = QScriptQObjectData::get(self);
    Q_ASSERT(engine == data->engine());
    QScriptContextPrivate context(engine, &args);

    v8::Local<v8::Value> error;
    QObject *qobject = data->cppObject(&error);
    if (!qobject)
        return error;

    QString name;
    if (args.Length() != 0)
        name = QScriptConverter::toString(args[0]->ToString());
    QObject *child = qobject->findChild<QObject *>(name);
    QScriptEngine::QObjectWrapOptions opt = QScriptEngine::PreferExistingWrapperObject;
    return handleScope.Close(engine->newQObject(child, QScriptEngine::QtOwnership, opt));
}

static v8::Handle<v8::Value> findChildrenWithRegExp(QScriptEnginePrivate *engine, QObject *qobject, v8::Handle<v8::Object> regExp)
{
    const QList<QObject *> children = qobject->findChildren<QObject *>();
    v8::Handle<v8::Object> testFunction = v8::Handle<v8::Object>::Cast(regExp->Get(v8::String::New("test")));

    v8::Local<v8::Array> result = v8::Array::New(children.length());
    v8::Handle<v8::Value> argv[1];

    for (int i = 0, resultIndex = 0; i < children.length(); i++) {
        argv[0] = QScriptConverter::toString(children.at(i)->objectName());
        if (testFunction->Call(regExp, 1, argv)->IsTrue()) {
            result->Set(resultIndex, engine->newQObject(children.at(i), QScriptEngine::QtOwnership,
                                                          QScriptEngine::PreferExistingWrapperObject));
            resultIndex++;
        }
    }

    return result;
}

static v8::Handle<v8::Value> findChildrenCallback(const v8::Arguments& args)
{
    v8::HandleScope handleScope;
    QScriptEnginePrivate *engine = reinterpret_cast<QScriptEnginePrivate *>(v8::External::Unwrap(args.Data()));
    Q_ASSERT(engine);
    v8::Local<v8::Object> self = args.This();
    if (!engine->qobjectTemplate()->HasInstance(self))
        return v8::Handle<v8::Value>(); //the QObject prototype is being used on another object.
        QScriptQObjectData *data = QScriptQObjectData::get(self);
    Q_ASSERT(engine == data->engine());
    QScriptContextPrivate context(engine, &args);

    v8::Local<v8::Value> error;
    QObject *qobject = data->cppObject(&error);
    if (!qobject)
        return error;

    QString name;
    if (args.Length() != 0) {
        if (args[0]->IsRegExp()) {
            v8::Handle<v8::Object> regExp = v8::Handle<v8::Object>::Cast(args[0]);
            return handleScope.Close(findChildrenWithRegExp(engine, qobject, regExp));
        }
        name = QScriptConverter::toString(args[0]->ToString());
    }
    const QList<QObject *> children = qobject->findChildren<QObject *>(name);
    v8::Local<v8::Array> array = v8::Array::New(children.length());
    const QScriptEngine::QObjectWrapOptions opt = QScriptEngine::PreferExistingWrapperObject;
    for (int i = 0; i < children.length(); i++) {
        array->Set(i , engine->newQObject(children.at(i), QScriptEngine::QtOwnership, opt));
    }
    return handleScope.Close(array);
}

v8::Handle<v8::FunctionTemplate> createQtClassTemplate(QScriptEnginePrivate *engine, const QMetaObject *mo, const QScriptEngine::QObjectWrapOptions &options)
{
    v8::HandleScope handleScope;
    v8::Handle<v8::FunctionTemplate> funcTempl = v8::FunctionTemplate::New();
    funcTempl->SetClassName(v8::String::New(mo->className()));

    // Make the template inherit the super class's template.
    // This is different from the old back-end, where every wrapped
    // object exposed everything as own properties.
    const QMetaObject *superMo = mo->superClass();
    if (superMo && !(options & QScriptEngine::ExcludeSuperClassContents))
        funcTempl->Inherit(engine->qtClassTemplate(superMo, options));
    else
        funcTempl->Inherit(engine->qobjectTemplate());

    v8::Handle<v8::ObjectTemplate> instTempl = funcTempl->InstanceTemplate();
    // Internal field is used to hold QScriptQObjectData*.
    instTempl->SetInternalFieldCount(1);

    // Figure out method names (own and inherited).
    QHash<QByteArray, QList<int> > ownMethodNameToIndexes;
    QHash<QByteArray, QList<int> > methodNameToIndexes;
    int methodOffset = mo->methodOffset();
    if ((options & QScriptEngine::ExcludeSuperClassContents) == QScriptEngine::ExcludeSuperClassProperties)
        methodOffset = 0;  //if we excluded the superclass for the properties, we need to include the superclass methods
    for (int i = 0; i < mo->methodCount(); ++i) {
        QMetaMethod method = mo->method(i);
        // Ignore private methods.
        if (method.access() == QMetaMethod::Private)
            continue;

        if (options & QScriptEngine::ExcludeSlots && method.methodType() == QMetaMethod::Slot)
            continue;

        if (i == 2 && options & QScriptEngine::ExcludeDeleteLater) {
            Q_ASSERT(!qstrcmp(method.signature(), "deleteLater()"));
            continue;
        }

        QByteArray signature = method.signature();
        QByteArray name = signature.left(signature.indexOf('('));
        if (i >= methodOffset)
            ownMethodNameToIndexes[name].append(i);
        methodNameToIndexes[name].append(i);
    }

    // Add accessors for own meta-methods.
    {
        v8::PropertyAttribute methodAttributes = (options & QScriptEngine::SkipMethodsInEnumeration) ? v8::DontEnum : v8::None ;
        QHash<QByteArray, QList<int> >::const_iterator it;
        v8::Handle<v8::Signature> sig = v8::Signature::New(funcTempl);
        for (it = ownMethodNameToIndexes.constBegin(); it != ownMethodNameToIndexes.constEnd(); ++it) {
            QByteArray name = it.key();
            QList<int> indexes = it.value();

            // Choose appropriate callback based on return type and parameter types.
            QMetaMethod method;
            foreach(int methodIndex, indexes) {
                method = mo->method(methodIndex);
                QScriptMetaMethodInfo info;
                info.index = methodIndex;
                info.voidvoid = !method.typeName()[0] && method.parameterTypes().isEmpty();
                info.resolveMode = 1;
                v8::Local<v8::Array> dataArray = v8::Array::New(2);
                dataArray->Set(0, v8::External::Wrap(engine));
                dataArray->Set(1, v8::Uint32::New(info.intData));
                instTempl->SetAccessor(v8::String::New(method.signature()), QtGetMetaMethod, 0, dataArray, v8::DEFAULT, methodAttributes);
            }
            QScriptMetaMethodInfo info;
            info.index = indexes.last(); // The largest index by that name.
            info.voidvoid = !method.typeName()[0] && method.parameterTypes().isEmpty();
            info.overloaded = methodNameToIndexes[name].size() > 1;
            v8::Local<v8::Array> dataArray = v8::Array::New(2);
            dataArray->Set(0, v8::External::Wrap(engine));
            dataArray->Set(1, v8::Uint32::New(info.intData));
            instTempl->SetAccessor(v8::String::New(name), QtGetMetaMethod, 0, dataArray, v8::DEFAULT, v8::DontEnum);

        }
    }

    // Add accessors for meta-properties.
    int propertyOffset = mo->propertyOffset();
    if ((options & QScriptEngine::ExcludeSuperClassContents) == QScriptEngine::ExcludeSuperClassMethods)
        propertyOffset = 0;  //if we excluded the superclass for the methods, we need to include the superclass properties
    for (int i = propertyOffset; i < mo->propertyCount(); ++i) {
        QMetaProperty prop = mo->property(i);
        if (!prop.isScriptable())
            continue;
        // Choose suitable callbacks for type.
        v8::AccessorGetter getter;
        v8::AccessorSetter setter;
#if 0
        if (prop.userType() == QMetaType::QString) {
            getter = QtMetaPropertyFastGetter<StringType>;
            setter = QtMetaPropertyFastSetter<StringType>;
        } else
#endif
        {
            getter = QtMetaPropertyGetter;
            setter = QtMetaPropertySetter;
        }

        v8::PropertyAttribute attribute = v8::DontDelete;
        if (!prop.isWritable()) {
            setter = 0;
            attribute = v8::PropertyAttribute(v8::DontDelete | v8::ReadOnly);
        }
        instTempl->SetAccessor(v8::String::New(prop.name()),
                               getter, setter, v8::Int32::New(i),
                               v8::DEFAULT, attribute);
    }

    instTempl->SetNamedPropertyHandler(QtLazyPropertyGetter,
                                        QtLazyPropertySetter,
                                        0, 0, 0,
                                        /*data=*/v8::External::Wrap(engine));

    return handleScope.Close(funcTempl);
}

// Returns the template for the almighty QObject class.
v8::Handle<v8::FunctionTemplate> QScriptEnginePrivate::qobjectTemplate()
{
    if (m_qobjectBaseTemplate.IsEmpty()) {
        v8::HandleScope handleScope;
        v8::Handle<v8::FunctionTemplate> funcTempl = v8::FunctionTemplate::New();
        funcTempl->SetClassName(v8::String::New("QObject"));

        v8::Handle<v8::ObjectTemplate> protoTempl = funcTempl->PrototypeTemplate();
        v8::Local<v8::Value> wEngine = v8::External::Wrap(this);
        protoTempl->Set(v8::String::New("findChild"), v8::FunctionTemplate::New(findChildCallback, wEngine)->GetFunction(), v8::DontEnum);
        protoTempl->Set(v8::String::New("findChildren"), v8::FunctionTemplate::New(findChildrenCallback, wEngine)->GetFunction(), v8::DontEnum);

        // Install QObject interceptor.
        // This interceptor will only get called if the access is not handled by the instance
        // itself nor other objects in the prototype chain.
        // The interceptor handles access to signals, dynamic properties and child objects.
        // The reason for putting the interceptor near the "back" of the prototype chain
        // is to avoid "slow" interceptor calls in what should be the common cases (accessing
        // meta-object-defined properties, and Q_INVOKABLE methods and slots).
        protoTempl->SetNamedPropertyHandler(QtLazyPropertyGetter,
                                            QtLazyPropertySetter,
                                            0, 0, 0,
                                            /*data=*/wEngine);

        m_qobjectBaseTemplate = v8::Persistent<v8::FunctionTemplate>::New(funcTempl);
    }
    return m_qobjectBaseTemplate;
}


v8::Handle<v8::Object> QScriptEnginePrivate::newQMetaObject(const QMetaObject *mo, const QScriptValue &ctor)
{
    v8::Handle<v8::FunctionTemplate> templ = metaObjectTemplate();
    Q_ASSERT(!templ.IsEmpty());
    v8::Handle<v8::ObjectTemplate> instanceTempl = templ->InstanceTemplate();
    Q_ASSERT(!instanceTempl.IsEmpty());
    v8::Handle<v8::Object> instance = instanceTempl->NewInstance();

    Q_ASSERT(instance->InternalFieldCount() == 1);

    // FIXME I'm not sure about that logic at least it is better than before as we are checking for an empty handle
    QScriptValuePrivate *tmp = QScriptValuePrivate::get(ctor);
    v8::Handle<v8::Value> ctorHandle;
    if (tmp->isObject())
        ctorHandle = *tmp;
    else
        ctorHandle = v8::Null();
    // FIXME that is likely to leak
    QtMetaObjectData *data = new QtMetaObjectData(this, mo, ctorHandle);
    instance->SetPointerInInternalField(0, data);

    v8::Persistent<v8::Object> persistent = v8::Persistent<v8::Object>::New(instance);
    persistent.MakeWeak(data, QScriptV8ObjectWrapperHelper::weakCallback<QtMetaObjectData>);
    return instance;
}

QT_END_NAMESPACE