1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qv8bindings_p.h"
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>
55 QV8Bindings::Binding::Binding()
56 : index(-1), enabled(false), updating(false), line(-1), column(-1), object(0), parent(0)
60 void QV8Bindings::Binding::setEnabled(bool e, QDeclarativePropertyPrivate::WriteFlags flags)
69 void QV8Bindings::Binding::refresh()
74 void QV8Bindings::Binding::update(QDeclarativePropertyPrivate::WriteFlags flags)
79 QDeclarativeTrace trace("V8 Binding Update");
80 trace.addDetail("URL", parent->url);
81 trace.addDetail("Line", line);
82 trace.addDetail("Column", column);
84 QDeclarativeContextData *context = QDeclarativeAbstractExpression::context();
85 if (!context || !context->isValid())
90 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(context->engine);
92 bool isUndefined = false;
94 QDeleteWatcher watcher(this);
95 ep->referenceScarceResources();
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)),
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);
109 if (!watcher.wasDeleted()) {
111 if (needsErrorData) {
112 QUrl url = QUrl(parent->url);
113 if (url.isEmpty()) url = QUrl(QLatin1String("<Unknown File>"));
120 if (error.isValid()) {
121 if (!addError(ep)) ep->warning(error);
129 ep->dereferenceScarceResources();
132 QDeclarativeProperty p = QDeclarativePropertyPrivate::restore(object, property, context);
133 QDeclarativeBindingPrivate::printBindingLoopError(p);
137 QString QV8Bindings::Binding::expressionIdentifier()
139 return parent->url.toString() + QLatin1String(":") + QString::number(line);
142 void QV8Bindings::Binding::expressionChanged()
144 update(QDeclarativePropertyPrivate::DontRemoveBinding);
147 void QV8Bindings::Binding::destroy()
156 QV8Bindings::QV8Bindings(const QString &program, int index, int line,
157 QDeclarativeCompiledData *compiled,
158 QDeclarativeContextData *context)
159 : bindingsCount(0), bindings(0)
161 QV8Engine *engine = QDeclarativeEnginePrivate::getV8Engine(context->engine);
163 if (compiled->v8bindings[index].IsEmpty()) {
164 v8::HandleScope handle_scope;
165 v8::Context::Scope scope(engine->context());
167 v8::Local<v8::Script> script = engine->qmlModeCompile(program, compiled->name, line);
168 v8::Local<v8::Value> result = script->Run(engine->contextWrapper()->sharedContext());
170 if (result->IsArray())
171 compiled->v8bindings[index] = qPersistentNew(v8::Local<v8::Array>::Cast(result));
175 functions = qPersistentNew(compiled->v8bindings[index]);
176 bindingsCount = functions->Length();
177 bindings = new QV8Bindings::Binding[bindingsCount];
182 QV8Bindings::~QV8Bindings()
184 qPersistentDispose(functions);
191 QDeclarativeAbstractBinding *QV8Bindings::configBinding(int index, QObject *target, QObject *scope,
192 const QDeclarativePropertyData &p,
193 int line, int column)
195 QV8Bindings::Binding *rv = bindings + index;
202 rv->setContext(context());
203 rv->setScopeObject(scope);
204 rv->setUseSharedContext(true);
205 rv->setNotifyOnValueChanged(true);
208 addref(); // This is decremented in Binding::destroy()