Commit 3096b2a0 authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJS: Set importing scopes for scripts correctly.

They had not yet been updated to the new way of importing javascript.

Reviewed-by: Erik Verbruggen
parent 3286f485
......@@ -54,14 +54,14 @@ Bind::~Bind()
{
}
QStringList Bind::includedScripts() const
QStringList Bind::fileImports() const
{
return _includedScripts;
return _fileImports;
}
QStringList Bind::fileImports() const
QStringList Bind::directoryImports() const
{
return _fileImports;
return _directoryImports;
}
QStringList Bind::libraryImports() const
......@@ -142,47 +142,11 @@ ExpressionNode *Bind::expression(UiScriptBinding *ast) const
return 0;
}
void Bind::processScript(AST::UiQualifiedId *qualifiedId, AST::UiObjectInitializer *initializer)
{
Q_UNUSED(qualifiedId);
if (! initializer)
return;
for (UiObjectMemberList *it = initializer->members; it; it = it->next) {
if (UiScriptBinding *binding = cast<UiScriptBinding *>(it->member)) {
const QString bindingName = toString(binding->qualifiedId);
if (bindingName == QLatin1String("source")) {
if (StringLiteral *literal = cast<StringLiteral *>(expression(binding))) {
QFileInfo fileInfo(QDir(_doc->path()), literal->value->asString());
const QString scriptPath = fileInfo.absoluteFilePath();
_includedScripts.append(scriptPath);
}
}
} else if (UiSourceElement *binding = cast<UiSourceElement *>(it->member)) {
if (FunctionDeclaration *decl = cast<FunctionDeclaration *>(binding->sourceElement)) {
accept(decl); // process the function declaration
} else {
// ### unexpected source element
}
} else {
// ### unexpected binding.
}
}
}
ObjectValue *Bind::bindObject(UiQualifiedId *qualifiedTypeNameId, UiObjectInitializer *initializer)
{
ObjectValue *parentObjectValue = 0;
const QString typeName = toString(qualifiedTypeNameId);
if (typeName == QLatin1String("Script")) {
// Script blocks all contribute to the same scope
parentObjectValue = switchObjectValue(_functionEnvironment);
processScript(qualifiedTypeNameId, initializer);
switchObjectValue(parentObjectValue);
}
// normal component instance
ASTObjectValue *objectValue = new ASTObjectValue(qualifiedTypeNameId, initializer, _doc, &_engine);
QmlPrototypeReference *prototypeReference =
......@@ -225,7 +189,13 @@ bool Bind::visit(UiImport *ast)
if (ast->importUri) {
_libraryImports += toString(ast->importUri, QLatin1Char('/'));
} else if (ast->fileName) {
_fileImports += ast->fileName->asString();
const QFileInfo importFileInfo(_doc->path() + QLatin1Char('/') + ast->fileName->asString());
if (importFileInfo.isFile())
_fileImports += importFileInfo.absoluteFilePath();
else if (importFileInfo.isDir())
_directoryImports += importFileInfo.absoluteFilePath();
//else
// error: file or directory does not exist
}
return false;
......
......@@ -50,8 +50,8 @@ public:
Bind(Document *doc);
virtual ~Bind();
QStringList includedScripts() const;
QStringList fileImports() const;
QStringList directoryImports() const;
QStringList libraryImports() const;
Interpreter::ObjectValue *currentObjectValue() const;
......@@ -91,7 +91,6 @@ protected:
Interpreter::ObjectValue *bindObject(AST::UiQualifiedId *qualifiedTypeNameId, AST::UiObjectInitializer *initializer);
AST::ExpressionNode *expression(AST::UiScriptBinding *ast) const;
void processScript(AST::UiQualifiedId *qualifiedId, AST::UiObjectInitializer *initializer);
private:
Document *_doc;
......@@ -107,6 +106,7 @@ private:
QStringList _includedScripts;
QStringList _fileImports;
QStringList _directoryImports;
QStringList _libraryImports;
};
......
......@@ -64,14 +64,15 @@ void Link::initializeScopeChain()
if (const ObjectValue *typeEnvironment = _context->typeEnvironment(_doc.data()))
scopeChain.qmlTypes = typeEnvironment;
} else {
// add scope chains for all components that source this document
// ### TODO: This needs updates for the new way of importing scripts
// add scope chains for all components that import this file
foreach (Document::Ptr otherDoc, _snapshot) {
if (otherDoc->bind()->includedScripts().contains(_doc->fileName())) {
ScopeChain::QmlComponentChain *component = new ScopeChain::QmlComponentChain;
componentScopes.insert(otherDoc.data(), component);
scopeChain.qmlComponentScope.instantiatingComponents += component;
makeComponentChain(otherDoc, component, &componentScopes);
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);
}
}
}
......@@ -114,16 +115,6 @@ void Link::makeComponentChain(
if (bind->rootObjectValue())
target->rootObject = bind->rootObjectValue();
const QStringList &includedScripts = bind->includedScripts();
for (int index = includedScripts.size() - 1; index != -1; --index) {
const QString &scriptFile = includedScripts.at(index);
if (Document::Ptr scriptDoc = _snapshot.document(scriptFile)) {
if (scriptDoc->jsProgram())
target->functionScopes += scriptDoc->bind()->rootObjectValue();
}
}
target->functionScopes += bind->functionEnvironment();
target->ids = bind->idEnvironment();
}
......
......@@ -227,17 +227,14 @@ static void findNewFileImports(const Document::Ptr &doc, const Snapshot &snapsho
{
// scan files and directories that are explicitly imported
foreach (const QString &fileImport, doc->bind()->fileImports()) {
const QFileInfo importFileInfo(doc->path() + QLatin1Char('/') + fileImport);
const QString &importFilePath = importFileInfo.absoluteFilePath();
if (importFileInfo.isFile()) {
if (! snapshot.document(importFilePath))
*importedFiles += importFilePath;
} else if (importFileInfo.isDir()) {
if (snapshot.documentsInDirectory(importFilePath).isEmpty()) {
if (! scannedPaths->contains(importFilePath)) {
*importedFiles += qmlFilesInDirectory(importFilePath);
scannedPaths->insert(importFilePath);
}
if (! snapshot.document(fileImport))
*importedFiles += fileImport;
}
foreach (const QString &directoryImport, doc->bind()->directoryImports()) {
if (snapshot.documentsInDirectory(directoryImport).isEmpty()) {
if (! scannedPaths->contains(directoryImport)) {
*importedFiles += qmlFilesInDirectory(directoryImport);
scannedPaths->insert(directoryImport);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment