author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
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 test suite 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 <QtTest/QtTest> |
|
44 |
||
45 |
#include <qscriptengine.h> |
|
46 |
#include <qscriptengineagent.h> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
47 |
#include <qscriptprogram.h> |
0 | 48 |
#include <qscriptvalueiterator.h> |
49 |
#include <qgraphicsitem.h> |
|
50 |
#include <qstandarditemmodel.h> |
|
51 |
#include <QtCore/qnumeric.h> |
|
52 |
#include <stdlib.h> |
|
53 |
||
54 |
Q_DECLARE_METATYPE(QList<int>) |
|
55 |
Q_DECLARE_METATYPE(QObjectList) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
Q_DECLARE_METATYPE(QScriptProgram) |
0 | 57 |
|
58 |
//TESTED_CLASS= |
|
59 |
//TESTED_FILES= |
|
60 |
||
61 |
#if defined(Q_OS_SYMBIAN) |
|
62 |
# define STRINGIFY(x) #x |
|
63 |
# define TOSTRING(x) STRINGIFY(x) |
|
64 |
# define SRCDIR "C:/Private/" TOSTRING(SYMBIAN_SRCDIR_UID) |
|
65 |
#endif |
|
66 |
||
67 |
// The JavaScriptCore GC marks the C stack. To try to ensure that there is |
|
68 |
// no JSObject* left in stack memory by the compiler, we call this function |
|
69 |
// to zap some bytes of memory before calling collectGarbage(). |
|
70 |
static void zapSomeStack() |
|
71 |
{ |
|
72 |
char buf[4096]; |
|
73 |
memset(buf, 0, sizeof(buf)); |
|
74 |
} |
|
75 |
||
76 |
static void collectGarbage_helper(QScriptEngine &eng) |
|
77 |
{ |
|
78 |
zapSomeStack(); |
|
79 |
eng.collectGarbage(); |
|
80 |
} |
|
81 |
||
82 |
class tst_QScriptEngine : public QObject |
|
83 |
{ |
|
84 |
Q_OBJECT |
|
85 |
||
86 |
public: |
|
87 |
tst_QScriptEngine(); |
|
88 |
virtual ~tst_QScriptEngine(); |
|
89 |
||
90 |
private slots: |
|
91 |
void constructWithParent(); |
|
92 |
void currentContext(); |
|
93 |
void pushPopContext(); |
|
94 |
void getSetDefaultPrototype(); |
|
95 |
void newFunction(); |
|
96 |
void newObject(); |
|
97 |
void newArray(); |
|
98 |
void newVariant(); |
|
99 |
void newRegExp(); |
|
100 |
void newDate(); |
|
101 |
void newQObject(); |
|
102 |
void newQMetaObject(); |
|
103 |
void newActivationObject(); |
|
104 |
void getSetGlobalObject(); |
|
105 |
void globalObjectProperties(); |
|
106 |
void globalObjectGetterSetterProperty(); |
|
107 |
void builtinFunctionNames_data(); |
|
108 |
void builtinFunctionNames(); |
|
109 |
void checkSyntax_data(); |
|
110 |
void checkSyntax(); |
|
111 |
void canEvaluate_data(); |
|
112 |
void canEvaluate(); |
|
113 |
void evaluate_data(); |
|
114 |
void evaluate(); |
|
115 |
void nestedEvaluate(); |
|
116 |
void uncaughtException(); |
|
117 |
void errorMessage_QT679(); |
|
118 |
void valueConversion(); |
|
119 |
void importExtension(); |
|
120 |
void infiniteRecursion(); |
|
121 |
void castWithPrototypeChain(); |
|
122 |
void castWithMultipleInheritance(); |
|
123 |
void collectGarbage(); |
|
124 |
void gcWithNestedDataStructure(); |
|
125 |
void processEventsWhileRunning(); |
|
126 |
void throwErrorFromProcessEvents(); |
|
127 |
void stacktrace(); |
|
128 |
void numberParsing_data(); |
|
129 |
void numberParsing(); |
|
130 |
void automaticSemicolonInsertion(); |
|
131 |
void abortEvaluation(); |
|
132 |
void isEvaluating(); |
|
133 |
void printFunctionWithCustomHandler(); |
|
134 |
void printThrowsException(); |
|
135 |
void errorConstructors(); |
|
136 |
void argumentsProperty(); |
|
137 |
void numberClass(); |
|
138 |
void forInStatement(); |
|
139 |
void functionExpression(); |
|
140 |
void stringObjects(); |
|
141 |
void getterSetterThisObject(); |
|
142 |
void continueInSwitch(); |
|
143 |
void readOnlyPrototypeProperty(); |
|
144 |
void toObject(); |
|
145 |
void reservedWords_data(); |
|
146 |
void reservedWords(); |
|
147 |
void futureReservedWords_data(); |
|
148 |
void futureReservedWords(); |
|
149 |
void throwInsideWithStatement(); |
|
150 |
void getSetAgent(); |
|
151 |
void reentrancy(); |
|
152 |
void incDecNonObjectProperty(); |
|
153 |
void installTranslatorFunctions(); |
|
154 |
void functionScopes(); |
|
155 |
void nativeFunctionScopes(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
void evaluateProgram(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
void collectGarbageAfterConnect(); |
0 | 158 |
|
159 |
void qRegExpInport_data(); |
|
160 |
void qRegExpInport(); |
|
161 |
}; |
|
162 |
||
163 |
tst_QScriptEngine::tst_QScriptEngine() |
|
164 |
{ |
|
165 |
} |
|
166 |
||
167 |
tst_QScriptEngine::~tst_QScriptEngine() |
|
168 |
{ |
|
169 |
} |
|
170 |
||
171 |
void tst_QScriptEngine::constructWithParent() |
|
172 |
{ |
|
173 |
QPointer<QScriptEngine> ptr; |
|
174 |
{ |
|
175 |
QObject obj; |
|
176 |
QScriptEngine *engine = new QScriptEngine(&obj); |
|
177 |
ptr = engine; |
|
178 |
} |
|
179 |
QVERIFY(ptr == 0); |
|
180 |
} |
|
181 |
||
182 |
void tst_QScriptEngine::currentContext() |
|
183 |
{ |
|
184 |
QScriptEngine eng; |
|
185 |
QScriptContext *globalCtx = eng.currentContext(); |
|
186 |
QVERIFY(globalCtx != 0); |
|
187 |
QVERIFY(globalCtx->parentContext() == 0); |
|
188 |
QCOMPARE(globalCtx->engine(), &eng); |
|
189 |
QCOMPARE(globalCtx->argumentCount(), 0); |
|
190 |
QCOMPARE(globalCtx->backtrace().size(), 1); |
|
191 |
QVERIFY(!globalCtx->isCalledAsConstructor()); |
|
192 |
QVERIFY(!globalCtx->callee().isValid()); |
|
193 |
QCOMPARE(globalCtx->state(), QScriptContext::NormalState); |
|
194 |
QVERIFY(globalCtx->thisObject().strictlyEquals(eng.globalObject())); |
|
195 |
QVERIFY(globalCtx->activationObject().strictlyEquals(eng.globalObject())); |
|
196 |
QVERIFY(globalCtx->argumentsObject().isObject()); |
|
197 |
} |
|
198 |
||
199 |
void tst_QScriptEngine::pushPopContext() |
|
200 |
{ |
|
201 |
QScriptEngine eng; |
|
202 |
QScriptContext *globalCtx = eng.currentContext(); |
|
203 |
QScriptContext *ctx = eng.pushContext(); |
|
204 |
QVERIFY(ctx != 0); |
|
205 |
QCOMPARE(ctx->parentContext(), globalCtx); |
|
206 |
QVERIFY(!ctx->isCalledAsConstructor()); |
|
207 |
QVERIFY(!ctx->callee().isValid()); |
|
208 |
QVERIFY(ctx->thisObject().strictlyEquals(eng.globalObject())); |
|
209 |
QCOMPARE(ctx->argumentCount(), 0); |
|
210 |
QCOMPARE(ctx->backtrace().size(), 2); |
|
211 |
QCOMPARE(ctx->engine(), &eng); |
|
212 |
QCOMPARE(ctx->state(), QScriptContext::NormalState); |
|
213 |
QVERIFY(ctx->activationObject().isObject()); |
|
214 |
QVERIFY(ctx->argumentsObject().isObject()); |
|
215 |
||
216 |
QScriptContext *ctx2 = eng.pushContext(); |
|
217 |
QVERIFY(ctx2 != 0); |
|
218 |
QCOMPARE(ctx2->parentContext(), ctx); |
|
219 |
QVERIFY(!ctx2->activationObject().strictlyEquals(ctx->activationObject())); |
|
220 |
QVERIFY(!ctx2->argumentsObject().strictlyEquals(ctx->argumentsObject())); |
|
221 |
||
222 |
eng.popContext(); |
|
223 |
eng.popContext(); |
|
224 |
QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::popContext() doesn't match with pushContext()"); |
|
225 |
eng.popContext(); // ignored |
|
226 |
QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::popContext() doesn't match with pushContext()"); |
|
227 |
eng.popContext(); // ignored |
|
228 |
} |
|
229 |
||
230 |
static QScriptValue myFunction(QScriptContext *, QScriptEngine *eng) |
|
231 |
{ |
|
232 |
return eng->nullValue(); |
|
233 |
} |
|
234 |
||
235 |
static QScriptValue myFunctionWithVoidArg(QScriptContext *, QScriptEngine *eng, void *) |
|
236 |
{ |
|
237 |
return eng->nullValue(); |
|
238 |
} |
|
239 |
||
240 |
static QScriptValue myThrowingFunction(QScriptContext *ctx, QScriptEngine *) |
|
241 |
{ |
|
242 |
return ctx->throwError("foo"); |
|
243 |
} |
|
244 |
||
245 |
void tst_QScriptEngine::newFunction() |
|
246 |
{ |
|
247 |
QScriptEngine eng; |
|
248 |
{ |
|
249 |
QScriptValue fun = eng.newFunction(myFunction); |
|
250 |
QCOMPARE(fun.isValid(), true); |
|
251 |
QCOMPARE(fun.isFunction(), true); |
|
252 |
QCOMPARE(fun.isObject(), true); |
|
253 |
QCOMPARE(fun.scriptClass(), (QScriptClass*)0); |
|
254 |
// a prototype property is automatically constructed |
|
255 |
{ |
|
256 |
QScriptValue prot = fun.property("prototype", QScriptValue::ResolveLocal); |
|
257 |
QVERIFY(prot.isObject()); |
|
258 |
QVERIFY(prot.property("constructor").strictlyEquals(fun)); |
|
259 |
QCOMPARE(fun.propertyFlags("prototype"), QScriptValue::Undeletable); |
|
260 |
QCOMPARE(prot.propertyFlags("constructor"), QScriptValue::Undeletable | QScriptValue::SkipInEnumeration); |
|
261 |
} |
|
262 |
// prototype should be Function.prototype |
|
263 |
QCOMPARE(fun.prototype().isValid(), true); |
|
264 |
QCOMPARE(fun.prototype().isFunction(), true); |
|
265 |
QCOMPARE(fun.prototype().strictlyEquals(eng.evaluate("Function.prototype")), true); |
|
266 |
||
267 |
QCOMPARE(fun.call().isNull(), true); |
|
268 |
QCOMPARE(fun.construct().isObject(), true); |
|
269 |
} |
|
270 |
||
271 |
// the overload that takes a void* |
|
272 |
{ |
|
273 |
QScriptValue fun = eng.newFunction(myFunctionWithVoidArg, (void*)this); |
|
274 |
QVERIFY(fun.isFunction()); |
|
275 |
QCOMPARE(fun.scriptClass(), (QScriptClass*)0); |
|
276 |
// a prototype property is automatically constructed |
|
277 |
{ |
|
278 |
QScriptValue prot = fun.property("prototype", QScriptValue::ResolveLocal); |
|
279 |
QVERIFY(prot.isObject()); |
|
280 |
QVERIFY(prot.property("constructor").strictlyEquals(fun)); |
|
281 |
QCOMPARE(fun.propertyFlags("prototype"), QScriptValue::Undeletable); |
|
282 |
QCOMPARE(prot.propertyFlags("constructor"), QScriptValue::Undeletable | QScriptValue::SkipInEnumeration); |
|
283 |
} |
|
284 |
// prototype should be Function.prototype |
|
285 |
QCOMPARE(fun.prototype().isValid(), true); |
|
286 |
QCOMPARE(fun.prototype().isFunction(), true); |
|
287 |
QCOMPARE(fun.prototype().strictlyEquals(eng.evaluate("Function.prototype")), true); |
|
288 |
||
289 |
QCOMPARE(fun.call().isNull(), true); |
|
290 |
QCOMPARE(fun.construct().isObject(), true); |
|
291 |
} |
|
292 |
||
293 |
// the overload that takes a prototype |
|
294 |
{ |
|
295 |
QScriptValue proto = eng.newObject(); |
|
296 |
QScriptValue fun = eng.newFunction(myFunction, proto); |
|
297 |
QCOMPARE(fun.isValid(), true); |
|
298 |
QCOMPARE(fun.isFunction(), true); |
|
299 |
QCOMPARE(fun.isObject(), true); |
|
300 |
// internal prototype should be Function.prototype |
|
301 |
QCOMPARE(fun.prototype().isValid(), true); |
|
302 |
QCOMPARE(fun.prototype().isFunction(), true); |
|
303 |
QCOMPARE(fun.prototype().strictlyEquals(eng.evaluate("Function.prototype")), true); |
|
304 |
// public prototype should be the one we passed |
|
305 |
QCOMPARE(fun.property("prototype").strictlyEquals(proto), true); |
|
306 |
QCOMPARE(fun.propertyFlags("prototype"), QScriptValue::Undeletable); |
|
307 |
QCOMPARE(proto.property("constructor").strictlyEquals(fun), true); |
|
308 |
QCOMPARE(proto.propertyFlags("constructor"), |
|
309 |
QScriptValue::Undeletable | QScriptValue::SkipInEnumeration); |
|
310 |
||
311 |
QCOMPARE(fun.call().isNull(), true); |
|
312 |
QCOMPARE(fun.construct().isObject(), true); |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
void tst_QScriptEngine::newObject() |
|
317 |
{ |
|
318 |
QScriptEngine eng; |
|
319 |
QScriptValue object = eng.newObject(); |
|
320 |
QCOMPARE(object.isValid(), true); |
|
321 |
QCOMPARE(object.isObject(), true); |
|
322 |
QCOMPARE(object.isFunction(), false); |
|
323 |
QCOMPARE(object.scriptClass(), (QScriptClass*)0); |
|
324 |
// prototype should be Object.prototype |
|
325 |
QCOMPARE(object.prototype().isValid(), true); |
|
326 |
QCOMPARE(object.prototype().isObject(), true); |
|
327 |
QCOMPARE(object.prototype().strictlyEquals(eng.evaluate("Object.prototype")), true); |
|
328 |
} |
|
329 |
||
330 |
void tst_QScriptEngine::newArray() |
|
331 |
{ |
|
332 |
QScriptEngine eng; |
|
333 |
QScriptValue array = eng.newArray(); |
|
334 |
QCOMPARE(array.isValid(), true); |
|
335 |
QCOMPARE(array.isArray(), true); |
|
336 |
QCOMPARE(array.isObject(), true); |
|
337 |
QVERIFY(!array.isFunction()); |
|
338 |
QCOMPARE(array.scriptClass(), (QScriptClass*)0); |
|
339 |
// prototype should be Array.prototype |
|
340 |
QCOMPARE(array.prototype().isValid(), true); |
|
341 |
QCOMPARE(array.prototype().isArray(), true); |
|
342 |
QCOMPARE(array.prototype().strictlyEquals(eng.evaluate("Array.prototype")), true); |
|
343 |
||
344 |
// task 218092 |
|
345 |
{ |
|
346 |
QScriptValue ret = eng.evaluate("[].splice(0, 0, 'a')"); |
|
347 |
QVERIFY(ret.isArray()); |
|
348 |
QCOMPARE(ret.property("length").toInt32(), 0); |
|
349 |
} |
|
350 |
{ |
|
351 |
QScriptValue ret = eng.evaluate("['a'].splice(0, 1, 'b')"); |
|
352 |
QVERIFY(ret.isArray()); |
|
353 |
QCOMPARE(ret.property("length").toInt32(), 1); |
|
354 |
} |
|
355 |
{ |
|
356 |
QScriptValue ret = eng.evaluate("['a', 'b'].splice(0, 1, 'c')"); |
|
357 |
QVERIFY(ret.isArray()); |
|
358 |
QCOMPARE(ret.property("length").toInt32(), 1); |
|
359 |
} |
|
360 |
{ |
|
361 |
QScriptValue ret = eng.evaluate("['a', 'b', 'c'].splice(0, 2, 'd')"); |
|
362 |
QVERIFY(ret.isArray()); |
|
363 |
QCOMPARE(ret.property("length").toInt32(), 2); |
|
364 |
} |
|
365 |
{ |
|
366 |
QScriptValue ret = eng.evaluate("['a', 'b', 'c'].splice(1, 2, 'd', 'e', 'f')"); |
|
367 |
QVERIFY(ret.isArray()); |
|
368 |
QCOMPARE(ret.property("length").toInt32(), 2); |
|
369 |
} |
|
370 |
||
371 |
// task 233836 |
|
372 |
{ |
|
373 |
QScriptValue ret = eng.evaluate("a = new Array(4294967295); a.push('foo')"); |
|
374 |
QVERIFY(ret.isNumber()); |
|
375 |
QCOMPARE(ret.toInt32(), 0); |
|
376 |
QCOMPARE(eng.evaluate("a[4294967295]").toString(), QString::fromLatin1("foo")); |
|
377 |
} |
|
378 |
{ |
|
379 |
QScriptValue ret = eng.newArray(0xFFFFFFFF); |
|
380 |
QCOMPARE(ret.property("length").toUInt32(), uint(0xFFFFFFFF)); |
|
381 |
ret.setProperty(0xFFFFFFFF, 123); |
|
382 |
QCOMPARE(ret.property("length").toUInt32(), uint(0xFFFFFFFF)); |
|
383 |
QVERIFY(ret.property(0xFFFFFFFF).isNumber()); |
|
384 |
QCOMPARE(ret.property(0xFFFFFFFF).toInt32(), 123); |
|
385 |
ret.setProperty(123, 456); |
|
386 |
QCOMPARE(ret.property("length").toUInt32(), uint(0xFFFFFFFF)); |
|
387 |
QVERIFY(ret.property(123).isNumber()); |
|
388 |
QCOMPARE(ret.property(123).toInt32(), 456); |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
void tst_QScriptEngine::newVariant() |
|
393 |
{ |
|
394 |
QScriptEngine eng; |
|
395 |
{ |
|
396 |
QScriptValue opaque = eng.newVariant(QVariant()); |
|
397 |
QCOMPARE(opaque.isValid(), true); |
|
398 |
QCOMPARE(opaque.isVariant(), true); |
|
399 |
QVERIFY(!opaque.isFunction()); |
|
400 |
QCOMPARE(opaque.isObject(), true); |
|
401 |
QCOMPARE(opaque.prototype().isValid(), true); |
|
402 |
QCOMPARE(opaque.prototype().isVariant(), true); |
|
403 |
QVERIFY(opaque.property("valueOf").call(opaque).isUndefined()); |
|
404 |
} |
|
405 |
// default prototype should be set automatically |
|
406 |
{ |
|
407 |
QScriptValue proto = eng.newObject(); |
|
408 |
eng.setDefaultPrototype(qMetaTypeId<QString>(), proto); |
|
409 |
QScriptValue ret = eng.newVariant(QVariant(QString::fromLatin1("hello"))); |
|
410 |
QVERIFY(ret.isVariant()); |
|
411 |
QCOMPARE(ret.scriptClass(), (QScriptClass*)0); |
|
412 |
QVERIFY(ret.prototype().strictlyEquals(proto)); |
|
413 |
eng.setDefaultPrototype(qMetaTypeId<QString>(), QScriptValue()); |
|
414 |
QScriptValue ret2 = eng.newVariant(QVariant(QString::fromLatin1("hello"))); |
|
415 |
QVERIFY(ret2.isVariant()); |
|
416 |
QVERIFY(!ret2.prototype().strictlyEquals(proto)); |
|
417 |
} |
|
418 |
// "promote" plain object to variant |
|
419 |
{ |
|
420 |
QScriptValue object = eng.newObject(); |
|
421 |
object.setProperty("foo", eng.newObject()); |
|
422 |
object.setProperty("bar", object.property("foo")); |
|
423 |
QVERIFY(object.property("foo").isObject()); |
|
424 |
QVERIFY(!object.property("foo").isVariant()); |
|
425 |
QScriptValue originalProto = object.property("foo").prototype(); |
|
426 |
QScriptValue ret = eng.newVariant(object.property("foo"), QVariant(123)); |
|
427 |
QVERIFY(ret.isValid()); |
|
428 |
QVERIFY(ret.strictlyEquals(object.property("foo"))); |
|
429 |
QVERIFY(ret.isVariant()); |
|
430 |
QVERIFY(object.property("foo").isVariant()); |
|
431 |
QVERIFY(object.property("bar").isVariant()); |
|
432 |
QCOMPARE(ret.toVariant(), QVariant(123)); |
|
433 |
QVERIFY(ret.prototype().strictlyEquals(originalProto)); |
|
434 |
} |
|
435 |
// replace value of existing object |
|
436 |
{ |
|
437 |
QScriptValue object = eng.newVariant(QVariant(123)); |
|
438 |
QScriptValue ret = eng.newVariant(object, QVariant(456)); |
|
439 |
QVERIFY(ret.isValid()); |
|
440 |
QVERIFY(ret.strictlyEquals(object)); |
|
441 |
QVERIFY(ret.isVariant()); |
|
442 |
QCOMPARE(ret.toVariant(), QVariant(456)); |
|
443 |
} |
|
444 |
||
445 |
// valueOf() and toString() |
|
446 |
{ |
|
447 |
QScriptValue object = eng.newVariant(QVariant(123)); |
|
448 |
QScriptValue value = object.property("valueOf").call(object); |
|
449 |
QVERIFY(value.isNumber()); |
|
450 |
QCOMPARE(value.toInt32(), 123); |
|
451 |
QCOMPARE(object.toString(), QString::fromLatin1("123")); |
|
452 |
QCOMPARE(object.toVariant().toString(), object.toString()); |
|
453 |
} |
|
454 |
{ |
|
455 |
QScriptValue object = eng.newVariant(QVariant(QString::fromLatin1("hello"))); |
|
456 |
QScriptValue value = object.property("valueOf").call(object); |
|
457 |
QVERIFY(value.isString()); |
|
458 |
QCOMPARE(value.toString(), QString::fromLatin1("hello")); |
|
459 |
QCOMPARE(object.toString(), QString::fromLatin1("hello")); |
|
460 |
QCOMPARE(object.toVariant().toString(), object.toString()); |
|
461 |
} |
|
462 |
{ |
|
463 |
QScriptValue object = eng.newVariant(QVariant(false)); |
|
464 |
QScriptValue value = object.property("valueOf").call(object); |
|
465 |
QVERIFY(value.isBoolean()); |
|
466 |
QCOMPARE(value.toBoolean(), false); |
|
467 |
QCOMPARE(object.toString(), QString::fromLatin1("false")); |
|
468 |
QCOMPARE(object.toVariant().toString(), object.toString()); |
|
469 |
} |
|
470 |
{ |
|
471 |
QScriptValue object = eng.newVariant(QVariant(QPoint(10, 20))); |
|
472 |
QScriptValue value = object.property("valueOf").call(object); |
|
473 |
QVERIFY(value.isObject()); |
|
474 |
QVERIFY(value.strictlyEquals(object)); |
|
475 |
QCOMPARE(object.toString(), QString::fromLatin1("QVariant(QPoint)")); |
|
476 |
} |
|
477 |
} |
|
478 |
||
479 |
void tst_QScriptEngine::newRegExp() |
|
480 |
{ |
|
481 |
QScriptEngine eng; |
|
482 |
for (int x = 0; x < 2; ++x) { |
|
483 |
QScriptValue rexp; |
|
484 |
if (x == 0) |
|
485 |
rexp = eng.newRegExp("foo", "bar"); |
|
486 |
else |
|
487 |
rexp = eng.newRegExp(QRegExp("foo")); |
|
488 |
QCOMPARE(rexp.isValid(), true); |
|
489 |
QCOMPARE(rexp.isRegExp(), true); |
|
490 |
QCOMPARE(rexp.isObject(), true); |
|
491 |
QVERIFY(rexp.isFunction()); // in JSC, RegExp objects are callable |
|
492 |
// prototype should be RegExp.prototype |
|
493 |
QCOMPARE(rexp.prototype().isValid(), true); |
|
494 |
QCOMPARE(rexp.prototype().isObject(), true); |
|
495 |
QCOMPARE(rexp.prototype().isRegExp(), false); |
|
496 |
QCOMPARE(rexp.prototype().strictlyEquals(eng.evaluate("RegExp.prototype")), true); |
|
497 |
||
498 |
QCOMPARE(rexp.toRegExp().pattern(), QRegExp("foo").pattern()); |
|
499 |
} |
|
500 |
{ |
|
501 |
QScriptValue r = eng.evaluate("/foo/gim"); |
|
502 |
QVERIFY(r.isRegExp()); |
|
503 |
QCOMPARE(r.toString(), QString::fromLatin1("/foo/gim")); |
|
504 |
||
505 |
QScriptValue rxCtor = eng.globalObject().property("RegExp"); |
|
506 |
QScriptValue r2 = rxCtor.call(QScriptValue(), QScriptValueList() << r); |
|
507 |
QVERIFY(r2.isRegExp()); |
|
508 |
QVERIFY(r2.strictlyEquals(r)); |
|
509 |
||
510 |
QScriptValue r3 = rxCtor.call(QScriptValue(), QScriptValueList() << r << "gim"); |
|
511 |
QVERIFY(r3.isError()); |
|
512 |
QCOMPARE(r3.toString(), QString::fromLatin1("TypeError: Cannot supply flags when constructing one RegExp from another.")); |
|
513 |
||
514 |
QScriptValue r4 = rxCtor.call(QScriptValue(), QScriptValueList() << "foo" << "gim"); |
|
515 |
QVERIFY(r4.isRegExp()); |
|
516 |
||
517 |
QScriptValue r5 = rxCtor.construct(QScriptValueList() << r); |
|
518 |
QVERIFY(r5.isRegExp()); |
|
519 |
QCOMPARE(r5.toString(), QString::fromLatin1("/foo/gim")); |
|
520 |
// In JSC, constructing a RegExp from another produces the same identical object. |
|
521 |
// This is different from SpiderMonkey and old back-end. |
|
522 |
QVERIFY(r5.strictlyEquals(r)); |
|
523 |
||
524 |
QScriptValue r6 = rxCtor.construct(QScriptValueList() << "foo" << "bar"); |
|
525 |
QVERIFY(r6.isError()); |
|
526 |
QCOMPARE(r6.toString(), QString::fromLatin1("SyntaxError: Invalid regular expression: invalid regular expression flag")); |
|
527 |
||
528 |
QScriptValue r7 = eng.evaluate("/foo/gimp"); |
|
529 |
QVERIFY(r7.isError()); |
|
530 |
QCOMPARE(r7.toString(), QString::fromLatin1("SyntaxError: Invalid regular expression: invalid regular expression flag")); |
|
531 |
||
532 |
QScriptValue r8 = eng.evaluate("/foo/migmigmig"); |
|
533 |
QVERIFY(r8.isRegExp()); |
|
534 |
QCOMPARE(r8.toString(), QString::fromLatin1("/foo/gim")); |
|
535 |
||
536 |
QScriptValue r9 = rxCtor.construct(); |
|
537 |
QVERIFY(r9.isRegExp()); |
|
538 |
QCOMPARE(r9.toString(), QString::fromLatin1("/(?:)/")); |
|
539 |
||
540 |
QScriptValue r10 = rxCtor.construct(QScriptValueList() << "" << "gim"); |
|
541 |
QVERIFY(r10.isRegExp()); |
|
542 |
QCOMPARE(r10.toString(), QString::fromLatin1("/(?:)/gim")); |
|
543 |
||
544 |
QScriptValue r11 = rxCtor.construct(QScriptValueList() << "{1.*}" << "g"); |
|
545 |
QVERIFY(r11.isRegExp()); |
|
546 |
QCOMPARE(r11.toString(), QString::fromLatin1("/{1.*}/g")); |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
void tst_QScriptEngine::newDate() |
|
551 |
{ |
|
552 |
QScriptEngine eng; |
|
553 |
||
554 |
{ |
|
555 |
QScriptValue date = eng.newDate(0); |
|
556 |
QCOMPARE(date.isValid(), true); |
|
557 |
QCOMPARE(date.isDate(), true); |
|
558 |
QCOMPARE(date.isObject(), true); |
|
559 |
QVERIFY(!date.isFunction()); |
|
560 |
// prototype should be Date.prototype |
|
561 |
QCOMPARE(date.prototype().isValid(), true); |
|
562 |
QCOMPARE(date.prototype().isDate(), true); |
|
563 |
QCOMPARE(date.prototype().strictlyEquals(eng.evaluate("Date.prototype")), true); |
|
564 |
} |
|
565 |
||
566 |
{ |
|
567 |
QDateTime dt = QDateTime(QDate(1, 2, 3), QTime(4, 5, 6, 7), Qt::LocalTime); |
|
568 |
QScriptValue date = eng.newDate(dt); |
|
569 |
QCOMPARE(date.isValid(), true); |
|
570 |
QCOMPARE(date.isDate(), true); |
|
571 |
QCOMPARE(date.isObject(), true); |
|
572 |
// prototype should be Date.prototype |
|
573 |
QCOMPARE(date.prototype().isValid(), true); |
|
574 |
QCOMPARE(date.prototype().isDate(), true); |
|
575 |
QCOMPARE(date.prototype().strictlyEquals(eng.evaluate("Date.prototype")), true); |
|
576 |
||
577 |
QCOMPARE(date.toDateTime(), dt); |
|
578 |
} |
|
579 |
||
580 |
{ |
|
581 |
QDateTime dt = QDateTime(QDate(1, 2, 3), QTime(4, 5, 6, 7), Qt::UTC); |
|
582 |
QScriptValue date = eng.newDate(dt); |
|
583 |
// toDateTime() result should be in local time |
|
584 |
QCOMPARE(date.toDateTime(), dt.toLocalTime()); |
|
585 |
} |
|
586 |
||
587 |
// Date.parse() should return NaN when it fails |
|
588 |
{ |
|
589 |
QScriptValue ret = eng.evaluate("Date.parse()"); |
|
590 |
QVERIFY(ret.isNumber()); |
|
591 |
QVERIFY(qIsNaN(ret.toNumber())); |
|
592 |
} |
|
593 |
||
594 |
// Date.parse() should be able to parse the output of Date().toString() |
|
595 |
#ifndef Q_WS_WIN // TODO: Test and remove this since 169701 has been fixed |
|
596 |
{ |
|
597 |
QScriptValue ret = eng.evaluate("var x = new Date(); var s = x.toString(); s == new Date(Date.parse(s)).toString()"); |
|
598 |
QVERIFY(ret.isBoolean()); |
|
599 |
QCOMPARE(ret.toBoolean(), true); |
|
600 |
} |
|
601 |
#endif |
|
602 |
} |
|
603 |
||
604 |
void tst_QScriptEngine::newQObject() |
|
605 |
{ |
|
606 |
QScriptEngine eng; |
|
607 |
||
608 |
{ |
|
609 |
QScriptValue qobject = eng.newQObject(0); |
|
610 |
QCOMPARE(qobject.isValid(), true); |
|
611 |
QCOMPARE(qobject.isNull(), true); |
|
612 |
QCOMPARE(qobject.isObject(), false); |
|
613 |
QCOMPARE(qobject.toQObject(), (QObject *)0); |
|
614 |
} |
|
615 |
{ |
|
616 |
QScriptValue qobject = eng.newQObject(this); |
|
617 |
QCOMPARE(qobject.isValid(), true); |
|
618 |
QCOMPARE(qobject.isQObject(), true); |
|
619 |
QCOMPARE(qobject.isObject(), true); |
|
620 |
QCOMPARE(qobject.toQObject(), (QObject *)this); |
|
621 |
QVERIFY(!qobject.isFunction()); |
|
622 |
// prototype should be QObject.prototype |
|
623 |
QCOMPARE(qobject.prototype().isValid(), true); |
|
624 |
QCOMPARE(qobject.prototype().isQObject(), true); |
|
625 |
QCOMPARE(qobject.scriptClass(), (QScriptClass*)0); |
|
626 |
} |
|
627 |
||
628 |
// test ownership |
|
629 |
{ |
|
630 |
QPointer<QObject> ptr = new QObject(); |
|
631 |
QVERIFY(ptr != 0); |
|
632 |
{ |
|
633 |
QScriptValue v = eng.newQObject(ptr, QScriptEngine::ScriptOwnership); |
|
634 |
} |
|
635 |
eng.evaluate("gc()"); |
|
636 |
if (ptr) |
|
637 |
QEXPECT_FAIL("", "In the JSC-based back-end, script-owned QObjects are not always deleted immediately during GC", Continue); |
|
638 |
QVERIFY(ptr == 0); |
|
639 |
} |
|
640 |
{ |
|
641 |
QPointer<QObject> ptr = new QObject(); |
|
642 |
QVERIFY(ptr != 0); |
|
643 |
{ |
|
644 |
QScriptValue v = eng.newQObject(ptr, QScriptEngine::QtOwnership); |
|
645 |
} |
|
646 |
QObject *before = ptr; |
|
647 |
eng.evaluate("gc()"); |
|
648 |
QVERIFY(ptr == before); |
|
649 |
delete ptr; |
|
650 |
} |
|
651 |
{ |
|
652 |
QObject *parent = new QObject(); |
|
653 |
QObject *child = new QObject(parent); |
|
654 |
QScriptValue v = eng.newQObject(child, QScriptEngine::QtOwnership); |
|
655 |
QCOMPARE(v.toQObject(), child); |
|
656 |
delete parent; |
|
657 |
QCOMPARE(v.toQObject(), (QObject *)0); |
|
658 |
} |
|
659 |
{ |
|
660 |
QPointer<QObject> ptr = new QObject(); |
|
661 |
QVERIFY(ptr != 0); |
|
662 |
{ |
|
663 |
QScriptValue v = eng.newQObject(ptr, QScriptEngine::AutoOwnership); |
|
664 |
} |
|
665 |
eng.evaluate("gc()"); |
|
666 |
// no parent, so it should be like ScriptOwnership |
|
667 |
if (ptr) |
|
668 |
QEXPECT_FAIL("", "In the JSC-based back-end, script-owned QObjects are not always deleted immediately during GC", Continue); |
|
669 |
QVERIFY(ptr == 0); |
|
670 |
} |
|
671 |
{ |
|
672 |
QObject *parent = new QObject(); |
|
673 |
QPointer<QObject> child = new QObject(parent); |
|
674 |
QVERIFY(child != 0); |
|
675 |
{ |
|
676 |
QScriptValue v = eng.newQObject(child, QScriptEngine::AutoOwnership); |
|
677 |
} |
|
678 |
eng.evaluate("gc()"); |
|
679 |
// has parent, so it should be like QtOwnership |
|
680 |
QVERIFY(child != 0); |
|
681 |
delete parent; |
|
682 |
} |
|
683 |
||
684 |
// "promote" plain object to QObject |
|
685 |
{ |
|
686 |
QScriptValue obj = eng.newObject(); |
|
687 |
QScriptValue originalProto = obj.prototype(); |
|
688 |
QScriptValue ret = eng.newQObject(obj, this); |
|
689 |
QVERIFY(ret.isValid()); |
|
690 |
QVERIFY(ret.isQObject()); |
|
691 |
QVERIFY(ret.strictlyEquals(obj)); |
|
692 |
QVERIFY(obj.isQObject()); |
|
693 |
QCOMPARE(ret.toQObject(), (QObject *)this); |
|
694 |
QVERIFY(ret.prototype().strictlyEquals(originalProto)); |
|
695 |
QScriptValue val = ret.property("objectName"); |
|
696 |
QVERIFY(val.isString()); |
|
697 |
} |
|
698 |
// "promote" variant object to QObject |
|
699 |
{ |
|
700 |
QScriptValue obj = eng.newVariant(123); |
|
701 |
QVERIFY(obj.isVariant()); |
|
702 |
QScriptValue originalProto = obj.prototype(); |
|
703 |
QScriptValue ret = eng.newQObject(obj, this); |
|
704 |
QVERIFY(ret.isQObject()); |
|
705 |
QVERIFY(ret.strictlyEquals(obj)); |
|
706 |
QVERIFY(obj.isQObject()); |
|
707 |
QCOMPARE(ret.toQObject(), (QObject *)this); |
|
708 |
QVERIFY(ret.prototype().strictlyEquals(originalProto)); |
|
709 |
} |
|
710 |
// replace QObject* of existing object |
|
711 |
{ |
|
712 |
QScriptValue object = eng.newVariant(123); |
|
713 |
QScriptValue originalProto = object.prototype(); |
|
714 |
QObject otherQObject; |
|
715 |
QScriptValue ret = eng.newQObject(object, &otherQObject); |
|
716 |
QVERIFY(ret.isValid()); |
|
717 |
QVERIFY(ret.isQObject()); |
|
718 |
QVERIFY(ret.strictlyEquals(object)); |
|
719 |
QCOMPARE(ret.toQObject(), (QObject *)&otherQObject); |
|
720 |
QVERIFY(ret.prototype().strictlyEquals(originalProto)); |
|
721 |
} |
|
722 |
||
723 |
// calling newQObject() several times with same object |
|
724 |
for (int x = 0; x < 2; ++x) { |
|
725 |
QObject qobj; |
|
726 |
// the default is to create a new wrapper object |
|
727 |
QScriptValue obj1 = eng.newQObject(&qobj); |
|
728 |
QScriptValue obj2 = eng.newQObject(&qobj); |
|
729 |
QVERIFY(!obj2.strictlyEquals(obj1)); |
|
730 |
||
731 |
QScriptEngine::QObjectWrapOptions opt = 0; |
|
732 |
bool preferExisting = (x != 0); |
|
733 |
if (preferExisting) |
|
734 |
opt |= QScriptEngine::PreferExistingWrapperObject; |
|
735 |
||
736 |
QScriptValue obj3 = eng.newQObject(&qobj, QScriptEngine::AutoOwnership, opt); |
|
737 |
QVERIFY(!obj3.strictlyEquals(obj2)); |
|
738 |
QScriptValue obj4 = eng.newQObject(&qobj, QScriptEngine::AutoOwnership, opt); |
|
739 |
QCOMPARE(obj4.strictlyEquals(obj3), preferExisting); |
|
740 |
||
741 |
QScriptValue obj5 = eng.newQObject(&qobj, QScriptEngine::ScriptOwnership, opt); |
|
742 |
QVERIFY(!obj5.strictlyEquals(obj4)); |
|
743 |
QScriptValue obj6 = eng.newQObject(&qobj, QScriptEngine::ScriptOwnership, opt); |
|
744 |
QCOMPARE(obj6.strictlyEquals(obj5), preferExisting); |
|
745 |
||
746 |
QScriptValue obj7 = eng.newQObject(&qobj, QScriptEngine::ScriptOwnership, |
|
747 |
QScriptEngine::ExcludeSuperClassMethods | opt); |
|
748 |
QVERIFY(!obj7.strictlyEquals(obj6)); |
|
749 |
QScriptValue obj8 = eng.newQObject(&qobj, QScriptEngine::ScriptOwnership, |
|
750 |
QScriptEngine::ExcludeSuperClassMethods | opt); |
|
751 |
QCOMPARE(obj8.strictlyEquals(obj7), preferExisting); |
|
752 |
} |
|
753 |
||
754 |
// newQObject() should set the default prototype, if one has been registered |
|
755 |
{ |
|
756 |
QScriptValue oldQObjectProto = eng.defaultPrototype(qMetaTypeId<QObject*>()); |
|
757 |
||
758 |
QScriptValue qobjectProto = eng.newObject(); |
|
759 |
eng.setDefaultPrototype(qMetaTypeId<QObject*>(), qobjectProto); |
|
760 |
{ |
|
761 |
QScriptValue ret = eng.newQObject(this); |
|
762 |
QVERIFY(ret.prototype().equals(qobjectProto)); |
|
763 |
} |
|
764 |
QScriptValue tstProto = eng.newObject(); |
|
765 |
int typeId = qRegisterMetaType<tst_QScriptEngine*>("tst_QScriptEngine*"); |
|
766 |
eng.setDefaultPrototype(typeId, tstProto); |
|
767 |
{ |
|
768 |
QScriptValue ret = eng.newQObject(this); |
|
769 |
QVERIFY(ret.prototype().equals(tstProto)); |
|
770 |
} |
|
771 |
||
772 |
eng.setDefaultPrototype(qMetaTypeId<QObject*>(), oldQObjectProto); |
|
773 |
eng.setDefaultPrototype(typeId, QScriptValue()); |
|
774 |
} |
|
775 |
} |
|
776 |
||
777 |
QT_BEGIN_NAMESPACE |
|
778 |
Q_SCRIPT_DECLARE_QMETAOBJECT(QObject, QObject*) |
|
779 |
Q_SCRIPT_DECLARE_QMETAOBJECT(QWidget, QWidget*) |
|
780 |
QT_END_NAMESPACE |
|
781 |
||
782 |
static QScriptValue myConstructor(QScriptContext *ctx, QScriptEngine *eng) |
|
783 |
{ |
|
784 |
QScriptValue obj; |
|
785 |
if (ctx->isCalledAsConstructor()) { |
|
786 |
obj = ctx->thisObject(); |
|
787 |
} else { |
|
788 |
obj = eng->newObject(); |
|
789 |
obj.setPrototype(ctx->callee().property("prototype")); |
|
790 |
} |
|
791 |
obj.setProperty("isCalledAsConstructor", QScriptValue(eng, ctx->isCalledAsConstructor())); |
|
792 |
return obj; |
|
793 |
} |
|
794 |
||
795 |
void tst_QScriptEngine::newQMetaObject() |
|
796 |
{ |
|
797 |
QScriptEngine eng; |
|
798 |
#if 0 |
|
799 |
QScriptValue qclass = eng.newQMetaObject<QObject>(); |
|
800 |
QScriptValue qclass2 = eng.newQMetaObject<QWidget>(); |
|
801 |
#else |
|
802 |
QScriptValue qclass = qScriptValueFromQMetaObject<QObject>(&eng); |
|
803 |
QScriptValue qclass2 = qScriptValueFromQMetaObject<QWidget>(&eng); |
|
804 |
#endif |
|
805 |
QCOMPARE(qclass.isValid(), true); |
|
806 |
QCOMPARE(qclass.isQMetaObject(), true); |
|
807 |
QCOMPARE(qclass.toQMetaObject(), &QObject::staticMetaObject); |
|
808 |
QCOMPARE(qclass.isFunction(), true); |
|
809 |
QVERIFY(qclass.property("prototype").isObject()); |
|
810 |
||
811 |
QCOMPARE(qclass2.isValid(), true); |
|
812 |
QCOMPARE(qclass2.isQMetaObject(), true); |
|
813 |
QCOMPARE(qclass2.toQMetaObject(), &QWidget::staticMetaObject); |
|
814 |
QCOMPARE(qclass2.isFunction(), true); |
|
815 |
QVERIFY(qclass2.property("prototype").isObject()); |
|
816 |
||
817 |
// prototype should be QMetaObject.prototype |
|
818 |
QCOMPARE(qclass.prototype().isObject(), true); |
|
819 |
QCOMPARE(qclass2.prototype().isObject(), true); |
|
820 |
||
821 |
QScriptValue instance = qclass.construct(); |
|
822 |
QCOMPARE(instance.isQObject(), true); |
|
823 |
QCOMPARE(instance.toQObject()->metaObject(), qclass.toQMetaObject()); |
|
824 |
QVERIFY(instance.instanceOf(qclass)); |
|
825 |
||
826 |
QScriptValue instance2 = qclass2.construct(); |
|
827 |
QCOMPARE(instance2.isQObject(), true); |
|
828 |
QCOMPARE(instance2.toQObject()->metaObject(), qclass2.toQMetaObject()); |
|
829 |
QVERIFY(instance2.instanceOf(qclass2)); |
|
830 |
||
831 |
QScriptValueList args; |
|
832 |
args << instance; |
|
833 |
QScriptValue instance3 = qclass.construct(args); |
|
834 |
QCOMPARE(instance3.isQObject(), true); |
|
835 |
QCOMPARE(instance3.toQObject()->parent(), instance.toQObject()); |
|
836 |
QVERIFY(instance3.instanceOf(qclass)); |
|
837 |
args.clear(); |
|
838 |
||
839 |
QPointer<QObject> qpointer1 = instance.toQObject(); |
|
840 |
QPointer<QObject> qpointer2 = instance2.toQObject(); |
|
841 |
QPointer<QObject> qpointer3 = instance3.toQObject(); |
|
842 |
||
843 |
QVERIFY(qpointer1); |
|
844 |
QVERIFY(qpointer2); |
|
845 |
QVERIFY(qpointer3); |
|
846 |
||
847 |
// verify that AutoOwnership is in effect |
|
848 |
instance = QScriptValue(); |
|
849 |
collectGarbage_helper(eng); |
|
850 |
||
851 |
QVERIFY(!qpointer1); |
|
852 |
QVERIFY(qpointer2); |
|
853 |
QVERIFY(!qpointer3); // was child of instance |
|
854 |
||
855 |
QVERIFY(instance.toQObject() == 0); |
|
856 |
QVERIFY(instance3.toQObject() == 0); // was child of instance |
|
857 |
QVERIFY(instance2.toQObject() != 0); |
|
858 |
instance2 = QScriptValue(); |
|
859 |
collectGarbage_helper(eng); |
|
860 |
QVERIFY(instance2.toQObject() == 0); |
|
861 |
||
862 |
// with custom constructor |
|
863 |
QScriptValue ctor = eng.newFunction(myConstructor); |
|
864 |
QScriptValue qclass3 = eng.newQMetaObject(&QObject::staticMetaObject, ctor); |
|
865 |
QVERIFY(qclass3.property("prototype").equals(ctor.property("prototype"))); |
|
866 |
{ |
|
867 |
QScriptValue ret = qclass3.call(); |
|
868 |
QVERIFY(ret.isObject()); |
|
869 |
QVERIFY(ret.property("isCalledAsConstructor").isBoolean()); |
|
870 |
QVERIFY(!ret.property("isCalledAsConstructor").toBoolean()); |
|
871 |
QVERIFY(ret.instanceOf(qclass3)); |
|
872 |
} |
|
873 |
{ |
|
874 |
QScriptValue ret = qclass3.construct(); |
|
875 |
QVERIFY(ret.isObject()); |
|
876 |
QVERIFY(ret.property("isCalledAsConstructor").isBoolean()); |
|
877 |
QVERIFY(ret.property("isCalledAsConstructor").toBoolean()); |
|
878 |
QVERIFY(ret.instanceOf(qclass3)); |
|
879 |
} |
|
880 |
||
881 |
// subclassing |
|
882 |
qclass2.setProperty("prototype", qclass.construct()); |
|
883 |
QVERIFY(qclass2.construct().instanceOf(qclass)); |
|
884 |
||
885 |
// with meta-constructor |
|
886 |
QScriptValue qclass4 = eng.newQMetaObject(&QObject::staticMetaObject); |
|
887 |
{ |
|
888 |
QScriptValue inst = qclass4.construct(); |
|
889 |
QVERIFY(inst.isQObject()); |
|
890 |
QVERIFY(inst.toQObject() != 0); |
|
891 |
QCOMPARE(inst.toQObject()->parent(), (QObject*)0); |
|
892 |
QVERIFY(inst.instanceOf(qclass4)); |
|
893 |
} |
|
894 |
{ |
|
895 |
QScriptValue inst = qclass4.construct(QScriptValueList() << eng.newQObject(this)); |
|
896 |
QVERIFY(inst.isQObject()); |
|
897 |
QVERIFY(inst.toQObject() != 0); |
|
898 |
QCOMPARE(inst.toQObject()->parent(), (QObject*)this); |
|
899 |
QVERIFY(inst.instanceOf(qclass4)); |
|
900 |
} |
|
901 |
} |
|
902 |
||
903 |
void tst_QScriptEngine::newActivationObject() |
|
904 |
{ |
|
905 |
QSKIP("internal function not implemented in JSC-based back-end", SkipAll); |
|
906 |
QScriptEngine eng; |
|
907 |
QScriptValue act = eng.newActivationObject(); |
|
908 |
QEXPECT_FAIL("", "", Continue); |
|
909 |
QCOMPARE(act.isValid(), true); |
|
910 |
QEXPECT_FAIL("", "", Continue); |
|
911 |
QCOMPARE(act.isObject(), true); |
|
912 |
QVERIFY(!act.isFunction()); |
|
913 |
QScriptValue v(&eng, 123); |
|
914 |
act.setProperty("prop", v); |
|
915 |
QEXPECT_FAIL("", "", Continue); |
|
916 |
QCOMPARE(act.property("prop").strictlyEquals(v), true); |
|
917 |
QCOMPARE(act.scope().isValid(), false); |
|
918 |
QEXPECT_FAIL("", "", Continue); |
|
919 |
QVERIFY(act.prototype().isNull()); |
|
920 |
} |
|
921 |
||
922 |
void tst_QScriptEngine::getSetGlobalObject() |
|
923 |
{ |
|
924 |
QScriptEngine eng; |
|
925 |
QScriptValue glob = eng.globalObject(); |
|
926 |
QCOMPARE(glob.isValid(), true); |
|
927 |
QCOMPARE(glob.isObject(), true); |
|
928 |
QVERIFY(!glob.isFunction()); |
|
929 |
QVERIFY(eng.currentContext()->thisObject().strictlyEquals(glob)); |
|
930 |
QVERIFY(eng.currentContext()->activationObject().strictlyEquals(glob)); |
|
931 |
QCOMPARE(glob.toString(), QString::fromLatin1("[object global]")); |
|
932 |
// prototype should be Object.prototype |
|
933 |
QCOMPARE(glob.prototype().isValid(), true); |
|
934 |
QCOMPARE(glob.prototype().isObject(), true); |
|
935 |
QCOMPARE(glob.prototype().strictlyEquals(eng.evaluate("Object.prototype")), true); |
|
936 |
||
937 |
QScriptValue obj = eng.newObject(); |
|
938 |
eng.setGlobalObject(obj); |
|
939 |
QVERIFY(eng.globalObject().strictlyEquals(obj)); |
|
940 |
QVERIFY(eng.currentContext()->thisObject().strictlyEquals(obj)); |
|
941 |
QVERIFY(eng.currentContext()->activationObject().strictlyEquals(obj)); |
|
942 |
QVERIFY(eng.evaluate("this").strictlyEquals(obj)); |
|
943 |
QCOMPARE(eng.globalObject().toString(), QString::fromLatin1("[object Object]")); |
|
944 |
||
945 |
glob = QScriptValue(); // kill reference to old global object |
|
946 |
collectGarbage_helper(eng); |
|
947 |
obj = eng.newObject(); |
|
948 |
eng.setGlobalObject(obj); |
|
949 |
QVERIFY(eng.globalObject().strictlyEquals(obj)); |
|
950 |
QVERIFY(eng.currentContext()->thisObject().strictlyEquals(obj)); |
|
951 |
QVERIFY(eng.currentContext()->activationObject().strictlyEquals(obj)); |
|
952 |
||
953 |
collectGarbage_helper(eng); |
|
954 |
QVERIFY(eng.globalObject().strictlyEquals(obj)); |
|
955 |
QVERIFY(eng.currentContext()->thisObject().strictlyEquals(obj)); |
|
956 |
QVERIFY(eng.currentContext()->activationObject().strictlyEquals(obj)); |
|
957 |
||
958 |
QVERIFY(!obj.property("foo").isValid()); |
|
959 |
eng.evaluate("var foo = 123"); |
|
960 |
{ |
|
961 |
QScriptValue ret = obj.property("foo"); |
|
962 |
QVERIFY(ret.isNumber()); |
|
963 |
QCOMPARE(ret.toInt32(), 123); |
|
964 |
} |
|
965 |
||
966 |
QVERIFY(!obj.property("bar").isValid()); |
|
967 |
eng.evaluate("bar = 456"); |
|
968 |
{ |
|
969 |
QScriptValue ret = obj.property("bar"); |
|
970 |
QVERIFY(ret.isNumber()); |
|
971 |
QCOMPARE(ret.toInt32(), 456); |
|
972 |
} |
|
973 |
||
974 |
QVERIFY(!obj.property("baz").isValid()); |
|
975 |
eng.evaluate("this['baz'] = 789"); |
|
976 |
{ |
|
977 |
QScriptValue ret = obj.property("baz"); |
|
978 |
QVERIFY(ret.isNumber()); |
|
979 |
QCOMPARE(ret.toInt32(), 789); |
|
980 |
} |
|
981 |
||
982 |
{ |
|
983 |
QScriptValue ret = eng.evaluate("(function() { return this; })()"); |
|
984 |
QVERIFY(ret.strictlyEquals(obj)); |
|
985 |
} |
|
986 |
} |
|
987 |
||
988 |
static QScriptValue getSetFoo(QScriptContext *ctx, QScriptEngine *) |
|
989 |
{ |
|
990 |
if (ctx->argumentCount() > 0) |
|
991 |
ctx->thisObject().setProperty("foo", ctx->argument(0)); |
|
992 |
return ctx->thisObject().property("foo"); |
|
993 |
} |
|
994 |
||
995 |
void tst_QScriptEngine::globalObjectProperties() |
|
996 |
{ |
|
997 |
QScriptEngine eng; |
|
998 |
QScriptValue global = eng.globalObject(); |
|
999 |
||
1000 |
QVERIFY(global.property("NaN").isNumber()); |
|
1001 |
QVERIFY(qIsNaN(global.property("NaN").toNumber())); |
|
1002 |
QCOMPARE(global.propertyFlags("NaN"), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); |
|
1003 |
||
1004 |
QVERIFY(global.property("Infinity").isNumber()); |
|
1005 |
QVERIFY(qIsInf(global.property("Infinity").toNumber())); |
|
1006 |
QCOMPARE(global.propertyFlags("NaN"), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); |
|
1007 |
||
1008 |
QVERIFY(global.property("undefined").isUndefined()); |
|
1009 |
QCOMPARE(global.propertyFlags("undefined"), QScriptValue::SkipInEnumeration | QScriptValue::Undeletable); |
|
1010 |
||
1011 |
QVERIFY(global.property("eval").isFunction()); |
|
1012 |
QCOMPARE(global.propertyFlags("eval"), QScriptValue::SkipInEnumeration); |
|
1013 |
||
1014 |
QVERIFY(global.property("parseInt").isFunction()); |
|
1015 |
QCOMPARE(global.propertyFlags("parseInt"), QScriptValue::SkipInEnumeration); |
|
1016 |
||
1017 |
QVERIFY(global.property("parseFloat").isFunction()); |
|
1018 |
QCOMPARE(global.propertyFlags("parseFloat"), QScriptValue::SkipInEnumeration); |
|
1019 |
||
1020 |
QVERIFY(global.property("isNaN").isFunction()); |
|
1021 |
QCOMPARE(global.propertyFlags("isNaN"), QScriptValue::SkipInEnumeration); |
|
1022 |
||
1023 |
QVERIFY(global.property("isFinite").isFunction()); |
|
1024 |
QCOMPARE(global.propertyFlags("isFinite"), QScriptValue::SkipInEnumeration); |
|
1025 |
||
1026 |
QVERIFY(global.property("decodeURI").isFunction()); |
|
1027 |
QCOMPARE(global.propertyFlags("decodeURI"), QScriptValue::SkipInEnumeration); |
|
1028 |
||
1029 |
QVERIFY(global.property("decodeURIComponent").isFunction()); |
|
1030 |
QCOMPARE(global.propertyFlags("decodeURIComponent"), QScriptValue::SkipInEnumeration); |
|
1031 |
||
1032 |
QVERIFY(global.property("encodeURI").isFunction()); |
|
1033 |
QCOMPARE(global.propertyFlags("encodeURI"), QScriptValue::SkipInEnumeration); |
|
1034 |
||
1035 |
QVERIFY(global.property("encodeURIComponent").isFunction()); |
|
1036 |
QCOMPARE(global.propertyFlags("encodeURIComponent"), QScriptValue::SkipInEnumeration); |
|
1037 |
||
1038 |
QVERIFY(global.property("Object").isFunction()); |
|
1039 |
QCOMPARE(global.propertyFlags("Object"), QScriptValue::SkipInEnumeration); |
|
1040 |
QVERIFY(global.property("Function").isFunction()); |
|
1041 |
QCOMPARE(global.propertyFlags("Function"), QScriptValue::SkipInEnumeration); |
|
1042 |
QVERIFY(global.property("Array").isFunction()); |
|
1043 |
QCOMPARE(global.propertyFlags("Array"), QScriptValue::SkipInEnumeration); |
|
1044 |
QVERIFY(global.property("String").isFunction()); |
|
1045 |
QCOMPARE(global.propertyFlags("String"), QScriptValue::SkipInEnumeration); |
|
1046 |
QVERIFY(global.property("Boolean").isFunction()); |
|
1047 |
QCOMPARE(global.propertyFlags("Boolean"), QScriptValue::SkipInEnumeration); |
|
1048 |
QVERIFY(global.property("Number").isFunction()); |
|
1049 |
QCOMPARE(global.propertyFlags("Number"), QScriptValue::SkipInEnumeration); |
|
1050 |
QVERIFY(global.property("Date").isFunction()); |
|
1051 |
QCOMPARE(global.propertyFlags("Date"), QScriptValue::SkipInEnumeration); |
|
1052 |
QVERIFY(global.property("RegExp").isFunction()); |
|
1053 |
QCOMPARE(global.propertyFlags("RegExp"), QScriptValue::SkipInEnumeration); |
|
1054 |
QVERIFY(global.property("Error").isFunction()); |
|
1055 |
QCOMPARE(global.propertyFlags("Error"), QScriptValue::SkipInEnumeration); |
|
1056 |
QVERIFY(global.property("EvalError").isFunction()); |
|
1057 |
QCOMPARE(global.propertyFlags("EvalError"), QScriptValue::SkipInEnumeration); |
|
1058 |
QVERIFY(global.property("RangeError").isFunction()); |
|
1059 |
QCOMPARE(global.propertyFlags("RangeError"), QScriptValue::SkipInEnumeration); |
|
1060 |
QVERIFY(global.property("ReferenceError").isFunction()); |
|
1061 |
QCOMPARE(global.propertyFlags("ReferenceError"), QScriptValue::SkipInEnumeration); |
|
1062 |
QVERIFY(global.property("SyntaxError").isFunction()); |
|
1063 |
QCOMPARE(global.propertyFlags("SyntaxError"), QScriptValue::SkipInEnumeration); |
|
1064 |
QVERIFY(global.property("TypeError").isFunction()); |
|
1065 |
QCOMPARE(global.propertyFlags("TypeError"), QScriptValue::SkipInEnumeration); |
|
1066 |
QVERIFY(global.property("URIError").isFunction()); |
|
1067 |
QCOMPARE(global.propertyFlags("URIError"), QScriptValue::SkipInEnumeration); |
|
1068 |
QVERIFY(global.property("Math").isObject()); |
|
1069 |
QVERIFY(!global.property("Math").isFunction()); |
|
1070 |
QEXPECT_FAIL("", "[ECMA compliance] JSC sets DontDelete flag for Math object", Continue); |
|
1071 |
QCOMPARE(global.propertyFlags("Math"), QScriptValue::SkipInEnumeration); |
|
1072 |
||
1073 |
// enumeration |
|
1074 |
QSet<QString> expectedNames; |
|
1075 |
expectedNames |
|
1076 |
<< "isNaN" |
|
1077 |
<< "parseFloat" |
|
1078 |
<< "String" |
|
1079 |
<< "EvalError" |
|
1080 |
<< "URIError" |
|
1081 |
<< "Math" |
|
1082 |
<< "encodeURIComponent" |
|
1083 |
<< "RangeError" |
|
1084 |
<< "eval" |
|
1085 |
<< "isFinite" |
|
1086 |
<< "ReferenceError" |
|
1087 |
<< "Infinity" |
|
1088 |
<< "Function" |
|
1089 |
<< "RegExp" |
|
1090 |
<< "Number" |
|
1091 |
<< "parseInt" |
|
1092 |
<< "Object" |
|
1093 |
<< "decodeURI" |
|
1094 |
<< "TypeError" |
|
1095 |
<< "Boolean" |
|
1096 |
<< "encodeURI" |
|
1097 |
<< "NaN" |
|
1098 |
<< "Error" |
|
1099 |
<< "decodeURIComponent" |
|
1100 |
<< "Date" |
|
1101 |
<< "Array" |
|
1102 |
<< "escape" |
|
1103 |
<< "unescape" |
|
1104 |
<< "SyntaxError" |
|
1105 |
<< "undefined" |
|
1106 |
// non-standard |
|
1107 |
<< "gc" |
|
1108 |
<< "version" |
|
1109 |
<< "print" |
|
1110 |
// JavaScriptCore |
|
1111 |
<< "JSON" |
|
1112 |
; |
|
1113 |
QSet<QString> actualNames; |
|
1114 |
{ |
|
1115 |
QScriptValueIterator it(global); |
|
1116 |
while (it.hasNext()) { |
|
1117 |
it.next(); |
|
1118 |
actualNames.insert(it.name()); |
|
1119 |
} |
|
1120 |
} |
|
1121 |
||
1122 |
QSet<QString> remainingNames = actualNames; |
|
1123 |
{ |
|
1124 |
QSet<QString>::const_iterator it; |
|
1125 |
for (it = expectedNames.constBegin(); it != expectedNames.constEnd(); ++it) { |
|
1126 |
QString name = *it; |
|
1127 |
QVERIFY(actualNames.contains(name)); |
|
1128 |
remainingNames.remove(name); |
|
1129 |
} |
|
1130 |
} |
|
1131 |
QVERIFY(remainingNames.isEmpty()); |
|
1132 |
||
1133 |
// create property with no attributes |
|
1134 |
{ |
|
1135 |
QString name = QString::fromLatin1("foo"); |
|
1136 |
QVERIFY(!global.property(name).isValid()); |
|
1137 |
QScriptValue val(123); |
|
1138 |
global.setProperty(name, val); |
|
1139 |
QVERIFY(global.property(name).equals(val)); |
|
1140 |
QVERIFY(global.propertyFlags(name) == 0); |
|
1141 |
global.setProperty(name, QScriptValue()); |
|
1142 |
QVERIFY(!global.property(name).isValid()); |
|
1143 |
} |
|
1144 |
// create property with attributes |
|
1145 |
{ |
|
1146 |
QString name = QString::fromLatin1("bar"); |
|
1147 |
QVERIFY(!global.property(name).isValid()); |
|
1148 |
QScriptValue val(QString::fromLatin1("ciao")); |
|
1149 |
QScriptValue::PropertyFlags flags = QScriptValue::ReadOnly | QScriptValue::SkipInEnumeration; |
|
1150 |
global.setProperty(name, val, flags); |
|
1151 |
QVERIFY(global.property(name).equals(val)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1152 |
QEXPECT_FAIL("", "QTBUG-6134: custom Global Object properties don't retain attributes", Continue); |
0 | 1153 |
QCOMPARE(global.propertyFlags(name), flags); |
1154 |
global.setProperty(name, QScriptValue()); |
|
1155 |
QVERIFY(!global.property(name).isValid()); |
|
1156 |
} |
|
1157 |
} |
|
1158 |
||
1159 |
void tst_QScriptEngine::globalObjectGetterSetterProperty() |
|
1160 |
{ |
|
1161 |
QScriptEngine engine; |
|
1162 |
QScriptValue global = engine.globalObject(); |
|
1163 |
global.setProperty("bar", engine.newFunction(getSetFoo), |
|
1164 |
QScriptValue::PropertySetter | QScriptValue::PropertyGetter); |
|
1165 |
global.setProperty("foo", 123); |
|
1166 |
QVERIFY(global.property("bar").equals(global.property("foo"))); |
|
1167 |
QVERIFY(engine.evaluate("bar").equals(global.property("foo"))); |
|
1168 |
global.setProperty("bar", 456); |
|
1169 |
QVERIFY(global.property("bar").equals(global.property("foo"))); |
|
1170 |
||
1171 |
engine.evaluate("__defineGetter__('baz', function() { return 789; })"); |
|
1172 |
QVERIFY(engine.evaluate("baz").equals(789)); |
|
1173 |
QVERIFY(global.property("baz").equals(789)); |
|
1174 |
} |
|
1175 |
||
1176 |
void tst_QScriptEngine::builtinFunctionNames_data() |
|
1177 |
{ |
|
1178 |
QTest::addColumn<QString>("expression"); |
|
1179 |
QTest::addColumn<QString>("expectedName"); |
|
1180 |
||
1181 |
QTest::newRow("print") << QString("print") << QString("print"); |
|
1182 |
QTest::newRow("parseInt") << QString("parseInt") << QString("parseInt"); |
|
1183 |
QTest::newRow("parseFloat") << QString("parseFloat") << QString("parseFloat"); |
|
1184 |
QTest::newRow("isNaN") << QString("isNaN") << QString("isNaN"); |
|
1185 |
QTest::newRow("isFinite") << QString("isFinite") << QString("isFinite"); |
|
1186 |
QTest::newRow("decodeURI") << QString("decodeURI") << QString("decodeURI"); |
|
1187 |
QTest::newRow("decodeURIComponent") << QString("decodeURIComponent") << QString("decodeURIComponent"); |
|
1188 |
QTest::newRow("encodeURI") << QString("encodeURI") << QString("encodeURI"); |
|
1189 |
QTest::newRow("encodeURIComponent") << QString("encodeURIComponent") << QString("encodeURIComponent"); |
|
1190 |
QTest::newRow("escape") << QString("escape") << QString("escape"); |
|
1191 |
QTest::newRow("unescape") << QString("unescape") << QString("unescape"); |
|
1192 |
QTest::newRow("version") << QString("version") << QString("version"); |
|
1193 |
QTest::newRow("gc") << QString("gc") << QString("gc"); |
|
1194 |
||
1195 |
QTest::newRow("Array") << QString("Array") << QString("Array"); |
|
1196 |
QTest::newRow("Array.prototype.toString") << QString("Array.prototype.toString") << QString("toString"); |
|
1197 |
QTest::newRow("Array.prototype.toLocaleString") << QString("Array.prototype.toLocaleString") << QString("toLocaleString"); |
|
1198 |
QTest::newRow("Array.prototype.concat") << QString("Array.prototype.concat") << QString("concat"); |
|
1199 |
QTest::newRow("Array.prototype.join") << QString("Array.prototype.join") << QString("join"); |
|
1200 |
QTest::newRow("Array.prototype.pop") << QString("Array.prototype.pop") << QString("pop"); |
|
1201 |
QTest::newRow("Array.prototype.push") << QString("Array.prototype.push") << QString("push"); |
|
1202 |
QTest::newRow("Array.prototype.reverse") << QString("Array.prototype.reverse") << QString("reverse"); |
|
1203 |
QTest::newRow("Array.prototype.shift") << QString("Array.prototype.shift") << QString("shift"); |
|
1204 |
QTest::newRow("Array.prototype.slice") << QString("Array.prototype.slice") << QString("slice"); |
|
1205 |
QTest::newRow("Array.prototype.sort") << QString("Array.prototype.sort") << QString("sort"); |
|
1206 |
QTest::newRow("Array.prototype.splice") << QString("Array.prototype.splice") << QString("splice"); |
|
1207 |
QTest::newRow("Array.prototype.unshift") << QString("Array.prototype.unshift") << QString("unshift"); |
|
1208 |
||
1209 |
QTest::newRow("Boolean") << QString("Boolean") << QString("Boolean"); |
|
1210 |
QTest::newRow("Boolean.prototype.toString") << QString("Boolean.prototype.toString") << QString("toString"); |
|
1211 |
||
1212 |
QTest::newRow("Date") << QString("Date") << QString("Date"); |
|
1213 |
QTest::newRow("Date.prototype.toString") << QString("Date.prototype.toString") << QString("toString"); |
|
1214 |
QTest::newRow("Date.prototype.toDateString") << QString("Date.prototype.toDateString") << QString("toDateString"); |
|
1215 |
QTest::newRow("Date.prototype.toTimeString") << QString("Date.prototype.toTimeString") << QString("toTimeString"); |
|
1216 |
QTest::newRow("Date.prototype.toLocaleString") << QString("Date.prototype.toLocaleString") << QString("toLocaleString"); |
|
1217 |
QTest::newRow("Date.prototype.toLocaleDateString") << QString("Date.prototype.toLocaleDateString") << QString("toLocaleDateString"); |
|
1218 |
QTest::newRow("Date.prototype.toLocaleTimeString") << QString("Date.prototype.toLocaleTimeString") << QString("toLocaleTimeString"); |
|
1219 |
QTest::newRow("Date.prototype.valueOf") << QString("Date.prototype.valueOf") << QString("valueOf"); |
|
1220 |
QTest::newRow("Date.prototype.getTime") << QString("Date.prototype.getTime") << QString("getTime"); |
|
1221 |
QTest::newRow("Date.prototype.getYear") << QString("Date.prototype.getYear") << QString("getYear"); |
|
1222 |
QTest::newRow("Date.prototype.getFullYear") << QString("Date.prototype.getFullYear") << QString("getFullYear"); |
|
1223 |
QTest::newRow("Date.prototype.getUTCFullYear") << QString("Date.prototype.getUTCFullYear") << QString("getUTCFullYear"); |
|
1224 |
QTest::newRow("Date.prototype.getMonth") << QString("Date.prototype.getMonth") << QString("getMonth"); |
|
1225 |
QTest::newRow("Date.prototype.getUTCMonth") << QString("Date.prototype.getUTCMonth") << QString("getUTCMonth"); |
|
1226 |
QTest::newRow("Date.prototype.getDate") << QString("Date.prototype.getDate") << QString("getDate"); |
|
1227 |
QTest::newRow("Date.prototype.getUTCDate") << QString("Date.prototype.getUTCDate") << QString("getUTCDate"); |
|
1228 |
QTest::newRow("Date.prototype.getDay") << QString("Date.prototype.getDay") << QString("getDay"); |
|
1229 |
QTest::newRow("Date.prototype.getUTCDay") << QString("Date.prototype.getUTCDay") << QString("getUTCDay"); |
|
1230 |
QTest::newRow("Date.prototype.getHours") << QString("Date.prototype.getHours") << QString("getHours"); |
|
1231 |
QTest::newRow("Date.prototype.getUTCHours") << QString("Date.prototype.getUTCHours") << QString("getUTCHours"); |
|
1232 |
QTest::newRow("Date.prototype.getMinutes") << QString("Date.prototype.getMinutes") << QString("getMinutes"); |
|
1233 |
QTest::newRow("Date.prototype.getUTCMinutes") << QString("Date.prototype.getUTCMinutes") << QString("getUTCMinutes"); |
|
1234 |
QTest::newRow("Date.prototype.getSeconds") << QString("Date.prototype.getSeconds") << QString("getSeconds"); |
|
1235 |
QTest::newRow("Date.prototype.getUTCSeconds") << QString("Date.prototype.getUTCSeconds") << QString("getUTCSeconds"); |
|
1236 |
QTest::newRow("Date.prototype.getMilliseconds") << QString("Date.prototype.getMilliseconds") << QString("getMilliseconds"); |
|
1237 |
QTest::newRow("Date.prototype.getUTCMilliseconds") << QString("Date.prototype.getUTCMilliseconds") << QString("getUTCMilliseconds"); |
|
1238 |
QTest::newRow("Date.prototype.getTimezoneOffset") << QString("Date.prototype.getTimezoneOffset") << QString("getTimezoneOffset"); |
|
1239 |
QTest::newRow("Date.prototype.setTime") << QString("Date.prototype.setTime") << QString("setTime"); |
|
1240 |
QTest::newRow("Date.prototype.setMilliseconds") << QString("Date.prototype.setMilliseconds") << QString("setMilliseconds"); |
|
1241 |
QTest::newRow("Date.prototype.setUTCMilliseconds") << QString("Date.prototype.setUTCMilliseconds") << QString("setUTCMilliseconds"); |
|
1242 |
QTest::newRow("Date.prototype.setSeconds") << QString("Date.prototype.setSeconds") << QString("setSeconds"); |
|
1243 |
QTest::newRow("Date.prototype.setUTCSeconds") << QString("Date.prototype.setUTCSeconds") << QString("setUTCSeconds"); |
|
1244 |
QTest::newRow("Date.prototype.setMinutes") << QString("Date.prototype.setMinutes") << QString("setMinutes"); |
|
1245 |
QTest::newRow("Date.prototype.setUTCMinutes") << QString("Date.prototype.setUTCMinutes") << QString("setUTCMinutes"); |
|
1246 |
QTest::newRow("Date.prototype.setHours") << QString("Date.prototype.setHours") << QString("setHours"); |
|
1247 |
QTest::newRow("Date.prototype.setUTCHours") << QString("Date.prototype.setUTCHours") << QString("setUTCHours"); |
|
1248 |
QTest::newRow("Date.prototype.setDate") << QString("Date.prototype.setDate") << QString("setDate"); |
|
1249 |
QTest::newRow("Date.prototype.setUTCDate") << QString("Date.prototype.setUTCDate") << QString("setUTCDate"); |
|
1250 |
QTest::newRow("Date.prototype.setMonth") << QString("Date.prototype.setMonth") << QString("setMonth"); |
|
1251 |
QTest::newRow("Date.prototype.setUTCMonth") << QString("Date.prototype.setUTCMonth") << QString("setUTCMonth"); |
|
1252 |
QTest::newRow("Date.prototype.setYear") << QString("Date.prototype.setYear") << QString("setYear"); |
|
1253 |
QTest::newRow("Date.prototype.setFullYear") << QString("Date.prototype.setFullYear") << QString("setFullYear"); |
|
1254 |
QTest::newRow("Date.prototype.setUTCFullYear") << QString("Date.prototype.setUTCFullYear") << QString("setUTCFullYear"); |
|
1255 |
QTest::newRow("Date.prototype.toUTCString") << QString("Date.prototype.toUTCString") << QString("toUTCString"); |
|
1256 |
QTest::newRow("Date.prototype.toGMTString") << QString("Date.prototype.toGMTString") << QString("toGMTString"); |
|
1257 |
||
1258 |
QTest::newRow("Error") << QString("Error") << QString("Error"); |
|
1259 |
// QTest::newRow("Error.prototype.backtrace") << QString("Error.prototype.backtrace") << QString("backtrace"); |
|
1260 |
QTest::newRow("Error.prototype.toString") << QString("Error.prototype.toString") << QString("toString"); |
|
1261 |
||
1262 |
QTest::newRow("EvalError") << QString("EvalError") << QString("EvalError"); |
|
1263 |
QTest::newRow("RangeError") << QString("RangeError") << QString("RangeError"); |
|
1264 |
QTest::newRow("ReferenceError") << QString("ReferenceError") << QString("ReferenceError"); |
|
1265 |
QTest::newRow("SyntaxError") << QString("SyntaxError") << QString("SyntaxError"); |
|
1266 |
QTest::newRow("TypeError") << QString("TypeError") << QString("TypeError"); |
|
1267 |
QTest::newRow("URIError") << QString("URIError") << QString("URIError"); |
|
1268 |
||
1269 |
QTest::newRow("Function") << QString("Function") << QString("Function"); |
|
1270 |
QTest::newRow("Function.prototype.toString") << QString("Function.prototype.toString") << QString("toString"); |
|
1271 |
QTest::newRow("Function.prototype.apply") << QString("Function.prototype.apply") << QString("apply"); |
|
1272 |
QTest::newRow("Function.prototype.call") << QString("Function.prototype.call") << QString("call"); |
|
1273 |
QTest::newRow("Function.prototype.connect") << QString("Function.prototype.connect") << QString("connect"); |
|
1274 |
QTest::newRow("Function.prototype.disconnect") << QString("Function.prototype.disconnect") << QString("disconnect"); |
|
1275 |
||
1276 |
QTest::newRow("Math.abs") << QString("Math.abs") << QString("abs"); |
|
1277 |
QTest::newRow("Math.acos") << QString("Math.acos") << QString("acos"); |
|
1278 |
QTest::newRow("Math.asin") << QString("Math.asin") << QString("asin"); |
|
1279 |
QTest::newRow("Math.atan") << QString("Math.atan") << QString("atan"); |
|
1280 |
QTest::newRow("Math.atan2") << QString("Math.atan2") << QString("atan2"); |
|
1281 |
QTest::newRow("Math.ceil") << QString("Math.ceil") << QString("ceil"); |
|
1282 |
QTest::newRow("Math.cos") << QString("Math.cos") << QString("cos"); |
|
1283 |
QTest::newRow("Math.exp") << QString("Math.exp") << QString("exp"); |
|
1284 |
QTest::newRow("Math.floor") << QString("Math.floor") << QString("floor"); |
|
1285 |
QTest::newRow("Math.log") << QString("Math.log") << QString("log"); |
|
1286 |
QTest::newRow("Math.max") << QString("Math.max") << QString("max"); |
|
1287 |
QTest::newRow("Math.min") << QString("Math.min") << QString("min"); |
|
1288 |
QTest::newRow("Math.pow") << QString("Math.pow") << QString("pow"); |
|
1289 |
QTest::newRow("Math.random") << QString("Math.random") << QString("random"); |
|
1290 |
QTest::newRow("Math.round") << QString("Math.round") << QString("round"); |
|
1291 |
QTest::newRow("Math.sin") << QString("Math.sin") << QString("sin"); |
|
1292 |
QTest::newRow("Math.sqrt") << QString("Math.sqrt") << QString("sqrt"); |
|
1293 |
QTest::newRow("Math.tan") << QString("Math.tan") << QString("tan"); |
|
1294 |
||
1295 |
QTest::newRow("Number") << QString("Number") << QString("Number"); |
|
1296 |
QTest::newRow("Number.prototype.toString") << QString("Number.prototype.toString") << QString("toString"); |
|
1297 |
QTest::newRow("Number.prototype.toLocaleString") << QString("Number.prototype.toLocaleString") << QString("toLocaleString"); |
|
1298 |
QTest::newRow("Number.prototype.valueOf") << QString("Number.prototype.valueOf") << QString("valueOf"); |
|
1299 |
QTest::newRow("Number.prototype.toFixed") << QString("Number.prototype.toFixed") << QString("toFixed"); |
|
1300 |
QTest::newRow("Number.prototype.toExponential") << QString("Number.prototype.toExponential") << QString("toExponential"); |
|
1301 |
QTest::newRow("Number.prototype.toPrecision") << QString("Number.prototype.toPrecision") << QString("toPrecision"); |
|
1302 |
||
1303 |
QTest::newRow("Object") << QString("Object") << QString("Object"); |
|
1304 |
QTest::newRow("Object.prototype.toString") << QString("Object.prototype.toString") << QString("toString"); |
|
1305 |
QTest::newRow("Object.prototype.toLocaleString") << QString("Object.prototype.toLocaleString") << QString("toLocaleString"); |
|
1306 |
QTest::newRow("Object.prototype.valueOf") << QString("Object.prototype.valueOf") << QString("valueOf"); |
|
1307 |
QTest::newRow("Object.prototype.hasOwnProperty") << QString("Object.prototype.hasOwnProperty") << QString("hasOwnProperty"); |
|
1308 |
QTest::newRow("Object.prototype.isPrototypeOf") << QString("Object.prototype.isPrototypeOf") << QString("isPrototypeOf"); |
|
1309 |
QTest::newRow("Object.prototype.propertyIsEnumerable") << QString("Object.prototype.propertyIsEnumerable") << QString("propertyIsEnumerable"); |
|
1310 |
QTest::newRow("Object.prototype.__defineGetter__") << QString("Object.prototype.__defineGetter__") << QString("__defineGetter__"); |
|
1311 |
QTest::newRow("Object.prototype.__defineSetter__") << QString("Object.prototype.__defineSetter__") << QString("__defineSetter__"); |
|
1312 |
||
1313 |
QTest::newRow("RegExp") << QString("RegExp") << QString("RegExp"); |
|
1314 |
QTest::newRow("RegExp.prototype.exec") << QString("RegExp.prototype.exec") << QString("exec"); |
|
1315 |
QTest::newRow("RegExp.prototype.test") << QString("RegExp.prototype.test") << QString("test"); |
|
1316 |
QTest::newRow("RegExp.prototype.toString") << QString("RegExp.prototype.toString") << QString("toString"); |
|
1317 |
||
1318 |
QTest::newRow("String") << QString("String") << QString("String"); |
|
1319 |
QTest::newRow("String.prototype.toString") << QString("String.prototype.toString") << QString("toString"); |
|
1320 |
QTest::newRow("String.prototype.valueOf") << QString("String.prototype.valueOf") << QString("valueOf"); |
|
1321 |
QTest::newRow("String.prototype.charAt") << QString("String.prototype.charAt") << QString("charAt"); |
|
1322 |
QTest::newRow("String.prototype.charCodeAt") << QString("String.prototype.charCodeAt") << QString("charCodeAt"); |
|
1323 |
QTest::newRow("String.prototype.concat") << QString("String.prototype.concat") << QString("concat"); |
|
1324 |
QTest::newRow("String.prototype.indexOf") << QString("String.prototype.indexOf") << QString("indexOf"); |
|
1325 |
QTest::newRow("String.prototype.lastIndexOf") << QString("String.prototype.lastIndexOf") << QString("lastIndexOf"); |
|
1326 |
QTest::newRow("String.prototype.localeCompare") << QString("String.prototype.localeCompare") << QString("localeCompare"); |
|
1327 |
QTest::newRow("String.prototype.match") << QString("String.prototype.match") << QString("match"); |
|
1328 |
QTest::newRow("String.prototype.replace") << QString("String.prototype.replace") << QString("replace"); |
|
1329 |
QTest::newRow("String.prototype.search") << QString("String.prototype.search") << QString("search"); |
|
1330 |
QTest::newRow("String.prototype.slice") << QString("String.prototype.slice") << QString("slice"); |
|
1331 |
QTest::newRow("String.prototype.split") << QString("String.prototype.split") << QString("split"); |
|
1332 |
QTest::newRow("String.prototype.substring") << QString("String.prototype.substring") << QString("substring"); |
|
1333 |
QTest::newRow("String.prototype.toLowerCase") << QString("String.prototype.toLowerCase") << QString("toLowerCase"); |
|
1334 |
QTest::newRow("String.prototype.toLocaleLowerCase") << QString("String.prototype.toLocaleLowerCase") << QString("toLocaleLowerCase"); |
|
1335 |
QTest::newRow("String.prototype.toUpperCase") << QString("String.prototype.toUpperCase") << QString("toUpperCase"); |
|
1336 |
QTest::newRow("String.prototype.toLocaleUpperCase") << QString("String.prototype.toLocaleUpperCase") << QString("toLocaleUpperCase"); |
|
1337 |
} |
|
1338 |
||
1339 |
void tst_QScriptEngine::builtinFunctionNames() |
|
1340 |
{ |
|
1341 |
QFETCH(QString, expression); |
|
1342 |
QFETCH(QString, expectedName); |
|
1343 |
QScriptEngine eng; |
|
1344 |
QScriptValue ret = eng.evaluate(QString::fromLatin1("%0.name").arg(expression)); |
|
1345 |
QVERIFY(ret.isString()); |
|
1346 |
QCOMPARE(ret.toString(), expectedName); |
|
1347 |
} |
|
1348 |
||
1349 |
void tst_QScriptEngine::checkSyntax_data() |
|
1350 |
{ |
|
1351 |
QTest::addColumn<QString>("code"); |
|
1352 |
QTest::addColumn<int>("expectedState"); |
|
1353 |
QTest::addColumn<int>("errorLineNumber"); |
|
1354 |
QTest::addColumn<int>("errorColumnNumber"); |
|
1355 |
QTest::addColumn<QString>("errorMessage"); |
|
1356 |
||
1357 |
QTest::newRow("0") |
|
1358 |
<< QString("0") << int(QScriptSyntaxCheckResult::Valid) |
|
1359 |
<< -1 << -1 << ""; |
|
1360 |
QTest::newRow("if (") |
|
1361 |
<< QString("if (\n") << int(QScriptSyntaxCheckResult::Intermediate) |
|
1362 |
<< 1 << 4 << ""; |
|
1363 |
QTest::newRow("if else") |
|
1364 |
<< QString("\nif else") << int(QScriptSyntaxCheckResult::Error) |
|
1365 |
<< 2 << 4 << "Expected `('"; |
|
1366 |
QTest::newRow("foo[") |
|
1367 |
<< QString("foo[") << int(QScriptSyntaxCheckResult::Error) |
|
1368 |
<< 1 << 4 << ""; |
|
1369 |
QTest::newRow("foo['bar']") |
|
1370 |
<< QString("foo['bar']") << int(QScriptSyntaxCheckResult::Valid) |
|
1371 |
<< -1 << -1 << ""; |
|
1372 |
||
1373 |
QTest::newRow("/*") |
|
1374 |
<< QString("/*") << int(QScriptSyntaxCheckResult::Intermediate) |
|
1375 |
<< 1 << 1 << "Unclosed comment at end of file"; |
|
1376 |
QTest::newRow("/*\nMy comment") |
|
1377 |
<< QString("/*\nMy comment") << int(QScriptSyntaxCheckResult::Intermediate) |
|
1378 |
<< 1 << 1 << "Unclosed comment at end of file"; |
|
1379 |
QTest::newRow("/*\nMy comment */\nfoo = 10") |
|
1380 |
<< QString("/*\nMy comment */\nfoo = 10") << int(QScriptSyntaxCheckResult::Valid) |
|
1381 |
<< -1 << -1 << ""; |
|
1382 |
QTest::newRow("foo = 10 /*") |
|
1383 |
<< QString("foo = 10 /*") << int(QScriptSyntaxCheckResult::Intermediate) |
|
1384 |
<< -1 << -1 << ""; |
|
1385 |
QTest::newRow("foo = 10; /*") |
|
1386 |
<< QString("foo = 10; /*") << int(QScriptSyntaxCheckResult::Intermediate) |
|
1387 |
<< 1 << 11 << "Expected `end of file'"; |
|
1388 |
QTest::newRow("foo = 10 /* My comment */") |
|
1389 |
<< QString("foo = 10 /* My comment */") << int(QScriptSyntaxCheckResult::Valid) |
|
1390 |
<< -1 << -1 << ""; |
|
1391 |
||
1392 |
QTest::newRow("/=/") |
|
1393 |
<< QString("/=/") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; |
|
1394 |
QTest::newRow("/=/g") |
|
1395 |
<< QString("/=/g") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; |
|
1396 |
QTest::newRow("/a/") |
|
1397 |
<< QString("/a/") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; |
|
1398 |
QTest::newRow("/a/g") |
|
1399 |
<< QString("/a/g") << int(QScriptSyntaxCheckResult::Valid) << -1 << -1 << ""; |
|
1400 |
} |
|
1401 |
||
1402 |
void tst_QScriptEngine::checkSyntax() |
|
1403 |
{ |
|
1404 |
QFETCH(QString, code); |
|
1405 |
QFETCH(int, expectedState); |
|
1406 |
QFETCH(int, errorLineNumber); |
|
1407 |
QFETCH(int, errorColumnNumber); |
|
1408 |
QFETCH(QString, errorMessage); |
|
1409 |
||
1410 |
QScriptSyntaxCheckResult result = QScriptEngine::checkSyntax(code); |
|
1411 |
QCOMPARE(result.state(), QScriptSyntaxCheckResult::State(expectedState)); |
|
1412 |
QCOMPARE(result.errorLineNumber(), errorLineNumber); |
|
1413 |
QCOMPARE(result.errorColumnNumber(), errorColumnNumber); |
|
1414 |
QCOMPARE(result.errorMessage(), errorMessage); |
|
1415 |
||
1416 |
// assignment |
|
1417 |
{ |
|
1418 |
QScriptSyntaxCheckResult copy = result; |
|
1419 |
QCOMPARE(copy.state(), result.state()); |
|
1420 |
QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); |
|
1421 |
QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); |
|
1422 |
QCOMPARE(copy.errorMessage(), result.errorMessage()); |
|
1423 |
} |
|
1424 |
{ |
|
1425 |
QScriptSyntaxCheckResult copy(result); |
|
1426 |
QCOMPARE(copy.state(), result.state()); |
|
1427 |
QCOMPARE(copy.errorLineNumber(), result.errorLineNumber()); |
|
1428 |
QCOMPARE(copy.errorColumnNumber(), result.errorColumnNumber()); |
|
1429 |
QCOMPARE(copy.errorMessage(), result.errorMessage()); |
|
1430 |
} |
|
1431 |
} |
|
1432 |
||
1433 |
void tst_QScriptEngine::canEvaluate_data() |
|
1434 |
{ |
|
1435 |
QTest::addColumn<QString>("code"); |
|
1436 |
QTest::addColumn<bool>("expectSuccess"); |
|
1437 |
||
1438 |
QTest::newRow("") << QString("") << true; |
|
1439 |
QTest::newRow("0") << QString("0") << true; |
|
1440 |
QTest::newRow("!") << QString("!\n") << false; |
|
1441 |
QTest::newRow("if (") << QString("if (\n") << false; |
|
1442 |
QTest::newRow("if (10) //") << QString("if (10) //\n") << false; |
|
1443 |
QTest::newRow("a = 1; if (") << QString("a = 1;\nif (\n") << false; |
|
1444 |
QTest::newRow("./test.js") << QString("./test.js\n") << true; |
|
1445 |
QTest::newRow("if (0) print(1)") << QString("if (0)\nprint(1)\n") << true; |
|
1446 |
QTest::newRow("0 = ") << QString("0 = \n") << false; |
|
1447 |
QTest::newRow("0 = 0") << QString("0 = 0\n") << true; |
|
1448 |
QTest::newRow("foo[") << QString("foo[") << true; // automatic semicolon will be inserted |
|
1449 |
QTest::newRow("foo[") << QString("foo[\n") << false; |
|
1450 |
QTest::newRow("foo['bar']") << QString("foo['bar']") << true; |
|
1451 |
||
1452 |
QTest::newRow("/*") << QString("/*") << false; |
|
1453 |
QTest::newRow("/*\nMy comment") << QString("/*\nMy comment") << false; |
|
1454 |
QTest::newRow("/*\nMy comment */\nfoo = 10") << QString("/*\nMy comment */\nfoo = 10") << true; |
|
1455 |
QTest::newRow("foo = 10 /*") << QString("foo = 10 /*") << false; |
|
1456 |
QTest::newRow("foo = 10; /*") << QString("foo = 10; /*") << false; |
|
1457 |
QTest::newRow("foo = 10 /* My comment */") << QString("foo = 10 /* My comment */") << true; |
|
1458 |
||
1459 |
QTest::newRow("/=/") << QString("/=/") << true; |
|
1460 |
QTest::newRow("/=/g") << QString("/=/g") << true; |
|
1461 |
QTest::newRow("/a/") << QString("/a/") << true; |
|
1462 |
QTest::newRow("/a/g") << QString("/a/g") << true; |
|
1463 |
} |
|
1464 |
||
1465 |
void tst_QScriptEngine::canEvaluate() |
|
1466 |
{ |
|
1467 |
QFETCH(QString, code); |
|
1468 |
QFETCH(bool, expectSuccess); |
|
1469 |
||
1470 |
QScriptEngine eng; |
|
1471 |
QCOMPARE(eng.canEvaluate(code), expectSuccess); |
|
1472 |
} |
|
1473 |
||
1474 |
void tst_QScriptEngine::evaluate_data() |
|
1475 |
{ |
|
1476 |
QTest::addColumn<QString>("code"); |
|
1477 |
QTest::addColumn<int>("lineNumber"); |
|
1478 |
QTest::addColumn<bool>("expectHadError"); |
|
1479 |
QTest::addColumn<int>("expectErrorLineNumber"); |
|
1480 |
||
1481 |
QTest::newRow("(newline)") << QString("\n") << -1 << false << -1; |
|
1482 |
QTest::newRow("0 //") << QString("0 //") << -1 << false << -1; |
|
1483 |
QTest::newRow("/* */") << QString("/* */") << -1 << false << -1; |
|
1484 |
QTest::newRow("//") << QString("//") << -1 << false << -1; |
|
1485 |
QTest::newRow("(spaces)") << QString(" ") << -1 << false << -1; |
|
1486 |
QTest::newRow("(empty)") << QString("") << -1 << false << -1; |
|
1487 |
QTest::newRow("0") << QString("0") << -1 << false << -1; |
|
1488 |
QTest::newRow("0=1") << QString("\n0=1;\n") << -1 << true << 2; |
|
1489 |
QTest::newRow("a=1") << QString("a=1\n") << -1 << false << -1; |
|
1490 |
QTest::newRow("a=1;K") << QString("a=1;\nK") << -1 << true << 2; |
|
1491 |
||
1492 |
QTest::newRow("f()") << QString("function f()\n" |
|
1493 |
"{\n" |
|
1494 |
" var a;\n" |
|
1495 |
" var b=\";\n" // here's the error |
|
1496 |
"}\n" |
|
1497 |
"f();\n") |
|
1498 |
<< -1 << true << 4; |
|
1499 |
||
1500 |
QTest::newRow("0") << QString("0") << 10 << false << -1; |
|
1501 |
QTest::newRow("0=1") << QString("\n\n0=1\n") << 10 << true << 13; |
|
1502 |
QTest::newRow("a=1") << QString("a=1\n") << 10 << false << -1; |
|
1503 |
QTest::newRow("a=1;K") << QString("a=1;\n\nK") << 10 << true << 12; |
|
1504 |
||
1505 |
QTest::newRow("f()") << QString("function f()\n" |
|
1506 |
"{\n" |
|
1507 |
" var a;\n" |
|
1508 |
"\n\n" |
|
1509 |
" var b=\";\n" // here's the error |
|
1510 |
"}\n" |
|
1511 |
"f();\n") |
|
1512 |
<< 10 << true << 15; |
|
1513 |
QTest::newRow("functionThatDoesntExist()") |
|
1514 |
<< QString(";\n;\n;\nfunctionThatDoesntExist()") |
|
1515 |
<< -1 << true << 4; |
|
1516 |
QTest::newRow("for (var p in this) { continue labelThatDoesntExist; }") |
|
1517 |
<< QString("for (var p in this) {\ncontinue labelThatDoesntExist; }") |
|
1518 |
<< 4 << true << 5; |
|
1519 |
QTest::newRow("duplicateLabel: { duplicateLabel: ; }") |
|
1520 |
<< QString("duplicateLabel: { duplicateLabel: ; }") |
|
1521 |
<< 12 << true << 12; |
|
1522 |
||
1523 |
QTest::newRow("/=/") << QString("/=/") << -1 << false << -1; |
|
1524 |
QTest::newRow("/=/g") << QString("/=/g") << -1 << false << -1; |
|
1525 |
QTest::newRow("/a/") << QString("/a/") << -1 << false << -1; |
|
1526 |
QTest::newRow("/a/g") << QString("/a/g") << -1 << false << -1; |
|
1527 |
QTest::newRow("/a/gim") << QString("/a/gim") << -1 << false << -1; |
|
1528 |
QTest::newRow("/a/gimp") << QString("/a/gimp") << 1 << true << 1; |
|
1529 |
} |
|
1530 |
||
1531 |
void tst_QScriptEngine::evaluate() |
|
1532 |
{ |
|
1533 |
QFETCH(QString, code); |
|
1534 |
QFETCH(int, lineNumber); |
|
1535 |
QFETCH(bool, expectHadError); |
|
1536 |
QFETCH(int, expectErrorLineNumber); |
|
1537 |
||
1538 |
QScriptEngine eng; |
|
1539 |
QScriptValue ret; |
|
1540 |
if (lineNumber != -1) |
|
1541 |
ret = eng.evaluate(code, /*fileName =*/QString(), lineNumber); |
|
1542 |
else |
|
1543 |
ret = eng.evaluate(code); |
|
1544 |
QCOMPARE(eng.hasUncaughtException(), expectHadError); |
|
1545 |
QCOMPARE(eng.uncaughtExceptionLineNumber(), expectErrorLineNumber); |
|
1546 |
if (eng.hasUncaughtException() && ret.isError()) |
|
1547 |
QVERIFY(ret.property("lineNumber").strictlyEquals(QScriptValue(&eng, expectErrorLineNumber))); |
|
1548 |
else |
|
1549 |
QVERIFY(eng.uncaughtExceptionBacktrace().isEmpty()); |
|
1550 |
} |
|
1551 |
||
1552 |
static QScriptValue eval_nested(QScriptContext *ctx, QScriptEngine *eng) |
|
1553 |
{ |
|
1554 |
QScriptValue result = eng->newObject(); |
|
1555 |
eng->evaluate("var bar = 'local';"); |
|
1556 |
result.setProperty("thisObjectIdBefore", ctx->thisObject().property("id")); |
|
1557 |
QScriptValue evaluatedThisObject = eng->evaluate("this"); |
|
1558 |
result.setProperty("thisObjectIdAfter", ctx->thisObject().property("id")); |
|
1559 |
result.setProperty("evaluatedThisObjectId", evaluatedThisObject.property("id")); |
|
1560 |
result.setProperty("local_bar", eng->evaluate("bar")); |
|
1561 |
||
1562 |
return result; |
|
1563 |
} |
|
1564 |
||
1565 |
void tst_QScriptEngine::nestedEvaluate() |
|
1566 |
{ |
|
1567 |
QScriptEngine eng; |
|
1568 |
QScriptValue fun = eng.newFunction(eval_nested); |
|
1569 |
eng.globalObject().setProperty("fun", fun); |
|
1570 |
{ |
|
1571 |
QScriptValue result = eng.evaluate("o = { id:'foo'}; o.fun = fun; o.fun()"); |
|
1572 |
QCOMPARE(result.property("local_bar").toString(), QString("local")); |
|
1573 |
QCOMPARE(result.property("thisObjectIdBefore").toString(), QString("foo")); |
|
1574 |
QCOMPARE(result.property("thisObjectIdAfter").toString(), QString("foo")); |
|
1575 |
QCOMPARE(result.property("evaluatedThisObjectId").toString(), QString("foo")); |
|
1576 |
QScriptValue bar = eng.evaluate("bar"); |
|
1577 |
QVERIFY(bar.isError()); |
|
1578 |
QCOMPARE(bar.toString(), QString::fromLatin1("ReferenceError: Can't find variable: bar")); |
|
1579 |
} |
|
1580 |
||
1581 |
{ |
|
1582 |
QScriptValue result = fun.call(eng.evaluate("p = { id:'foo' }") , QScriptValueList() ); |
|
1583 |
QCOMPARE(result.property("local_bar").toString(), QString("local")); |
|
1584 |
QCOMPARE(result.property("thisObjectIdBefore").toString(), QString("foo")); |
|
1585 |
QCOMPARE(result.property("thisObjectIdAfter").toString(), QString("foo")); |
|
1586 |
QCOMPARE(result.property("evaluatedThisObjectId").toString(), QString("foo")); |
|
1587 |
QScriptValue bar = eng.evaluate("bar"); |
|
1588 |
QVERIFY(bar.isError()); |
|
1589 |
QCOMPARE(bar.toString(), QString::fromLatin1("ReferenceError: Can't find variable: bar")); |
|
1590 |
} |
|
1591 |
} |
|
1592 |
||
1593 |
void tst_QScriptEngine::uncaughtException() |
|
1594 |
{ |
|
1595 |
QScriptEngine eng; |
|
1596 |
QScriptValue fun = eng.newFunction(myFunction); |
|
1597 |
QScriptValue throwFun = eng.newFunction(myThrowingFunction); |
|
1598 |
for (int x = -1; x < 2; ++x) { |
|
1599 |
{ |
|
1600 |
QScriptValue ret = eng.evaluate("a = 10;\nb = 20;\n0 = 0;\n", /*fileName=*/QString(), /*lineNumber=*/x); |
|
1601 |
QVERIFY(eng.hasUncaughtException()); |
|
1602 |
QCOMPARE(eng.uncaughtExceptionLineNumber(), x+2); |
|
1603 |
QVERIFY(eng.uncaughtException().strictlyEquals(ret)); |
|
1604 |
(void)ret.toString(); |
|
1605 |
QVERIFY(eng.hasUncaughtException()); |
|
1606 |
QVERIFY(eng.uncaughtException().strictlyEquals(ret)); |
|
1607 |
QVERIFY(fun.call().isNull()); |
|
1608 |
QVERIFY(eng.hasUncaughtException()); |
|
1609 |
QCOMPARE(eng.uncaughtExceptionLineNumber(), x+2); |
|
1610 |
QVERIFY(eng.uncaughtException().strictlyEquals(ret)); |
|
1611 |
eng.clearExceptions(); |
|
1612 |
QVERIFY(!eng.hasUncaughtException()); |
|
1613 |
QCOMPARE(eng.uncaughtExceptionLineNumber(), -1); |
|
1614 |
QVERIFY(!eng.uncaughtException().isValid()); |
|
1615 |
||
1616 |
eng.evaluate("2 = 3"); |
|
1617 |
QVERIFY(eng.hasUncaughtException()); |
|
1618 |
QScriptValue ret2 = throwFun.call(); |
|
1619 |
QVERIFY(ret2.isError()); |
|
1620 |
QVERIFY(eng.hasUncaughtException()); |
|
1621 |
QVERIFY(eng.uncaughtException().strictlyEquals(ret2)); |
|
1622 |
QCOMPARE(eng.uncaughtExceptionLineNumber(), 0); |
|
1623 |
eng.clearExceptions(); |
|
1624 |
QVERIFY(!eng.hasUncaughtException()); |
|
1625 |
eng.evaluate("1 + 2"); |
|
1626 |
QVERIFY(!eng.hasUncaughtException()); |
|
1627 |
} |
|
1628 |
{ |
|
1629 |
QScriptValue ret = eng.evaluate("a = 10"); |
|
1630 |
QVERIFY(!eng.hasUncaughtException()); |
|
1631 |
QVERIFY(!eng.uncaughtException().isValid()); |
|
1632 |
} |
|
1633 |
{ |
|
1634 |
QScriptValue ret = eng.evaluate("1 = 2"); |
|
1635 |
QVERIFY(eng.hasUncaughtException()); |
|
1636 |
eng.clearExceptions(); |
|
1637 |
QVERIFY(!eng.hasUncaughtException()); |
|
1638 |
} |
|
1639 |
{ |
|
1640 |
eng.globalObject().setProperty("throwFun", throwFun); |
|
1641 |
eng.evaluate("1;\nthrowFun();"); |
|
1642 |
QVERIFY(eng.hasUncaughtException()); |
|
1643 |
QCOMPARE(eng.uncaughtExceptionLineNumber(), 2); |
|
1644 |
eng.clearExceptions(); |
|
1645 |
QVERIFY(!eng.hasUncaughtException()); |
|
1646 |
} |
|
1647 |
} |
|
1648 |
} |
|
1649 |
||
1650 |
void tst_QScriptEngine::errorMessage_QT679() |
|
1651 |
{ |
|
1652 |
QScriptEngine engine; |
|
1653 |
engine.globalObject().setProperty("foo", 15); |
|
1654 |
QScriptValue error = engine.evaluate("'hello world';\nfoo.bar.blah"); |
|
1655 |
QVERIFY(error.isError()); |
|
1656 |
QCOMPARE(error.toString(), QString::fromLatin1("TypeError: Result of expression 'foo.bar' [undefined] is not an object.")); |
|
1657 |
} |
|
1658 |
||
1659 |
struct Foo { |
|
1660 |
public: |
|
1661 |
int x, y; |
|
1662 |
Foo() : x(-1), y(-1) { } |
|
1663 |
}; |
|
1664 |
||
1665 |
Q_DECLARE_METATYPE(Foo) |
|
1666 |
Q_DECLARE_METATYPE(Foo*) |
|
1667 |
||
1668 |
void tst_QScriptEngine::getSetDefaultPrototype() |
|
1669 |
{ |
|
1670 |
QScriptEngine eng; |
|
1671 |
{ |
|
1672 |
QScriptValue object = eng.newObject(); |
|
1673 |
QCOMPARE(eng.defaultPrototype(qMetaTypeId<int>()).isValid(), false); |
|
1674 |
eng.setDefaultPrototype(qMetaTypeId<int>(), object); |
|
1675 |
QCOMPARE(eng.defaultPrototype(qMetaTypeId<int>()).strictlyEquals(object), true); |
|
1676 |
QScriptValue value = eng.newVariant(int(123)); |
|
1677 |
QCOMPARE(value.prototype().isObject(), true); |
|
1678 |
QCOMPARE(value.prototype().strictlyEquals(object), true); |
|
1679 |
||
1680 |
eng.setDefaultPrototype(qMetaTypeId<int>(), QScriptValue()); |
|
1681 |
QCOMPARE(eng.defaultPrototype(qMetaTypeId<int>()).isValid(), false); |
|
1682 |
QScriptValue value2 = eng.newVariant(int(123)); |
|
1683 |
QCOMPARE(value2.prototype().strictlyEquals(object), false); |
|
1684 |
} |
|
1685 |
{ |
|
1686 |
QScriptValue object = eng.newObject(); |
|
1687 |
QCOMPARE(eng.defaultPrototype(qMetaTypeId<Foo>()).isValid(), false); |
|
1688 |
eng.setDefaultPrototype(qMetaTypeId<Foo>(), object); |
|
1689 |
QCOMPARE(eng.defaultPrototype(qMetaTypeId<Foo>()).strictlyEquals(object), true); |
|
1690 |
QScriptValue value = eng.newVariant(qVariantFromValue(Foo())); |
|
1691 |
QCOMPARE(value.prototype().isObject(), true); |
|
1692 |
QCOMPARE(value.prototype().strictlyEquals(object), true); |
|
1693 |
||
1694 |
eng.setDefaultPrototype(qMetaTypeId<Foo>(), QScriptValue()); |
|
1695 |
QCOMPARE(eng.defaultPrototype(qMetaTypeId<Foo>()).isValid(), false); |
|
1696 |
QScriptValue value2 = eng.newVariant(qVariantFromValue(Foo())); |
|
1697 |
QCOMPARE(value2.prototype().strictlyEquals(object), false); |
|
1698 |
} |
|
1699 |
} |
|
1700 |
||
1701 |
static QScriptValue fooToScriptValue(QScriptEngine *eng, const Foo &foo) |
|
1702 |
{ |
|
1703 |
QScriptValue obj = eng->newObject(); |
|
1704 |
obj.setProperty("x", QScriptValue(eng, foo.x)); |
|
1705 |
obj.setProperty("y", QScriptValue(eng, foo.y)); |
|
1706 |
return obj; |
|
1707 |
} |
|
1708 |
||
1709 |
static void fooFromScriptValue(const QScriptValue &value, Foo &foo) |
|
1710 |
{ |
|
1711 |
foo.x = value.property("x").toInt32(); |
|
1712 |
foo.y = value.property("y").toInt32(); |
|
1713 |
} |
|
1714 |
||
1715 |
static QScriptValue fooToScriptValueV2(QScriptEngine *eng, const Foo &foo) |
|
1716 |
{ |
|
1717 |
return QScriptValue(eng, foo.x); |
|
1718 |
} |
|
1719 |
||
1720 |
static void fooFromScriptValueV2(const QScriptValue &value, Foo &foo) |
|
1721 |
{ |
|
1722 |
foo.x = value.toInt32(); |
|
1723 |
} |
|
1724 |
||
1725 |
Q_DECLARE_METATYPE(QLinkedList<QString>) |
|
1726 |
Q_DECLARE_METATYPE(QList<Foo>) |
|
1727 |
Q_DECLARE_METATYPE(QVector<QChar>) |
|
1728 |
Q_DECLARE_METATYPE(QStack<int>) |
|
1729 |
Q_DECLARE_METATYPE(QQueue<char>) |
|
1730 |
Q_DECLARE_METATYPE(QLinkedList<QStack<int> >) |
|
1731 |
||
1732 |
void tst_QScriptEngine::valueConversion() |
|
1733 |
{ |
|
1734 |
QScriptEngine eng; |
|
1735 |
{ |
|
1736 |
QScriptValue num = qScriptValueFromValue(&eng, 123); |
|
1737 |
QCOMPARE(num.isNumber(), true); |
|
1738 |
QCOMPARE(num.strictlyEquals(QScriptValue(&eng, 123)), true); |
|
1739 |
||
1740 |
int inum = qScriptValueToValue<int>(num); |
|
1741 |
QCOMPARE(inum, 123); |
|
1742 |
||
1743 |
QString snum = qScriptValueToValue<QString>(num); |
|
1744 |
QCOMPARE(snum, QLatin1String("123")); |
|
1745 |
} |
|
1746 |
#ifndef QT_NO_MEMBER_TEMPLATES |
|
1747 |
{ |
|
1748 |
QScriptValue num = eng.toScriptValue(123); |
|
1749 |
QCOMPARE(num.isNumber(), true); |
|
1750 |
QCOMPARE(num.strictlyEquals(QScriptValue(&eng, 123)), true); |
|
1751 |
||
1752 |
int inum = eng.fromScriptValue<int>(num); |
|
1753 |
QCOMPARE(inum, 123); |
|
1754 |
||
1755 |
QString snum = eng.fromScriptValue<QString>(num); |
|
1756 |
QCOMPARE(snum, QLatin1String("123")); |
|
1757 |
} |
|
1758 |
#endif |
|
1759 |
{ |
|
1760 |
QScriptValue num(&eng, 123); |
|
1761 |
QCOMPARE(qScriptValueToValue<char>(num), char(123)); |
|
1762 |
QCOMPARE(qScriptValueToValue<unsigned char>(num), (unsigned char)(123)); |
|
1763 |
QCOMPARE(qScriptValueToValue<short>(num), short(123)); |
|
1764 |
QCOMPARE(qScriptValueToValue<unsigned short>(num), (unsigned short)(123)); |
|
1765 |
QCOMPARE(qScriptValueToValue<float>(num), float(123)); |
|
1766 |
QCOMPARE(qScriptValueToValue<double>(num), double(123)); |
|
1767 |
QCOMPARE(qScriptValueToValue<qlonglong>(num), qlonglong(123)); |
|
1768 |
QCOMPARE(qScriptValueToValue<qulonglong>(num), qulonglong(123)); |
|
1769 |
} |
|
1770 |
{ |
|
1771 |
QScriptValue num(123); |
|
1772 |
QCOMPARE(qScriptValueToValue<char>(num), char(123)); |
|
1773 |
QCOMPARE(qScriptValueToValue<unsigned char>(num), (unsigned char)(123)); |
|
1774 |
QCOMPARE(qScriptValueToValue<short>(num), short(123)); |
|
1775 |
QCOMPARE(qScriptValueToValue<unsigned short>(num), (unsigned short)(123)); |
|
1776 |
QCOMPARE(qScriptValueToValue<float>(num), float(123)); |
|
1777 |
QCOMPARE(qScriptValueToValue<double>(num), double(123)); |
|
1778 |
QCOMPARE(qScriptValueToValue<qlonglong>(num), qlonglong(123)); |
|
1779 |
QCOMPARE(qScriptValueToValue<qulonglong>(num), qulonglong(123)); |
|
1780 |
} |
|
1781 |
||
1782 |
{ |
|
1783 |
QScriptValue num = qScriptValueFromValue(&eng, Q_INT64_C(0x100000000)); |
|
1784 |
QCOMPARE(qScriptValueToValue<qlonglong>(num), Q_INT64_C(0x100000000)); |
|
1785 |
QCOMPARE(qScriptValueToValue<qulonglong>(num), Q_UINT64_C(0x100000000)); |
|
1786 |
} |
|
1787 |
||
1788 |
{ |
|
1789 |
QChar c = QLatin1Char('c'); |
|
1790 |
QScriptValue str = QScriptValue(&eng, "ciao"); |
|
1791 |
QCOMPARE(qScriptValueToValue<QChar>(str), c); |
|
1792 |
QScriptValue code = QScriptValue(&eng, c.unicode()); |
|
1793 |
QCOMPARE(qScriptValueToValue<QChar>(code), c); |
|
1794 |
QCOMPARE(qScriptValueToValue<QChar>(qScriptValueFromValue(&eng, c)), c); |
|
1795 |
} |
|
1796 |
||
1797 |
{ |
|
1798 |
// a type that we don't have built-in conversion of |
|
1799 |
// (it's stored as a variant) |
|
1800 |
QTime tm(1, 2, 3, 4); |
|
1801 |
QScriptValue val = qScriptValueFromValue(&eng, tm); |
|
1802 |
QCOMPARE(qScriptValueToValue<QTime>(val), tm); |
|
1803 |
} |
|
1804 |
||
1805 |
{ |
|
1806 |
Foo foo; |
|
1807 |
foo.x = 12; |
|
1808 |
foo.y = 34; |
|
1809 |
QScriptValue fooVal = qScriptValueFromValue(&eng, foo); |
|
1810 |
QCOMPARE(fooVal.isVariant(), true); |
|
1811 |
||
1812 |
Foo foo2 = qScriptValueToValue<Foo>(fooVal); |
|
1813 |
QCOMPARE(foo2.x, foo.x); |
|
1814 |
QCOMPARE(foo2.y, foo.y); |
|
1815 |
} |
|
1816 |
||
1817 |
qScriptRegisterMetaType<Foo>(&eng, fooToScriptValue, fooFromScriptValue); |
|
1818 |
||
1819 |
{ |
|
1820 |
Foo foo; |
|
1821 |
foo.x = 12; |
|
1822 |
foo.y = 34; |
|
1823 |
QScriptValue fooVal = qScriptValueFromValue(&eng, foo); |
|
1824 |
QCOMPARE(fooVal.isObject(), true); |
|
1825 |
QVERIFY(fooVal.prototype().strictlyEquals(eng.evaluate("Object.prototype"))); |
|
1826 |
QCOMPARE(fooVal.property("x").strictlyEquals(QScriptValue(&eng, 12)), true); |
|
1827 |
QCOMPARE(fooVal.property("y").strictlyEquals(QScriptValue(&eng, 34)), true); |
|
1828 |
fooVal.setProperty("x", QScriptValue(&eng, 56)); |
|
1829 |
fooVal.setProperty("y", QScriptValue(&eng, 78)); |
|
1830 |
||
1831 |
Foo foo2 = qScriptValueToValue<Foo>(fooVal); |
|
1832 |
QCOMPARE(foo2.x, 56); |
|
1833 |
QCOMPARE(foo2.y, 78); |
|
1834 |
||
1835 |
QScriptValue fooProto = eng.newObject(); |
|
1836 |
eng.setDefaultPrototype(qMetaTypeId<Foo>(), fooProto); |
|
1837 |
QScriptValue fooVal2 = qScriptValueFromValue(&eng, foo2); |
|
1838 |
QVERIFY(fooVal2.prototype().strictlyEquals(fooProto)); |
|
1839 |
QVERIFY(fooVal2.property("x").strictlyEquals(QScriptValue(&eng, 56))); |
|
1840 |
QVERIFY(fooVal2.property("y").strictlyEquals(QScriptValue(&eng, 78))); |
|
1841 |
} |
|
1842 |
||
1843 |
qScriptRegisterSequenceMetaType<QLinkedList<QString> >(&eng); |
|
1844 |
||
1845 |
{ |
|
1846 |
QLinkedList<QString> lst; |
|
1847 |
lst << QLatin1String("foo") << QLatin1String("bar"); |
|
1848 |
QScriptValue lstVal = qScriptValueFromValue(&eng, lst); |
|
1849 |
QCOMPARE(lstVal.isArray(), true); |
|
1850 |
QCOMPARE(lstVal.property("length").toInt32(), 2); |
|
1851 |
QCOMPARE(lstVal.property("0").isString(), true); |
|
1852 |
QCOMPARE(lstVal.property("0").toString(), QLatin1String("foo")); |
|
1853 |
QCOMPARE(lstVal.property("1").isString(), true); |
|
1854 |
QCOMPARE(lstVal.property("1").toString(), QLatin1String("bar")); |
|
1855 |
} |
|
1856 |
||
1857 |
qScriptRegisterSequenceMetaType<QList<Foo> >(&eng); |
|
1858 |
qScriptRegisterSequenceMetaType<QStack<int> >(&eng); |
|
1859 |
qScriptRegisterSequenceMetaType<QVector<QChar> >(&eng); |
|
1860 |
qScriptRegisterSequenceMetaType<QQueue<char> >(&eng); |
|
1861 |
qScriptRegisterSequenceMetaType<QLinkedList<QStack<int> > >(&eng); |
|
1862 |
||
1863 |
{ |
|
1864 |
QLinkedList<QStack<int> > lst; |
|
1865 |
QStack<int> first; first << 13 << 49; lst << first; |
|
1866 |
QStack<int> second; second << 99999;lst << second; |
|
1867 |
QScriptValue lstVal = qScriptValueFromValue(&eng, lst); |
|
1868 |
QCOMPARE(lstVal.isArray(), true); |
|
1869 |
QCOMPARE(lstVal.property("length").toInt32(), 2); |
|
1870 |
QCOMPARE(lstVal.property("0").isArray(), true); |
|
1871 |
QCOMPARE(lstVal.property("0").property("length").toInt32(), 2); |
|
1872 |
QCOMPARE(lstVal.property("0").property("0").toInt32(), first.at(0)); |
|
1873 |
QCOMPARE(lstVal.property("0").property("1").toInt32(), first.at(1)); |
|
1874 |
QCOMPARE(lstVal.property("1").isArray(), true); |
|
1875 |
QCOMPARE(lstVal.property("1").property("length").toInt32(), 1); |
|
1876 |
QCOMPARE(lstVal.property("1").property("0").toInt32(), second.at(0)); |
|
1877 |
QCOMPARE(qscriptvalue_cast<QStack<int> >(lstVal.property("0")), first); |
|
1878 |
QCOMPARE(qscriptvalue_cast<QStack<int> >(lstVal.property("1")), second); |
|
1879 |
QCOMPARE(qscriptvalue_cast<QLinkedList<QStack<int> > >(lstVal), lst); |
|
1880 |
} |
|
1881 |
||
1882 |
// pointers |
|
1883 |
{ |
|
1884 |
Foo foo; |
|
1885 |
{ |
|
1886 |
QScriptValue v = qScriptValueFromValue(&eng, &foo); |
|
1887 |
Foo *pfoo = qscriptvalue_cast<Foo*>(v); |
|
1888 |
QCOMPARE(pfoo, &foo); |
|
1889 |
} |
|
1890 |
{ |
|
1891 |
Foo *pfoo = 0; |
|
1892 |
QScriptValue v = qScriptValueFromValue(&eng, pfoo); |
|
1893 |
QCOMPARE(v.isNull(), true); |
|
1894 |
QVERIFY(qscriptvalue_cast<Foo*>(v) == 0); |
|
1895 |
} |
|
1896 |
} |
|
1897 |
||
1898 |
// QList<int> and QObjectList should be converted from/to arrays by default |
|
1899 |
{ |
|
1900 |
QList<int> lst; |
|
1901 |
lst << 1 << 2 << 3; |
|
1902 |
QScriptValue val = qScriptValueFromValue(&eng, lst); |
|
1903 |
QVERIFY(val.isArray()); |
|
1904 |
QCOMPARE(val.property("length").toInt32(), lst.size()); |
|
1905 |
QCOMPARE(val.property(0).toInt32(), lst.at(0)); |
|
1906 |
QCOMPARE(val.property(1).toInt32(), lst.at(1)); |
|
1907 |
QCOMPARE(val.property(2).toInt32(), lst.at(2)); |
|
1908 |
||
1909 |
QCOMPARE(qscriptvalue_cast<QList<int> >(val), lst); |
|
1910 |
} |
|
1911 |
{ |
|
1912 |
QObjectList lst; |
|
1913 |
lst << this; |
|
1914 |
QScriptValue val = qScriptValueFromValue(&eng, lst); |
|
1915 |
QVERIFY(val.isArray()); |
|
1916 |
QCOMPARE(val.property("length").toInt32(), lst.size()); |
|
1917 |
QCOMPARE(val.property(0).toQObject(), (QObject *)this); |
|
1918 |
||
1919 |
QCOMPARE(qscriptvalue_cast<QObjectList>(val), lst); |
|
1920 |
} |
|
1921 |
||
1922 |
// qScriptValueFromValue() should be "smart" when the argument is a QVariant |
|
1923 |
{ |
|
1924 |
QScriptValue val = qScriptValueFromValue(&eng, QVariant()); |
|
1925 |
QVERIFY(!val.isVariant()); |
|
1926 |
QVERIFY(val.isUndefined()); |
|
1927 |
} |
|
1928 |
{ |
|
1929 |
QScriptValue val = qScriptValueFromValue(&eng, QVariant(true)); |
|
1930 |
QVERIFY(!val.isVariant()); |
|
1931 |
QVERIFY(val.isBoolean()); |
|
1932 |
QCOMPARE(val.toBoolean(), true); |
|
1933 |
} |
|
1934 |
{ |
|
1935 |
QScriptValue val = qScriptValueFromValue(&eng, QVariant(int(123))); |
|
1936 |
QVERIFY(!val.isVariant()); |
|
1937 |
QVERIFY(val.isNumber()); |
|
1938 |
QCOMPARE(val.toNumber(), qsreal(123)); |
|
1939 |
} |
|
1940 |
{ |
|
1941 |
QScriptValue val = qScriptValueFromValue(&eng, QVariant(qsreal(1.25))); |
|
1942 |
QVERIFY(!val.isVariant()); |
|
1943 |
QVERIFY(val.isNumber()); |
|
1944 |
QCOMPARE(val.toNumber(), qsreal(1.25)); |
|
1945 |
} |
|
1946 |
{ |
|
1947 |
QString str = QString::fromLatin1("ciao"); |
|
1948 |
QScriptValue val = qScriptValueFromValue(&eng, QVariant(str)); |
|
1949 |
QVERIFY(!val.isVariant()); |
|
1950 |
QVERIFY(val.isString()); |
|
1951 |
QCOMPARE(val.toString(), str); |
|
1952 |
} |
|
1953 |
{ |
|
1954 |
QScriptValue val = qScriptValueFromValue(&eng, qVariantFromValue((QObject*)this)); |
|
1955 |
QVERIFY(!val.isVariant()); |
|
1956 |
QVERIFY(val.isQObject()); |
|
1957 |
QCOMPARE(val.toQObject(), (QObject*)this); |
|
1958 |
} |
|
1959 |
{ |
|
1960 |
QVariant var = qVariantFromValue(QPoint(123, 456)); |
|
1961 |
QScriptValue val = qScriptValueFromValue(&eng, var); |
|
1962 |
QVERIFY(val.isVariant()); |
|
1963 |
QCOMPARE(val.toVariant(), var); |
|
1964 |
} |
|
1965 |
||
1966 |
// task 248802 |
|
1967 |
qScriptRegisterMetaType<Foo>(&eng, fooToScriptValueV2, fooFromScriptValueV2); |
|
1968 |
{ |
|
1969 |
QScriptValue num(&eng, 123); |
|
1970 |
Foo foo = qScriptValueToValue<Foo>(num); |
|
1971 |
QCOMPARE(foo.x, 123); |
|
1972 |
} |
|
1973 |
{ |
|
1974 |
QScriptValue num(123); |
|
1975 |
Foo foo = qScriptValueToValue<Foo>(num); |
|
1976 |
QCOMPARE(foo.x, -1); |
|
1977 |
} |
|
1978 |
{ |
|
1979 |
QScriptValue str(&eng, "123"); |
|
1980 |
Foo foo = qScriptValueToValue<Foo>(str); |
|
1981 |
QCOMPARE(foo.x, 123); |
|
1982 |
} |
|
1983 |
||
1984 |
// more built-in types |
|
1985 |
{ |
|
1986 |
QScriptValue val = qScriptValueFromValue(&eng, uint(123)); |
|
1987 |
QVERIFY(val.isNumber()); |
|
1988 |
QCOMPARE(val.toInt32(), 123); |
|
1989 |
} |
|
1990 |
{ |
|
1991 |
QScriptValue val = qScriptValueFromValue(&eng, qulonglong(123)); |
|
1992 |
QVERIFY(val.isNumber()); |
|
1993 |
QCOMPARE(val.toInt32(), 123); |
|
1994 |
} |
|
1995 |
{ |
|
1996 |
QScriptValue val = qScriptValueFromValue(&eng, float(123)); |
|
1997 |
QVERIFY(val.isNumber()); |
|
1998 |
QCOMPARE(val.toInt32(), 123); |
|
1999 |
} |
|
2000 |
{ |
|
2001 |
QScriptValue val = qScriptValueFromValue(&eng, short(123)); |
|
2002 |
QVERIFY(val.isNumber()); |
|
2003 |
QCOMPARE(val.toInt32(), 123); |
|
2004 |
} |
|
2005 |
{ |
|
2006 |
QScriptValue val = qScriptValueFromValue(&eng, ushort(123)); |
|
2007 |
QVERIFY(val.isNumber()); |
|
2008 |
QCOMPARE(val.toInt32(), 123); |
|
2009 |
} |
|
2010 |
{ |
|
2011 |
QScriptValue val = qScriptValueFromValue(&eng, char(123)); |
|
2012 |
QVERIFY(val.isNumber()); |
|
2013 |
QCOMPARE(val.toInt32(), 123); |
|
2014 |
} |
|
2015 |
{ |
|
2016 |
QScriptValue val = qScriptValueFromValue(&eng, uchar(123)); |
|
2017 |
QVERIFY(val.isNumber()); |
|
2018 |
QCOMPARE(val.toInt32(), 123); |
|
2019 |
} |
|
2020 |
{ |
|
2021 |
QDateTime in = QDateTime::currentDateTime(); |
|
2022 |
QScriptValue val = qScriptValueFromValue(&eng, in); |
|
2023 |
QVERIFY(val.isDate()); |
|
2024 |
QCOMPARE(val.toDateTime(), in); |
|
2025 |
} |
|
2026 |
{ |
|
2027 |
QDate in = QDate::currentDate(); |
|
2028 |
QScriptValue val = qScriptValueFromValue(&eng, in); |
|
2029 |
QVERIFY(val.isDate()); |
|
2030 |
QCOMPARE(val.toDateTime().date(), in); |
|
2031 |
} |
|
2032 |
{ |
|
2033 |
QRegExp in = QRegExp("foo"); |
|
2034 |
QScriptValue val = qScriptValueFromValue(&eng, in); |
|
2035 |
QVERIFY(val.isRegExp()); |
|
2036 |
QRegExp out = val.toRegExp(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2037 |
QEXPECT_FAIL("", "QTBUG-6136: JSC-based back-end doesn't preserve QRegExp::patternSyntax (always uses RegExp2)", Continue); |
0 | 2038 |
QCOMPARE(out.patternSyntax(), in.patternSyntax()); |
2039 |
QCOMPARE(out.pattern(), in.pattern()); |
|
2040 |
QCOMPARE(out.caseSensitivity(), in.caseSensitivity()); |
|
2041 |
QCOMPARE(out.isMinimal(), in.isMinimal()); |
|
2042 |
} |
|
2043 |
{ |
|
2044 |
QRegExp in = QRegExp("foo", Qt::CaseSensitive, QRegExp::RegExp2); |
|
2045 |
QScriptValue val = qScriptValueFromValue(&eng, in); |
|
2046 |
QVERIFY(val.isRegExp()); |
|
2047 |
QCOMPARE(val.toRegExp(), in); |
|
2048 |
} |
|
2049 |
{ |
|
2050 |
QRegExp in = QRegExp("foo"); |
|
2051 |
in.setMinimal(true); |
|
2052 |
QScriptValue val = qScriptValueFromValue(&eng, in); |
|
2053 |
QVERIFY(val.isRegExp()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2054 |
QEXPECT_FAIL("", "QTBUG-6136: JSC-based back-end doesn't preserve QRegExp::minimal (always false)", Continue); |
0 | 2055 |
QCOMPARE(val.toRegExp().isMinimal(), in.isMinimal()); |
2056 |
} |
|
2057 |
} |
|
2058 |
||
2059 |
static QScriptValue __import__(QScriptContext *ctx, QScriptEngine *eng) |
|
2060 |
{ |
|
2061 |
return eng->importExtension(ctx->argument(0).toString()); |
|
2062 |
} |
|
2063 |
||
2064 |
void tst_QScriptEngine::importExtension() |
|
2065 |
{ |
|
2066 |
QStringList libPaths = QCoreApplication::instance()->libraryPaths(); |
|
2067 |
QCoreApplication::instance()->setLibraryPaths(QStringList() << SRCDIR); |
|
2068 |
||
2069 |
QStringList availableExtensions; |
|
2070 |
{ |
|
2071 |
QScriptEngine eng; |
|
2072 |
QVERIFY(eng.importedExtensions().isEmpty()); |
|
2073 |
QStringList ret = eng.availableExtensions(); |
|
2074 |
QCOMPARE(ret.size(), 4); |
|
2075 |
QCOMPARE(ret.at(0), QString::fromLatin1("com")); |
|
2076 |
QCOMPARE(ret.at(1), QString::fromLatin1("com.trolltech")); |
|
2077 |
QCOMPARE(ret.at(2), QString::fromLatin1("com.trolltech.recursive")); |
|
2078 |
QCOMPARE(ret.at(3), QString::fromLatin1("com.trolltech.syntaxerror")); |
|
2079 |
availableExtensions = ret; |
|
2080 |
} |
|
2081 |
||
2082 |
// try to import something that doesn't exist |
|
2083 |
{ |
|
2084 |
QScriptEngine eng; |
|
2085 |
QScriptValue ret = eng.importExtension("this.extension.does.not.exist"); |
|
2086 |
QCOMPARE(eng.hasUncaughtException(), true); |
|
2087 |
QCOMPARE(ret.isError(), true); |
|
2088 |
QCOMPARE(ret.toString(), QString::fromLatin1("Error: Unable to import this.extension.does.not.exist: no such extension")); |
|
2089 |
} |
|
2090 |
||
2091 |
{ |
|
2092 |
QScriptEngine eng; |
|
2093 |
for (int x = 0; x < 2; ++x) { |
|
2094 |
QCOMPARE(eng.globalObject().property("com").isValid(), x == 1); |
|
2095 |
QScriptValue ret = eng.importExtension("com.trolltech"); |
|
2096 |
QCOMPARE(eng.hasUncaughtException(), false); |
|
2097 |
QCOMPARE(ret.isUndefined(), true); |
|
2098 |
||
2099 |
QScriptValue com = eng.globalObject().property("com"); |
|
2100 |
QCOMPARE(com.isObject(), true); |
|
2101 |
QCOMPARE(com.property("wasDefinedAlready") |
|
2102 |
.strictlyEquals(QScriptValue(&eng, false)), true); |
|
2103 |
QCOMPARE(com.property("name") |
|
2104 |
.strictlyEquals(QScriptValue(&eng, "com")), true); |
|
2105 |
QCOMPARE(com.property("level") |
|
2106 |
.strictlyEquals(QScriptValue(&eng, 1)), true); |
|
2107 |
QVERIFY(com.property("originalPostInit").isUndefined()); |
|
2108 |
QVERIFY(com.property("postInitCallCount").strictlyEquals(1)); |
|
2109 |
||
2110 |
QScriptValue trolltech = com.property("trolltech"); |
|
2111 |
QCOMPARE(trolltech.isObject(), true); |
|
2112 |
QCOMPARE(trolltech.property("wasDefinedAlready") |
|
2113 |
.strictlyEquals(QScriptValue(&eng, false)), true); |
|
2114 |
QCOMPARE(trolltech.property("name") |
|
2115 |
.strictlyEquals(QScriptValue(&eng, "com.trolltech")), true); |
|
2116 |
QCOMPARE(trolltech.property("level") |
|
2117 |
.strictlyEquals(QScriptValue(&eng, 2)), true); |
|
2118 |
QVERIFY(trolltech.property("originalPostInit").isUndefined()); |
|
2119 |
QVERIFY(trolltech.property("postInitCallCount").strictlyEquals(1)); |
|
2120 |
} |
|
2121 |
QStringList imp = eng.importedExtensions(); |
|
2122 |
QCOMPARE(imp.size(), 2); |
|
2123 |
QCOMPARE(imp.at(0), QString::fromLatin1("com")); |
|
2124 |
QCOMPARE(imp.at(1), QString::fromLatin1("com.trolltech")); |
|
2125 |
QCOMPARE(eng.availableExtensions(), availableExtensions); |
|
2126 |
} |
|
2127 |
||
2128 |
// recursive import should throw an error |
|
2129 |
{ |
|
2130 |
QScriptEngine eng; |
|
2131 |
QVERIFY(eng.importedExtensions().isEmpty()); |
|
2132 |
eng.globalObject().setProperty("__import__", eng.newFunction(__import__)); |
|
2133 |
QScriptValue ret = eng.importExtension("com.trolltech.recursive"); |
|
2134 |
QCOMPARE(eng.hasUncaughtException(), true); |
|
2135 |
QVERIFY(ret.isError()); |
|
2136 |
QCOMPARE(ret.toString(), QString::fromLatin1("Error: recursive import of com.trolltech.recursive")); |
|
2137 |
QStringList imp = eng.importedExtensions(); |
|
2138 |
QCOMPARE(imp.size(), 2); |
|
2139 |
QCOMPARE(imp.at(0), QString::fromLatin1("com")); |
|
2140 |
QCOMPARE(imp.at(1), QString::fromLatin1("com.trolltech")); |
|
2141 |
QCOMPARE(eng.availableExtensions(), availableExtensions); |
|
2142 |
} |
|
2143 |
||
2144 |
{ |
|
2145 |
QScriptEngine eng; |
|
2146 |
eng.globalObject().setProperty("__import__", eng.newFunction(__import__)); |
|
2147 |
for (int x = 0; x < 2; ++x) { |
|
2148 |
if (x == 0) |
|
2149 |
QVERIFY(eng.importedExtensions().isEmpty()); |
|
2150 |
QScriptValue ret = eng.importExtension("com.trolltech.syntaxerror"); |
|
2151 |
QVERIFY(eng.hasUncaughtException()); |
|
2152 |
QEXPECT_FAIL("", "JSC throws syntax error eagerly", Continue); |
|
2153 |
QCOMPARE(eng.uncaughtExceptionLineNumber(), 4); |
|
2154 |
QVERIFY(ret.isError()); |
|
2155 |
QCOMPARE(ret.property("message").toString(), QLatin1String("Parse error")); |
|
2156 |
} |
|
2157 |
QStringList imp = eng.importedExtensions(); |
|
2158 |
QCOMPARE(imp.size(), 2); |
|
2159 |
QCOMPARE(imp.at(0), QString::fromLatin1("com")); |
|
2160 |
QCOMPARE(imp.at(1), QString::fromLatin1("com.trolltech")); |
|
2161 |
QCOMPARE(eng.availableExtensions(), availableExtensions); |
|
2162 |
} |
|
2163 |
||
2164 |
QCoreApplication::instance()->setLibraryPaths(libPaths); |
|
2165 |
} |
|
2166 |
||
2167 |
static QScriptValue recurse(QScriptContext *ctx, QScriptEngine *eng) |
|
2168 |
{ |
|
2169 |
Q_UNUSED(eng); |
|
2170 |
return ctx->callee().call(); |
|
2171 |
} |
|
2172 |
||
2173 |
static QScriptValue recurse2(QScriptContext *ctx, QScriptEngine *eng) |
|
2174 |
{ |
|
2175 |
Q_UNUSED(eng); |
|
2176 |
return ctx->callee().construct(); |
|
2177 |
} |
|
2178 |
||
2179 |
void tst_QScriptEngine::infiniteRecursion() |
|
2180 |
{ |
|
2181 |
const QString stackOverflowError = QString::fromLatin1("RangeError: Maximum call stack size exceeded."); |
|
2182 |
QScriptEngine eng; |
|
2183 |
{ |
|
2184 |
QScriptValue ret = eng.evaluate("function foo() { foo(); }; foo();"); |
|
2185 |
QCOMPARE(ret.isError(), true); |
|
2186 |
QCOMPARE(ret.toString(), stackOverflowError); |
|
2187 |
} |
|
2188 |
#if 0 //The native C++ stack overflow before the JS stack |
|
2189 |
{ |
|
2190 |
QScriptValue fun = eng.newFunction(recurse); |
|
2191 |
QScriptValue ret = fun.call(); |
|
2192 |
QCOMPARE(ret.isError(), true); |
|
2193 |
QCOMPARE(ret.toString(), stackOverflowError); |
|
2194 |
} |
|
2195 |
{ |
|
2196 |
QScriptValue fun = eng.newFunction(recurse2); |
|
2197 |
QScriptValue ret = fun.construct(); |
|
2198 |
QCOMPARE(ret.isError(), true); |
|
2199 |
QCOMPARE(ret.toString(), stackOverflowError); |
|
2200 |
} |
|
2201 |
#endif |
|
2202 |
} |
|
2203 |
||
2204 |
struct Bar { |
|
2205 |
int a; |
|
2206 |
}; |
|
2207 |
||
2208 |
struct Baz : public Bar { |
|
2209 |
int b; |
|
2210 |
}; |
|
2211 |
||
2212 |
Q_DECLARE_METATYPE(Bar*) |
|
2213 |
Q_DECLARE_METATYPE(Baz*) |
|
2214 |
||
2215 |
Q_DECLARE_METATYPE(QGradient) |
|
2216 |
Q_DECLARE_METATYPE(QGradient*) |
|
2217 |
Q_DECLARE_METATYPE(QLinearGradient) |
|
2218 |
||
2219 |
class Zoo : public QObject |
|
2220 |
{ |
|
2221 |
Q_OBJECT |
|
2222 |
public: |
|
2223 |
Zoo() { } |
|
2224 |
public slots: |
|
2225 |
Baz *toBaz(Bar *b) { return reinterpret_cast<Baz*>(b); } |
|
2226 |
}; |
|
2227 |
||
2228 |
void tst_QScriptEngine::castWithPrototypeChain() |
|
2229 |
{ |
|
2230 |
QScriptEngine eng; |
|
2231 |
Bar bar; |
|
2232 |
Baz baz; |
|
2233 |
QScriptValue barProto = qScriptValueFromValue(&eng, &bar); |
|
2234 |
QScriptValue bazProto = qScriptValueFromValue(&eng, &baz); |
|
2235 |
eng.setDefaultPrototype(qMetaTypeId<Bar*>(), barProto); |
|
2236 |
eng.setDefaultPrototype(qMetaTypeId<Baz*>(), bazProto); |
|
2237 |
||
2238 |
Baz baz2; |
|
2239 |
baz2.a = 123; |
|
2240 |
baz2.b = 456; |
|
2241 |
QScriptValue baz2Value = qScriptValueFromValue(&eng, &baz2); |
|
2242 |
{ |
|
2243 |
Baz *pbaz = qscriptvalue_cast<Baz*>(baz2Value); |
|
2244 |
QVERIFY(pbaz != 0); |
|
2245 |
QCOMPARE(pbaz->b, baz2.b); |
|
2246 |
||
2247 |
Zoo zoo; |
|
2248 |
QScriptValue scriptZoo = eng.newQObject(&zoo); |
|
2249 |
QScriptValue toBaz = scriptZoo.property("toBaz"); |
|
2250 |
QVERIFY(toBaz.isFunction()); |
|
2251 |
||
2252 |
// no relation between Bar and Baz's proto --> casting fails |
|
2253 |
{ |
|
2254 |
Bar *pbar = qscriptvalue_cast<Bar*>(baz2Value); |
|
2255 |
QVERIFY(pbar == 0); |
|
2256 |
} |
|
2257 |
||
2258 |
{ |
|
2259 |
QScriptValue ret = toBaz.call(scriptZoo, QScriptValueList() << baz2Value); |
|
2260 |
QVERIFY(ret.isError()); |
|
2261 |
QCOMPARE(ret.toString(), QLatin1String("TypeError: incompatible type of argument(s) in call to toBaz(); candidates were\n toBaz(Bar*)")); |
|
2262 |
} |
|
2263 |
||
2264 |
// establish chain -- now casting should work |
|
2265 |
bazProto.setPrototype(barProto); |
|
2266 |
||
2267 |
{ |
|
2268 |
Bar *pbar = qscriptvalue_cast<Bar*>(baz2Value); |
|
2269 |
QVERIFY(pbar != 0); |
|
2270 |
QCOMPARE(pbar->a, baz2.a); |
|
2271 |
} |
|
2272 |
||
2273 |
{ |
|
2274 |
QScriptValue ret = toBaz.call(scriptZoo, QScriptValueList() << baz2Value); |
|
2275 |
QVERIFY(!ret.isError()); |
|
2276 |
QCOMPARE(qscriptvalue_cast<Baz*>(ret), pbaz); |
|
2277 |
} |
|
2278 |
} |
|
2279 |
||
2280 |
bazProto.setPrototype(barProto.prototype()); // kill chain |
|
2281 |
{ |
|
2282 |
Baz *pbaz = qscriptvalue_cast<Baz*>(baz2Value); |
|
2283 |
QVERIFY(pbaz != 0); |
|
2284 |
// should not work anymore |
|
2285 |
Bar *pbar = qscriptvalue_cast<Bar*>(baz2Value); |
|
2286 |
QVERIFY(pbar == 0); |
|
2287 |
} |
|
2288 |
||
2289 |
bazProto.setPrototype(eng.newQObject(this)); |
|
2290 |
{ |
|
2291 |
Baz *pbaz = qscriptvalue_cast<Baz*>(baz2Value); |
|
2292 |
QVERIFY(pbaz != 0); |
|
2293 |
// should not work now either |
|
2294 |
Bar *pbar = qscriptvalue_cast<Bar*>(baz2Value); |
|
2295 |
QVERIFY(pbar == 0); |
|
2296 |
} |
|
2297 |
||
2298 |
{ |
|
2299 |
QScriptValue b = qScriptValueFromValue(&eng, QBrush()); |
|
2300 |
b.setPrototype(barProto); |
|
2301 |
// this shows that a "wrong" cast is possible, if you |
|
2302 |
// don't play by the rules (the pointer is actually a QBrush*)... |
|
2303 |
Bar *pbar = qscriptvalue_cast<Bar*>(b); |
|
2304 |
QVERIFY(pbar != 0); |
|
2305 |
} |
|
2306 |
||
2307 |
{ |
|
2308 |
QScriptValue gradientProto = qScriptValueFromValue(&eng, QGradient()); |
|
2309 |
QScriptValue linearGradientProto = qScriptValueFromValue(&eng, QLinearGradient()); |
|
2310 |
linearGradientProto.setPrototype(gradientProto); |
|
2311 |
QLinearGradient lg(10, 20, 30, 40); |
|
2312 |
QScriptValue linearGradient = qScriptValueFromValue(&eng, lg); |
|
2313 |
{ |
|
2314 |
QGradient *pgrad = qscriptvalue_cast<QGradient*>(linearGradient); |
|
2315 |
QVERIFY(pgrad == 0); |
|
2316 |
} |
|
2317 |
linearGradient.setPrototype(linearGradientProto); |
|
2318 |
{ |
|
2319 |
QGradient *pgrad = qscriptvalue_cast<QGradient*>(linearGradient); |
|
2320 |
QVERIFY(pgrad != 0); |
|
2321 |
QCOMPARE(pgrad->type(), QGradient::LinearGradient); |
|
2322 |
QLinearGradient *plingrad = static_cast<QLinearGradient*>(pgrad); |
|
2323 |
QCOMPARE(plingrad->start(), lg.start()); |
|
2324 |
QCOMPARE(plingrad->finalStop(), lg.finalStop()); |
|
2325 |
} |
|
2326 |
} |
|
2327 |
} |
|
2328 |
||
2329 |
class Klazz : public QWidget, |
|
2330 |
public QStandardItem, |
|
2331 |
public QGraphicsItem |
|
2332 |
{ |
|
2333 |
Q_OBJECT |
|
2334 |
public: |
|
2335 |
Klazz(QWidget *parent = 0) : QWidget(parent) { } |
|
2336 |
virtual QRectF boundingRect() const { return QRectF(); } |
|
2337 |
virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) { } |
|
2338 |
}; |
|
2339 |
||
2340 |
Q_DECLARE_METATYPE(Klazz*) |
|
2341 |
Q_DECLARE_METATYPE(QStandardItem*) |
|
2342 |
||
2343 |
void tst_QScriptEngine::castWithMultipleInheritance() |
|
2344 |
{ |
|
2345 |
QScriptEngine eng; |
|
2346 |
Klazz klz; |
|
2347 |
QScriptValue v = eng.newQObject(&klz); |
|
2348 |
||
2349 |
QCOMPARE(qscriptvalue_cast<Klazz*>(v), &klz); |
|
2350 |
QCOMPARE(qscriptvalue_cast<QWidget*>(v), (QWidget *)&klz); |
|
2351 |
QCOMPARE(qscriptvalue_cast<QObject*>(v), (QObject *)&klz); |
|
2352 |
QCOMPARE(qscriptvalue_cast<QStandardItem*>(v), (QStandardItem *)&klz); |
|
2353 |
QCOMPARE(qscriptvalue_cast<QGraphicsItem*>(v), (QGraphicsItem *)&klz); |
|
2354 |
} |
|
2355 |
||
2356 |
void tst_QScriptEngine::collectGarbage() |
|
2357 |
{ |
|
2358 |
QScriptEngine eng; |
|
2359 |
eng.evaluate("a = new Object(); a = new Object(); a = new Object()"); |
|
2360 |
QScriptValue a = eng.newObject(); |
|
2361 |
a = eng.newObject(); |
|
2362 |
a = eng.newObject(); |
|
2363 |
QPointer<QObject> ptr = new QObject(); |
|
2364 |
QVERIFY(ptr != 0); |
|
2365 |
(void)eng.newQObject(ptr, QScriptEngine::ScriptOwnership); |
|
2366 |
collectGarbage_helper(eng); |
|
2367 |
QVERIFY(ptr == 0); |
|
2368 |
} |
|
2369 |
||
2370 |
void tst_QScriptEngine::gcWithNestedDataStructure() |
|
2371 |
{ |
|
2372 |
QScriptEngine eng; |
|
2373 |
eng.evaluate( |
|
2374 |
"function makeList(size)" |
|
2375 |
"{" |
|
2376 |
" var head = { };" |
|
2377 |
" var l = head;" |
|
2378 |
" for (var i = 0; i < size; ++i) {" |
|
2379 |
" l.data = i + \"\";" |
|
2380 |
" l.next = { }; l = l.next;" |
|
2381 |
" }" |
|
2382 |
" l.next = null;" |
|
2383 |
" return head;" |
|
2384 |
"}"); |
|
2385 |
QCOMPARE(eng.hasUncaughtException(), false); |
|
2386 |
const int size = 200; |
|
2387 |
QScriptValue head = eng.evaluate(QString::fromLatin1("makeList(%0)").arg(size)); |
|
2388 |
QCOMPARE(eng.hasUncaughtException(), false); |
|
2389 |
for (int x = 0; x < 2; ++x) { |
|
2390 |
if (x == 1) |
|
2391 |
eng.evaluate("gc()"); |
|
2392 |
QScriptValue l = head; |
|
2393 |
for (int i = 0; i < 200; ++i) { |
|
2394 |
QCOMPARE(l.property("data").toString(), QString::number(i)); |
|
2395 |
l = l.property("next"); |
|
2396 |
} |
|
2397 |
} |
|
2398 |
} |
|
2399 |
||
2400 |
class EventReceiver : public QObject |
|
2401 |
{ |
|
2402 |
public: |
|
2403 |
EventReceiver() { |
|
2404 |
received = false; |
|
2405 |
} |
|
2406 |
||
2407 |
bool event(QEvent *e) { |
|
2408 |
received |= (e->type() == QEvent::User + 1); |
|
2409 |
return QObject::event(e); |
|
2410 |
} |
|
2411 |
||
2412 |
bool received; |
|
2413 |
}; |
|
2414 |
||
2415 |
void tst_QScriptEngine::processEventsWhileRunning() |
|
2416 |
{ |
|
2417 |
for (int x = 0; x < 2; ++x) { |
|
2418 |
QScriptEngine eng; |
|
2419 |
if (x == 0) |
|
2420 |
eng.pushContext(); |
|
2421 |
||
2422 |
QString script = QString::fromLatin1( |
|
2423 |
"var end = Number(new Date()) + 2000;" |
|
2424 |
"var x = 0;" |
|
2425 |
"while (Number(new Date()) < end) {" |
|
2426 |
" ++x;" |
|
2427 |
"}"); |
|
2428 |
||
2429 |
EventReceiver receiver; |
|
2430 |
QCoreApplication::postEvent(&receiver, new QEvent(QEvent::Type(QEvent::User+1))); |
|
2431 |
||
2432 |
eng.evaluate(script); |
|
2433 |
QVERIFY(!eng.hasUncaughtException()); |
|
2434 |
QVERIFY(!receiver.received); |
|
2435 |
||
2436 |
QCOMPARE(eng.processEventsInterval(), -1); |
|
2437 |
eng.setProcessEventsInterval(100); |
|
2438 |
eng.evaluate(script); |
|
2439 |
QVERIFY(!eng.hasUncaughtException()); |
|
2440 |
QVERIFY(receiver.received); |
|
2441 |
||
2442 |
if (x == 0) |
|
2443 |
eng.popContext(); |
|
2444 |
} |
|
2445 |
} |
|
2446 |
||
2447 |
class EventReceiver2 : public QObject |
|
2448 |
{ |
|
2449 |
public: |
|
2450 |
EventReceiver2(QScriptEngine *eng) { |
|
2451 |
engine = eng; |
|
2452 |
} |
|
2453 |
||
2454 |
bool event(QEvent *e) { |
|
2455 |
if (e->type() == QEvent::User + 1) { |
|
2456 |
engine->currentContext()->throwError("Killed"); |
|
2457 |
} |
|
2458 |
return QObject::event(e); |
|
2459 |
} |
|
2460 |
||
2461 |
QScriptEngine *engine; |
|
2462 |
}; |
|
2463 |
||
2464 |
void tst_QScriptEngine::throwErrorFromProcessEvents() |
|
2465 |
{ |
|
2466 |
QScriptEngine eng; |
|
2467 |
||
2468 |
EventReceiver2 receiver(&eng); |
|
2469 |
QCoreApplication::postEvent(&receiver, new QEvent(QEvent::Type(QEvent::User+1))); |
|
2470 |
||
2471 |
eng.setProcessEventsInterval(100); |
|
2472 |
QScriptValue ret = eng.evaluate(QString::fromLatin1("while (1) { }")); |
|
2473 |
QVERIFY(ret.isError()); |
|
2474 |
QCOMPARE(ret.toString(), QString::fromLatin1("Error: Killed")); |
|
2475 |
} |
|
2476 |
||
2477 |
void tst_QScriptEngine::stacktrace() |
|
2478 |
{ |
|
2479 |
QString script = QString::fromLatin1( |
|
2480 |
"function foo(counter) {\n" |
|
2481 |
" switch (counter) {\n" |
|
2482 |
" case 0: foo(counter+1); break;\n" |
|
2483 |
" case 1: foo(counter+1); break;\n" |
|
2484 |
" case 2: foo(counter+1); break;\n" |
|
2485 |
" case 3: foo(counter+1); break;\n" |
|
2486 |
" case 4: foo(counter+1); break;\n" |
|
2487 |
" default:\n" |
|
2488 |
" throw new Error('blah');\n" |
|
2489 |
" }\n" |
|
2490 |
"}\n" |
|
2491 |
"foo(0);"); |
|
2492 |
||
2493 |
const QString fileName("testfile"); |
|
2494 |
||
2495 |
QStringList backtrace; |
|
2496 |
backtrace << "foo(5)@testfile:9" |
|
2497 |
<< "foo(4)@testfile:7" |
|
2498 |
<< "foo(3)@testfile:6" |
|
2499 |
<< "foo(2)@testfile:5" |
|
2500 |
<< "foo(1)@testfile:4" |
|
2501 |
<< "foo(0)@testfile:3" |
|
2502 |
<< "<global>()@testfile:12"; |
|
2503 |
||
2504 |
QScriptEngine eng; |
|
2505 |
QScriptValue result = eng.evaluate(script, fileName); |
|
2506 |
QVERIFY(eng.hasUncaughtException()); |
|
2507 |
QVERIFY(result.isError()); |
|
2508 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2509 |
QEXPECT_FAIL("", "QTBUG-6139: uncaughtExceptionBacktrace() doesn't give the full backtrace", Abort); |
0 | 2510 |
QCOMPARE(eng.uncaughtExceptionBacktrace(), backtrace); |
2511 |
QVERIFY(eng.hasUncaughtException()); |
|
2512 |
QVERIFY(result.strictlyEquals(eng.uncaughtException())); |
|
2513 |
||
2514 |
QCOMPARE(result.property("fileName").toString(), fileName); |
|
2515 |
QCOMPARE(result.property("lineNumber").toInt32(), 9); |
|
2516 |
||
2517 |
QScriptValue stack = result.property("stack"); |
|
2518 |
QVERIFY(stack.isArray()); |
|
2519 |
||
2520 |
QCOMPARE(stack.property("length").toInt32(), 7); |
|
2521 |
||
2522 |
QScriptValueIterator it(stack); |
|
2523 |
int counter = 5; |
|
2524 |
while (it.hasNext()) { |
|
2525 |
it.next(); |
|
2526 |
QScriptValue obj = it.value(); |
|
2527 |
QScriptValue frame = obj.property("frame"); |
|
2528 |
||
2529 |
QCOMPARE(obj.property("fileName").toString(), fileName); |
|
2530 |
if (counter >= 0) { |
|
2531 |
QScriptValue callee = frame.property("arguments").property("callee"); |
|
2532 |
QVERIFY(callee.strictlyEquals(eng.globalObject().property("foo"))); |
|
2533 |
QCOMPARE(obj.property("functionName").toString(), QString("foo")); |
|
2534 |
int line = obj.property("lineNumber").toInt32(); |
|
2535 |
if (counter == 5) |
|
2536 |
QCOMPARE(line, 9); |
|
2537 |
else |
|
2538 |
QCOMPARE(line, 3 + counter); |
|
2539 |
} else { |
|
2540 |
QVERIFY(frame.strictlyEquals(eng.globalObject())); |
|
2541 |
QVERIFY(obj.property("functionName").toString().isEmpty()); |
|
2542 |
} |
|
2543 |
||
2544 |
--counter; |
|
2545 |
} |
|
2546 |
||
2547 |
{ |
|
2548 |
QScriptValue bt = result.property("backtrace").call(result); |
|
2549 |
QCOMPARE(qscriptvalue_cast<QStringList>(bt), backtrace); |
|
2550 |
} |
|
2551 |
||
2552 |
// throw something that isn't an Error object |
|
2553 |
eng.clearExceptions(); |
|
2554 |
QVERIFY(eng.uncaughtExceptionBacktrace().isEmpty()); |
|
2555 |
QString script2 = QString::fromLatin1( |
|
2556 |
"function foo(counter) {\n" |
|
2557 |
" switch (counter) {\n" |
|
2558 |
" case 0: foo(counter+1); break;\n" |
|
2559 |
" case 1: foo(counter+1); break;\n" |
|
2560 |
" case 2: foo(counter+1); break;\n" |
|
2561 |
" case 3: foo(counter+1); break;\n" |
|
2562 |
" case 4: foo(counter+1); break;\n" |
|
2563 |
" default:\n" |
|
2564 |
" throw 'just a string';\n" |
|
2565 |
" }\n" |
|
2566 |
"}\n" |
|
2567 |
"foo(0);"); |
|
2568 |
||
2569 |
QScriptValue result2 = eng.evaluate(script2, fileName); |
|
2570 |
QVERIFY(eng.hasUncaughtException()); |
|
2571 |
QVERIFY(!result2.isError()); |
|
2572 |
QVERIFY(result2.isString()); |
|
2573 |
||
2574 |
QCOMPARE(eng.uncaughtExceptionBacktrace(), backtrace); |
|
2575 |
QVERIFY(eng.hasUncaughtException()); |
|
2576 |
||
2577 |
eng.clearExceptions(); |
|
2578 |
QVERIFY(!eng.hasUncaughtException()); |
|
2579 |
QVERIFY(eng.uncaughtExceptionBacktrace().isEmpty()); |
|
2580 |
} |
|
2581 |
||
2582 |
void tst_QScriptEngine::numberParsing_data() |
|
2583 |
{ |
|
2584 |
QTest::addColumn<QString>("string"); |
|
2585 |
QTest::addColumn<qsreal>("expect"); |
|
2586 |
||
2587 |
QTest::newRow("decimal 0") << QString("0") << qsreal(0); |
|
2588 |
QTest::newRow("octal 0") << QString("00") << qsreal(00); |
|
2589 |
QTest::newRow("hex 0") << QString("0x0") << qsreal(0x0); |
|
2590 |
QTest::newRow("decimal 100") << QString("100") << qsreal(100); |
|
2591 |
QTest::newRow("hex 100") << QString("0x100") << qsreal(0x100); |
|
2592 |
QTest::newRow("octal 100") << QString("0100") << qsreal(0100); |
|
2593 |
QTest::newRow("decimal 4G") << QString("4294967296") << qsreal(Q_UINT64_C(4294967296)); |
|
2594 |
QTest::newRow("hex 4G") << QString("0x100000000") << qsreal(Q_UINT64_C(0x100000000)); |
|
2595 |
QTest::newRow("octal 4G") << QString("040000000000") << qsreal(Q_UINT64_C(040000000000)); |
|
2596 |
QTest::newRow("0.5") << QString("0.5") << qsreal(0.5); |
|
2597 |
QTest::newRow("1.5") << QString("1.5") << qsreal(1.5); |
|
2598 |
QTest::newRow("1e2") << QString("1e2") << qsreal(100); |
|
2599 |
} |
|
2600 |
||
2601 |
void tst_QScriptEngine::numberParsing() |
|
2602 |
{ |
|
2603 |
QFETCH(QString, string); |
|
2604 |
QFETCH(qsreal, expect); |
|
2605 |
||
2606 |
QScriptEngine eng; |
|
2607 |
QScriptValue ret = eng.evaluate(string); |
|
2608 |
QVERIFY(ret.isNumber()); |
|
2609 |
qsreal actual = ret.toNumber(); |
|
2610 |
QCOMPARE(actual, expect); |
|
2611 |
} |
|
2612 |
||
2613 |
// see ECMA-262, section 7.9 |
|
2614 |
void tst_QScriptEngine::automaticSemicolonInsertion() |
|
2615 |
{ |
|
2616 |
QScriptEngine eng; |
|
2617 |
{ |
|
2618 |
QScriptValue ret = eng.evaluate("{ 1 2 } 3"); |
|
2619 |
QVERIFY(ret.isError()); |
|
2620 |
QCOMPARE(ret.toString(), QString::fromLatin1("SyntaxError: Parse error")); |
|
2621 |
} |
|
2622 |
{ |
|
2623 |
QScriptValue ret = eng.evaluate("{ 1\n2 } 3"); |
|
2624 |
QVERIFY(ret.isNumber()); |
|
2625 |
QCOMPARE(ret.toInt32(), 3); |
|
2626 |
} |
|
2627 |
{ |
|
2628 |
QScriptValue ret = eng.evaluate("for (a; b\n)"); |
|
2629 |
QVERIFY(ret.isError()); |
|
2630 |
QCOMPARE(ret.toString(), QString::fromLatin1("SyntaxError: Parse error")); |
|
2631 |
} |
|
2632 |
{ |
|
2633 |
QScriptValue ret = eng.evaluate("(function() { return\n1 + 2 })()"); |
|
2634 |
QVERIFY(ret.isUndefined()); |
|
2635 |
} |
|
2636 |
{ |
|
2637 |
eng.evaluate("c = 2; b = 1"); |
|
2638 |
QScriptValue ret = eng.evaluate("a = b\n++c"); |
|
2639 |
QVERIFY(ret.isNumber()); |
|
2640 |
QCOMPARE(ret.toInt32(), 3); |
|
2641 |
} |
|
2642 |
{ |
|
2643 |
QScriptValue ret = eng.evaluate("if (a > b)\nelse c = d"); |
|
2644 |
QVERIFY(ret.isError()); |
|
2645 |
QCOMPARE(ret.toString(), QString::fromLatin1("SyntaxError: Parse error")); |
|
2646 |
} |
|
2647 |
{ |
|
2648 |
eng.evaluate("function c() { return { foo: function() { return 5; } } }"); |
|
2649 |
eng.evaluate("b = 1; d = 2; e = 3"); |
|
2650 |
QScriptValue ret = eng.evaluate("a = b + c\n(d + e).foo()"); |
|
2651 |
QVERIFY(ret.isNumber()); |
|
2652 |
QCOMPARE(ret.toInt32(), 6); |
|
2653 |
} |
|
2654 |
{ |
|
2655 |
QScriptValue ret = eng.evaluate("throw\n1"); |
|
2656 |
QVERIFY(ret.isError()); |
|
2657 |
QCOMPARE(ret.toString(), QString::fromLatin1("SyntaxError: Parse error")); |
|
2658 |
} |
|
2659 |
{ |
|
2660 |
QScriptValue ret = eng.evaluate("a = Number(1)\n++a"); |
|
2661 |
QVERIFY(ret.isNumber()); |
|
2662 |
QCOMPARE(ret.toInt32(), 2); |
|
2663 |
} |
|
2664 |
||
2665 |
// "a semicolon is never inserted automatically if the semicolon |
|
2666 |
// would then be parsed as an empty statement" |
|
2667 |
{ |
|
2668 |
eng.evaluate("a = 123"); |
|
2669 |
QScriptValue ret = eng.evaluate("if (0)\n ++a; a"); |
|
2670 |
QVERIFY(ret.isNumber()); |
|
2671 |
QCOMPARE(ret.toInt32(), 123); |
|
2672 |
} |
|
2673 |
{ |
|
2674 |
eng.evaluate("a = 123"); |
|
2675 |
QScriptValue ret = eng.evaluate("if (0)\n --a; a"); |
|
2676 |
QVERIFY(ret.isNumber()); |
|
2677 |
QCOMPARE(ret.toInt32(), 123); |
|
2678 |
} |
|
2679 |
{ |
|
2680 |
eng.evaluate("a = 123"); |
|
2681 |
QScriptValue ret = eng.evaluate("if ((0))\n ++a; a"); |
|
2682 |
QVERIFY(ret.isNumber()); |
|
2683 |
QCOMPARE(ret.toInt32(), 123); |
|
2684 |
} |
|
2685 |
{ |
|
2686 |
eng.evaluate("a = 123"); |
|
2687 |
QScriptValue ret = eng.evaluate("if ((0))\n --a; a"); |
|
2688 |
QVERIFY(ret.isNumber()); |
|
2689 |
QCOMPARE(ret.toInt32(), 123); |
|
2690 |
} |
|
2691 |
{ |
|
2692 |
eng.evaluate("a = 123"); |
|
2693 |
QScriptValue ret = eng.evaluate("if (0\n)\n ++a; a"); |
|
2694 |
QVERIFY(ret.isNumber()); |
|
2695 |
QCOMPARE(ret.toInt32(), 123); |
|
2696 |
} |
|
2697 |
{ |
|
2698 |
eng.evaluate("a = 123"); |
|
2699 |
QScriptValue ret = eng.evaluate("if (0\n ++a; a"); |
|
2700 |
QVERIFY(ret.isError()); |
|
2701 |
} |
|
2702 |
{ |
|
2703 |
eng.evaluate("a = 123"); |
|
2704 |
QScriptValue ret = eng.evaluate("if (0))\n ++a; a"); |
|
2705 |
QVERIFY(ret.isError()); |
|
2706 |
} |
|
2707 |
{ |
|
2708 |
QScriptValue ret = eng.evaluate("n = 0; for (i = 0; i < 10; ++i)\n ++n; n"); |
|
2709 |
QVERIFY(ret.isNumber()); |
|
2710 |
QCOMPARE(ret.toInt32(), 10); |
|
2711 |
} |
|
2712 |
{ |
|
2713 |
QScriptValue ret = eng.evaluate("n = 30; for (i = 0; i < 10; ++i)\n --n; n"); |
|
2714 |
QVERIFY(ret.isNumber()); |
|
2715 |
QCOMPARE(ret.toInt32(), 20); |
|
2716 |
} |
|
2717 |
{ |
|
2718 |
QScriptValue ret = eng.evaluate("n = 0; for (var i = 0; i < 10; ++i)\n ++n; n"); |
|
2719 |
QVERIFY(ret.isNumber()); |
|
2720 |
QCOMPARE(ret.toInt32(), 10); |
|
2721 |
} |
|
2722 |
{ |
|
2723 |
QScriptValue ret = eng.evaluate("n = 30; for (var i = 0; i < 10; ++i)\n --n; n"); |
|
2724 |
QVERIFY(ret.isNumber()); |
|
2725 |
QCOMPARE(ret.toInt32(), 20); |
|
2726 |
} |
|
2727 |
{ |
|
2728 |
QScriptValue ret = eng.evaluate("n = 0; i = 0; while (i++ < 10)\n ++n; n"); |
|
2729 |
QVERIFY(ret.isNumber()); |
|
2730 |
QCOMPARE(ret.toInt32(), 10); |
|
2731 |
} |
|
2732 |
{ |
|
2733 |
QScriptValue ret = eng.evaluate("n = 30; i = 0; while (i++ < 10)\n --n; n"); |
|
2734 |
QVERIFY(ret.isNumber()); |
|
2735 |
QCOMPARE(ret.toInt32(), 20); |
|
2736 |
} |
|
2737 |
{ |
|
2738 |
QScriptValue ret = eng.evaluate("o = { a: 0, b: 1, c: 2 }; n = 0; for (i in o)\n ++n; n"); |
|
2739 |
QVERIFY(ret.isNumber()); |
|
2740 |
QCOMPARE(ret.toInt32(), 3); |
|
2741 |
} |
|
2742 |
{ |
|
2743 |
QScriptValue ret = eng.evaluate("o = { a: 0, b: 1, c: 2 }; n = 9; for (i in o)\n --n; n"); |
|
2744 |
QVERIFY(ret.isNumber()); |
|
2745 |
QCOMPARE(ret.toInt32(), 6); |
|
2746 |
} |
|
2747 |
{ |
|
2748 |
QScriptValue ret = eng.evaluate("o = { a: 0, b: 1, c: 2 }; n = 0; for (var i in o)\n ++n; n"); |
|
2749 |
QVERIFY(ret.isNumber()); |
|
2750 |
QCOMPARE(ret.toInt32(), 3); |
|
2751 |
} |
|
2752 |
{ |
|
2753 |
QScriptValue ret = eng.evaluate("o = { a: 0, b: 1, c: 2 }; n = 9; for (var i in o)\n --n; n"); |
|
2754 |
QVERIFY(ret.isNumber()); |
|
2755 |
QCOMPARE(ret.toInt32(), 6); |
|
2756 |
} |
|
2757 |
{ |
|
2758 |
QScriptValue ret = eng.evaluate("o = { n: 3 }; n = 5; with (o)\n ++n; n"); |
|
2759 |
QVERIFY(ret.isNumber()); |
|
2760 |
QCOMPARE(ret.toInt32(), 5); |
|
2761 |
} |
|
2762 |
{ |
|
2763 |
QScriptValue ret = eng.evaluate("o = { n: 3 }; n = 10; with (o)\n --n; n"); |
|
2764 |
QVERIFY(ret.isNumber()); |
|
2765 |
QCOMPARE(ret.toInt32(), 10); |
|
2766 |
} |
|
2767 |
{ |
|
2768 |
QScriptValue ret = eng.evaluate("n = 5; i = 0; do\n ++n; while (++i < 10); n"); |
|
2769 |
QVERIFY(ret.isNumber()); |
|
2770 |
QCOMPARE(ret.toInt32(), 15); |
|
2771 |
} |
|
2772 |
{ |
|
2773 |
QScriptValue ret = eng.evaluate("n = 20; i = 0; do\n --n; while (++i < 10); n"); |
|
2774 |
QVERIFY(ret.isNumber()); |
|
2775 |
QCOMPARE(ret.toInt32(), 10); |
|
2776 |
} |
|
2777 |
||
2778 |
{ |
|
2779 |
QScriptValue ret = eng.evaluate("n = 1; i = 0; if (n) i\n++n; n"); |
|
2780 |
QVERIFY(ret.isNumber()); |
|
2781 |
QCOMPARE(ret.toInt32(), 2); |
|
2782 |
} |
|
2783 |
{ |
|
2784 |
QScriptValue ret = eng.evaluate("n = 1; i = 0; if (n) i\n--n; n"); |
|
2785 |
QVERIFY(ret.isNumber()); |
|
2786 |
QCOMPARE(ret.toInt32(), 0); |
|
2787 |
} |
|
2788 |
||
2789 |
{ |
|
2790 |
QScriptValue ret = eng.evaluate("if (0)"); |
|
2791 |
QVERIFY(ret.isError()); |
|
2792 |
} |
|
2793 |
{ |
|
2794 |
QScriptValue ret = eng.evaluate("while (0)"); |
|
2795 |
QVERIFY(ret.isError()); |
|
2796 |
} |
|
2797 |
{ |
|
2798 |
QScriptValue ret = eng.evaluate("for (;;)"); |
|
2799 |
QVERIFY(ret.isError()); |
|
2800 |
} |
|
2801 |
{ |
|
2802 |
QScriptValue ret = eng.evaluate("for (p in this)"); |
|
2803 |
QVERIFY(ret.isError()); |
|
2804 |
} |
|
2805 |
{ |
|
2806 |
QScriptValue ret = eng.evaluate("with (this)"); |
|
2807 |
QVERIFY(ret.isError()); |
|
2808 |
} |
|
2809 |
{ |
|
2810 |
QScriptValue ret = eng.evaluate("do"); |
|
2811 |
QVERIFY(ret.isError()); |
|
2812 |
} |
|
2813 |
} |
|
2814 |
||
2815 |
class EventReceiver3 : public QObject |
|
2816 |
{ |
|
2817 |
public: |
|
2818 |
enum AbortionResult { |
|
2819 |
None = 0, |
|
2820 |
String = 1, |
|
2821 |
Error = 2 |
|
2822 |
}; |
|
2823 |
||
2824 |
EventReceiver3(QScriptEngine *eng) { |
|
2825 |
engine = eng; |
|
2826 |
resultType = None; |
|
2827 |
} |
|
2828 |
||
2829 |
bool event(QEvent *e) { |
|
2830 |
if (e->type() == QEvent::User + 1) { |
|
2831 |
switch (resultType) { |
|
2832 |
case None: |
|
2833 |
engine->abortEvaluation(); |
|
2834 |
break; |
|
2835 |
case String: |
|
2836 |
engine->abortEvaluation(QScriptValue(engine, QString::fromLatin1("Aborted"))); |
|
2837 |
break; |
|
2838 |
case Error: |
|
2839 |
engine->abortEvaluation(engine->currentContext()->throwError("AbortedWithError")); |
|
2840 |
break; |
|
2841 |
} |
|
2842 |
} |
|
2843 |
return QObject::event(e); |
|
2844 |
} |
|
2845 |
||
2846 |
AbortionResult resultType; |
|
2847 |
QScriptEngine *engine; |
|
2848 |
}; |
|
2849 |
||
2850 |
static QScriptValue myFunctionAbortingEvaluation(QScriptContext *, QScriptEngine *eng) |
|
2851 |
{ |
|
2852 |
eng->abortEvaluation(); |
|
2853 |
return eng->nullValue(); // should be ignored |
|
2854 |
} |
|
2855 |
||
2856 |
void tst_QScriptEngine::abortEvaluation() |
|
2857 |
{ |
|
2858 |
QScriptEngine eng; |
|
2859 |
||
2860 |
eng.abortEvaluation(); |
|
2861 |
QVERIFY(!eng.hasUncaughtException()); |
|
2862 |
||
2863 |
eng.abortEvaluation(123); |
|
2864 |
{ |
|
2865 |
QScriptValue ret = eng.evaluate("'ciao'"); |
|
2866 |
QVERIFY(ret.isString()); |
|
2867 |
QCOMPARE(ret.toString(), QString::fromLatin1("ciao")); |
|
2868 |
} |
|
2869 |
||
2870 |
EventReceiver3 receiver(&eng); |
|
2871 |
||
2872 |
eng.setProcessEventsInterval(100); |
|
2873 |
for (int x = 0; x < 3; ++x) { |
|
2874 |
QCoreApplication::postEvent(&receiver, new QEvent(QEvent::Type(QEvent::User+1))); |
|
2875 |
receiver.resultType = EventReceiver3::AbortionResult(x); |
|
2876 |
QScriptValue ret = eng.evaluate(QString::fromLatin1("while (1) { }")); |
|
2877 |
switch (receiver.resultType) { |
|
2878 |
case EventReceiver3::None: |
|
2879 |
QVERIFY(!eng.hasUncaughtException()); |
|
2880 |
QVERIFY(!ret.isValid()); |
|
2881 |
break; |
|
2882 |
case EventReceiver3::String: |
|
2883 |
QVERIFY(!eng.hasUncaughtException()); |
|
2884 |
QVERIFY(ret.isString()); |
|
2885 |
QCOMPARE(ret.toString(), QString::fromLatin1("Aborted")); |
|
2886 |
break; |
|
2887 |
case EventReceiver3::Error: |
|
2888 |
QVERIFY(eng.hasUncaughtException()); |
|
2889 |
QVERIFY(ret.isError()); |
|
2890 |
QCOMPARE(ret.toString(), QString::fromLatin1("Error: AbortedWithError")); |
|
2891 |
break; |
|
2892 |
} |
|
2893 |
} |
|
2894 |
||
2895 |
// scripts cannot intercept the abortion with try/catch |
|
2896 |
for (int y = 0; y < 3; ++y) { |
|
2897 |
QCoreApplication::postEvent(&receiver, new QEvent(QEvent::Type(QEvent::User+1))); |
|
2898 |
receiver.resultType = EventReceiver3::AbortionResult(y); |
|
2899 |
QScriptValue ret = eng.evaluate(QString::fromLatin1( |
|
2900 |
"while (1) {\n" |
|
2901 |
" try {\n" |
|
2902 |
" (function() { while (1) { } })();\n" |
|
2903 |
" } catch (e) {\n" |
|
2904 |
" ;\n" |
|
2905 |
" }\n" |
|
2906 |
"}")); |
|
2907 |
switch (receiver.resultType) { |
|
2908 |
case EventReceiver3::None: |
|
2909 |
QVERIFY(!eng.hasUncaughtException()); |
|
2910 |
QVERIFY(!ret.isValid()); |
|
2911 |
break; |
|
2912 |
case EventReceiver3::String: |
|
2913 |
QVERIFY(!eng.hasUncaughtException()); |
|
2914 |
QVERIFY(ret.isString()); |
|
2915 |
QCOMPARE(ret.toString(), QString::fromLatin1("Aborted")); |
|
2916 |
break; |
|
2917 |
case EventReceiver3::Error: |
|
2918 |
QVERIFY(eng.hasUncaughtException()); |
|
2919 |
QVERIFY(ret.isError()); |
|
2920 |
break; |
|
2921 |
} |
|
2922 |
} |
|
2923 |
||
2924 |
{ |
|
2925 |
QScriptValue fun = eng.newFunction(myFunctionAbortingEvaluation); |
|
2926 |
eng.globalObject().setProperty("myFunctionAbortingEvaluation", fun); |
|
2927 |
QScriptValue ret = eng.evaluate("myFunctionAbortingEvaluation()"); |
|
2928 |
QVERIFY(!ret.isValid()); |
|
2929 |
} |
|
2930 |
} |
|
2931 |
||
2932 |
static QScriptValue myFunctionReturningIsEvaluating(QScriptContext *, QScriptEngine *eng) |
|
2933 |
{ |
|
2934 |
return QScriptValue(eng, eng->isEvaluating()); |
|
2935 |
} |
|
2936 |
||
2937 |
class EventReceiver4 : public QObject |
|
2938 |
{ |
|
2939 |
public: |
|
2940 |
EventReceiver4(QScriptEngine *eng) { |
|
2941 |
engine = eng; |
|
2942 |
wasEvaluating = false; |
|
2943 |
} |
|
2944 |
||
2945 |
bool event(QEvent *e) { |
|
2946 |
if (e->type() == QEvent::User + 1) { |
|
2947 |
wasEvaluating = engine->isEvaluating(); |
|
2948 |
} |
|
2949 |
return QObject::event(e); |
|
2950 |
} |
|
2951 |
||
2952 |
QScriptEngine *engine; |
|
2953 |
bool wasEvaluating; |
|
2954 |
}; |
|
2955 |
||
2956 |
void tst_QScriptEngine::isEvaluating() |
|
2957 |
{ |
|
2958 |
QScriptEngine eng; |
|
2959 |
||
2960 |
QVERIFY(!eng.isEvaluating()); |
|
2961 |
||
2962 |
eng.evaluate(""); |
|
2963 |
QVERIFY(!eng.isEvaluating()); |
|
2964 |
eng.evaluate("123"); |
|
2965 |
QVERIFY(!eng.isEvaluating()); |
|
2966 |
eng.evaluate("0 = 0"); |
|
2967 |
QVERIFY(!eng.isEvaluating()); |
|
2968 |
||
2969 |
{ |
|
2970 |
QScriptValue fun = eng.newFunction(myFunctionReturningIsEvaluating); |
|
2971 |
eng.globalObject().setProperty("myFunctionReturningIsEvaluating", fun); |
|
2972 |
QScriptValue ret = eng.evaluate("myFunctionReturningIsEvaluating()"); |
|
2973 |
QVERIFY(ret.isBoolean()); |
|
2974 |
QVERIFY(ret.toBoolean()); |
|
2975 |
} |
|
2976 |
||
2977 |
{ |
|
2978 |
EventReceiver4 receiver(&eng); |
|
2979 |
QCoreApplication::postEvent(&receiver, new QEvent(QEvent::Type(QEvent::User+1))); |
|
2980 |
||
2981 |
QString script = QString::fromLatin1( |
|
2982 |
"var end = Number(new Date()) + 1000;" |
|
2983 |
"var x = 0;" |
|
2984 |
"while (Number(new Date()) < end) {" |
|
2985 |
" ++x;" |
|
2986 |
"}"); |
|
2987 |
||
2988 |
eng.setProcessEventsInterval(100); |
|
2989 |
eng.evaluate(script); |
|
2990 |
QVERIFY(receiver.wasEvaluating); |
|
2991 |
} |
|
2992 |
} |
|
2993 |
||
2994 |
static QtMsgType theMessageType; |
|
2995 |
static QString theMessage; |
|
2996 |
||
2997 |
static void myMsgHandler(QtMsgType type, const char *msg) |
|
2998 |
{ |
|
2999 |
theMessageType = type; |
|
3000 |
theMessage = QString::fromLatin1(msg); |
|
3001 |
} |
|
3002 |
||
3003 |
void tst_QScriptEngine::printFunctionWithCustomHandler() |
|
3004 |
{ |
|
3005 |
QScriptEngine eng; |
|
3006 |
QtMsgHandler oldHandler = qInstallMsgHandler(myMsgHandler); |
|
3007 |
QVERIFY(eng.globalObject().property("print").isFunction()); |
|
3008 |
theMessageType = QtSystemMsg; |
|
3009 |
QVERIFY(theMessage.isEmpty()); |
|
3010 |
QVERIFY(eng.evaluate("print('test')").isUndefined()); |
|
3011 |
QCOMPARE(theMessageType, QtDebugMsg); |
|
3012 |
QCOMPARE(theMessage, QString::fromLatin1("test")); |
|
3013 |
theMessageType = QtSystemMsg; |
|
3014 |
theMessage.clear(); |
|
3015 |
QVERIFY(eng.evaluate("print(3, true, 'little pigs')").isUndefined()); |
|
3016 |
QCOMPARE(theMessageType, QtDebugMsg); |
|
3017 |
QCOMPARE(theMessage, QString::fromLatin1("3 true little pigs")); |
|
3018 |
qInstallMsgHandler(oldHandler); |
|
3019 |
} |
|
3020 |
||
3021 |
void tst_QScriptEngine::printThrowsException() |
|
3022 |
{ |
|
3023 |
QScriptEngine eng; |
|
3024 |
QScriptValue ret = eng.evaluate("print({ toString: function() { throw 'foo'; } });"); |
|
3025 |
QVERIFY(eng.hasUncaughtException()); |
|
3026 |
QVERIFY(ret.strictlyEquals(QScriptValue(&eng, QLatin1String("foo")))); |
|
3027 |
} |
|
3028 |
||
3029 |
void tst_QScriptEngine::errorConstructors() |
|
3030 |
{ |
|
3031 |
QScriptEngine eng; |
|
3032 |
QStringList prefixes; |
|
3033 |
prefixes << "" << "Eval" << "Range" << "Reference" << "Syntax" << "Type" << "URI"; |
|
3034 |
for (int x = 0; x < 3; ++x) { |
|
3035 |
for (int i = 0; i < prefixes.size(); ++i) { |
|
3036 |
QString name = prefixes.at(i) + QLatin1String("Error"); |
|
3037 |
QString code = QString(i+1, QLatin1Char('\n')); |
|
3038 |
if (x == 0) |
|
3039 |
code += QLatin1String("throw "); |
|
3040 |
else if (x == 1) |
|
3041 |
code += QLatin1String("new "); |
|
3042 |
code += name + QLatin1String("()"); |
|
3043 |
QScriptValue ret = eng.evaluate(code); |
|
3044 |
QVERIFY(ret.isError()); |
|
3045 |
QCOMPARE(eng.hasUncaughtException(), x == 0); |
|
3046 |
eng.clearExceptions(); |
|
3047 |
QVERIFY(ret.toString().startsWith(name)); |
|
3048 |
if (x != 0) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3049 |
QEXPECT_FAIL("", "QTBUG-6138: JSC doesn't assign lineNumber when errors are not thrown", Continue); |
0 | 3050 |
QCOMPARE(ret.property("lineNumber").toInt32(), i+2); |
3051 |
} |
|
3052 |
} |
|
3053 |
} |
|
3054 |
||
3055 |
static QScriptValue argumentsProperty_fun(QScriptContext *, QScriptEngine *eng) |
|
3056 |
{ |
|
3057 |
eng->evaluate("var a = arguments[0];"); |
|
3058 |
eng->evaluate("arguments[0] = 200;"); |
|
3059 |
return eng->evaluate("a + arguments[0]"); |
|
3060 |
} |
|
3061 |
||
3062 |
||
3063 |
void tst_QScriptEngine::argumentsProperty() |
|
3064 |
{ |
|
3065 |
{ |
|
3066 |
QScriptEngine eng; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3067 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3068 |
QScriptValue ret = eng.evaluate("arguments"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3069 |
QVERIFY(ret.isError()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3070 |
QCOMPARE(ret.toString(), QString::fromLatin1("ReferenceError: Can't find variable: arguments")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3071 |
} |
0 | 3072 |
eng.evaluate("arguments = 10"); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3073 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3074 |
QScriptValue ret = eng.evaluate("arguments"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3075 |
QVERIFY(ret.isNumber()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3076 |
QCOMPARE(ret.toInt32(), 10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3077 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3078 |
QVERIFY(eng.evaluate("delete arguments").toBoolean()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3079 |
QVERIFY(!eng.globalObject().property("arguments").isValid()); |
0 | 3080 |
} |
3081 |
{ |
|
3082 |
QScriptEngine eng; |
|
3083 |
eng.evaluate("o = { arguments: 123 }"); |
|
3084 |
QScriptValue ret = eng.evaluate("with (o) { arguments; }"); |
|
3085 |
QVERIFY(ret.isNumber()); |
|
3086 |
QCOMPARE(ret.toInt32(), 123); |
|
3087 |
} |
|
3088 |
{ |
|
3089 |
QScriptEngine eng; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3090 |
QVERIFY(!eng.globalObject().property("arguments").isValid()); |
0 | 3091 |
QScriptValue ret = eng.evaluate("(function() { arguments = 456; return arguments; })()"); |
3092 |
QVERIFY(ret.isNumber()); |
|
3093 |
QCOMPARE(ret.toInt32(), 456); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3094 |
QVERIFY(!eng.globalObject().property("arguments").isValid()); |
0 | 3095 |
} |
3096 |
||
3097 |
{ |
|
3098 |
QScriptEngine eng; |
|
3099 |
QScriptValue fun = eng.newFunction(argumentsProperty_fun); |
|
3100 |
eng.globalObject().setProperty("fun", eng.newFunction(argumentsProperty_fun)); |
|
3101 |
QScriptValue result = eng.evaluate("fun(18)"); |
|
3102 |
QVERIFY(result.isNumber()); |
|
3103 |
QCOMPARE(result.toInt32(), 218); |
|
3104 |
} |
|
3105 |
} |
|
3106 |
||
3107 |
void tst_QScriptEngine::numberClass() |
|
3108 |
{ |
|
3109 |
QScriptEngine eng; |
|
3110 |
||
3111 |
QScriptValue ctor = eng.globalObject().property("Number"); |
|
3112 |
QVERIFY(ctor.property("length").isNumber()); |
|
3113 |
QCOMPARE(ctor.property("length").toNumber(), qsreal(1)); |
|
3114 |
QScriptValue proto = ctor.property("prototype"); |
|
3115 |
QVERIFY(proto.isObject()); |
|
3116 |
{ |
|
3117 |
QScriptValue::PropertyFlags flags = QScriptValue::SkipInEnumeration |
|
3118 |
| QScriptValue::Undeletable |
|
3119 |
| QScriptValue::ReadOnly; |
|
3120 |
QCOMPARE(ctor.propertyFlags("prototype"), flags); |
|
3121 |
QVERIFY(ctor.property("MAX_VALUE").isNumber()); |
|
3122 |
QCOMPARE(ctor.propertyFlags("MAX_VALUE"), flags); |
|
3123 |
QVERIFY(ctor.property("MIN_VALUE").isNumber()); |
|
3124 |
QCOMPARE(ctor.propertyFlags("MIN_VALUE"), flags); |
|
3125 |
QVERIFY(ctor.property("NaN").isNumber()); |
|
3126 |
QCOMPARE(ctor.propertyFlags("NaN"), flags); |
|
3127 |
QVERIFY(ctor.property("NEGATIVE_INFINITY").isNumber()); |
|
3128 |
QCOMPARE(ctor.propertyFlags("NEGATIVE_INFINITY"), flags); |
|
3129 |
QVERIFY(ctor.property("POSITIVE_INFINITY").isNumber()); |
|
3130 |
QCOMPARE(ctor.propertyFlags("POSITIVE_INFINITY"), flags); |
|
3131 |
} |
|
3132 |
QVERIFY(proto.instanceOf(eng.globalObject().property("Object"))); |
|
3133 |
QCOMPARE(proto.toNumber(), qsreal(0)); |
|
3134 |
QVERIFY(proto.property("constructor").strictlyEquals(ctor)); |
|
3135 |
||
3136 |
{ |
|
3137 |
QScriptValue ret = eng.evaluate("Number()"); |
|
3138 |
QVERIFY(ret.isNumber()); |
|
3139 |
QCOMPARE(ret.toNumber(), qsreal(0)); |
|
3140 |
} |
|
3141 |
{ |
|
3142 |
QScriptValue ret = eng.evaluate("Number(123)"); |
|
3143 |
QVERIFY(ret.isNumber()); |
|
3144 |
QCOMPARE(ret.toNumber(), qsreal(123)); |
|
3145 |
} |
|
3146 |
{ |
|
3147 |
QScriptValue ret = eng.evaluate("Number('456')"); |
|
3148 |
QVERIFY(ret.isNumber()); |
|
3149 |
QCOMPARE(ret.toNumber(), qsreal(456)); |
|
3150 |
} |
|
3151 |
{ |
|
3152 |
QScriptValue ret = eng.evaluate("new Number()"); |
|
3153 |
QVERIFY(!ret.isNumber()); |
|
3154 |
QVERIFY(ret.isObject()); |
|
3155 |
QCOMPARE(ret.toNumber(), qsreal(0)); |
|
3156 |
} |
|
3157 |
{ |
|
3158 |
QScriptValue ret = eng.evaluate("new Number(123)"); |
|
3159 |
QVERIFY(!ret.isNumber()); |
|
3160 |
QVERIFY(ret.isObject()); |
|
3161 |
QCOMPARE(ret.toNumber(), qsreal(123)); |
|
3162 |
} |
|
3163 |
{ |
|
3164 |
QScriptValue ret = eng.evaluate("new Number('456')"); |
|
3165 |
QVERIFY(!ret.isNumber()); |
|
3166 |
QVERIFY(ret.isObject()); |
|
3167 |
QCOMPARE(ret.toNumber(), qsreal(456)); |
|
3168 |
} |
|
3169 |
||
3170 |
QVERIFY(proto.property("toString").isFunction()); |
|
3171 |
{ |
|
3172 |
QScriptValue ret = eng.evaluate("new Number(123).toString()"); |
|
3173 |
QVERIFY(ret.isString()); |
|
3174 |
QCOMPARE(ret.toString(), QString::fromLatin1("123")); |
|
3175 |
} |
|
3176 |
{ |
|
3177 |
QScriptValue ret = eng.evaluate("new Number(123).toString(8)"); |
|
3178 |
QVERIFY(ret.isString()); |
|
3179 |
QCOMPARE(ret.toString(), QString::fromLatin1("173")); |
|
3180 |
} |
|
3181 |
{ |
|
3182 |
QScriptValue ret = eng.evaluate("new Number(123).toString(16)"); |
|
3183 |
QVERIFY(ret.isString()); |
|
3184 |
QCOMPARE(ret.toString(), QString::fromLatin1("7b")); |
|
3185 |
} |
|
3186 |
QVERIFY(proto.property("toLocaleString").isFunction()); |
|
3187 |
{ |
|
3188 |
QScriptValue ret = eng.evaluate("new Number(123).toLocaleString()"); |
|
3189 |
QVERIFY(ret.isString()); |
|
3190 |
QCOMPARE(ret.toString(), QString::fromLatin1("123")); |
|
3191 |
} |
|
3192 |
QVERIFY(proto.property("valueOf").isFunction()); |
|
3193 |
{ |
|
3194 |
QScriptValue ret = eng.evaluate("new Number(123).valueOf()"); |
|
3195 |
QVERIFY(ret.isNumber()); |
|
3196 |
QCOMPARE(ret.toNumber(), qsreal(123)); |
|
3197 |
} |
|
3198 |
QVERIFY(proto.property("toExponential").isFunction()); |
|
3199 |
{ |
|
3200 |
QScriptValue ret = eng.evaluate("new Number(123).toExponential()"); |
|
3201 |
QVERIFY(ret.isString()); |
|
3202 |
QCOMPARE(ret.toString(), QString::fromLatin1("1.23e+2")); |
|
3203 |
} |
|
3204 |
QVERIFY(proto.property("toFixed").isFunction()); |
|
3205 |
{ |
|
3206 |
QScriptValue ret = eng.evaluate("new Number(123).toFixed()"); |
|
3207 |
QVERIFY(ret.isString()); |
|
3208 |
QCOMPARE(ret.toString(), QString::fromLatin1("123")); |
|
3209 |
} |
|
3210 |
QVERIFY(proto.property("toPrecision").isFunction()); |
|
3211 |
{ |
|
3212 |
QScriptValue ret = eng.evaluate("new Number(123).toPrecision()"); |
|
3213 |
QVERIFY(ret.isString()); |
|
3214 |
QCOMPARE(ret.toString(), QString::fromLatin1("123")); |
|
3215 |
} |
|
3216 |
} |
|
3217 |
||
3218 |
void tst_QScriptEngine::forInStatement() |
|
3219 |
{ |
|
3220 |
QScriptEngine eng; |
|
3221 |
{ |
|
3222 |
QScriptValue ret = eng.evaluate("o = { }; r = []; for (var p in o) r[r.length] = p; r"); |
|
3223 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3224 |
QVERIFY(lst.isEmpty()); |
|
3225 |
} |
|
3226 |
{ |
|
3227 |
QScriptValue ret = eng.evaluate("o = { p: 123 }; r = [];" |
|
3228 |
"for (var p in o) r[r.length] = p; r"); |
|
3229 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3230 |
QCOMPARE(lst.size(), 1); |
|
3231 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3232 |
} |
|
3233 |
{ |
|
3234 |
QScriptValue ret = eng.evaluate("o = { p: 123, q: 456 }; r = [];" |
|
3235 |
"for (var p in o) r[r.length] = p; r"); |
|
3236 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3237 |
QCOMPARE(lst.size(), 2); |
|
3238 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3239 |
QCOMPARE(lst.at(1), QString::fromLatin1("q")); |
|
3240 |
} |
|
3241 |
||
3242 |
// properties in prototype |
|
3243 |
{ |
|
3244 |
QScriptValue ret = eng.evaluate("o = { }; o.__proto__ = { p: 123 }; r = [];" |
|
3245 |
"for (var p in o) r[r.length] = p; r"); |
|
3246 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3247 |
QCOMPARE(lst.size(), 1); |
|
3248 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3249 |
} |
|
3250 |
{ |
|
3251 |
QScriptValue ret = eng.evaluate("o = { p: 123 }; o.__proto__ = { q: 456 }; r = [];" |
|
3252 |
"for (var p in o) r[r.length] = p; r"); |
|
3253 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3254 |
QCOMPARE(lst.size(), 2); |
|
3255 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3256 |
QCOMPARE(lst.at(1), QString::fromLatin1("q")); |
|
3257 |
} |
|
3258 |
{ |
|
3259 |
// shadowed property |
|
3260 |
QScriptValue ret = eng.evaluate("o = { p: 123 }; o.__proto__ = { p: 456 }; r = [];" |
|
3261 |
"for (var p in o) r[r.length] = p; r"); |
|
3262 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3263 |
QCOMPARE(lst.size(), 1); |
|
3264 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3265 |
} |
|
3266 |
||
3267 |
// deleting property during enumeration |
|
3268 |
{ |
|
3269 |
QScriptValue ret = eng.evaluate("o = { p: 123 }; r = [];" |
|
3270 |
"for (var p in o) { r[r.length] = p; delete r[p]; } r"); |
|
3271 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3272 |
QCOMPARE(lst.size(), 1); |
|
3273 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3274 |
} |
|
3275 |
{ |
|
3276 |
QScriptValue ret = eng.evaluate("o = { p: 123, q: 456 }; r = [];" |
|
3277 |
"for (var p in o) { r[r.length] = p; delete o.q; } r"); |
|
3278 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3279 |
QCOMPARE(lst.size(), 1); |
|
3280 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3281 |
} |
|
3282 |
||
3283 |
// adding property during enumeration |
|
3284 |
{ |
|
3285 |
QScriptValue ret = eng.evaluate("o = { p: 123 }; r = [];" |
|
3286 |
"for (var p in o) { r[r.length] = p; o.q = 456; } r"); |
|
3287 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3288 |
QCOMPARE(lst.size(), 1); |
|
3289 |
QCOMPARE(lst.at(0), QString::fromLatin1("p")); |
|
3290 |
} |
|
3291 |
||
3292 |
// arrays |
|
3293 |
{ |
|
3294 |
QScriptValue ret = eng.evaluate("a = [123, 456]; r = [];" |
|
3295 |
"for (var p in a) r[r.length] = p; r"); |
|
3296 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3297 |
QCOMPARE(lst.size(), 2); |
|
3298 |
QCOMPARE(lst.at(0), QString::fromLatin1("0")); |
|
3299 |
QCOMPARE(lst.at(1), QString::fromLatin1("1")); |
|
3300 |
} |
|
3301 |
{ |
|
3302 |
QScriptValue ret = eng.evaluate("a = [123, 456]; a.foo = 'bar'; r = [];" |
|
3303 |
"for (var p in a) r[r.length] = p; r"); |
|
3304 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3305 |
QCOMPARE(lst.size(), 3); |
|
3306 |
QCOMPARE(lst.at(0), QString::fromLatin1("0")); |
|
3307 |
QCOMPARE(lst.at(1), QString::fromLatin1("1")); |
|
3308 |
QCOMPARE(lst.at(2), QString::fromLatin1("foo")); |
|
3309 |
} |
|
3310 |
{ |
|
3311 |
QScriptValue ret = eng.evaluate("a = [123, 456]; a.foo = 'bar';" |
|
3312 |
"b = [111, 222, 333]; b.bar = 'baz';" |
|
3313 |
"a.__proto__ = b; r = [];" |
|
3314 |
"for (var p in a) r[r.length] = p; r"); |
|
3315 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3316 |
QCOMPARE(lst.size(), 5); |
|
3317 |
QCOMPARE(lst.at(0), QString::fromLatin1("0")); |
|
3318 |
QCOMPARE(lst.at(1), QString::fromLatin1("1")); |
|
3319 |
QCOMPARE(lst.at(2), QString::fromLatin1("foo")); |
|
3320 |
QCOMPARE(lst.at(3), QString::fromLatin1("2")); |
|
3321 |
QCOMPARE(lst.at(4), QString::fromLatin1("bar")); |
|
3322 |
} |
|
3323 |
||
3324 |
// null and undefined |
|
3325 |
// according to the spec, we should throw an exception; however, for |
|
3326 |
// compability with the real world, we don't |
|
3327 |
{ |
|
3328 |
QScriptValue ret = eng.evaluate("r = true; for (var p in undefined) r = false; r"); |
|
3329 |
QVERIFY(ret.isBool()); |
|
3330 |
QVERIFY(ret.toBool()); |
|
3331 |
} |
|
3332 |
{ |
|
3333 |
QScriptValue ret = eng.evaluate("r = true; for (var p in null) r = false; r"); |
|
3334 |
QVERIFY(ret.isBool()); |
|
3335 |
QVERIFY(ret.toBool()); |
|
3336 |
} |
|
3337 |
} |
|
3338 |
||
3339 |
void tst_QScriptEngine::functionExpression() |
|
3340 |
{ |
|
3341 |
// task 175679 |
|
3342 |
QScriptEngine eng; |
|
3343 |
QVERIFY(!eng.globalObject().property("bar").isValid()); |
|
3344 |
eng.evaluate("function foo(arg) {\n" |
|
3345 |
" if (arg == 'bar')\n" |
|
3346 |
" function bar() { return 'bar'; }\n" |
|
3347 |
" else\n" |
|
3348 |
" function baz() { return 'baz'; }\n" |
|
3349 |
" return (arg == 'bar') ? bar : baz;\n" |
|
3350 |
"}"); |
|
3351 |
QVERIFY(!eng.globalObject().property("bar").isValid()); |
|
3352 |
QVERIFY(!eng.globalObject().property("baz").isValid()); |
|
3353 |
QVERIFY(eng.evaluate("foo").isFunction()); |
|
3354 |
{ |
|
3355 |
QScriptValue ret = eng.evaluate("foo('bar')"); |
|
3356 |
QVERIFY(ret.isFunction()); |
|
3357 |
QScriptValue ret2 = ret.call(QScriptValue()); |
|
3358 |
QCOMPARE(ret2.toString(), QString::fromLatin1("bar")); |
|
3359 |
QVERIFY(!eng.globalObject().property("bar").isValid()); |
|
3360 |
QVERIFY(!eng.globalObject().property("baz").isValid()); |
|
3361 |
} |
|
3362 |
{ |
|
3363 |
QScriptValue ret = eng.evaluate("foo('baz')"); |
|
3364 |
QVERIFY(ret.isFunction()); |
|
3365 |
QScriptValue ret2 = ret.call(QScriptValue()); |
|
3366 |
QCOMPARE(ret2.toString(), QString::fromLatin1("baz")); |
|
3367 |
QVERIFY(!eng.globalObject().property("bar").isValid()); |
|
3368 |
QVERIFY(!eng.globalObject().property("baz").isValid()); |
|
3369 |
} |
|
3370 |
} |
|
3371 |
||
3372 |
void tst_QScriptEngine::stringObjects() |
|
3373 |
{ |
|
3374 |
QScriptEngine eng; |
|
3375 |
QString str("ciao"); |
|
3376 |
// in C++ |
|
3377 |
{ |
|
3378 |
QScriptValue obj = QScriptValue(&eng, str).toObject(); |
|
3379 |
QCOMPARE(obj.property("length").toInt32(), str.length()); |
|
3380 |
QCOMPARE(obj.propertyFlags("length"), QScriptValue::PropertyFlags(QScriptValue::Undeletable | QScriptValue::SkipInEnumeration | QScriptValue::ReadOnly)); |
|
3381 |
for (int i = 0; i < str.length(); ++i) { |
|
3382 |
QString pname = QString::number(i); |
|
3383 |
QVERIFY(obj.property(pname).isString()); |
|
3384 |
QCOMPARE(obj.property(pname).toString(), QString(str.at(i))); |
|
3385 |
QCOMPARE(obj.propertyFlags(pname), QScriptValue::PropertyFlags(QScriptValue::Undeletable | QScriptValue::ReadOnly)); |
|
3386 |
obj.setProperty(pname, QScriptValue()); |
|
3387 |
obj.setProperty(pname, QScriptValue(&eng, 123)); |
|
3388 |
QVERIFY(obj.property(pname).isString()); |
|
3389 |
QCOMPARE(obj.property(pname).toString(), QString(str.at(i))); |
|
3390 |
} |
|
3391 |
QVERIFY(!obj.property("-1").isValid()); |
|
3392 |
QVERIFY(!obj.property(QString::number(str.length())).isValid()); |
|
3393 |
||
3394 |
QScriptValue val(&eng, 123); |
|
3395 |
obj.setProperty("-1", val); |
|
3396 |
QVERIFY(obj.property("-1").strictlyEquals(val)); |
|
3397 |
obj.setProperty("100", val); |
|
3398 |
QVERIFY(obj.property("100").strictlyEquals(val)); |
|
3399 |
} |
|
3400 |
||
3401 |
// in script |
|
3402 |
{ |
|
3403 |
QScriptValue ret = eng.evaluate("s = new String('ciao'); r = []; for (var p in s) r.push(p); r"); |
|
3404 |
QVERIFY(ret.isArray()); |
|
3405 |
QStringList lst = qscriptvalue_cast<QStringList>(ret); |
|
3406 |
QCOMPARE(lst.size(), str.length()); |
|
3407 |
for (int i = 0; i < str.length(); ++i) |
|
3408 |
QCOMPARE(lst.at(i), QString::number(i)); |
|
3409 |
||
3410 |
QScriptValue ret2 = eng.evaluate("s[0] = 123; s[0]"); |
|
3411 |
QVERIFY(ret2.isString()); |
|
3412 |
QCOMPARE(ret2.toString().length(), 1); |
|
3413 |
QCOMPARE(ret2.toString().at(0), str.at(0)); |
|
3414 |
||
3415 |
QScriptValue ret3 = eng.evaluate("s[-1] = 123; s[-1]"); |
|
3416 |
QVERIFY(ret3.isNumber()); |
|
3417 |
QCOMPARE(ret3.toInt32(), 123); |
|
3418 |
||
3419 |
QScriptValue ret4 = eng.evaluate("s[s.length] = 456; s[s.length]"); |
|
3420 |
QVERIFY(ret4.isNumber()); |
|
3421 |
QCOMPARE(ret4.toInt32(), 456); |
|
3422 |
||
3423 |
QScriptValue ret5 = eng.evaluate("delete s[0]"); |
|
3424 |
QVERIFY(ret5.isBoolean()); |
|
3425 |
QVERIFY(!ret5.toBoolean()); |
|
3426 |
||
3427 |
QScriptValue ret6 = eng.evaluate("delete s[-1]"); |
|
3428 |
QVERIFY(ret6.isBoolean()); |
|
3429 |
QVERIFY(ret6.toBoolean()); |
|
3430 |
||
3431 |
QScriptValue ret7 = eng.evaluate("delete s[s.length]"); |
|
3432 |
QVERIFY(ret7.isBoolean()); |
|
3433 |
QVERIFY(ret7.toBoolean()); |
|
3434 |
} |
|
3435 |
||
3436 |
// task 212440 |
|
3437 |
{ |
|
3438 |
QScriptValue ret = eng.evaluate("replace_args = []; \"a a a\".replace(/(a)/g, function() { replace_args.push(arguments); }); replace_args"); |
|
3439 |
QVERIFY(ret.isArray()); |
|
3440 |
int len = ret.property("length").toInt32(); |
|
3441 |
QCOMPARE(len, 3); |
|
3442 |
for (int i = 0; i < len; ++i) { |
|
3443 |
QScriptValue args = ret.property(i); |
|
3444 |
QCOMPARE(args.property("length").toInt32(), 4); |
|
3445 |
QCOMPARE(args.property(0).toString(), QString::fromLatin1("a")); // matched string |
|
3446 |
QCOMPARE(args.property(1).toString(), QString::fromLatin1("a")); // capture |
|
3447 |
QVERIFY(args.property(2).isNumber()); |
|
3448 |
QCOMPARE(args.property(2).toInt32(), i*2); // index of match |
|
3449 |
QCOMPARE(args.property(3).toString(), QString::fromLatin1("a a a")); |
|
3450 |
} |
|
3451 |
} |
|
3452 |
// task 212501 |
|
3453 |
{ |
|
3454 |
QScriptValue ret = eng.evaluate("\"foo\".replace(/a/g, function() {})"); |
|
3455 |
QVERIFY(ret.isString()); |
|
3456 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
3457 |
} |
|
3458 |
} |
|
3459 |
||
3460 |
void tst_QScriptEngine::getterSetterThisObject() |
|
3461 |
{ |
|
3462 |
// Global Object |
|
3463 |
{ |
|
3464 |
QScriptEngine eng; |
|
3465 |
// read |
|
3466 |
eng.evaluate("__defineGetter__('x', function() { return this; });"); |
|
3467 |
{ |
|
3468 |
QScriptValue ret = eng.evaluate("x"); |
|
3469 |
QVERIFY(ret.equals(eng.globalObject())); |
|
3470 |
} |
|
3471 |
{ |
|
3472 |
QScriptValue ret = eng.evaluate("(function() { return x; })()"); |
|
3473 |
QVERIFY(ret.equals(eng.globalObject())); |
|
3474 |
} |
|
3475 |
{ |
|
3476 |
QScriptValue ret = eng.evaluate("with (this) x"); |
|
3477 |
QVERIFY(ret.equals(eng.globalObject())); |
|
3478 |
} |
|
3479 |
{ |
|
3480 |
QScriptValue ret = eng.evaluate("with ({}) x"); |
|
3481 |
QVERIFY(ret.equals(eng.globalObject())); |
|
3482 |
} |
|
3483 |
{ |
|
3484 |
QScriptValue ret = eng.evaluate("(function() { with ({}) return x; })()"); |
|
3485 |
QVERIFY(ret.equals(eng.globalObject())); |
|
3486 |
} |
|
3487 |
// write |
|
3488 |
eng.evaluate("__defineSetter__('x', function() { return this; });"); |
|
3489 |
{ |
|
3490 |
QScriptValue ret = eng.evaluate("x = 'foo'"); |
|
3491 |
// SpiderMonkey says setter return value, JSC says RHS. |
|
3492 |
QVERIFY(ret.isString()); |
|
3493 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
3494 |
} |
|
3495 |
{ |
|
3496 |
QScriptValue ret = eng.evaluate("(function() { return x = 'foo'; })()"); |
|
3497 |
// SpiderMonkey says setter return value, JSC says RHS. |
|
3498 |
QVERIFY(ret.isString()); |
|
3499 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
3500 |
} |
|
3501 |
{ |
|
3502 |
QScriptValue ret = eng.evaluate("with (this) x = 'foo'"); |
|
3503 |
// SpiderMonkey says setter return value, JSC says RHS. |
|
3504 |
QVERIFY(ret.isString()); |
|
3505 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
3506 |
} |
|
3507 |
{ |
|
3508 |
QScriptValue ret = eng.evaluate("with ({}) x = 'foo'"); |
|
3509 |
// SpiderMonkey says setter return value, JSC says RHS. |
|
3510 |
QVERIFY(ret.isString()); |
|
3511 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
3512 |
} |
|
3513 |
{ |
|
3514 |
QScriptValue ret = eng.evaluate("(function() { with ({}) return x = 'foo'; })()"); |
|
3515 |
// SpiderMonkey says setter return value, JSC says RHS. |
|
3516 |
QVERIFY(ret.isString()); |
|
3517 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
3518 |
} |
|
3519 |
} |
|
3520 |
||
3521 |
// other object |
|
3522 |
{ |
|
3523 |
QScriptEngine eng; |
|
3524 |
eng.evaluate("o = {}"); |
|
3525 |
// read |
|
3526 |
eng.evaluate("o.__defineGetter__('x', function() { return this; })"); |
|
3527 |
QVERIFY(eng.evaluate("o.x === o").toBoolean()); |
|
3528 |
QVERIFY(eng.evaluate("with (o) x").equals(eng.evaluate("o"))); |
|
3529 |
QVERIFY(eng.evaluate("(function() { with (o) return x; })() === o").toBoolean()); |
|
3530 |
eng.evaluate("q = {}; with (o) with (q) x").equals(eng.evaluate("o")); |
|
3531 |
// write |
|
3532 |
eng.evaluate("o.__defineSetter__('x', function() { return this; });"); |
|
3533 |
// SpiderMonkey says setter return value, JSC says RHS. |
|
3534 |
QVERIFY(eng.evaluate("(o.x = 'foo') === 'foo'").toBoolean()); |
|
3535 |
QVERIFY(eng.evaluate("with (o) x = 'foo'").equals("foo")); |
|
3536 |
QVERIFY(eng.evaluate("with (o) with (q) x = 'foo'").equals("foo")); |
|
3537 |
} |
|
3538 |
||
3539 |
// getter+setter in prototype chain |
|
3540 |
{ |
|
3541 |
QScriptEngine eng; |
|
3542 |
eng.evaluate("o = {}; p = {}; o.__proto__ = p"); |
|
3543 |
// read |
|
3544 |
eng.evaluate("p.__defineGetter__('x', function() { return this; })"); |
|
3545 |
QVERIFY(eng.evaluate("o.x === o").toBoolean()); |
|
3546 |
QVERIFY(eng.evaluate("with (o) x").equals(eng.evaluate("o"))); |
|
3547 |
QVERIFY(eng.evaluate("(function() { with (o) return x; })() === o").toBoolean()); |
|
3548 |
eng.evaluate("q = {}; with (o) with (q) x").equals(eng.evaluate("o")); |
|
3549 |
eng.evaluate("with (q) with (o) x").equals(eng.evaluate("o")); |
|
3550 |
// write |
|
3551 |
eng.evaluate("o.__defineSetter__('x', function() { return this; });"); |
|
3552 |
// SpiderMonkey says setter return value, JSC says RHS. |
|
3553 |
QVERIFY(eng.evaluate("(o.x = 'foo') === 'foo'").toBoolean()); |
|
3554 |
QVERIFY(eng.evaluate("with (o) x = 'foo'").equals("foo")); |
|
3555 |
QVERIFY(eng.evaluate("with (o) with (q) x = 'foo'").equals("foo")); |
|
3556 |
} |
|
3557 |
||
3558 |
// getter+setter in activation |
|
3559 |
{ |
|
3560 |
QScriptEngine eng; |
|
3561 |
QScriptContext *ctx = eng.pushContext(); |
|
3562 |
QVERIFY(ctx != 0); |
|
3563 |
QScriptValue act = ctx->activationObject(); |
|
3564 |
act.setProperty("act", act); |
|
3565 |
// read |
|
3566 |
eng.evaluate("act.__defineGetter__('x', function() { return this; })"); |
|
3567 |
QVERIFY(eng.evaluate("x === act").toBoolean()); |
|
3568 |
QEXPECT_FAIL("", "Exotic overload (don't care for now)", Continue); |
|
3569 |
QVERIFY(eng.evaluate("with (act) x").equals("foo")); |
|
3570 |
QVERIFY(eng.evaluate("(function() { with (act) return x; })() === act").toBoolean()); |
|
3571 |
eng.evaluate("q = {}; with (act) with (q) x").equals(eng.evaluate("act")); |
|
3572 |
eng.evaluate("with (q) with (act) x").equals(eng.evaluate("act")); |
|
3573 |
// write |
|
3574 |
eng.evaluate("act.__defineSetter__('x', function() { return this; });"); |
|
3575 |
QVERIFY(eng.evaluate("(x = 'foo') === 'foo'").toBoolean()); |
|
3576 |
QVERIFY(eng.evaluate("with (act) x = 'foo'").equals("foo")); |
|
3577 |
QVERIFY(eng.evaluate("with (act) with (q) x = 'foo'").equals("foo")); |
|
3578 |
eng.popContext(); |
|
3579 |
} |
|
3580 |
} |
|
3581 |
||
3582 |
void tst_QScriptEngine::continueInSwitch() |
|
3583 |
{ |
|
3584 |
QScriptEngine eng; |
|
3585 |
// switch - continue |
|
3586 |
{ |
|
3587 |
QScriptValue ret = eng.evaluate("switch (1) { default: continue; }"); |
|
3588 |
QVERIFY(ret.isError()); |
|
3589 |
} |
|
3590 |
// for - switch - case - continue |
|
3591 |
{ |
|
3592 |
QScriptValue ret = eng.evaluate("j = 0; for (i = 0; i < 100000; ++i) {\n" |
|
3593 |
" switch (i) {\n" |
|
3594 |
" case 1: ++j; continue;\n" |
|
3595 |
" case 100: ++j; continue;\n" |
|
3596 |
" case 1000: ++j; continue;\n" |
|
3597 |
" }\n" |
|
3598 |
"}; j"); |
|
3599 |
QVERIFY(ret.isNumber()); |
|
3600 |
QCOMPARE(ret.toInt32(), 3); |
|
3601 |
} |
|
3602 |
// for - switch - case - default - continue |
|
3603 |
{ |
|
3604 |
QScriptValue ret = eng.evaluate("j = 0; for (i = 0; i < 100000; ++i) {\n" |
|
3605 |
" switch (i) {\n" |
|
3606 |
" case 1: ++j; continue;\n" |
|
3607 |
" case 100: ++j; continue;\n" |
|
3608 |
" case 1000: ++j; continue;\n" |
|
3609 |
" default: if (i < 50000) break; else continue;\n" |
|
3610 |
" }\n" |
|
3611 |
"}; j"); |
|
3612 |
QVERIFY(ret.isNumber()); |
|
3613 |
QCOMPARE(ret.toInt32(), 3); |
|
3614 |
} |
|
3615 |
// switch - for - continue |
|
3616 |
{ |
|
3617 |
QScriptValue ret = eng.evaluate("j = 123; switch (j) {\n" |
|
3618 |
" case 123:\n" |
|
3619 |
" for (i = 0; i < 100000; ++i) {\n" |
|
3620 |
" continue;\n" |
|
3621 |
" }\n" |
|
3622 |
"}; i\n"); |
|
3623 |
QVERIFY(ret.isNumber()); |
|
3624 |
QCOMPARE(ret.toInt32(), 100000); |
|
3625 |
} |
|
3626 |
// switch - switch - continue |
|
3627 |
{ |
|
3628 |
QScriptValue ret = eng.evaluate("i = 1; switch (i) { default: switch (i) { case 1: continue; } }"); |
|
3629 |
QVERIFY(ret.isError()); |
|
3630 |
} |
|
3631 |
// for - switch - switch - continue |
|
3632 |
{ |
|
3633 |
QScriptValue ret = eng.evaluate("j = 0; for (i = 0; i < 100000; ++i) {\n" |
|
3634 |
" switch (i) {\n" |
|
3635 |
" case 1:\n" |
|
3636 |
" switch (i) {\n" |
|
3637 |
" case 1: ++j; continue;\n" |
|
3638 |
" }\n" |
|
3639 |
" }\n" |
|
3640 |
"}; j"); |
|
3641 |
QVERIFY(ret.isNumber()); |
|
3642 |
QCOMPARE(ret.toInt32(), 1); |
|
3643 |
} |
|
3644 |
// switch - for - switch - continue |
|
3645 |
{ |
|
3646 |
QScriptValue ret = eng.evaluate("j = 123; switch (j) {\n" |
|
3647 |
" case 123:\n" |
|
3648 |
" for (i = 0; i < 100000; ++i) {\n" |
|
3649 |
" switch (i) {\n" |
|
3650 |
" case 1:\n" |
|
3651 |
" ++j; continue;\n" |
|
3652 |
" }\n" |
|
3653 |
" }\n" |
|
3654 |
"}; i\n"); |
|
3655 |
QVERIFY(ret.isNumber()); |
|
3656 |
QCOMPARE(ret.toInt32(), 100000); |
|
3657 |
} |
|
3658 |
} |
|
3659 |
||
3660 |
void tst_QScriptEngine::readOnlyPrototypeProperty() |
|
3661 |
{ |
|
3662 |
QSKIP("JSC semantics differ from old back-end and SpiderMonkey", SkipAll); |
|
3663 |
QScriptEngine eng; |
|
3664 |
QCOMPARE(eng.evaluate("o = {}; o.__proto__ = parseInt; o.length").toInt32(), 2); |
|
3665 |
QCOMPARE(eng.evaluate("o.length = 4; o.length").toInt32(), 2); |
|
3666 |
QVERIFY(!eng.evaluate("o.hasOwnProperty('length')").toBoolean()); |
|
3667 |
QCOMPARE(eng.evaluate("o.length *= 2; o.length").toInt32(), 2); |
|
3668 |
QCOMPARE(eng.evaluate("o.length /= 2; o.length").toInt32(), 2); |
|
3669 |
QCOMPARE(eng.evaluate("o.length %= 2; o.length").toInt32(), 2); |
|
3670 |
QCOMPARE(eng.evaluate("o.length += 2; o.length").toInt32(), 2); |
|
3671 |
QCOMPARE(eng.evaluate("o.length -= 2; o.length").toInt32(), 2); |
|
3672 |
QCOMPARE(eng.evaluate("o.length <<= 2; o.length").toInt32(), 2); |
|
3673 |
QCOMPARE(eng.evaluate("o.length >>= 2; o.length").toInt32(), 2); |
|
3674 |
QCOMPARE(eng.evaluate("o.length >>>= 2; o.length").toInt32(), 2); |
|
3675 |
QCOMPARE(eng.evaluate("o.length &= 0; o.length").toInt32(), 2); |
|
3676 |
QCOMPARE(eng.evaluate("o.length ^= 255; o.length").toInt32(), 2); |
|
3677 |
QCOMPARE(eng.evaluate("o.length |= 255; o.length").toInt32(), 2); |
|
3678 |
||
3679 |
{ |
|
3680 |
QScriptValue ret = eng.evaluate("o.__defineGetter__('length', function() {})"); |
|
3681 |
QVERIFY(ret.isError()); |
|
3682 |
QCOMPARE(ret.toString(), QString::fromLatin1("Error: cannot redefine read-only property")); |
|
3683 |
} |
|
3684 |
{ |
|
3685 |
QScriptValue ret = eng.evaluate("o.__defineSetter__('length', function() {})"); |
|
3686 |
QVERIFY(ret.isError()); |
|
3687 |
QCOMPARE(ret.toString(), QString::fromLatin1("Error: cannot redefine read-only property")); |
|
3688 |
} |
|
3689 |
} |
|
3690 |
||
3691 |
void tst_QScriptEngine::toObject() |
|
3692 |
{ |
|
3693 |
QScriptEngine eng; |
|
3694 |
||
3695 |
QVERIFY(!eng.toObject(eng.undefinedValue()).isValid()); |
|
3696 |
||
3697 |
QVERIFY(!eng.toObject(eng.nullValue()).isValid()); |
|
3698 |
||
3699 |
QScriptValue falskt(false); |
|
3700 |
{ |
|
3701 |
QScriptValue tmp = eng.toObject(falskt); |
|
3702 |
QVERIFY(tmp.isObject()); |
|
3703 |
QCOMPARE(tmp.toNumber(), falskt.toNumber()); |
|
3704 |
} |
|
3705 |
||
3706 |
QScriptValue sant(true); |
|
3707 |
{ |
|
3708 |
QScriptValue tmp = eng.toObject(sant); |
|
3709 |
QVERIFY(tmp.isObject()); |
|
3710 |
QCOMPARE(tmp.toNumber(), sant.toNumber()); |
|
3711 |
} |
|
3712 |
||
3713 |
QScriptValue number(123.0); |
|
3714 |
{ |
|
3715 |
QScriptValue tmp = eng.toObject(number); |
|
3716 |
QVERIFY(tmp.isObject()); |
|
3717 |
QCOMPARE(tmp.toNumber(), number.toNumber()); |
|
3718 |
} |
|
3719 |
||
3720 |
QScriptValue str = QScriptValue(&eng, QString("ciao")); |
|
3721 |
{ |
|
3722 |
QScriptValue tmp = eng.toObject(str); |
|
3723 |
QVERIFY(tmp.isObject()); |
|
3724 |
QCOMPARE(tmp.toString(), str.toString()); |
|
3725 |
} |
|
3726 |
||
3727 |
QScriptValue object = eng.newObject(); |
|
3728 |
{ |
|
3729 |
QScriptValue tmp = eng.toObject(object); |
|
3730 |
QVERIFY(tmp.isObject()); |
|
3731 |
QVERIFY(tmp.strictlyEquals(object)); |
|
3732 |
} |
|
3733 |
||
3734 |
QScriptValue qobject = eng.newQObject(this); |
|
3735 |
QVERIFY(eng.toObject(qobject).strictlyEquals(qobject)); |
|
3736 |
||
3737 |
QVERIFY(!eng.toObject(QScriptValue()).isValid()); |
|
3738 |
} |
|
3739 |
||
3740 |
void tst_QScriptEngine::reservedWords_data() |
|
3741 |
{ |
|
3742 |
QTest::addColumn<QString>("word"); |
|
3743 |
QTest::newRow("break") << QString("break"); |
|
3744 |
QTest::newRow("case") << QString("case"); |
|
3745 |
QTest::newRow("catch") << QString("catch"); |
|
3746 |
QTest::newRow("continue") << QString("continue"); |
|
3747 |
QTest::newRow("default") << QString("default"); |
|
3748 |
QTest::newRow("delete") << QString("delete"); |
|
3749 |
QTest::newRow("do") << QString("do"); |
|
3750 |
QTest::newRow("else") << QString("else"); |
|
3751 |
QTest::newRow("false") << QString("false"); |
|
3752 |
QTest::newRow("finally") << QString("finally"); |
|
3753 |
QTest::newRow("for") << QString("for"); |
|
3754 |
QTest::newRow("function") << QString("function"); |
|
3755 |
QTest::newRow("if") << QString("if"); |
|
3756 |
QTest::newRow("in") << QString("in"); |
|
3757 |
QTest::newRow("instanceof") << QString("instanceof"); |
|
3758 |
QTest::newRow("new") << QString("new"); |
|
3759 |
QTest::newRow("null") << QString("null"); |
|
3760 |
QTest::newRow("return") << QString("return"); |
|
3761 |
QTest::newRow("switch") << QString("switch"); |
|
3762 |
QTest::newRow("this") << QString("this"); |
|
3763 |
QTest::newRow("throw") << QString("throw"); |
|
3764 |
QTest::newRow("true") << QString("true"); |
|
3765 |
QTest::newRow("try") << QString("try"); |
|
3766 |
QTest::newRow("typeof") << QString("typeof"); |
|
3767 |
QTest::newRow("var") << QString("var"); |
|
3768 |
QTest::newRow("void") << QString("void"); |
|
3769 |
QTest::newRow("while") << QString("while"); |
|
3770 |
QTest::newRow("with") << QString("with"); |
|
3771 |
} |
|
3772 |
||
3773 |
void tst_QScriptEngine::reservedWords() |
|
3774 |
{ |
|
3775 |
QFETCH(QString, word); |
|
3776 |
{ |
|
3777 |
QScriptEngine eng; |
|
3778 |
QScriptValue ret = eng.evaluate(word + " = 123"); |
|
3779 |
QVERIFY(ret.isError()); |
|
3780 |
QString str = ret.toString(); |
|
3781 |
QVERIFY(str.startsWith("SyntaxError") || str.startsWith("ReferenceError")); |
|
3782 |
} |
|
3783 |
{ |
|
3784 |
QScriptEngine eng; |
|
3785 |
QScriptValue ret = eng.evaluate("var " + word + " = 123"); |
|
3786 |
QVERIFY(ret.isError()); |
|
3787 |
QVERIFY(ret.toString().startsWith("SyntaxError")); |
|
3788 |
} |
|
3789 |
{ |
|
3790 |
QScriptEngine eng; |
|
3791 |
QScriptValue ret = eng.evaluate("o = {}; o." + word + " = 123"); |
|
3792 |
// in the old back-end and in SpiderMonkey this is allowed, but not in JSC |
|
3793 |
QVERIFY(ret.isError()); |
|
3794 |
QVERIFY(ret.toString().startsWith("SyntaxError")); |
|
3795 |
} |
|
3796 |
{ |
|
3797 |
QScriptEngine eng; |
|
3798 |
QScriptValue ret = eng.evaluate("o = { " + word + ": 123 }"); |
|
3799 |
// in the old back-end and in SpiderMonkey this is allowed, but not in JSC |
|
3800 |
QVERIFY(ret.isError()); |
|
3801 |
QVERIFY(ret.toString().startsWith("SyntaxError")); |
|
3802 |
} |
|
3803 |
{ |
|
3804 |
// SpiderMonkey allows this, but we don't |
|
3805 |
QScriptEngine eng; |
|
3806 |
QScriptValue ret = eng.evaluate("function " + word + "() {}"); |
|
3807 |
QVERIFY(ret.isError()); |
|
3808 |
QVERIFY(ret.toString().startsWith("SyntaxError")); |
|
3809 |
} |
|
3810 |
} |
|
3811 |
||
3812 |
void tst_QScriptEngine::futureReservedWords_data() |
|
3813 |
{ |
|
3814 |
QTest::addColumn<QString>("word"); |
|
3815 |
QTest::addColumn<bool>("allowed"); |
|
3816 |
QTest::newRow("abstract") << QString("abstract") << true; |
|
3817 |
QTest::newRow("boolean") << QString("boolean") << true; |
|
3818 |
QTest::newRow("byte") << QString("byte") << true; |
|
3819 |
QTest::newRow("char") << QString("char") << true; |
|
3820 |
QTest::newRow("class") << QString("class") << false; |
|
3821 |
QTest::newRow("const") << QString("const") << false; |
|
3822 |
QTest::newRow("debugger") << QString("debugger") << false; |
|
3823 |
QTest::newRow("double") << QString("double") << true; |
|
3824 |
QTest::newRow("enum") << QString("enum") << false; |
|
3825 |
QTest::newRow("export") << QString("export") << false; |
|
3826 |
QTest::newRow("extends") << QString("extends") << false; |
|
3827 |
QTest::newRow("final") << QString("final") << true; |
|
3828 |
QTest::newRow("float") << QString("float") << true; |
|
3829 |
QTest::newRow("goto") << QString("goto") << true; |
|
3830 |
QTest::newRow("implements") << QString("implements") << true; |
|
3831 |
QTest::newRow("import") << QString("import") << false; |
|
3832 |
QTest::newRow("int") << QString("int") << true; |
|
3833 |
QTest::newRow("interface") << QString("interface") << true; |
|
3834 |
QTest::newRow("long") << QString("long") << true; |
|
3835 |
QTest::newRow("native") << QString("native") << true; |
|
3836 |
QTest::newRow("package") << QString("package") << true; |
|
3837 |
QTest::newRow("private") << QString("private") << true; |
|
3838 |
QTest::newRow("protected") << QString("protected") << true; |
|
3839 |
QTest::newRow("public") << QString("public") << true; |
|
3840 |
QTest::newRow("short") << QString("short") << true; |
|
3841 |
QTest::newRow("static") << QString("static") << true; |
|
3842 |
QTest::newRow("super") << QString("super") << false; |
|
3843 |
QTest::newRow("synchronized") << QString("synchronized") << true; |
|
3844 |
QTest::newRow("throws") << QString("throws") << true; |
|
3845 |
QTest::newRow("transient") << QString("transient") << true; |
|
3846 |
QTest::newRow("volatile") << QString("volatile") << true; |
|
3847 |
} |
|
3848 |
||
3849 |
void tst_QScriptEngine::futureReservedWords() |
|
3850 |
{ |
|
3851 |
QFETCH(QString, word); |
|
3852 |
QFETCH(bool, allowed); |
|
3853 |
{ |
|
3854 |
QScriptEngine eng; |
|
3855 |
QScriptValue ret = eng.evaluate(word + " = 123"); |
|
3856 |
QCOMPARE(!ret.isError(), allowed); |
|
3857 |
} |
|
3858 |
{ |
|
3859 |
QScriptEngine eng; |
|
3860 |
QScriptValue ret = eng.evaluate("var " + word + " = 123"); |
|
3861 |
QCOMPARE(!ret.isError(), allowed); |
|
3862 |
} |
|
3863 |
{ |
|
3864 |
// this should probably be allowed (see task 162567) |
|
3865 |
QScriptEngine eng; |
|
3866 |
QScriptValue ret = eng.evaluate("o = {}; o." + word + " = 123"); |
|
3867 |
QCOMPARE(ret.isNumber(), allowed); |
|
3868 |
QCOMPARE(!ret.isError(), allowed); |
|
3869 |
} |
|
3870 |
{ |
|
3871 |
// this should probably be allowed (see task 162567) |
|
3872 |
QScriptEngine eng; |
|
3873 |
QScriptValue ret = eng.evaluate("o = { " + word + ": 123 }"); |
|
3874 |
QCOMPARE(!ret.isError(), allowed); |
|
3875 |
} |
|
3876 |
} |
|
3877 |
||
3878 |
void tst_QScriptEngine::throwInsideWithStatement() |
|
3879 |
{ |
|
3880 |
// task 209988 |
|
3881 |
QScriptEngine eng; |
|
3882 |
{ |
|
3883 |
QScriptValue ret = eng.evaluate( |
|
3884 |
"try {" |
|
3885 |
" o = { bad : \"bug\" };" |
|
3886 |
" with (o) {" |
|
3887 |
" throw 123;" |
|
3888 |
" }" |
|
3889 |
"} catch (e) {" |
|
3890 |
" bad;" |
|
3891 |
"}"); |
|
3892 |
QVERIFY(ret.isError()); |
|
3893 |
QCOMPARE(ret.toString(), QString::fromLatin1("ReferenceError: Can't find variable: bad")); |
|
3894 |
} |
|
3895 |
{ |
|
3896 |
QScriptValue ret = eng.evaluate( |
|
3897 |
"try {" |
|
3898 |
" o = { bad : \"bug\" };" |
|
3899 |
" with (o) {" |
|
3900 |
" throw 123;" |
|
3901 |
" }" |
|
3902 |
"} finally {" |
|
3903 |
" bad;" |
|
3904 |
"}"); |
|
3905 |
QVERIFY(ret.isError()); |
|
3906 |
QCOMPARE(ret.toString(), QString::fromLatin1("ReferenceError: Can't find variable: bad")); |
|
3907 |
} |
|
3908 |
{ |
|
3909 |
eng.clearExceptions(); |
|
3910 |
QScriptValue ret = eng.evaluate( |
|
3911 |
"o = { bug : \"no bug\" };" |
|
3912 |
"with (o) {" |
|
3913 |
" try {" |
|
3914 |
" throw 123;" |
|
3915 |
" } finally {" |
|
3916 |
" bug;" |
|
3917 |
" }" |
|
3918 |
"}"); |
|
3919 |
QVERIFY(ret.isNumber()); |
|
3920 |
QCOMPARE(ret.toInt32(), 123); |
|
3921 |
QVERIFY(eng.hasUncaughtException()); |
|
3922 |
} |
|
3923 |
{ |
|
3924 |
eng.clearExceptions(); |
|
3925 |
QScriptValue ret = eng.evaluate( |
|
3926 |
"o = { bug : \"no bug\" };" |
|
3927 |
"with (o) {" |
|
3928 |
" throw 123;" |
|
3929 |
"}"); |
|
3930 |
QVERIFY(ret.isNumber()); |
|
3931 |
QScriptValue ret2 = eng.evaluate("bug"); |
|
3932 |
QVERIFY(ret2.isError()); |
|
3933 |
QCOMPARE(ret2.toString(), QString::fromLatin1("ReferenceError: Can't find variable: bug")); |
|
3934 |
} |
|
3935 |
} |
|
3936 |
||
3937 |
class TestAgent : public QScriptEngineAgent |
|
3938 |
{ |
|
3939 |
public: |
|
3940 |
TestAgent(QScriptEngine *engine) : QScriptEngineAgent(engine) {} |
|
3941 |
}; |
|
3942 |
||
3943 |
void tst_QScriptEngine::getSetAgent() |
|
3944 |
{ |
|
3945 |
// case 1: engine deleted before agent --> agent deleted too |
|
3946 |
{ |
|
3947 |
QScriptEngine *eng = new QScriptEngine; |
|
3948 |
QCOMPARE(eng->agent(), (QScriptEngineAgent*)0); |
|
3949 |
TestAgent *agent = new TestAgent(eng); |
|
3950 |
eng->setAgent(agent); |
|
3951 |
QCOMPARE(eng->agent(), (QScriptEngineAgent*)agent); |
|
3952 |
eng->setAgent(0); // the engine maintains ownership of the old agent |
|
3953 |
QCOMPARE(eng->agent(), (QScriptEngineAgent*)0); |
|
3954 |
delete eng; |
|
3955 |
} |
|
3956 |
// case 2: agent deleted before engine --> engine's agent should become 0 |
|
3957 |
{ |
|
3958 |
QScriptEngine *eng = new QScriptEngine; |
|
3959 |
TestAgent *agent = new TestAgent(eng); |
|
3960 |
eng->setAgent(agent); |
|
3961 |
QCOMPARE(eng->agent(), (QScriptEngineAgent*)agent); |
|
3962 |
delete agent; |
|
3963 |
QCOMPARE(eng->agent(), (QScriptEngineAgent*)0); |
|
3964 |
eng->evaluate("(function(){ return 123; })()"); |
|
3965 |
delete eng; |
|
3966 |
} |
|
3967 |
{ |
|
3968 |
QScriptEngine eng; |
|
3969 |
QScriptEngine eng2; |
|
3970 |
TestAgent *agent = new TestAgent(&eng); |
|
3971 |
QTest::ignoreMessage(QtWarningMsg, "QScriptEngine::setAgent(): cannot set agent belonging to different engine"); |
|
3972 |
eng2.setAgent(agent); |
|
3973 |
QCOMPARE(eng2.agent(), (QScriptEngineAgent*)0); |
|
3974 |
} |
|
3975 |
} |
|
3976 |
||
3977 |
void tst_QScriptEngine::reentrancy() |
|
3978 |
{ |
|
3979 |
{ |
|
3980 |
QScriptEngine eng1; |
|
3981 |
QScriptEngine eng2; |
|
3982 |
QScriptString s1 = eng1.toStringHandle("foo"); |
|
3983 |
QScriptString s2 = eng2.toStringHandle("foo"); |
|
3984 |
QVERIFY(s1 != s2); |
|
3985 |
} |
|
3986 |
{ |
|
3987 |
QScriptEngine eng1; |
|
3988 |
QScriptEngine eng2; |
|
3989 |
eng1.setProcessEventsInterval(123); |
|
3990 |
QCOMPARE(eng2.processEventsInterval(), -1); |
|
3991 |
eng2.setProcessEventsInterval(456); |
|
3992 |
QCOMPARE(eng1.processEventsInterval(), 123); |
|
3993 |
} |
|
3994 |
{ |
|
3995 |
QScriptEngine eng1; |
|
3996 |
QScriptEngine eng2; |
|
3997 |
qScriptRegisterMetaType<Foo>(&eng1, fooToScriptValue, fooFromScriptValue); |
|
3998 |
Foo foo; |
|
3999 |
foo.x = 12; |
|
4000 |
foo.y = 34; |
|
4001 |
{ |
|
4002 |
QScriptValue fooVal = qScriptValueFromValue(&eng1, foo); |
|
4003 |
QVERIFY(fooVal.isObject()); |
|
4004 |
QVERIFY(!fooVal.isVariant()); |
|
4005 |
QCOMPARE(fooVal.property("x").toInt32(), 12); |
|
4006 |
QCOMPARE(fooVal.property("y").toInt32(), 34); |
|
4007 |
fooVal.setProperty("x", 56); |
|
4008 |
fooVal.setProperty("y", 78); |
|
4009 |
||
4010 |
Foo foo2 = qScriptValueToValue<Foo>(fooVal); |
|
4011 |
QCOMPARE(foo2.x, 56); |
|
4012 |
QCOMPARE(foo2.y, 78); |
|
4013 |
} |
|
4014 |
{ |
|
4015 |
QScriptValue fooVal = qScriptValueFromValue(&eng2, foo); |
|
4016 |
QVERIFY(fooVal.isVariant()); |
|
4017 |
||
4018 |
Foo foo2 = qScriptValueToValue<Foo>(fooVal); |
|
4019 |
QCOMPARE(foo2.x, 12); |
|
4020 |
QCOMPARE(foo2.y, 34); |
|
4021 |
} |
|
4022 |
QVERIFY(!eng1.defaultPrototype(qMetaTypeId<Foo>()).isValid()); |
|
4023 |
QVERIFY(!eng2.defaultPrototype(qMetaTypeId<Foo>()).isValid()); |
|
4024 |
QScriptValue proto1 = eng1.newObject(); |
|
4025 |
eng1.setDefaultPrototype(qMetaTypeId<Foo>(), proto1); |
|
4026 |
QVERIFY(!eng2.defaultPrototype(qMetaTypeId<Foo>()).isValid()); |
|
4027 |
QScriptValue proto2 = eng2.newObject(); |
|
4028 |
eng2.setDefaultPrototype(qMetaTypeId<Foo>(), proto2); |
|
4029 |
QVERIFY(eng2.defaultPrototype(qMetaTypeId<Foo>()).isValid()); |
|
4030 |
QVERIFY(eng1.defaultPrototype(qMetaTypeId<Foo>()).strictlyEquals(proto1)); |
|
4031 |
} |
|
4032 |
{ |
|
4033 |
QScriptEngine eng1; |
|
4034 |
QScriptEngine eng2; |
|
4035 |
QVERIFY(!eng2.globalObject().property("a").isValid()); |
|
4036 |
eng1.evaluate("a = 10"); |
|
4037 |
QVERIFY(eng1.globalObject().property("a").isNumber()); |
|
4038 |
QVERIFY(!eng2.globalObject().property("a").isValid()); |
|
4039 |
eng2.evaluate("a = 20"); |
|
4040 |
QVERIFY(eng2.globalObject().property("a").isNumber()); |
|
4041 |
QCOMPARE(eng1.globalObject().property("a").toInt32(), 10); |
|
4042 |
} |
|
4043 |
// weird bug with JSC backend |
|
4044 |
{ |
|
4045 |
QScriptEngine eng; |
|
4046 |
QCOMPARE(eng.evaluate("Array()").toString(), QString()); |
|
4047 |
eng.evaluate("Array.prototype.toString"); |
|
4048 |
QCOMPARE(eng.evaluate("Array()").toString(), QString()); |
|
4049 |
} |
|
4050 |
{ |
|
4051 |
QScriptEngine eng; |
|
4052 |
QCOMPARE(eng.evaluate("Array()").toString(), QString()); |
|
4053 |
} |
|
4054 |
} |
|
4055 |
||
4056 |
void tst_QScriptEngine:: incDecNonObjectProperty() |
|
4057 |
{ |
|
4058 |
QScriptEngine eng; |
|
4059 |
{ |
|
4060 |
QScriptValue ret = eng.evaluate("var a; a.n++"); |
|
4061 |
QVERIFY(ret.isError()); |
|
4062 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [undefined] is not an object.")); |
|
4063 |
} |
|
4064 |
{ |
|
4065 |
QScriptValue ret = eng.evaluate("var a; a.n--"); |
|
4066 |
QVERIFY(ret.isError()); |
|
4067 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [undefined] is not an object.")); |
|
4068 |
} |
|
4069 |
{ |
|
4070 |
QScriptValue ret = eng.evaluate("var a = null; a.n++"); |
|
4071 |
QVERIFY(ret.isError()); |
|
4072 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [null] is not an object.")); |
|
4073 |
} |
|
4074 |
{ |
|
4075 |
QScriptValue ret = eng.evaluate("var a = null; a.n--"); |
|
4076 |
QVERIFY(ret.isError()); |
|
4077 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [null] is not an object.")); |
|
4078 |
} |
|
4079 |
{ |
|
4080 |
QScriptValue ret = eng.evaluate("var a; ++a.n"); |
|
4081 |
QVERIFY(ret.isError()); |
|
4082 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [null] is not an object.")); |
|
4083 |
} |
|
4084 |
{ |
|
4085 |
QScriptValue ret = eng.evaluate("var a; --a.n"); |
|
4086 |
QVERIFY(ret.isError()); |
|
4087 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [null] is not an object.")); |
|
4088 |
} |
|
4089 |
{ |
|
4090 |
QScriptValue ret = eng.evaluate("var a; a.n += 1"); |
|
4091 |
QVERIFY(ret.isError()); |
|
4092 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [null] is not an object.")); |
|
4093 |
} |
|
4094 |
{ |
|
4095 |
QScriptValue ret = eng.evaluate("var a; a.n -= 1"); |
|
4096 |
QVERIFY(ret.isError()); |
|
4097 |
QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'a' [null] is not an object.")); |
|
4098 |
} |
|
4099 |
{ |
|
4100 |
QScriptValue ret = eng.evaluate("var a = 'ciao'; a.length++"); |
|
4101 |
QVERIFY(ret.isNumber()); |
|
4102 |
QCOMPARE(ret.toInt32(), 4); |
|
4103 |
} |
|
4104 |
{ |
|
4105 |
QScriptValue ret = eng.evaluate("var a = 'ciao'; a.length--"); |
|
4106 |
QVERIFY(ret.isNumber()); |
|
4107 |
QCOMPARE(ret.toInt32(), 4); |
|
4108 |
} |
|
4109 |
{ |
|
4110 |
QScriptValue ret = eng.evaluate("var a = 'ciao'; ++a.length"); |
|
4111 |
QVERIFY(ret.isNumber()); |
|
4112 |
QCOMPARE(ret.toInt32(), 5); |
|
4113 |
} |
|
4114 |
{ |
|
4115 |
QScriptValue ret = eng.evaluate("var a = 'ciao'; --a.length"); |
|
4116 |
QVERIFY(ret.isNumber()); |
|
4117 |
QCOMPARE(ret.toInt32(), 3); |
|
4118 |
} |
|
4119 |
} |
|
4120 |
||
4121 |
void tst_QScriptEngine::installTranslatorFunctions() |
|
4122 |
{ |
|
4123 |
QScriptEngine eng; |
|
4124 |
QScriptValue global = eng.globalObject(); |
|
4125 |
QVERIFY(!global.property("qsTranslate").isValid()); |
|
4126 |
QVERIFY(!global.property("QT_TRANSLATE_NOOP").isValid()); |
|
4127 |
QVERIFY(!global.property("qsTr").isValid()); |
|
4128 |
QVERIFY(!global.property("QT_TR_NOOP").isValid()); |
|
4129 |
QVERIFY(!global.property("String").property("prototype").property("arg").isValid()); |
|
4130 |
||
4131 |
eng.installTranslatorFunctions(); |
|
4132 |
QVERIFY(global.property("qsTranslate").isFunction()); |
|
4133 |
QVERIFY(global.property("QT_TRANSLATE_NOOP").isFunction()); |
|
4134 |
QVERIFY(global.property("qsTr").isFunction()); |
|
4135 |
QVERIFY(global.property("QT_TR_NOOP").isFunction()); |
|
4136 |
QVERIFY(global.property("String").property("prototype").property("arg").isFunction()); |
|
4137 |
||
4138 |
{ |
|
4139 |
QScriptValue ret = eng.evaluate("qsTr('foo')"); |
|
4140 |
QVERIFY(ret.isString()); |
|
4141 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
4142 |
} |
|
4143 |
{ |
|
4144 |
QScriptValue ret = eng.evaluate("qsTranslate('foo', 'bar')"); |
|
4145 |
QVERIFY(ret.isString()); |
|
4146 |
QCOMPARE(ret.toString(), QString::fromLatin1("bar")); |
|
4147 |
} |
|
4148 |
{ |
|
4149 |
QScriptValue ret = eng.evaluate("QT_TR_NOOP('foo')"); |
|
4150 |
QVERIFY(ret.isString()); |
|
4151 |
QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
4152 |
} |
|
4153 |
{ |
|
4154 |
QScriptValue ret = eng.evaluate("QT_TRANSLATE_NOOP('foo', 'bar')"); |
|
4155 |
QVERIFY(ret.isString()); |
|
4156 |
QCOMPARE(ret.toString(), QString::fromLatin1("bar")); |
|
4157 |
} |
|
4158 |
{ |
|
4159 |
QScriptValue ret = eng.evaluate("'foo%0'.arg('bar')"); |
|
4160 |
QVERIFY(ret.isString()); |
|
4161 |
QCOMPARE(ret.toString(), QString::fromLatin1("foobar")); |
|
4162 |
} |
|
4163 |
} |
|
4164 |
||
4165 |
void tst_QScriptEngine::functionScopes() |
|
4166 |
{ |
|
4167 |
QScriptEngine eng; |
|
4168 |
{ |
|
4169 |
// top-level functions have only the global object in their scope |
|
4170 |
QScriptValue fun = eng.evaluate("(function() {})"); |
|
4171 |
QVERIFY(fun.isFunction()); |
|
4172 |
QEXPECT_FAIL("", "Function scope proxying is not implemented", Abort); |
|
4173 |
QVERIFY(fun.scope().isObject()); |
|
4174 |
QVERIFY(fun.scope().strictlyEquals(eng.globalObject())); |
|
4175 |
QVERIFY(!eng.globalObject().scope().isValid()); |
|
4176 |
} |
|
4177 |
{ |
|
4178 |
QScriptValue fun = eng.globalObject().property("Object"); |
|
4179 |
QVERIFY(fun.isFunction()); |
|
4180 |
// native built-in functions don't have scope |
|
4181 |
QVERIFY(!fun.scope().isValid()); |
|
4182 |
} |
|
4183 |
{ |
|
4184 |
// closure |
|
4185 |
QScriptValue fun = eng.evaluate("(function(arg) { var foo = arg; return function() { return foo; }; })(123)"); |
|
4186 |
QVERIFY(fun.isFunction()); |
|
4187 |
{ |
|
4188 |
QScriptValue ret = fun.call(); |
|
4189 |
QVERIFY(ret.isNumber()); |
|
4190 |
QCOMPARE(ret.toInt32(), 123); |
|
4191 |
} |
|
4192 |
QScriptValue scope = fun.scope(); |
|
4193 |
QVERIFY(scope.isObject()); |
|
4194 |
{ |
|
4195 |
QScriptValue ret = scope.property("foo"); |
|
4196 |
QVERIFY(ret.isNumber()); |
|
4197 |
QCOMPARE(ret.toInt32(), 123); |
|
4198 |
QCOMPARE(scope.propertyFlags("foo"), QScriptValue::Undeletable); |
|
4199 |
} |
|
4200 |
{ |
|
4201 |
QScriptValue ret = scope.property("arg"); |
|
4202 |
QVERIFY(ret.isNumber()); |
|
4203 |
QCOMPARE(ret.toInt32(), 123); |
|
4204 |
QCOMPARE(scope.propertyFlags("arg"), QScriptValue::Undeletable | QScriptValue::SkipInEnumeration); |
|
4205 |
} |
|
4206 |
||
4207 |
scope.setProperty("foo", 456); |
|
4208 |
{ |
|
4209 |
QScriptValue ret = fun.call(); |
|
4210 |
QVERIFY(ret.isNumber()); |
|
4211 |
QCOMPARE(ret.toInt32(), 456); |
|
4212 |
} |
|
4213 |
||
4214 |
scope = scope.scope(); |
|
4215 |
QVERIFY(scope.isObject()); |
|
4216 |
QVERIFY(scope.strictlyEquals(eng.globalObject())); |
|
4217 |
} |
|
4218 |
} |
|
4219 |
||
4220 |
static QScriptValue counter_inner(QScriptContext *ctx, QScriptEngine *) |
|
4221 |
{ |
|
4222 |
QScriptValue outerAct = ctx->callee().scope(); |
|
4223 |
double count = outerAct.property("count").toNumber(); |
|
4224 |
outerAct.setProperty("count", count+1); |
|
4225 |
return count; |
|
4226 |
} |
|
4227 |
||
4228 |
static QScriptValue counter(QScriptContext *ctx, QScriptEngine *eng) |
|
4229 |
{ |
|
4230 |
QScriptValue act = ctx->activationObject(); |
|
4231 |
act.setProperty("count", ctx->argument(0).toInt32()); |
|
4232 |
QScriptValue result = eng->newFunction(counter_inner); |
|
4233 |
result.setScope(act); |
|
4234 |
return result; |
|
4235 |
} |
|
4236 |
||
4237 |
static QScriptValue counter_hybrid(QScriptContext *ctx, QScriptEngine *eng) |
|
4238 |
{ |
|
4239 |
QScriptValue act = ctx->activationObject(); |
|
4240 |
act.setProperty("count", ctx->argument(0).toInt32()); |
|
4241 |
return eng->evaluate("(function() { return count++; })"); |
|
4242 |
} |
|
4243 |
||
4244 |
void tst_QScriptEngine::nativeFunctionScopes() |
|
4245 |
{ |
|
4246 |
QScriptEngine eng; |
|
4247 |
{ |
|
4248 |
QScriptValue fun = eng.newFunction(counter); |
|
4249 |
QScriptValue cnt = fun.call(QScriptValue(), QScriptValueList() << 123); |
|
4250 |
QVERIFY(cnt.isFunction()); |
|
4251 |
{ |
|
4252 |
QScriptValue ret = cnt.call(); |
|
4253 |
QVERIFY(ret.isNumber()); |
|
4254 |
QCOMPARE(ret.toInt32(), 123); |
|
4255 |
} |
|
4256 |
} |
|
4257 |
{ |
|
4258 |
QScriptValue fun = eng.newFunction(counter_hybrid); |
|
4259 |
QScriptValue cnt = fun.call(QScriptValue(), QScriptValueList() << 123); |
|
4260 |
QVERIFY(cnt.isFunction()); |
|
4261 |
{ |
|
4262 |
QScriptValue ret = cnt.call(); |
|
4263 |
QVERIFY(ret.isNumber()); |
|
4264 |
QCOMPARE(ret.toInt32(), 123); |
|
4265 |
} |
|
4266 |
} |
|
4267 |
||
4268 |
//from http://doc.trolltech.com/latest/qtscript.html#nested-functions-and-the-scope-chain |
|
4269 |
{ |
|
4270 |
QScriptEngine eng; |
|
4271 |
eng.evaluate("function counter() { var count = 0; return function() { return count++; } }\n" |
|
4272 |
"var c1 = counter(); var c2 = counter(); "); |
|
4273 |
QCOMPARE(eng.evaluate("c1()").toString(), QString::fromLatin1("0")); |
|
4274 |
QCOMPARE(eng.evaluate("c1()").toString(), QString::fromLatin1("1")); |
|
4275 |
QCOMPARE(eng.evaluate("c2()").toString(), QString::fromLatin1("0")); |
|
4276 |
QCOMPARE(eng.evaluate("c2()").toString(), QString::fromLatin1("1")); |
|
4277 |
QVERIFY(!eng.hasUncaughtException()); |
|
4278 |
} |
|
4279 |
{ |
|
4280 |
QScriptEngine eng; |
|
4281 |
eng.globalObject().setProperty("counter", eng.newFunction(counter)); |
|
4282 |
eng.evaluate("var c1 = counter(); var c2 = counter(); "); |
|
4283 |
QCOMPARE(eng.evaluate("c1()").toString(), QString::fromLatin1("0")); |
|
4284 |
QCOMPARE(eng.evaluate("c1()").toString(), QString::fromLatin1("1")); |
|
4285 |
QCOMPARE(eng.evaluate("c2()").toString(), QString::fromLatin1("0")); |
|
4286 |
QCOMPARE(eng.evaluate("c2()").toString(), QString::fromLatin1("1")); |
|
4287 |
QVERIFY(!eng.hasUncaughtException()); |
|
4288 |
} |
|
4289 |
{ |
|
4290 |
QScriptEngine eng; |
|
4291 |
eng.globalObject().setProperty("counter", eng.newFunction(counter_hybrid)); |
|
4292 |
eng.evaluate("var c1 = counter(); var c2 = counter(); "); |
|
4293 |
QCOMPARE(eng.evaluate("c1()").toString(), QString::fromLatin1("0")); |
|
4294 |
QCOMPARE(eng.evaluate("c1()").toString(), QString::fromLatin1("1")); |
|
4295 |
QCOMPARE(eng.evaluate("c2()").toString(), QString::fromLatin1("0")); |
|
4296 |
QCOMPARE(eng.evaluate("c2()").toString(), QString::fromLatin1("1")); |
|
4297 |
QVERIFY(!eng.hasUncaughtException()); |
|
4298 |
} |
|
4299 |
} |
|
4300 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4301 |
static QScriptValue createProgram(QScriptContext *ctx, QScriptEngine *eng) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4302 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4303 |
QString code = ctx->argument(0).toString(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4304 |
QScriptProgram result(code); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4305 |
return qScriptValueFromValue(eng, result); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4306 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4307 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4308 |
void tst_QScriptEngine::evaluateProgram() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4309 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4310 |
QScriptEngine eng; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4311 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4312 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4313 |
QString code("1 + 2"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4314 |
QString fileName("hello.js"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4315 |
int lineNumber(123); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4316 |
QScriptProgram program(code, fileName, lineNumber); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4317 |
QVERIFY(!program.isNull()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4318 |
QCOMPARE(program.sourceCode(), code); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4319 |
QCOMPARE(program.fileName(), fileName); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4320 |
QCOMPARE(program.firstLineNumber(), lineNumber); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4321 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4322 |
QScriptValue expected = eng.evaluate(code); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4323 |
for (int x = 0; x < 10; ++x) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4324 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4325 |
QVERIFY(ret.equals(expected)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4326 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4327 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4328 |
// operator= |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4329 |
QScriptProgram sameProgram = program; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4330 |
QVERIFY(sameProgram == program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4331 |
QVERIFY(eng.evaluate(sameProgram).equals(expected)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4332 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4333 |
// copy constructor |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4334 |
QScriptProgram sameProgram2(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4335 |
QVERIFY(sameProgram2 == program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4336 |
QVERIFY(eng.evaluate(sameProgram2).equals(expected)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4337 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4338 |
QScriptProgram differentProgram("2 + 3"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4339 |
QVERIFY(differentProgram != program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4340 |
QVERIFY(!eng.evaluate(differentProgram).equals(expected)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4341 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4342 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4343 |
// Program that accesses variable in the scope |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4344 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4345 |
QScriptProgram program("a"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4346 |
QVERIFY(!program.isNull()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4347 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4348 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4349 |
QVERIFY(ret.isError()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4350 |
QCOMPARE(ret.toString(), QString::fromLatin1("ReferenceError: Can't find variable: a")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4351 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4352 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4353 |
QScriptValue obj = eng.newObject(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4354 |
obj.setProperty("a", 123); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4355 |
QScriptContext *ctx = eng.currentContext(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4356 |
ctx->pushScope(obj); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4357 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4358 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4359 |
QVERIFY(!ret.isError()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4360 |
QVERIFY(ret.equals(obj.property("a"))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4361 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4362 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4363 |
obj.setProperty("a", QScriptValue()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4364 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4365 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4366 |
QVERIFY(ret.isError()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4367 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4368 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4369 |
QScriptValue obj2 = eng.newObject(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4370 |
obj2.setProperty("a", 456); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4371 |
ctx->pushScope(obj2); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4372 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4373 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4374 |
QVERIFY(!ret.isError()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4375 |
QVERIFY(ret.equals(obj2.property("a"))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4376 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4377 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4378 |
ctx->popScope(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4379 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4380 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4381 |
// Program that creates closure |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4382 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4383 |
QScriptProgram program("(function() { var count = 0; return function() { return count++; }; })"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4384 |
QVERIFY(!program.isNull()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4385 |
QScriptValue createCounter = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4386 |
QVERIFY(createCounter.isFunction()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4387 |
QScriptValue counter = createCounter.call(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4388 |
QVERIFY(counter.isFunction()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4389 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4390 |
QScriptValue ret = counter.call(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4391 |
QVERIFY(ret.isNumber()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4392 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4393 |
QScriptValue counter2 = createCounter.call(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4394 |
QVERIFY(counter2.isFunction()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4395 |
QVERIFY(!counter2.equals(counter)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4396 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4397 |
QScriptValue ret = counter2.call(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4398 |
QVERIFY(ret.isNumber()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4399 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4400 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4401 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4402 |
// Program created in a function call, then executed later |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4403 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4404 |
QScriptValue fun = eng.newFunction(createProgram); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4405 |
QScriptProgram program = qscriptvalue_cast<QScriptProgram>( |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4406 |
fun.call(QScriptValue(), QScriptValueList() << "a + 1")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4407 |
QVERIFY(!program.isNull()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4408 |
eng.globalObject().setProperty("a", QScriptValue()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4409 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4410 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4411 |
QVERIFY(ret.isError()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4412 |
QCOMPARE(ret.toString(), QString::fromLatin1("ReferenceError: Can't find variable: a")); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4413 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4414 |
eng.globalObject().setProperty("a", 122); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4415 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4416 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4417 |
QVERIFY(!ret.isError()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4418 |
QVERIFY(ret.isNumber()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4419 |
QCOMPARE(ret.toInt32(), 123); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4420 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4421 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4422 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4423 |
// Same program run in different engines |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4424 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4425 |
QString code("1 + 2"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4426 |
QScriptProgram program(code); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4427 |
QVERIFY(!program.isNull()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4428 |
double expected = eng.evaluate(program).toNumber(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4429 |
for (int x = 0; x < 2; ++x) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4430 |
QScriptEngine eng2; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4431 |
for (int y = 0; y < 2; ++y) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4432 |
double ret = eng2.evaluate(program).toNumber(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4433 |
QCOMPARE(ret, expected); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4434 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4435 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4436 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4437 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4438 |
// No program |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4439 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4440 |
QScriptProgram program; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4441 |
QVERIFY(program.isNull()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4442 |
QScriptValue ret = eng.evaluate(program); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4443 |
QVERIFY(!ret.isValid()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4444 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4445 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4446 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4447 |
void tst_QScriptEngine::collectGarbageAfterConnect() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4448 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4449 |
// QTBUG-6366 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4450 |
QScriptEngine engine; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4451 |
QPointer<QWidget> widget = new QWidget; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4452 |
engine.globalObject().setProperty( |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4453 |
"widget", engine.newQObject(widget, QScriptEngine::ScriptOwnership)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4454 |
QVERIFY(engine.evaluate("widget.customContextMenuRequested.connect(\n" |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4455 |
" function() { print('hello'); }\n" |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4456 |
");") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4457 |
.isUndefined()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4458 |
QVERIFY(widget != 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4459 |
engine.evaluate("widget = null;"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4460 |
collectGarbage_helper(engine); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4461 |
QVERIFY(widget == 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4462 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4463 |
|
0 | 4464 |
static QRegExp minimal(QRegExp r) { r.setMinimal(true); return r; } |
4465 |
||
4466 |
void tst_QScriptEngine::qRegExpInport_data() |
|
4467 |
{ |
|
4468 |
QTest::addColumn<QRegExp>("rx"); |
|
4469 |
QTest::addColumn<QString>("string"); |
|
4470 |
QTest::addColumn<QString>("matched"); |
|
4471 |
||
4472 |
QTest::newRow("normal") << QRegExp("(test|foo)") << "test _ foo _ test _ Foo"; |
|
4473 |
QTest::newRow("normal2") << QRegExp("(Test|Foo)") << "test _ foo _ test _ Foo"; |
|
4474 |
QTest::newRow("case insensitive)") << QRegExp("(test|foo)", Qt::CaseInsensitive) << "test _ foo _ test _ Foo"; |
|
4475 |
QTest::newRow("case insensitive2)") << QRegExp("(Test|Foo)", Qt::CaseInsensitive) << "test _ foo _ test _ Foo"; |
|
4476 |
QTest::newRow("b(a*)(b*)") << QRegExp("b(a*)(b*)", Qt::CaseInsensitive) << "aaabbBbaAabaAaababaaabbaaab"; |
|
4477 |
QTest::newRow("greedy") << QRegExp("a*(a*)", Qt::CaseInsensitive, QRegExp::RegExp2) << "aaaabaaba"; |
|
4478 |
// this one will fail because we do not support the QRegExp::RegExp in JSC |
|
4479 |
//QTest::newRow("not_greedy") << QRegExp("a*(a*)", Qt::CaseInsensitive, QRegExp::RegExp) << "aaaabaaba"; |
|
4480 |
QTest::newRow("willcard") << QRegExp("*.txt", Qt::CaseSensitive, QRegExp::Wildcard) << "file.txt"; |
|
4481 |
QTest::newRow("willcard 2") << QRegExp("a?b.txt", Qt::CaseSensitive, QRegExp::Wildcard) << "ab.txt abb.rtc acb.txt"; |
|
4482 |
QTest::newRow("slash") << QRegExp("g/.*/s", Qt::CaseInsensitive, QRegExp::RegExp2) << "string/string/string"; |
|
4483 |
QTest::newRow("slash2") << QRegExp("g / .* / s", Qt::CaseInsensitive, QRegExp::RegExp2) << "string / string / string"; |
|
4484 |
QTest::newRow("fixed") << QRegExp("a*aa.a(ba)*a\\ba", Qt::CaseInsensitive, QRegExp::FixedString) << "aa*aa.a(ba)*a\\ba"; |
|
4485 |
QTest::newRow("fixed insensitive") << QRegExp("A*A", Qt::CaseInsensitive, QRegExp::FixedString) << "a*A A*a A*A a*a"; |
|
4486 |
QTest::newRow("fixed sensitive") << QRegExp("A*A", Qt::CaseSensitive, QRegExp::FixedString) << "a*A A*a A*A a*a"; |
|
4487 |
QTest::newRow("html") << QRegExp("<b>(.*)</b>", Qt::CaseSensitive, QRegExp::RegExp2) << "<b>bold</b><i>italic</i><b>bold</b>"; |
|
4488 |
QTest::newRow("html minimal") << minimal(QRegExp("<b>(.*)</b>", Qt::CaseSensitive, QRegExp::RegExp2)) << "<b>bold</b><i>italic</i><b>bold</b>"; |
|
4489 |
QTest::newRow("aaa") << QRegExp("a{2,5}") << "aAaAaaaaaAa"; |
|
4490 |
QTest::newRow("aaa minimal") << minimal(QRegExp("a{2,5}")) << "aAaAaaaaaAa"; |
|
4491 |
QTest::newRow("minimal") << minimal(QRegExp(".*\\} [*8]")) << "}?} ?} *"; |
|
4492 |
} |
|
4493 |
||
4494 |
void tst_QScriptEngine::qRegExpInport() |
|
4495 |
{ |
|
4496 |
QFETCH(QRegExp, rx); |
|
4497 |
QFETCH(QString, string); |
|
4498 |
||
4499 |
QScriptEngine eng; |
|
4500 |
QScriptValue rexp; |
|
4501 |
rexp = eng.newRegExp(rx); |
|
4502 |
||
4503 |
QCOMPARE(rexp.isValid(), true); |
|
4504 |
QCOMPARE(rexp.isRegExp(), true); |
|
4505 |
QVERIFY(rexp.isFunction()); |
|
4506 |
||
4507 |
QScriptValue func = eng.evaluate("(function(string, regexp) { return string.match(regexp); })"); |
|
4508 |
QScriptValue result = func.call(QScriptValue(), QScriptValueList() << string << rexp); |
|
4509 |
||
4510 |
rx.indexIn(string); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
4511 |
for (int i = 0; i <= rx.captureCount(); i++) { |
0 | 4512 |
QCOMPARE(result.property(i).toString(), rx.cap(i)); |
4513 |
} |
|
4514 |
} |
|
4515 |
||
4516 |
QTEST_MAIN(tst_QScriptEngine) |
|
4517 |
#include "tst_qscriptengine.moc" |