source: trunk/src/script/api/qscriptprogram.cpp@ 1050

Last change on this file since 1050 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtScript module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL-ONLY$
10** GNU Lesser General Public License Usage
11** This file may be used under the terms of the GNU Lesser
12** General Public License version 2.1 as published by the Free Software
13** Foundation and appearing in the file LICENSE.LGPL included in the
14** packaging of this file. Please review the following information to
15** ensure the GNU Lesser General Public License version 2.1 requirements
16** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17**
18** If you have questions regarding the use of this file, please contact
19** Nokia at [email protected].
20** $QT_END_LICENSE$
21**
22****************************************************************************/
23
24#include "config.h"
25#include "qscriptprogram.h"
26#include "qscriptprogram_p.h"
27#include "qscriptengine.h"
28#include "qscriptengine_p.h"
29
30#include "Executable.h"
31
32QT_BEGIN_NAMESPACE
33
34/*!
35 \since 4.7
36 \class QScriptProgram
37
38 \brief The QScriptProgram class encapsulates a Qt Script program.
39
40 \ingroup script
41
42 QScriptProgram retains the compiled representation of the script if
43 possible. Thus, QScriptProgram can be used to evaluate the same
44 script multiple times more efficiently.
45
46 \code
47 QScriptEngine engine;
48 QScriptProgram program("1 + 2");
49 QScriptValue result = engine.evaluate(program);
50 \endcode
51*/
52
53QScriptProgramPrivate::QScriptProgramPrivate(const QString &src,
54 const QString &fn,
55 int ln)
56 : sourceCode(src), fileName(fn), firstLineNumber(ln),
57 engine(0), _executable(0), sourceId(-1), isCompiled(false)
58{
59 ref = 0;
60}
61
62QScriptProgramPrivate::~QScriptProgramPrivate()
63{
64 if (engine) {
65 QScript::APIShim shim(engine);
66 _executable.clear();
67 engine->unregisterScriptProgram(this);
68 }
69}
70
71QScriptProgramPrivate *QScriptProgramPrivate::get(const QScriptProgram &q)
72{
73 return const_cast<QScriptProgramPrivate*>(q.d_func());
74}
75
76JSC::EvalExecutable *QScriptProgramPrivate::executable(JSC::ExecState *exec,
77 QScriptEnginePrivate *eng)
78{
79 if (_executable) {
80 if (eng == engine)
81 return _executable.get();
82 // "Migrating" to another engine; clean up old state
83 QScript::APIShim shim(engine);
84 _executable.clear();
85 engine->unregisterScriptProgram(this);
86 }
87 WTF::PassRefPtr<QScript::UStringSourceProviderWithFeedback> provider
88 = QScript::UStringSourceProviderWithFeedback::create(sourceCode, fileName, firstLineNumber, eng);
89 sourceId = provider->asID();
90 JSC::SourceCode source(provider, firstLineNumber); //after construction of SourceCode provider variable will be null.
91 _executable = JSC::EvalExecutable::create(exec, source);
92 engine = eng;
93 engine->registerScriptProgram(this);
94 isCompiled = false;
95 return _executable.get();
96}
97
98void QScriptProgramPrivate::detachFromEngine()
99{
100 _executable.clear();
101 sourceId = -1;
102 isCompiled = false;
103 engine = 0;
104}
105
106/*!
107 Constructs a null QScriptProgram.
108*/
109QScriptProgram::QScriptProgram()
110 : d_ptr(0)
111{
112}
113
114/*!
115 Constructs a new QScriptProgram with the given \a sourceCode, \a
116 fileName and \a firstLineNumber.
117*/
118QScriptProgram::QScriptProgram(const QString &sourceCode,
119 const QString fileName,
120 int firstLineNumber)
121 : d_ptr(new QScriptProgramPrivate(sourceCode, fileName, firstLineNumber))
122{
123}
124
125/*!
126 Constructs a new QScriptProgram that is a copy of \a other.
127*/
128QScriptProgram::QScriptProgram(const QScriptProgram &other)
129 : d_ptr(other.d_ptr)
130{
131}
132
133/*!
134 Destroys this QScriptProgram.
135*/
136QScriptProgram::~QScriptProgram()
137{
138}
139
140/*!
141 Assigns the \a other value to this QScriptProgram.
142*/
143QScriptProgram &QScriptProgram::operator=(const QScriptProgram &other)
144{
145 d_ptr = other.d_ptr;
146 return *this;
147}
148
149/*!
150 Returns true if this QScriptProgram is null; otherwise
151 returns false.
152*/
153bool QScriptProgram::isNull() const
154{
155 Q_D(const QScriptProgram);
156 return (d == 0);
157}
158
159/*!
160 Returns the source code of this program.
161*/
162QString QScriptProgram::sourceCode() const
163{
164 Q_D(const QScriptProgram);
165 if (!d)
166 return QString();
167 return d->sourceCode;
168}
169
170/*!
171 Returns the filename associated with this program.
172*/
173QString QScriptProgram::fileName() const
174{
175 Q_D(const QScriptProgram);
176 if (!d)
177 return QString();
178 return d->fileName;
179}
180
181/*!
182 Returns the line number associated with this program.
183*/
184int QScriptProgram::firstLineNumber() const
185{
186 Q_D(const QScriptProgram);
187 if (!d)
188 return -1;
189 return d->firstLineNumber;
190}
191
192/*!
193 Returns true if this QScriptProgram is equal to \a other;
194 otherwise returns false.
195*/
196bool QScriptProgram::operator==(const QScriptProgram &other) const
197{
198 Q_D(const QScriptProgram);
199 if (d == other.d_func())
200 return true;
201 return (sourceCode() == other.sourceCode())
202 && (fileName() == other.fileName())
203 && (firstLineNumber() == other.firstLineNumber());
204}
205
206/*!
207 Returns true if this QScriptProgram is not equal to \a other;
208 otherwise returns false.
209*/
210bool QScriptProgram::operator!=(const QScriptProgram &other) const
211{
212 return !operator==(other);
213}
214
215QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.