|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
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 qt-info@nokia.com. |
|
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 |
|
32 QT_BEGIN_NAMESPACE |
|
33 |
|
34 /*! |
|
35 \internal |
|
36 |
|
37 \since 4.6 |
|
38 \class QScriptProgram |
|
39 |
|
40 \brief The QScriptProgram class encapsulates a Qt Script program. |
|
41 |
|
42 \ingroup script |
|
43 |
|
44 QScriptProgram retains the compiled representation of the script if |
|
45 possible. Thus, QScriptProgram can be used to evaluate the same |
|
46 script multiple times more efficiently. |
|
47 |
|
48 \code |
|
49 QScriptEngine engine; |
|
50 QScriptProgram program("1 + 2"); |
|
51 QScriptValue result = engine.evaluate(program); |
|
52 \endcode |
|
53 */ |
|
54 |
|
55 QScriptProgramPrivate::QScriptProgramPrivate(const QString &src, |
|
56 const QString &fn, |
|
57 int ln) |
|
58 : sourceCode(src), fileName(fn), firstLineNumber(ln), |
|
59 engine(0), _executable(0), sourceId(-1), isCompiled(false) |
|
60 { |
|
61 ref = 0; |
|
62 } |
|
63 |
|
64 QScriptProgramPrivate::~QScriptProgramPrivate() |
|
65 { |
|
66 delete _executable; |
|
67 } |
|
68 |
|
69 QScriptProgramPrivate *QScriptProgramPrivate::get(const QScriptProgram &q) |
|
70 { |
|
71 return const_cast<QScriptProgramPrivate*>(q.d_func()); |
|
72 } |
|
73 |
|
74 JSC::EvalExecutable *QScriptProgramPrivate::executable(JSC::ExecState *exec, |
|
75 QScriptEnginePrivate *eng) |
|
76 { |
|
77 if (_executable) { |
|
78 if (eng == engine) |
|
79 return _executable; |
|
80 delete _executable; |
|
81 } |
|
82 WTF::PassRefPtr<QScript::UStringSourceProviderWithFeedback> provider |
|
83 = QScript::UStringSourceProviderWithFeedback::create(sourceCode, fileName, firstLineNumber, eng); |
|
84 sourceId = provider->asID(); |
|
85 JSC::SourceCode source(provider, firstLineNumber); //after construction of SourceCode provider variable will be null. |
|
86 _executable = new JSC::EvalExecutable(exec, source); |
|
87 engine = eng; |
|
88 isCompiled = false; |
|
89 return _executable; |
|
90 } |
|
91 |
|
92 /*! |
|
93 Constructs a null QScriptProgram. |
|
94 */ |
|
95 QScriptProgram::QScriptProgram() |
|
96 : d_ptr(0) |
|
97 { |
|
98 } |
|
99 |
|
100 /*! |
|
101 Constructs a new QScriptProgram with the given \a sourceCode, \a |
|
102 fileName and \a firstLineNumber. |
|
103 */ |
|
104 QScriptProgram::QScriptProgram(const QString &sourceCode, |
|
105 const QString fileName, |
|
106 int firstLineNumber) |
|
107 : d_ptr(new QScriptProgramPrivate(sourceCode, fileName, firstLineNumber)) |
|
108 { |
|
109 } |
|
110 |
|
111 /*! |
|
112 Constructs a new QScriptProgram that is a copy of \a other. |
|
113 */ |
|
114 QScriptProgram::QScriptProgram(const QScriptProgram &other) |
|
115 : d_ptr(other.d_ptr) |
|
116 { |
|
117 } |
|
118 |
|
119 /*! |
|
120 Destroys this QScriptProgram. |
|
121 */ |
|
122 QScriptProgram::~QScriptProgram() |
|
123 { |
|
124 Q_D(QScriptProgram); |
|
125 // if (d->engine && (d->ref == 1)) |
|
126 // d->engine->unregisterScriptProgram(d); |
|
127 } |
|
128 |
|
129 /*! |
|
130 Assigns the \a other value to this QScriptProgram. |
|
131 */ |
|
132 QScriptProgram &QScriptProgram::operator=(const QScriptProgram &other) |
|
133 { |
|
134 // if (d_func() && d_func()->engine && (d_func()->ref == 1)) |
|
135 // d_func()->engine->unregisterScriptProgram(d_func()); |
|
136 // } |
|
137 d_ptr = other.d_ptr; |
|
138 return *this; |
|
139 } |
|
140 |
|
141 /*! |
|
142 Returns true if this QScriptProgram is null; otherwise |
|
143 returns false. |
|
144 */ |
|
145 bool QScriptProgram::isNull() const |
|
146 { |
|
147 Q_D(const QScriptProgram); |
|
148 return (d == 0); |
|
149 } |
|
150 |
|
151 /*! |
|
152 Returns the source code of this program. |
|
153 */ |
|
154 QString QScriptProgram::sourceCode() const |
|
155 { |
|
156 Q_D(const QScriptProgram); |
|
157 if (!d) |
|
158 return QString(); |
|
159 return d->sourceCode; |
|
160 } |
|
161 |
|
162 /*! |
|
163 Returns the filename associated with this program. |
|
164 */ |
|
165 QString QScriptProgram::fileName() const |
|
166 { |
|
167 Q_D(const QScriptProgram); |
|
168 if (!d) |
|
169 return QString(); |
|
170 return d->fileName; |
|
171 } |
|
172 |
|
173 /*! |
|
174 Returns the line number associated with this program. |
|
175 */ |
|
176 int QScriptProgram::firstLineNumber() const |
|
177 { |
|
178 Q_D(const QScriptProgram); |
|
179 if (!d) |
|
180 return -1; |
|
181 return d->firstLineNumber; |
|
182 } |
|
183 |
|
184 /*! |
|
185 Returns true if this QScriptProgram is equal to \a other; |
|
186 otherwise returns false. |
|
187 */ |
|
188 bool QScriptProgram::operator==(const QScriptProgram &other) const |
|
189 { |
|
190 Q_D(const QScriptProgram); |
|
191 if (d == other.d_func()) |
|
192 return true; |
|
193 return (sourceCode() == other.sourceCode()) |
|
194 && (fileName() == other.fileName()) |
|
195 && (firstLineNumber() == other.firstLineNumber()); |
|
196 } |
|
197 |
|
198 /*! |
|
199 Returns true if this QScriptProgram is not equal to \a other; |
|
200 otherwise returns false. |
|
201 */ |
|
202 bool QScriptProgram::operator!=(const QScriptProgram &other) const |
|
203 { |
|
204 return !operator==(other); |
|
205 } |
|
206 |
|
207 QT_END_NAMESPACE |