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

source code of qtscript/src/script/api/qscriptprogram.cpp