1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
/*
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "qscriptvalue.h"
#include "qscriptengine.h"
#include "qscriptengine_p.h"
#include "qscriptvalue_p.h"
#include <QtCore/qdebug.h>
/*!
Constructs an invalid value.
*/
QScriptValue::QScriptValue()
: d_ptr(new QScriptValuePrivate())
{
}
/*!
Constructs a new QScriptValue with a boolean \a value.
*/
QScriptValue::QScriptValue(bool value)
: d_ptr(new QScriptValuePrivate(value))
{
}
/*!
Constructs a new QScriptValue with a number \a value.
*/
QScriptValue::QScriptValue(int value)
: d_ptr(new QScriptValuePrivate(value))
{
}
/*!
Constructs a new QScriptValue with a number \a value.
*/
QScriptValue::QScriptValue(uint value)
: d_ptr(new QScriptValuePrivate(value))
{
}
/*!
Constructs a new QScriptValue with a number \a value.
*/
QScriptValue::QScriptValue(qsreal value)
: d_ptr(new QScriptValuePrivate(value))
{
}
/*!
Constructs a new QScriptValue with a string \a value.
*/
QScriptValue::QScriptValue(const QString& value)
: d_ptr(new QScriptValuePrivate(value))
{
}
/*!
Constructs a new QScriptValue with a special \a value.
*/
QScriptValue::QScriptValue(SpecialValue value)
: d_ptr(new QScriptValuePrivate(value))
{
}
/*!
Constructs a new QScriptValue with a string \a value.
*/
QScriptValue::QScriptValue(const char* value)
: d_ptr(new QScriptValuePrivate(QString::fromUtf8(value)))
{
}
/*!
Block automatic convertion to bool
\internal
*/
QScriptValue::QScriptValue(void* d)
{
Q_ASSERT(false);
}
/*!
Constructs a new QScriptValue from private
\internal
*/
QScriptValue::QScriptValue(QScriptValuePrivate* d)
: d_ptr(d)
{
}
/*!
\obsolete
Constructs a new QScriptValue with the boolean \a value and
registers it with the script \a engine.
*/
QScriptValue::QScriptValue(QScriptEngine* engine, bool value)
{
if (engine)
d_ptr = new QScriptValuePrivate(QScriptEnginePrivate::get(engine), value);
else
d_ptr = new QScriptValuePrivate(value);
}
/*!
\obsolete
Constructs a new QScriptValue with the integer \a value and
registers it with the script \a engine.
*/
QScriptValue::QScriptValue(QScriptEngine* engine, int value)
{
if (engine)
d_ptr = new QScriptValuePrivate(QScriptEnginePrivate::get(engine), value);
else
d_ptr = new QScriptValuePrivate(value);
}
/*!
\obsolete
Constructs a new QScriptValue with the unsigned integer \a value and
registers it with the script \a engine.
*/
QScriptValue::QScriptValue(QScriptEngine* engine, uint value)
{
if (engine)
d_ptr = new QScriptValuePrivate(QScriptEnginePrivate::get(engine), value);
else
d_ptr = new QScriptValuePrivate(value);
}
/*!
\obsolete
Constructs a new QScriptValue with the qsreal \a value and
registers it with the script \a engine.
*/
QScriptValue::QScriptValue(QScriptEngine* engine, qsreal value)
{
if (engine)
d_ptr = new QScriptValuePrivate(QScriptEnginePrivate::get(engine), value);
else
d_ptr = new QScriptValuePrivate(value);
}
/*!
\obsolete
Constructs a new QScriptValue with the string \a value and
registers it with the script \a engine.
*/
QScriptValue::QScriptValue(QScriptEngine* engine, const QString& value)
{
if (engine)
d_ptr = new QScriptValuePrivate(QScriptEnginePrivate::get(engine), value);
else
d_ptr = new QScriptValuePrivate(value);
}
/*!
\obsolete
Constructs a new QScriptValue with the string \a value and
registers it with the script \a engine.
*/
QScriptValue::QScriptValue(QScriptEngine* engine, const char* value)
{
if (engine)
d_ptr = new QScriptValuePrivate(QScriptEnginePrivate::get(engine), QString::fromUtf8(value));
else
d_ptr = new QScriptValuePrivate(QString::fromUtf8(value));
}
/*!
\obsolete
Constructs a new QScriptValue with the special \a value and
registers it with the script \a engine.
*/
QScriptValue::QScriptValue(QScriptEngine* engine, SpecialValue value)
{
if (engine)
d_ptr = new QScriptValuePrivate(QScriptEnginePrivate::get(engine), value);
else
d_ptr = new QScriptValuePrivate(value);
}
/*!
Constructs a new QScriptValue that is a copy of \a other.
Note that if \a other is an object (i.e., isObject() would return
true), then only a reference to the underlying object is copied into
the new script value (i.e., the object itself is not copied).
*/
QScriptValue::QScriptValue(const QScriptValue& other)
: d_ptr(other.d_ptr)
{
}
/*!
Destroys this QScriptValue.
*/
QScriptValue::~QScriptValue()
{
}
/*!
Returns true if this QScriptValue is valid; otherwise returns
false.
*/
bool QScriptValue::isValid() const
{
return d_ptr->isValid();
}
/*!
Returns true if this QScriptValue is of the primitive type Boolean;
otherwise returns false.
\sa toBool()
*/
bool QScriptValue::isBool() const
{
return d_ptr->isBool();
}
/*!
\obsolete
Use isBool() instead.
Returns true if this QScriptValue is of the primitive type Boolean;
otherwise returns false.
*/
bool QScriptValue::isBoolean() const
{
return d_ptr->isBool();
}
/*!
Returns true if this QScriptValue is of the primitive type Number;
otherwise returns false.
\sa toNumber()
*/
bool QScriptValue::isNumber() const
{
return d_ptr->isNumber();
}
/*!
Returns true if this QScriptValue is of the primitive type Null;
otherwise returns false.
\sa QScriptEngine::nullValue()
*/
bool QScriptValue::isNull() const
{
return d_ptr->isNull();
}
/*!
Returns true if this QScriptValue is of the primitive type String;
otherwise returns false.
\sa toString()
*/
bool QScriptValue::isString() const
{
return d_ptr->isString();
}
/*!
Returns true if this QScriptValue is of the primitive type Undefined;
otherwise returns false.
\sa QScriptEngine::undefinedValue()
*/
bool QScriptValue::isUndefined() const
{
return d_ptr->isUndefined();
}
/*!
Returns true if this QScriptValue is an object of the Error class;
otherwise returns false.
\sa QScriptContext::throwError()
*/
bool QScriptValue::isError() const
{
return d_ptr->isError();
}
/*!
Returns true if this QScriptValue is of the Object type; otherwise
returns false.
Note that function values, variant values, and QObject values are
objects, so this function returns true for such values.
\sa toObject(), QScriptEngine::newObject()
*/
bool QScriptValue::isObject() const
{
return d_ptr->isObject();
}
/*!
Returns true if this QScriptValue is a function; otherwise returns
false.
\sa call()
*/
bool QScriptValue::isFunction() const
{
return d_ptr->isFunction();
}
/*!
Returns the string value of this QScriptValue, as defined in
\l{ECMA-262} section 9.8, "ToString".
Note that if this QScriptValue is an object, calling this function
has side effects on the script engine, since the engine will call
the object's toString() function (and possibly valueOf()) in an
attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
\sa isString()
*/
QString QScriptValue::toString() const
{
return d_ptr->toString();
}
/*!
Returns the number value of this QScriptValue, as defined in
\l{ECMA-262} section 9.3, "ToNumber".
Note that if this QScriptValue is an object, calling this function
has side effects on the script engine, since the engine will call
the object's valueOf() function (and possibly toString()) in an
attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
\sa isNumber(), toInteger(), toInt32(), toUInt32(), toUInt16()
*/
qsreal QScriptValue::toNumber() const
{
return d_ptr->toNumber();
}
/*!
Returns the boolean value of this QScriptValue, using the conversion
rules described in \l{ECMA-262} section 9.2, "ToBoolean".
Note that if this QScriptValue is an object, calling this function
has side effects on the script engine, since the engine will call
the object's valueOf() function (and possibly toString()) in an
attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
\sa isBool()
*/
bool QScriptValue::toBool() const
{
return d_ptr->toBool();
}
/*!
\obsolete
Use toBool() instead.
*/
bool QScriptValue::toBoolean() const
{
return d_ptr->toBool();
}
/*!
Returns the integer value of this QScriptValue, using the conversion
rules described in \l{ECMA-262} section 9.4, "ToInteger".
Note that if this QScriptValue is an object, calling this function
has side effects on the script engine, since the engine will call
the object's valueOf() function (and possibly toString()) in an
attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
\sa toNumber()
*/
qsreal QScriptValue::toInteger() const
{
return d_ptr->toInteger();
}
/*!
Returns the signed 32-bit integer value of this QScriptValue, using
the conversion rules described in \l{ECMA-262} section 9.5, "ToInt32".
Note that if this QScriptValue is an object, calling this function
has side effects on the script engine, since the engine will call
the object's valueOf() function (and possibly toString()) in an
attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
\sa toNumber(), toUInt32()
*/
qint32 QScriptValue::toInt32() const
{
return d_ptr->toInt32();
}
/*!
Returns the unsigned 32-bit integer value of this QScriptValue, using
the conversion rules described in \l{ECMA-262} section 9.6, "ToUint32".
Note that if this QScriptValue is an object, calling this function
has side effects on the script engine, since the engine will call
the object's valueOf() function (and possibly toString()) in an
attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
\sa toNumber(), toInt32()
*/
quint32 QScriptValue::toUInt32() const
{
return d_ptr->toUInt32();
}
/*!
Returns the unsigned 16-bit integer value of this QScriptValue, using
the conversion rules described in \l{ECMA-262} section 9.7, "ToUint16".
Note that if this QScriptValue is an object, calling this function
has side effects on the script engine, since the engine will call
the object's valueOf() function (and possibly toString()) in an
attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
\sa toNumber()
*/
quint16 QScriptValue::toUInt16() const
{
return d_ptr->toUInt16();
}
/*!
\obsolete
This function is obsolete; use QScriptEngine::toObject() instead.
*/
QScriptValue QScriptValue::toObject() const
{
return QScriptValuePrivate::get(d_ptr->toObject());
}
/*!
Calls this QScriptValue as a function, using \a thisObject as
the `this' object in the function call, and passing \a args
as arguments to the function. Returns the value returned from
the function.
If this QScriptValue is not a function, call() does nothing
and returns an invalid QScriptValue.
Note that if \a thisObject is not an object, the global object
(see \l{QScriptEngine::globalObject()}) will be used as the
`this' object.
Calling call() can cause an exception to occur in the script engine;
in that case, call() returns the value that was thrown (typically an
\c{Error} object). You can call
QScriptEngine::hasUncaughtException() to determine if an exception
occurred.
\snippet doc/src/snippets/code/src_script_qscriptvalue.cpp 2
\sa construct()
*/
QScriptValue QScriptValue::call(const QScriptValue& thisObject, const QScriptValueList& args)
{
return d_ptr->call(thisObject.d_ptr.data(), args);
}
/*!
Returns the QScriptEngine that created this QScriptValue,
or 0 if this QScriptValue is invalid or the value is not
associated with a particular engine.
*/
QScriptEngine* QScriptValue::engine() const
{
QScriptEnginePrivate* engine = d_ptr->engine();
if (engine)
return QScriptEnginePrivate::get(engine);
return 0;
}
/*!
If this QScriptValue is an object, returns the internal prototype
(\c{__proto__} property) of this object; otherwise returns an
invalid QScriptValue.
\sa setPrototype(), isObject()
*/
QScriptValue QScriptValue::prototype() const
{
return QScriptValuePrivate::get(d_ptr->prototype());
}
/*!
If this QScriptValue is an object, sets the internal prototype
(\c{__proto__} property) of this object to be \a prototype;
otherwise does nothing.
The internal prototype should not be confused with the public
property with name "prototype"; the public prototype is usually
only set on functions that act as constructors.
\sa prototype(), isObject()
*/
void QScriptValue::setPrototype(const QScriptValue& prototype)
{
d_ptr->setPrototype(QScriptValuePrivate::get(prototype));
}
/*!
Assigns the \a other value to this QScriptValue.
Note that if \a other is an object (isObject() returns true),
only a reference to the underlying object will be assigned;
the object itself will not be copied.
*/
QScriptValue& QScriptValue::operator=(const QScriptValue& other)
{
d_ptr = other.d_ptr;
return *this;
}
/*!
Returns true if this QScriptValue is equal to \a other, otherwise
returns false. The comparison follows the behavior described in
\l{ECMA-262} section 11.9.3, "The Abstract Equality Comparison
Algorithm".
This function can return true even if the type of this QScriptValue
is different from the type of the \a other value; i.e. the
comparison is not strict. For example, comparing the number 9 to
the string "9" returns true; comparing an undefined value to a null
value returns true; comparing a \c{Number} object whose primitive
value is 6 to a \c{String} object whose primitive value is "6"
returns true; and comparing the number 1 to the boolean value
\c{true} returns true. If you want to perform a comparison
without such implicit value conversion, use strictlyEquals().
Note that if this QScriptValue or the \a other value are objects,
calling this function has side effects on the script engine, since
the engine will call the object's valueOf() function (and possibly
toString()) in an attempt to convert the object to a primitive value
(possibly resulting in an uncaught script exception).
\sa strictlyEquals(), lessThan()
*/
bool QScriptValue::equals(const QScriptValue& other) const
{
return d_ptr->equals(QScriptValuePrivate::get(other));
}
/*!
Returns true if this QScriptValue is equal to \a other using strict
comparison (no conversion), otherwise returns false. The comparison
follows the behavior described in \l{ECMA-262} section 11.9.6, "The
Strict Equality Comparison Algorithm".
If the type of this QScriptValue is different from the type of the
\a other value, this function returns false. If the types are equal,
the result depends on the type, as shown in the following table:
\table
\header \o Type \o Result
\row \o Undefined \o true
\row \o Null \o true
\row \o Boolean \o true if both values are true, false otherwise
\row \o Number \o false if either value is NaN (Not-a-Number); true if values are equal, false otherwise
\row \o String \o true if both values are exactly the same sequence of characters, false otherwise
\row \o Object \o true if both values refer to the same object, false otherwise
\endtable
\sa equals()
*/
bool QScriptValue::strictlyEquals(const QScriptValue& other) const
{
return d_ptr->strictlyEquals(QScriptValuePrivate::get(other));
}
/*!
Returns true if this QScriptValue is an instance of
\a other; otherwise returns false.
This QScriptValue is considered to be an instance of \a other if
\a other is a function and the value of the \c{prototype}
property of \a other is in the prototype chain of this
QScriptValue.
*/
bool QScriptValue::instanceOf(const QScriptValue& other) const
{
return d_ptr->instanceOf(QScriptValuePrivate::get(other));
}
|