author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <qscriptengine.h> |
|
44 |
||
45 |
#include <QtCore/QFile> |
|
46 |
#include <QtCore/QTextStream> |
|
47 |
#include <QtCore/QStringList> |
|
48 |
#include <QtGui/QApplication> |
|
49 |
||
50 |
#include <stdlib.h> |
|
51 |
||
52 |
#include "bytearrayclass.h" |
|
53 |
||
54 |
static bool wantsToQuit; |
|
55 |
||
56 |
static QScriptValue qtscript_quit(QScriptContext *ctx, QScriptEngine *eng) |
|
57 |
{ |
|
58 |
Q_UNUSED(ctx); |
|
59 |
wantsToQuit = true; |
|
60 |
return eng->undefinedValue(); |
|
61 |
} |
|
62 |
||
63 |
static void interactive(QScriptEngine *eng) |
|
64 |
{ |
|
65 |
QScriptValue global = eng->globalObject(); |
|
66 |
QScriptValue quitFunction = eng->newFunction(qtscript_quit); |
|
67 |
if (!global.property(QLatin1String("exit")).isValid()) |
|
68 |
global.setProperty(QLatin1String("exit"), quitFunction); |
|
69 |
if (!global.property(QLatin1String("quit")).isValid()) |
|
70 |
global.setProperty(QLatin1String("quit"), quitFunction); |
|
71 |
wantsToQuit = false; |
|
72 |
||
73 |
QTextStream qin(stdin, QFile::ReadOnly); |
|
74 |
||
75 |
const char *qscript_prompt = "qs> "; |
|
76 |
const char *dot_prompt = ".... "; |
|
77 |
const char *prompt = qscript_prompt; |
|
78 |
||
79 |
QString code; |
|
80 |
||
81 |
forever { |
|
82 |
QString line; |
|
83 |
||
84 |
printf("%s", prompt); |
|
85 |
fflush(stdout); |
|
86 |
||
87 |
line = qin.readLine(); |
|
88 |
if (line.isNull()) |
|
89 |
break; |
|
90 |
||
91 |
code += line; |
|
92 |
code += QLatin1Char('\n'); |
|
93 |
||
94 |
if (line.trimmed().isEmpty()) { |
|
95 |
continue; |
|
96 |
||
97 |
} else if (! eng->canEvaluate(code)) { |
|
98 |
prompt = dot_prompt; |
|
99 |
||
100 |
} else { |
|
101 |
QScriptValue result = eng->evaluate(code, QLatin1String("typein")); |
|
102 |
||
103 |
code.clear(); |
|
104 |
prompt = qscript_prompt; |
|
105 |
||
106 |
if (! result.isUndefined()) |
|
107 |
fprintf(stderr, "%s\n", qPrintable(result.toString())); |
|
108 |
||
109 |
if (wantsToQuit) |
|
110 |
break; |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
static QScriptValue importExtension(QScriptContext *context, QScriptEngine *engine) |
|
116 |
{ |
|
117 |
return engine->importExtension(context->argument(0).toString()); |
|
118 |
} |
|
119 |
||
120 |
static QScriptValue loadScripts(QScriptContext *context, QScriptEngine *engine) |
|
121 |
{ |
|
122 |
for (int i = 0; i < context->argumentCount(); ++i) { |
|
123 |
QString fileName = context->argument(0).toString(); |
|
124 |
QFile file(fileName); |
|
125 |
if (!file.open(QIODevice::ReadOnly)) |
|
126 |
return context->throwError(QString::fromLatin1("could not open %0 for reading").arg(fileName)); |
|
127 |
QTextStream ts(&file); |
|
128 |
QString contents = ts.readAll(); |
|
129 |
file.close(); |
|
130 |
QScriptContext *pc = context->parentContext(); |
|
131 |
context->setActivationObject(pc->activationObject()); |
|
132 |
context->setThisObject(pc->thisObject()); |
|
133 |
QScriptValue ret = engine->evaluate(contents); |
|
134 |
if (engine->hasUncaughtException()) |
|
135 |
return ret; |
|
136 |
} |
|
137 |
return engine->undefinedValue(); |
|
138 |
} |
|
139 |
||
140 |
int main(int argc, char *argv[]) |
|
141 |
{ |
|
142 |
QApplication *app; |
|
143 |
if (argc >= 2 && !qstrcmp(argv[1], "-tty")) { |
|
144 |
++argv; |
|
145 |
--argc; |
|
146 |
app = new QApplication(argc, argv, QApplication::Tty); |
|
147 |
} else { |
|
148 |
app = new QApplication(argc, argv); |
|
149 |
} |
|
150 |
||
151 |
QScriptEngine *eng = new QScriptEngine(); |
|
152 |
||
153 |
QScriptValue globalObject = eng->globalObject(); |
|
154 |
||
155 |
globalObject.setProperty("load", eng->newFunction(loadScripts, /*length=*/1)); |
|
156 |
||
157 |
{ |
|
158 |
if (!globalObject.property("qt").isObject()) |
|
159 |
globalObject.setProperty("qt", eng->newObject()); |
|
160 |
QScriptValue qscript = eng->newObject(); |
|
161 |
qscript.setProperty("importExtension", eng->newFunction(importExtension)); |
|
162 |
globalObject.property("qt").setProperty("script", qscript); |
|
163 |
} |
|
164 |
||
165 |
ByteArrayClass *byteArrayClass = new ByteArrayClass(eng); |
|
166 |
globalObject.setProperty("ByteArray", byteArrayClass->constructor()); |
|
167 |
||
168 |
if (! *++argv) { |
|
169 |
interactive(eng); |
|
170 |
return EXIT_SUCCESS; |
|
171 |
} |
|
172 |
||
173 |
while (const char *arg = *argv++) { |
|
174 |
QString fn = QString::fromLocal8Bit(arg); |
|
175 |
||
176 |
if (fn == QLatin1String("-i")) { |
|
177 |
interactive(eng); |
|
178 |
break; |
|
179 |
} |
|
180 |
||
181 |
QString contents; |
|
182 |
int lineNumber = 1; |
|
183 |
||
184 |
if (fn == QLatin1String("-")) { |
|
185 |
QTextStream stream(stdin, QFile::ReadOnly); |
|
186 |
contents = stream.readAll(); |
|
187 |
} |
|
188 |
||
189 |
else { |
|
190 |
QFile file(fn); |
|
191 |
||
192 |
if (file.open(QFile::ReadOnly)) { |
|
193 |
QTextStream stream(&file); |
|
194 |
contents = stream.readAll(); |
|
195 |
file.close(); |
|
196 |
||
197 |
// strip off #!/usr/bin/env qscript line |
|
198 |
if (contents.startsWith("#!")) { |
|
199 |
contents.remove(0, contents.indexOf("\n")); |
|
200 |
++lineNumber; |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
if (contents.isEmpty()) |
|
206 |
continue; |
|
207 |
||
208 |
QScriptValue r = eng->evaluate(contents, fn, lineNumber); |
|
209 |
if (eng->hasUncaughtException()) { |
|
210 |
QStringList backtrace = eng->uncaughtExceptionBacktrace(); |
|
211 |
fprintf (stderr, " %s\n%s\n\n", qPrintable(r.toString()), |
|
212 |
qPrintable(backtrace.join("\n"))); |
|
213 |
return EXIT_FAILURE; |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
delete eng; |
|
218 |
delete app; |
|
219 |
||
220 |
return EXIT_SUCCESS; |
|
221 |
} |