Skip to content
Snippets Groups Projects
Commit a8bc5a4b authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Create bindings for the javascript soure elements.

parent 15ae1001
No related branches found
No related tags found
No related merge requests found
......@@ -120,6 +120,42 @@ public:
}
};
class ProcessSourceElements: protected AST::Visitor
{
Interpreter::Engine *_interp;
public:
ProcessSourceElements(Interpreter::Engine *interp)
: _interp(interp)
{
}
void operator()(AST::Node *node)
{
if (node)
node->accept(this);
}
protected:
using AST::Visitor::visit;
virtual bool visit(FunctionDeclaration *ast)
{
if (ast->name)
_interp->globalObject()->setProperty(ast->name->asString(), new ASTFunctionValue(ast, _interp));
return false;
}
virtual bool visit(VariableDeclaration *ast)
{
if (ast->name)
_interp->globalObject()->setProperty(ast->name->asString(), _interp->undefinedValue());
return false;
}
};
} // end of anonymous namespace
Bind::Bind(Document::Ptr doc, const Snapshot &snapshot, Interpreter::Engine *interp)
......@@ -152,6 +188,11 @@ Bind::~Bind()
{
}
QStringList Bind::includedScripts() const
{
return _includedScripts;
}
ObjectValue *Bind::switchObjectValue(ObjectValue *newObjectValue)
{
ObjectValue *oldObjectValue = _currentObjectValue;
......@@ -177,10 +218,30 @@ ObjectValue *Bind::scopeChainAt(Document::Ptr currentDocument, const Snapshot &s
LinkImports linkImports;
Link link;
// link the import directives
linkImports(binds);
// link the scripts
QStringList includedScriptFiles;
foreach (Bind *bind, binds)
includedScriptFiles += bind->includedScripts();
includedScriptFiles.removeDuplicates();
ProcessSourceElements processSourceElements(interp);
foreach (const QString &scriptFile, includedScriptFiles) {
if (Document::Ptr scriptDoc = snapshot.document(scriptFile)) {
if (AST::Program *program = scriptDoc->jsProgram()) {
processSourceElements(program);
}
}
}
ObjectValue *scope = interp->globalObject();
if (currentBind)
scope = link(binds, currentBind, currentObject);
qDeleteAll(binds);
return scope;
......@@ -222,8 +283,8 @@ void Bind::processScript(AST::UiQualifiedId *qualifiedId, AST::UiObjectInitializ
if (bindingName == QLatin1String("source")) {
if (StringLiteral *literal = cast<StringLiteral *>(expression(binding))) {
QFileInfo fileInfo(QDir(_doc->path()), literal->value->asString());
Document::Ptr script = _snapshot.document(fileInfo.absoluteFilePath());
// ### process the script
const QString scriptPath = fileInfo.absoluteFilePath();
_includedScripts.append(scriptPath);
}
}
} else if (UiSourceElement *binding = cast<UiSourceElement *>(it->member)) {
......
......@@ -35,6 +35,7 @@
#include <qmljs/qmljsinterpreter.h>
#include <QtCore/QHash>
#include <QtCore/QStringList>
namespace QmlJS {
......@@ -49,6 +50,8 @@ protected:
public:
virtual ~Bind();
QStringList includedScripts() const;
// ### TODO: This methods should go. Bind each document after parsing, link later.
static Interpreter::ObjectValue *scopeChainAt(Document::Ptr currentDocument,
const Snapshot &snapshot,
......@@ -91,6 +94,7 @@ private:
QHash<AST::UiObjectDefinition *, Interpreter::ObjectValue *> _qmlObjectDefinitions;
QHash<AST::UiObjectBinding *, Interpreter::ObjectValue *> _qmlObjectBindings;
QStringList _includedScripts;
friend class LinkImports;
friend class Link;
......
......@@ -148,7 +148,6 @@ void LinkImports::operator()(const QList<Bind *> &binds)
ObjectValue *Link::operator()(const QList<Bind *> &binds, Bind *currentBind, UiObjectMember *currentObject)
{
Q_UNUSED(binds);
ObjectValue *scopeObject;
if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(currentObject))
scopeObject = currentBind->_qmlObjectDefinitions.value(definition);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment