summaryrefslogtreecommitdiffstats
path: root/generator/setupgenerator.cpp
blob: c07601ef5c8ceab0070ed9ecd08b65d7683eafc9 (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
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the Qt Script Generator project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "setupgenerator.h"
#include "reporthandler.h"
#include "fileout.h"

//#define Q_SCRIPT_LAZY_GENERATOR

void SetupGenerator::addClass(const AbstractMetaClass *cls) 
{
    packHash[cls->package()].append(cls);
}

void writeQtScriptQtBindingsLicense(QTextStream &stream);

void maybeDeclareMetaType(QTextStream &stream, const QString &typeName,
                          QSet<QString> &registeredTypeNames);
bool hasDefaultConstructor(const AbstractMetaClass *meta_class);

void SetupGenerator::generate()
{
    QHashIterator<QString, QList<const AbstractMetaClass*> > pack(packHash);
    while (pack.hasNext()) {
        pack.next();
        QList<const AbstractMetaClass*> list = pack.value();
        if (list.isEmpty())
            continue;

        QString packName = pack.key();
        QStringList components = packName.split(".");
        if ((components.size() > 2) && (components.at(0) == "com")
            && (components.at(1) == "trolltech")) {
            // kill com.trolltech in key
            components.removeAt(0);
            components.removeAt(0);
        }
        packName.replace(".", "_");

        {
            FileOut initFile(m_out_dir + "/generated_cpp/" + packName + "/init.cpp");
            QTextStream &s = initFile.stream;

            if (FileOut::license)
                writeQtScriptQtBindingsLicense(s);

            s << "#include <QtScript/QScriptValue>" << endl
              << "#include <QtScript/QScriptEngine>" << endl;
#ifdef Q_SCRIPT_LAZY_GENERATOR
            s << "#include <QtScript/QScriptClass>" << endl
              << "#include <QtScript/QScriptString>" << endl;
#endif
            s << endl;

            // declare individual class creation functions
            foreach (const AbstractMetaClass *cls, list) {
                s << "QScriptValue qtscript_create_" << cls->name() << "_class(QScriptEngine *engine);" << endl;
            }
            s << endl;

            // write table of class names
            {
                s << "static const char * const qtscript_" << packName << "_class_names[] = {" << endl;
                bool needComma = false;
                foreach (const AbstractMetaClass *cls, list) {
                    s << "    ";
                    if (needComma)
                        s << ", ";
                    s << "\"" << cls->name() << "\"" << endl;
                    needComma = true;
                }
                s << "};" << endl << endl;
            }

            // write table of function pointers
            {
                s << "typedef QScriptValue (*QtBindingCreator)(QScriptEngine *engine);" << endl;
                s << "static const QtBindingCreator qtscript_" << packName << "_class_functions[] = {" << endl;
                bool needComma = false;
                foreach (const AbstractMetaClass *cls, list) {
                    s << "    ";
                    if (needComma)
                        s << ", ";
                    s << "qtscript_create_" << cls->name() << "_class" << endl;
                    needComma = true;
                }
                s << "};" << endl << endl;
            }

#ifdef Q_SCRIPT_LAZY_GENERATOR
            {
                // declare meta-types
                QSet<QString> registeredTypeNames = m_qmetatype_declared_typenames;
                foreach (const AbstractMetaClass *cls, list) {
                    if (cls->isNamespace())
                        continue;
                    QString name = cls->qualifiedCppName();
                    if (cls->typeEntry()->isValue() && ::hasDefaultConstructor(cls))
                        maybeDeclareMetaType(s, name, registeredTypeNames);
                    maybeDeclareMetaType(s, name + "*", registeredTypeNames);
                }
                s << endl;
                // write table of metatype-ids
                s << "static const int qtscript_" << packName << "_metatype_ids[] = {" << endl;
                for (int i = 0; i < list.size(); ++i) {
                    const AbstractMetaClass *cls = list.at(i);
                    s << "    ";
                    if (i > 0)
                        s << ", ";
                    if (cls->isNamespace()) {
                        s << "-1, -1";
                    } else {
                        QString name = cls->qualifiedCppName();
                        if (cls->typeEntry()->isValue() && ::hasDefaultConstructor(cls))
                            s << "qMetaTypeId<" << name << ">()";
                        else
                            s << "-1";
                        s << ", qMetaTypeId<" << name << "*>()";
                    }
                    s << endl;
                }
                s << "};" << endl << endl;
            }

            // write the fake prototype class
            {
                s << "class qtscript_" << packName << "_FakePrototype : public QScriptClass" << endl
                  << "{" << endl
                  << "public:" << endl
                  << "    qtscript_" << packName << "_FakePrototype(QScriptEngine *engine)" << endl
                  << "        : QScriptClass(engine) {}" << endl << endl

                  << "    QueryFlags queryProperty(const QScriptValue &fake," << endl
                  << "        const QScriptString &name, QueryFlags flags, uint *)" << endl
                  << "    {" << endl
                  << "        if (fake.prototype().isValid())" << endl
                  << "            return 0;" << endl
                  << "        int classIndex = fake.data().toInt32();" << endl
                  << "        const char *className = qtscript_" << packName << "_class_names[classIndex];" << endl
//              << "        qDebug() << \"faking\" << className;" << endl
                  << "        QScriptValue extensionObject = engine()->globalObject();" << endl
                  << "        QScriptValue ctor = extensionObject.property(className);" << endl
                  << "        QScriptValue genuine = ctor.property(\"prototype\");" << endl
                  << "        Q_ASSERT(genuine.isObject());" << endl
                  << "        const_cast<QScriptValue&>(fake).setPrototype(genuine);" << endl
                  << "        if (!genuine.property(name).isValid())" << endl
                  << "            flags &= ~HandlesReadAccess;" << endl
                  << "        return flags & ~HandlesWriteAccess;" << endl
                  << "    }" << endl << endl

                  << "    QScriptValue property(const QScriptValue &fake, "
                  << "const QScriptString &name, uint)" << endl
                  << "    {" << endl
                  << "        return fake.prototype().property(name, QScriptValue::ResolveLocal);" << endl
                  << "    }" << endl
                  << "};" << endl << endl;
            }

            // write the lazy class loader
            {
                s << "static QScriptValue qtscript_" << packName << "_getSetClass("
                  << "QScriptContext *context, QScriptEngine *engine)" << endl
                  << "{" << endl
                  << "    QScriptValue target = context->thisObject();" << endl
                  << "    int classIndex = context->callee().data().toInt32();" << endl
                  << "    const char *className = qtscript_" << packName << "_class_names[classIndex];" << endl
                  << "    qDebug() << \"loading\" << className;" << endl
                  << "    target.setProperty(className, QScriptValue(), "
                  << "QScriptValue::PropertyGetter|QScriptValue::PropertySetter);" << endl
                  << "    if (context->argumentCount() == 1) {" << endl
                  << "        target.setProperty(className, context->argument(0));" << endl
                  << "    } else {" << endl
                  << "        target.setProperty(className, qtscript_"
                  << packName << "_class_functions[classIndex](engine)," << endl
                  << "            QScriptValue::SkipInEnumeration);" << endl
                  << "    }" << endl
                  << "    return target.property(className);" << endl
                  << "}" << endl << endl;
            }
#endif

            // bindings init function
            s << "void qtscript_initialize_" << packName << "_bindings(QScriptValue &extensionObject)" << endl
              << "{" << endl
              << "    QScriptEngine *engine = extensionObject.engine();" << endl;
#ifdef Q_SCRIPT_LAZY_GENERATOR
            s << "    qtscript_" << packName << "_FakePrototype *fakeProtoClass;" << endl
              << "    fakeProtoClass = new qtscript_" << packName << "_FakePrototype(engine);" << endl;
#endif
            s << "    for (int i = 0; i < " << list.size() << "; ++i) {" << endl
#ifndef Q_SCRIPT_LAZY_GENERATOR
              << "        extensionObject.setProperty(qtscript_" << packName << "_class_names[i]," << endl
              << "            qtscript_" << packName << "_class_functions[i](engine)," << endl
              << "            QScriptValue::SkipInEnumeration);" << endl
#else
              << "        QScriptValue classIndex(engine, i);" << endl
              << "        QScriptValue fakeCtor = engine->newFunction(qtscript_" << packName << "_getSetClass);" << endl
              << "        fakeCtor.setData(classIndex);" << endl
              << "        extensionObject.setProperty(qtscript_" << packName << "_class_names[i]," << endl
              << "            fakeCtor, QScriptValue::PropertyGetter|QScriptValue::PropertySetter"
              << "|QScriptValue::SkipInEnumeration);" << endl
              << "        QScriptValue fakeProto = engine->newObject(fakeProtoClass, classIndex);" << endl
              << "        fakeProto.setPrototype(QScriptValue());" << endl
              << "        if (qtscript_" << packName << "_metatype_ids[i*2] != -1)" << endl
              << "            engine->setDefaultPrototype(qtscript_"
              << packName << "_metatype_ids[i*2], fakeProto);" << endl
              << "        if (qtscript_" << packName << "_metatype_ids[i*2+1] != -1)" << endl
              << "            engine->setDefaultPrototype(qtscript_"
              << packName << "_metatype_ids[i*2+1], fakeProto);" << endl
#endif
              << "    }" << endl
              << "}" << endl;

            if (initFile.done())
                ++m_num_generated_written;
            ++m_num_generated;
        }

        {
            FileOut pluginFile(m_out_dir + "/generated_cpp/" + packName + "/plugin.cpp");
            QTextStream &s = pluginFile.stream;

            if (FileOut::license)
                writeQtScriptQtBindingsLicense(s);

            s << "#include <QtScript/QScriptExtensionPlugin>" << endl
              << "#include <QtScript/QScriptValue>" << endl
              << "#include <QtScript/QScriptEngine>" << endl << endl;

            // declare the init function
            s << "void qtscript_initialize_" << packName << "_bindings(QScriptValue &);" << endl << endl;

            // plugin class declaration
            s << "class " << packName << "_ScriptPlugin : public QScriptExtensionPlugin" << endl
              << "{" << endl
              << "public:" << endl
              << "    QStringList keys() const;" << endl
              << "    void initialize(const QString &key, QScriptEngine *engine);" << endl
              << "};" << endl
              << "" << endl;

            // keys()
            s << "QStringList " << packName << "_ScriptPlugin::keys() const" << endl
              << "{" << endl
              << "    QStringList list;" << endl;
            {
                QString key;
                for (int i = 0; i < components.size(); ++i) {
                    if (i > 0)
                        key.append(".");
                    key.append(components.at(i));
                    s << "    list << QLatin1String(\"" << key << "\");" << endl;
                }
            }
            s << "    return list;" << endl
              << "}" << endl;

            // initialize()
            s << endl
              << "void " << packName << "_ScriptPlugin::initialize(const QString &key, QScriptEngine *engine)" << endl
              << "{";
            {
                QString key;
                for (int i = 0; i < components.size(); ++i) {
                    s << endl << "    ";
                    if (i > 0) {
                        key.append(".");
                        s << "} else ";
                    }
                    key.append(components.at(i));
                    s << "if (key == QLatin1String(\"" << key << "\")) {";
                }
            }
            s << endl << "        QScriptValue extensionObject = ";
            // ### generalize
            if (packName == "com.trolltech.qt.phonon")
                s << "setupPackage(\"phonon\", engine)";
            else
                s << "engine->globalObject()";
            s << ";" << endl;

            s << "        qtscript_initialize_" << packName << "_bindings(extensionObject);" << endl;

            s << "    } else {" << endl
              << "        Q_ASSERT_X(false, \"" << packName << "::initialize\", qPrintable(key));" << endl
              << "    }" << endl
              << "}" << endl << endl;

            s << "Q_EXPORT_STATIC_PLUGIN(" << packName << "_ScriptPlugin)" << endl
              << "Q_EXPORT_PLUGIN2(qtscript_" << packName.toLower() << ", " << packName << "_ScriptPlugin)" << endl;

            if (pluginFile.done())
                ++m_num_generated_written;
            ++m_num_generated;
        }
    }
}