qmljslink.cpp 6 KB
Newer Older
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
#include "qmljslink.h"

#include "parser/qmljsast_p.h"
#include "qmljsdocument.h"
#include "qmljsbind.h"

#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QDebug>

using namespace QmlJS;
using namespace QmlJS::Interpreter;
using namespace QmlJS::AST;

static QString componentName(const QString &fileName)
{
    QString componentName = fileName;
    int dotIndex = componentName.indexOf(QLatin1Char('.'));
    if (dotIndex != -1)
        componentName.truncate(dotIndex);
    componentName[0] = componentName[0].toUpper();
    return componentName;
}

void LinkImports::linkImports(Bind *bind, const QList<Bind *> &binds)
{
    // ### TODO: remove all properties from _typeEnv

    Document::Ptr doc = bind->_doc;
    if (! (doc->qmlProgram() && doc->qmlProgram()->imports))
        return;

    QFileInfo fileInfo(doc->fileName());
    const QString absolutePath = fileInfo.absolutePath();

36
37
    // implicit imports:
    // qml files in the same directory are available without explicit imports
38
39
40
41
42
43
44
45
46
47
48
    foreach (Bind *otherBind, binds) {
        if (otherBind == bind)
            continue;

        Document::Ptr otherDoc = otherBind->_doc;
        QFileInfo otherFileInfo(otherDoc->fileName());
        const QString otherAbsolutePath = otherFileInfo.absolutePath();

        if (otherAbsolutePath != absolutePath)
            continue;

49
50
        bind->_typeEnvironment->setProperty(componentName(otherFileInfo.fileName()),
                                            otherBind->_rootObjectValue);
51
52
53
54
55
56
57
58
59
60
61
62
    }

    // explicit imports, whether directories or files
    for (UiImportList *it = doc->qmlProgram()->imports; it; it = it->next) {
        if (! (it->import && it->import->fileName))
            continue;

        QString path = absolutePath;
        path += QLatin1Char('/');
        path += it->import->fileName->asString();
        path = QDir::cleanPath(path);

63
64
        ObjectValue *importNamespace = 0;

65
66
67
68
69
70
71
72
73
74
        foreach (Bind *otherBind, binds) {
            Document::Ptr otherDoc = otherBind->_doc;
            QFileInfo otherFileInfo(otherDoc->fileName());
            const QString otherAbsolutePath = otherFileInfo.absolutePath();

            bool directoryImport = (path == otherAbsolutePath);
            bool fileImport = (path == otherDoc->fileName());
            if (!directoryImport && !fileImport)
                continue;

75
76
77
            if (directoryImport && it->import->importId && !importNamespace) {
                importNamespace = bind->_interp->newObject(/*prototype =*/0);
                bind->_typeEnvironment->setProperty(it->import->importId->asString(), importNamespace);
78
79
80
81
82
83
84
85
86
            }

            QString targetName;
            if (fileImport && it->import->importId) {
                targetName = it->import->importId->asString();
            } else {
                targetName = componentName(otherFileInfo.fileName());
            }

87
88
89
90
            ObjectValue *importInto = bind->_typeEnvironment;
            if (importNamespace)
                importInto = importNamespace;

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
            importInto->setProperty(targetName, otherBind->_rootObjectValue);
        }
    }
}

static const ObjectValue *lookupType(ObjectValue *env, UiQualifiedId *id)
{
    const ObjectValue *objectValue = env;

    for (UiQualifiedId *iter = id; objectValue && iter; iter = iter->next) {
        if (! iter->name)
            return 0;

        const Value *value = objectValue->property(iter->name->asString());
        if (!value)
            return 0;

        objectValue = value->asObjectValue();
    }

    return objectValue;
}

void LinkImports::operator()(const QList<Bind *> &binds)
{
    foreach (Bind *bind, binds) {
        // Populate the _typeEnvironment with imports.
        linkImports(bind, binds);

        // Set the prototypes.
        {
            QHash<UiObjectDefinition *, ObjectValue *>::iterator it = bind->_qmlObjectDefinitions.begin();
            QHash<UiObjectDefinition *, ObjectValue *>::iterator end = bind->_qmlObjectDefinitions.end();
            for (; it != end; ++it) {
                UiObjectDefinition *key = it.key();
                ObjectValue *value = it.value();
                if (!key->qualifiedTypeNameId)
                    continue;

                value->setPrototype(lookupType(bind->_typeEnvironment, key->qualifiedTypeNameId));
            }
        }
        {
            QHash<UiObjectBinding *, ObjectValue *>::iterator it = bind->_qmlObjectBindings.begin();
            QHash<UiObjectBinding *, ObjectValue *>::iterator end = bind->_qmlObjectBindings.end();
            for (; it != end; ++it) {
                UiObjectBinding *key = it.key();
                ObjectValue *value = it.value();
                if (!key->qualifiedTypeNameId)
                    continue;

                value->setPrototype(lookupType(bind->_typeEnvironment, key->qualifiedTypeNameId));
            }
        }
    }
}

ObjectValue *Link::operator()(const QList<Bind *> &binds, Bind *currentBind, UiObjectMember *currentObject)
{
    ObjectValue *scopeObject;
    if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(currentObject))
        scopeObject = currentBind->_qmlObjectDefinitions.value(definition);
    else if (UiObjectBinding *binding = cast<UiObjectBinding *>(currentObject))
        scopeObject = currentBind->_qmlObjectBindings.value(binding);
    else
156
        return currentBind->_interp->globalObject();
157
158

    if (!scopeObject)
159
        return currentBind->_interp->globalObject();
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

    // Build the scope chain.
    currentBind->_typeEnvironment->setScope(currentBind->_idEnvironment);
    currentBind->_idEnvironment->setScope(currentBind->_functionEnvironment);
    currentBind->_functionEnvironment->setScope(scopeObject);
    if (scopeObject != currentBind->_rootObjectValue) {
        scopeObject->setScope(currentBind->_rootObjectValue);
        currentBind->_rootObjectValue->setScope(currentBind->_interp->globalObject());
    } else {
        scopeObject->setScope(currentBind->_interp->globalObject());
    }
    // May want to link to instantiating components from here.

    return currentBind->_typeEnvironment;
}