void ScopeBuilder::initializeScopeChain()
{
    ScopeChain &scopeChain = _context->scopeChain();
    if (scopeChain.qmlComponentScope
            && scopeChain.qmlComponentScope->document == _doc) {
        return;
    }

    scopeChain = ScopeChain(); // reset

    Interpreter::Engine *engine = _context->engine();

    // ### TODO: This object ought to contain the global namespace additions by QML.
    scopeChain.globalScope = engine->globalObject();

    if (! _doc) {
        scopeChain.update();
        return;
    }

    Bind *bind = _doc->bind();
    QHash<Document *, ScopeChain::QmlComponentChain *> componentScopes;

    ScopeChain::QmlComponentChain *chain = new ScopeChain::QmlComponentChain;
    scopeChain.qmlComponentScope = QSharedPointer<const ScopeChain::QmlComponentChain>(chain);
    if (_doc->qmlProgram()) {
        componentScopes.insert(_doc.data(), chain);
        makeComponentChain(_doc, chain, &componentScopes);

        if (const TypeEnvironment *typeEnvironment = _context->typeEnvironment(_doc.data())) {
            scopeChain.qmlTypes = typeEnvironment;
        }
    } else {
        // add scope chains for all components that import this file
        foreach (Document::Ptr otherDoc, _snapshot) {
            foreach (const ImportInfo &import, otherDoc->bind()->imports()) {
                if (import.type() == ImportInfo::FileImport && _doc->fileName() == import.name()) {
                    ScopeChain::QmlComponentChain *component = new ScopeChain::QmlComponentChain;
                    componentScopes.insert(otherDoc.data(), component);
                    chain->instantiatingComponents += component;
                    makeComponentChain(otherDoc, component, &componentScopes);
                }
            }
        }

        // ### TODO: Which type environment do scripts see?

        if (bind->rootObjectValue())
            scopeChain.jsScopes += bind->rootObjectValue();
    }
예제 #2
0
void Link::initializeScopeChain()
{
    ScopeChain &scopeChain = _context->scopeChain();

    // ### TODO: This object ought to contain the global namespace additions by QML.
    scopeChain.globalScope = engine()->globalObject();

    if (! _doc) {
        scopeChain.update();
        return;
    }

    Bind *bind = _doc->bind();
    QHash<Document *, ScopeChain::QmlComponentChain *> componentScopes;

    if (_doc->qmlProgram()) {
        scopeChain.qmlComponentScope.clear();
        componentScopes.insert(_doc.data(), &scopeChain.qmlComponentScope);
        makeComponentChain(_doc, &scopeChain.qmlComponentScope, &componentScopes);

        if (const ObjectValue *typeEnvironment = _context->typeEnvironment(_doc.data()))
            scopeChain.qmlTypes = typeEnvironment;
    } else {
        // add scope chains for all components that import this file
        foreach (Document::Ptr otherDoc, _snapshot) {
            foreach (const QString &fileImport, otherDoc->bind()->fileImports()) {
                if (_doc->fileName() == fileImport) {
                    ScopeChain::QmlComponentChain *component = new ScopeChain::QmlComponentChain;
                    componentScopes.insert(otherDoc.data(), component);
                    scopeChain.qmlComponentScope.instantiatingComponents += component;
                    makeComponentChain(otherDoc, component, &componentScopes);
                }
            }
        }

        // ### TODO: Which type environment do scripts see?

        if (bind->rootObjectValue())
            scopeChain.jsScopes += bind->rootObjectValue();
    }