3d0398d0875543cba75bae2f9bd35a006dda492e
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qv8bindings.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qv8bindings_p.h"
43
44 #include <private/qv8_p.h>
45 #include <private/qdeclarativebinding_p.h>
46 #include <private/qdeclarativecompiler_p.h>
47 #include <private/qdeclarativeproperty_p.h>
48 #include <private/qdeclarativebinding_p_p.h>
49 #include <private/qdeclarativeexpression_p.h>
50 #include <private/qobject_p.h>
51 #include <private/qdeclarativetrace_p.h>
52
53 QT_BEGIN_NAMESPACE
54
55 QV8Bindings::Binding::Binding()
56 : index(-1), enabled(false), updating(false), line(-1), column(-1), object(0), parent(0)
57 {
58 }
59
60 void QV8Bindings::Binding::setEnabled(bool e, QDeclarativePropertyPrivate::WriteFlags flags)
61 {
62     if (enabled != e) {
63         enabled = e;
64
65         if (e) update(flags);
66     }
67 }
68
69 void QV8Bindings::Binding::refresh()
70 {
71     update();
72 }
73
74 void QV8Bindings::Binding::update(QDeclarativePropertyPrivate::WriteFlags flags)
75 {
76     if (!enabled)
77         return;
78
79     QDeclarativeTrace trace("V8 Binding Update");
80     trace.addDetail("URL", parent->url);
81     trace.addDetail("Line", line);
82     trace.addDetail("Column", column);
83
84     QDeclarativeContextData *context = QDeclarativeAbstractExpression::context();
85     if (!context || !context->isValid())
86         return;
87
88     if (!updating) {
89         updating = true;
90         QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context->engine);
91
92         bool isUndefined = false;
93
94         QDeleteWatcher watcher(this);
95         ep->referenceScarceResources(); 
96
97         v8::HandleScope handle_scope;
98         v8::Context::Scope scope(ep->v8engine()->context());
99         v8::Local<v8::Value> result = evaluate(v8::Handle<v8::Function>::Cast(parent->functions->Get(index)), 
100                                                &isUndefined);
101
102         trace.event("writing V8 result");
103         bool needsErrorData = false;
104         if (!watcher.wasDeleted() && !error.isValid()) {
105             typedef QDeclarativePropertyPrivate PP;
106             needsErrorData = !PP::writeBinding(object, property, this, result, isUndefined, flags);
107         }
108
109         if (!watcher.wasDeleted()) {
110
111             if (needsErrorData) {
112                 QUrl url = QUrl(parent->url);
113                 if (url.isEmpty()) url = QUrl(QLatin1String("<Unknown File>"));
114
115                 error.setUrl(url);
116                 error.setLine(line);
117                 error.setColumn(-1);
118             }
119
120             if (error.isValid()) {
121                 if (!addError(ep)) ep->warning(error);
122             } else {
123                 removeError();
124             }
125
126             updating = false;
127         }
128
129         ep->dereferenceScarceResources(); 
130
131     } else {
132         QDeclarativeProperty p = QDeclarativePropertyPrivate::restore(object, property, context);
133         QDeclarativeBindingPrivate::printBindingLoopError(p);
134     }
135 }
136
137 QString QV8Bindings::Binding::expressionIdentifier()
138 {
139     return parent->url.toString() + QLatin1String(":") + QString::number(line);
140 }
141
142 void QV8Bindings::Binding::expressionChanged()
143 {
144     update(QDeclarativePropertyPrivate::DontRemoveBinding);
145 }
146
147 void QV8Bindings::Binding::destroy()
148 {
149     enabled = false;
150     removeFromObject();
151     clear();
152     removeError();
153     parent->release();
154 }
155
156 QV8Bindings::QV8Bindings(const QString &program, int index, int line,
157                          QDeclarativeCompiledData *compiled, 
158                          QDeclarativeContextData *context)
159 : bindingsCount(0), bindings(0)
160 {
161     QV8Engine *engine = QDeclarativeEnginePrivate::getV8Engine(context->engine);
162
163     if (compiled->v8bindings[index].IsEmpty()) {
164         v8::HandleScope handle_scope;
165         v8::Context::Scope scope(engine->context());
166
167         v8::Local<v8::Script> script = engine->qmlModeCompile(program, compiled->name, line);
168         v8::Local<v8::Value> result = script->Run(engine->contextWrapper()->sharedContext());
169
170         if (result->IsArray()) 
171             compiled->v8bindings[index] = qPersistentNew(v8::Local<v8::Array>::Cast(result));
172     }
173
174     url = compiled->url;
175     functions = qPersistentNew(compiled->v8bindings[index]);
176     bindingsCount = functions->Length();
177     bindings = new QV8Bindings::Binding[bindingsCount];
178     
179     setContext(context);
180 }
181
182 QV8Bindings::~QV8Bindings()
183 {
184     qPersistentDispose(functions);
185
186     delete [] bindings;
187     bindings = 0;
188     bindingsCount = 0;
189 }
190
191 QDeclarativeAbstractBinding *QV8Bindings::configBinding(int index, QObject *target, QObject *scope, 
192                                                         const QDeclarativePropertyData &p,
193                                                         int line, int column)
194 {
195     QV8Bindings::Binding *rv = bindings + index;
196
197     rv->line = line;
198     rv->column = column;
199     rv->index = index;
200     rv->object = target;
201     rv->property = p;
202     rv->setContext(context());
203     rv->setScopeObject(scope);
204     rv->setUseSharedContext(true);
205     rv->setNotifyOnValueChanged(true);
206     rv->parent = this;
207
208     addref(); // This is decremented in Binding::destroy()
209
210     return rv;
211 }
212
213 QT_END_NAMESPACE