Debian Codegraph @

examples/script/qscript/main.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51
52 #include <qscriptengine.h>cxx . . $ `<file>/include/QtScript/qscriptengine.h`/
53
54 #include <QtCore/QFile>
55 #include <QtCore/QTextStream>
56 #include <QtCore/QStringList>
57 #include <QtWidgets/QApplication>
58
59 #include <stdlib.h>
60
61 #include "bytearrayclass.h"cxx . . $ `<file>/examples/script/customclass/bytearrayclass.h`/
62
63 static bool wantsToQuitcxx . . $ wantsToQuit.;
64
65 static QScriptValuecxx . . $ QScriptValue# qtscript_quitcxx . . $ qtscript_quit(ee586b79a54a8223).(QScriptContextcxx . . $ QScriptContext# *ctx, QScriptEnginecxx . . $ QScriptEngine# *eng)
66 {
67 Q_UNUSEDcxx . . $ `<external>/15045578679651060896/qglobal.h:117:9`!(ctx);
68 wantsToQuitcxx . . $ wantsToQuit. = true;
69 return eng->undefinedValuecxx . . $ QScriptEngine#undefinedValue(68040aa6d67732df).();
70 }
71
72 static void interactivecxx . . $ interactive(d3e0164fcca5dac6).(QScriptEnginecxx . . $ QScriptEngine# *eng)
73 {
74 QScriptValuecxx . . $ QScriptValue# global = eng->globalObjectcxx . . $ QScriptEngine#globalObject(73013aefd0020d47).();
75 QScriptValuecxx . . $ QScriptValue# quitFunction = eng->newFunctioncxx . . $ QScriptEngine#newFunction(126b6e8a6e6977d3).(qtscript_quitcxx . . $ qtscript_quit(ee586b79a54a8223).);
76 if (!global.propertycxx . . $ QScriptValue#property(cc0ed881c2909ea3).(QLatin1Stringcxx . . $ QString#QString(4641beeb24e75d60).("exit")).isValidcxx . . $ QScriptValue#isValid(50ce9a9e25b4a850).())
77 global.setPropertycxx . . $ QScriptValue#setProperty(e166b3a4cfa646).(QLatin1Stringcxx . . $ QString#QString(4641beeb24e75d60).("exit"), quitFunction);
78 if (!global.propertycxx . . $ QScriptValue#property(cc0ed881c2909ea3).(QLatin1Stringcxx . . $ QString#QString(4641beeb24e75d60).("quit")).isValidcxx . . $ QScriptValue#isValid(50ce9a9e25b4a850).())
79 global.setPropertycxx . . $ QScriptValue#setProperty(e166b3a4cfa646).(QLatin1Stringcxx . . $ QString#QString(4641beeb24e75d60).("quit"), quitFunction);
80 wantsToQuitcxx . . $ wantsToQuit. = false;
81
82 QTextStreamcxx . . $ QTextStream# qincxx . . $ QTextStream#QTextStream(65339a794c789c54).(stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!, QFilecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.);
83
84 const char *qscript_prompt = "qs> ";
85 const char *dot_prompt = ".... ";
86 const char *prompt = qscript_prompt;
87
88 QStringcxx . . $ QString# codecxx . . $ QString#QString(ced63f7c635d850d).;
89
90 forevercxx . . $ `<external>/15045578679651060896/qglobal.h:1149:13`! {
91 QStringcxx . . $ QString# linecxx . . $ QString#QString(ced63f7c635d850d).;
92
93 printf("%s", prompt);
94 fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!);
95
96 line =cxx . . $ QString#`operator=`(4f35bf974e7f8568). qin.readLinecxx . . $ QTextStream#readLine(20e0a2766677f583).();
97 if (line.isNullcxx . . $ QString#isNull(50ce9a9e25b4a850).())
98 break;
99
100 code +=cxx . . $ QString#`operator+=`(6a89d0b7f4d2681c). line;
101 code +=cxx . . $ QString#`operator+=`(3a2ffbdf8ecc25e2). QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).('\n');
102
103 if (line.trimmedcxx . . $ QString#trimmed(dca2c5857e9adf82).().isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).()) {
104 continue;
105
106 } else if (! eng->canEvaluatecxx . . $ QScriptEngine#canEvaluate(f3b39adf77d9c36c).(code)) {
107 prompt = dot_prompt;
108
109 } else {
110 QScriptValuecxx . . $ QScriptValue# result = eng->evaluatecxx . . $ QScriptEngine#evaluate(bbfdac3a7d53e517).(code, QLatin1Stringcxx . . $ QString#QString(4641beeb24e75d60).("typein"));
111
112 code.clearcxx . . $ QString#clear(49f6e7a06ebc5aa8).();
113 prompt = qscript_prompt;
114
115 if (! result.isUndefinedcxx . . $ QScriptValue#isUndefined(50ce9a9e25b4a850).())
116 fprintf(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s\n", qPrintablecxx . . $ `<external>/15045578679651060896/qglobal.h:831:11`!(result.toString()));
117
118 if (wantsToQuitcxx . . $ wantsToQuit.)
119 break;
120 }
121 }
122 }
123
124 static QScriptValuecxx . . $ QScriptValue# importExtensioncxx . . $ importExtension(ee586b79a54a8223).(QScriptContextcxx . . $ QScriptContext# *context, QScriptEnginecxx . . $ QScriptEngine# *engine)
125 {
126 return engine->importExtensioncxx . . $ QScriptEngine#importExtension(df477f971a39d688).(context->argumentcxx . . $ QScriptContext#argument(437c608299e89444).(0).toStringcxx . . $ QScriptValue#toString(6ef7b6c27b00b43d).());
127 }
128
129 static QScriptValuecxx . . $ QScriptValue# loadScriptscxx . . $ loadScripts(ee586b79a54a8223).(QScriptContextcxx . . $ QScriptContext# *context, QScriptEnginecxx . . $ QScriptEngine# *engine)
130 {
131 for (int i = 0; i < context->argumentCountcxx . . $ QScriptContext#argumentCount(455f465bc33b4cdf).(); ++i) {
132 QStringcxx . . $ QString# fileName = context->argumentcxx . . $ QScriptContext#argument(437c608299e89444).(i).toStringcxx . . $ QScriptValue#toString(6ef7b6c27b00b43d).();
133 QFilecxx . . $ QFile# filecxx . . $ QFile#QFile(3520e46b33348bf6).(fileName);
134 if (!file.opencxx . . $ QFile#open(dca164408be960c8).(QIODevicecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.))
135 return context->throwErrorcxx . . $ QScriptContext#throwError(df477f971a39d688).(QStringcxx . . $ QString#::fromLatin1cxx . . $ QString#fromLatin1(5ac70cc0fa2a874e).("could not open %0 for reading").argcxx . . $ QString#arg(bc823072288cbbb5).(fileName));
136 QTextStreamcxx . . $ QTextStream# tscxx . . $ QTextStream#QTextStream(bebe8f2dee7f9e86).(&file);
137 QStringcxx . . $ QString# contents = ts.readAllcxx . . $ QTextStream#readAll(4365c3963150523e).();
138 file.closecxx . . $ QFileDevice#close(49f6e7a06ebc5aa8).();
139 QScriptContextcxx . . $ QScriptContext# *pc = context->parentContextcxx . . $ QScriptContext#parentContext(3b7b8e48bf7da82e).();
140 context->setActivationObjectcxx . . $ QScriptContext#setActivationObject(6b35c8bde2804237).(pc->activationObjectcxx . . $ QScriptContext#activationObject(73013aefd0020d47).());
141 context->setThisObjectcxx . . $ QScriptContext#setThisObject(6b35c8bde2804237).(pc->thisObjectcxx . . $ QScriptContext#thisObject(73013aefd0020d47).());
142 QScriptValuecxx . . $ QScriptValue# ret = engine->evaluatecxx . . $ QScriptEngine#evaluate(bbfdac3a7d53e517).(contents);
143 if (engine->hasUncaughtExceptioncxx . . $ QScriptEngine#hasUncaughtException(50ce9a9e25b4a850).())
144 return retcxx . . $ QScriptValue#QScriptValue(6b35c8bde2804237).;
145 }
146 return engine->undefinedValuecxx . . $ QScriptEngine#undefinedValue(68040aa6d67732df).();
147 }
148
149 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
150 {
151 QCoreApplicationcxx . . $ QCoreApplication# *app = new QCoreApplicationcxx . . $ QCoreApplication#QCoreApplication(6e9b1c7df49e191d).(argc, argv);
152 QScriptEnginecxx . . $ QScriptEngine# *eng = new QScriptEnginecxx . . $ QScriptEngine#QScriptEngine(49f6e7a06ebc5aa8).();
153
154 QScriptValuecxx . . $ QScriptValue# globalObject = eng->globalObjectcxx . . $ QScriptEngine#globalObject(73013aefd0020d47).();
155
156 globalObject.setPropertycxx . . $ QScriptValue#setProperty(e166b3a4cfa646).("load"cxx . . $ QString#QString(85c52e162fed56f9)., eng->newFunctioncxx . . $ QScriptEngine#newFunction(126b6e8a6e6977d3).(loadScriptscxx . . $ loadScripts(ee586b79a54a8223)., /*length=*/1));
157
158 {
159 if (!globalObject.propertycxx . . $ QScriptValue#property(cc0ed881c2909ea3).("qt"cxx . . $ QString#QString(85c52e162fed56f9).).isObjectcxx . . $ QScriptValue#isObject(50ce9a9e25b4a850).())
160 globalObject.setPropertycxx . . $ QScriptValue#setProperty(e166b3a4cfa646).("qt"cxx . . $ QString#QString(85c52e162fed56f9)., eng->newObjectcxx . . $ QScriptEngine#newObject(68040aa6d67732df).());
161 QScriptValuecxx . . $ QScriptValue# qscript = eng->newObjectcxx . . $ QScriptEngine#newObject(68040aa6d67732df).();
162 qscript.setPropertycxx . . $ QScriptValue#setProperty(e166b3a4cfa646).("importExtension"cxx . . $ QString#QString(85c52e162fed56f9)., eng->newFunctioncxx . . $ QScriptEngine#newFunction(126b6e8a6e6977d3).(importExtensioncxx . . $ importExtension(ee586b79a54a8223).));
163 globalObject.propertycxx . . $ QScriptValue#property(cc0ed881c2909ea3).("qt"cxx . . $ QString#QString(85c52e162fed56f9).).setPropertycxx . . $ QScriptValue#setProperty(e166b3a4cfa646).("script"cxx . . $ QString#QString(85c52e162fed56f9)., qscript);
164 }
165
166 ByteArrayClasscxx . . $ ByteArrayClass# *byteArrayClass = new ByteArrayClasscxx . . $ ByteArrayClass#(eng);
167 globalObject.setPropertycxx . . $ QScriptValue#setProperty(e166b3a4cfa646).("ByteArray"cxx . . $ QString#QString(85c52e162fed56f9)., byteArrayClass->constructorcxx . . $ ByteArrayClass#constructor(68040aa6d67732df).());
168
169 if (! *++argv) {
170 interactivecxx . . $ interactive(d3e0164fcca5dac6).(eng);
171 return EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!;
172 }
173
174 while (const char *arg = *argv++) {
175 QStringcxx . . $ QString# fn = QStringcxx . . $ QString#::fromLocal8Bitcxx . . $ QString#fromLocal8Bit(5ac70cc0fa2a874e).(arg);
176
177 if (fn ==cxx . . $ QString#`operator==`(b3b98e09a392cdf9). QLatin1Stringcxx . . $ QLatin1String#QLatin1String(aac1607cfe1c1740).("-i")) {
178 interactivecxx . . $ interactive(d3e0164fcca5dac6).(eng);
179 break;
180 }
181
182 QStringcxx . . $ QString# contentscxx . . $ QString#QString(ced63f7c635d850d).;
183 int lineNumber = 1;
184
185 if (fn ==cxx . . $ QString#`operator==`(b3b98e09a392cdf9). QLatin1Stringcxx . . $ QLatin1String#QLatin1String(aac1607cfe1c1740).("-")) {
186 QTextStreamcxx . . $ QTextStream# streamcxx . . $ QTextStream#QTextStream(65339a794c789c54).(stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!, QFilecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.);
187 contents =cxx . . $ QString#`operator=`(4f35bf974e7f8568). stream.readAllcxx . . $ QTextStream#readAll(4365c3963150523e).();
188 }
189
190 else {
191 QFilecxx . . $ QFile# filecxx . . $ QFile#QFile(3520e46b33348bf6).(fn);
192
193 if (file.opencxx . . $ QFile#open(dca164408be960c8).(QFilecxx . . $ QFlags#QFlags(93d577a3e9528307).::ReadOnlycxx . . $ QIODevice#ReadOnly.)) {
194 QTextStreamcxx . . $ QTextStream# streamcxx . . $ QTextStream#QTextStream(bebe8f2dee7f9e86).(&file);
195 contents =cxx . . $ QString#`operator=`(4f35bf974e7f8568). stream.readAllcxx . . $ QTextStream#readAll(4365c3963150523e).();
196 file.closecxx . . $ QFileDevice#close(49f6e7a06ebc5aa8).();
197
198 // strip off #!/usr/bin/env qscript line
199 if (contents.startsWithcxx . . $ QString#startsWith(8cce4688c16f48f3).("#!"cxx . . $ QString#QString(85c52e162fed56f9).)) {
200 contents.removecxx . . $ QString#remove(6483560ce2afa901).(0, contents.indexOfcxx . . $ QString#indexOf(a1e52806cd31c200).("\n"cxx . . $ QString#QString(85c52e162fed56f9).));
201 ++lineNumber;
202 }
203 }
204 }
205
206 if (contents.isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).())
207 continue;
208
209 QScriptValuecxx . . $ QScriptValue# r = eng->evaluatecxx . . $ QScriptEngine#evaluate(bbfdac3a7d53e517).(contents, fn, lineNumber);
210 if (eng->hasUncaughtExceptioncxx . . $ QScriptEngine#hasUncaughtException(50ce9a9e25b4a850).()) {
211 QStringListcxx . . $ QStringList# backtrace = eng->uncaughtExceptionBacktracecxx . . $ QScriptEngine#uncaughtExceptionBacktrace(e1dbd5c4feadc8f5).();
212 fprintf (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " %s\n%s\n\n", qPrintablecxx . . $ `<external>/15045578679651060896/qglobal.h:831:11`!(r.toString()),
213 qPrintablecxx . . $ `<external>/15045578679651060896/qglobal.h:831:11`!(backtrace.join("\n")));
214 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!;
215 }
216 }
217
218 delete eng;
219 delete app;
220
221 return EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:93:9`!;
222 }