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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
|
/*
Copyright (C) 2008 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.
*/
#ifndef qscriptvalue_p_h
#define qscriptvalue_p_h
#include "qscriptconverter_p.h"
#include "qscriptengine_p.h"
#include "qscriptvalue.h"
#include <JavaScriptCore/JavaScript.h>
#include <JavaScriptCore/JSRetainPtr.h>
#include <QtCore/qmath.h>
#include <QtCore/qnumeric.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qvarlengtharray.h>
class QScriptEngine;
class QScriptValue;
/*
\internal
\class QScriptValuePrivate
Implementation of QScriptValue.
The implementation is based on a state machine. The states names are included in
QScriptValuePrivate::State. Each method should check for the current state and then perform a
correct action.
State:
Invalid -> QSVP is invalid, no assumptions should be made about class members (apart from m_value).
CString -> QSVP is created from QString or const char* and no JSC engine has been associated yet.
Current value is kept in m_string,
CNumber -> QSVP is created from int, uint, double... and no JSC engine has been bind yet. Current
value is kept in m_number
CBool -> QSVP is created from bool and no JSC engine has been associated yet. Current value is kept
in m_number
CNull -> QSVP is null, but a JSC engine hasn't been associated yet.
CUndefined -> QSVP is undefined, but a JSC engine hasn't been associated yet.
JSValue -> QSVP is associated with engine, but there is no information about real type, the state
have really short live cycle. Normally it is created as a function call result.
JSPrimitive -> QSVP is associated with engine, and it is sure that it isn't a JavaScript object.
JSObject -> QSVP is associated with engine, and it is sure that it is a JavaScript object.
Each state keep all necessary information to invoke all methods, if not it should be changed to
a proper state. Changed state shouldn't be reverted.
The QScriptValuePrivate use the JSC C API directly. The QSVP type is equal to combination of
the JSValueRef and the JSObjectRef, and it could be automatically casted to these types by cast
operators.
*/
class QScriptValuePrivate : public QSharedData {
public:
inline static QScriptValuePrivate* get(const QScriptValue& q);
inline static QScriptValue get(const QScriptValuePrivate* d);
inline static QScriptValue get(QScriptValuePrivate* d);
inline ~QScriptValuePrivate();
inline QScriptValuePrivate();
inline QScriptValuePrivate(const QString& string);
inline QScriptValuePrivate(bool value);
inline QScriptValuePrivate(int number);
inline QScriptValuePrivate(uint number);
inline QScriptValuePrivate(qsreal number);
inline QScriptValuePrivate(QScriptValue::SpecialValue value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, bool value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, int value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, uint value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, qsreal value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, const QString& value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, QScriptValue::SpecialValue value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, JSValueRef value);
inline QScriptValuePrivate(const QScriptEnginePrivate* engine, JSValueRef value, JSObjectRef object);
inline bool isValid() const;
inline bool isBool();
inline bool isNumber();
inline bool isNull();
inline bool isString();
inline bool isUndefined();
inline bool isError();
inline bool isObject();
inline bool isFunction();
inline QString toString() const;
inline qsreal toNumber() const;
inline bool toBool() const;
inline qsreal toInteger() const;
inline qint32 toInt32() const;
inline quint32 toUInt32() const;
inline quint16 toUInt16() const;
inline QScriptValuePrivate* toObject(QScriptEnginePrivate* engine);
inline QScriptValuePrivate* toObject();
inline bool equals(QScriptValuePrivate* other);
inline bool strictlyEquals(QScriptValuePrivate* other);
inline bool instanceOf(QScriptValuePrivate* other);
inline bool assignEngine(QScriptEnginePrivate* engine);
inline QScriptValuePrivate* call(const QScriptValuePrivate* , const QScriptValueList& args);
inline operator JSValueRef() const;
inline operator JSObjectRef() const;
inline QScriptEnginePrivate* engine() const;
private:
// Please, update class documentation when you change the enum.
enum State {
Invalid = 0,
CString = 0x1000,
CNumber,
CBool,
CNull,
CUndefined,
JSValue = 0x2000, // JS values are equal or higher then this value.
JSPrimitive,
JSObject
} m_state;
QScriptEnginePtr m_engine;
QString m_string;
qsreal m_number;
JSValueRef m_value;
JSObjectRef m_object;
inline void setValue(JSValueRef);
inline bool inherits(const char*);
inline State refinedJSValue();
inline bool isJSBased() const;
inline bool isNumberBased() const;
inline bool isStringBased() const;
};
QScriptValuePrivate* QScriptValuePrivate::get(const QScriptValue& q) { return q.d_ptr.data(); }
QScriptValue QScriptValuePrivate::get(const QScriptValuePrivate* d)
{
return QScriptValue(const_cast<QScriptValuePrivate*>(d));
}
QScriptValue QScriptValuePrivate::get(QScriptValuePrivate* d)
{
return QScriptValue(d);
}
QScriptValuePrivate::~QScriptValuePrivate()
{
if (m_value)
JSValueUnprotect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate()
: m_state(Invalid)
, m_value(0)
{
}
QScriptValuePrivate::QScriptValuePrivate(const QString& string)
: m_state(CString)
, m_string(string)
, m_value(0)
{
}
QScriptValuePrivate::QScriptValuePrivate(bool value)
: m_state(CBool)
, m_number(value)
, m_value(0)
{
}
QScriptValuePrivate::QScriptValuePrivate(int number)
: m_state(CNumber)
, m_number(number)
, m_value(0)
{
}
QScriptValuePrivate::QScriptValuePrivate(uint number)
: m_state(CNumber)
, m_number(number)
, m_value(0)
{
}
QScriptValuePrivate::QScriptValuePrivate(qsreal number)
: m_state(CNumber)
, m_number(number)
, m_value(0)
{
}
QScriptValuePrivate::QScriptValuePrivate(QScriptValue::SpecialValue value)
: m_state(value == QScriptValue::NullValue ? CNull : CUndefined)
, m_value(0)
{
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, bool value)
: m_state(JSPrimitive)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(engine->makeJSValue(value))
{
Q_ASSERT(engine);
JSValueProtect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, int value)
: m_state(JSPrimitive)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(m_engine->makeJSValue(value))
{
Q_ASSERT(engine);
JSValueProtect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, uint value)
: m_state(JSPrimitive)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(m_engine->makeJSValue(value))
{
Q_ASSERT(engine);
JSValueProtect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, qsreal value)
: m_state(JSPrimitive)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(m_engine->makeJSValue(value))
{
Q_ASSERT(engine);
JSValueProtect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, const QString& value)
: m_state(JSPrimitive)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(m_engine->makeJSValue(value))
{
Q_ASSERT(engine);
JSValueProtect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, QScriptValue::SpecialValue value)
: m_state(JSPrimitive)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(m_engine->makeJSValue(value))
{
Q_ASSERT(engine);
JSValueProtect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, JSValueRef value)
: m_state(JSValue)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(value)
{
Q_ASSERT(engine);
Q_ASSERT(value);
JSValueProtect(*m_engine, m_value);
}
QScriptValuePrivate::QScriptValuePrivate(const QScriptEnginePrivate* engine, JSValueRef value, JSObjectRef object)
: m_state(JSObject)
, m_engine(const_cast<QScriptEnginePrivate*>(engine))
, m_value(value)
, m_object(object)
{
Q_ASSERT(engine);
Q_ASSERT(value);
Q_ASSERT(object);
JSValueProtect(*m_engine, m_value);
}
bool QScriptValuePrivate::isValid() const { return m_state != Invalid; }
bool QScriptValuePrivate::isBool()
{
switch (m_state) {
case CBool:
return true;
case JSValue:
if (refinedJSValue() != JSPrimitive)
return false;
// Fall-through.
case JSPrimitive:
return JSValueIsBoolean(*m_engine, *this);
default:
return false;
}
}
bool QScriptValuePrivate::isNumber()
{
switch (m_state) {
case CNumber:
return true;
case JSValue:
if (refinedJSValue() != JSPrimitive)
return false;
// Fall-through.
case JSPrimitive:
return JSValueIsNumber(*m_engine, *this);
default:
return false;
}
}
bool QScriptValuePrivate::isNull()
{
switch (m_state) {
case CNull:
return true;
case JSValue:
if (refinedJSValue() != JSPrimitive)
return false;
// Fall-through.
case JSPrimitive:
return JSValueIsNull(*m_engine, *this);
default:
return false;
}
}
bool QScriptValuePrivate::isString()
{
switch (m_state) {
case CString:
return true;
case JSValue:
if (refinedJSValue() != JSPrimitive)
return false;
// Fall-through.
case JSPrimitive:
return JSValueIsString(*m_engine, *this);
default:
return false;
}
}
bool QScriptValuePrivate::isUndefined()
{
switch (m_state) {
case CUndefined:
return true;
case JSValue:
if (refinedJSValue() != JSPrimitive)
return false;
// Fall-through.
case JSPrimitive:
return JSValueIsUndefined(*m_engine, *this);
default:
return false;
}
}
bool QScriptValuePrivate::isError()
{
switch (m_state) {
case JSValue:
if (refinedJSValue() != JSObject)
return false;
// Fall-through.
case JSObject:
return inherits("Error");
default:
return false;
}
}
bool QScriptValuePrivate::isObject()
{
switch (m_state) {
case JSValue:
return refinedJSValue() == JSObject;
case JSObject:
return true;
default:
return false;
}
}
bool QScriptValuePrivate::isFunction()
{
switch (m_state) {
case JSValue:
if (refinedJSValue() != JSObject)
return false;
// Fall-through.
case JSObject:
return JSObjectIsFunction(*m_engine, *this);
default:
return false;
}
}
QString QScriptValuePrivate::toString() const
{
switch (m_state) {
case Invalid:
return QString();
case CBool:
return m_number ? QString::fromLatin1("true") : QString::fromLatin1("false");
case CString:
return m_string;
case CNumber:
return QScriptConverter::toString(m_number);
case CNull:
return QString::fromLatin1("null");
case CUndefined:
return QString::fromLatin1("undefined");
case JSValue:
case JSPrimitive:
case JSObject:
JSRetainPtr<JSStringRef> ptr(Adopt, JSValueToStringCopy(*m_engine, *this, /* exception */ 0));
return QScriptConverter::toString(ptr.get());
}
Q_ASSERT_X(false, "toString()", "Not all states are included in the previous switch statement.");
return QString(); // Avoid compiler warning.
}
qsreal QScriptValuePrivate::toNumber() const
{
switch (m_state) {
case JSValue:
case JSPrimitive:
case JSObject:
return JSValueToNumber(*m_engine, *this, /* exception */ 0);
case CNumber:
return m_number;
case CBool:
return m_number ? 1 : 0;
case CNull:
case Invalid:
return 0;
case CUndefined:
return qQNaN();
case CString:
bool ok;
qsreal result = m_string.toDouble(&ok);
if (ok)
return result;
result = m_string.toInt(&ok, 0); // Try other bases.
if (ok)
return result;
if (m_string == "Infinity" || m_string == "-Infinity")
return qInf();
return m_string.length() ? qQNaN() : 0;
}
Q_ASSERT_X(false, "toNumber()", "Not all states are included in the previous switch statement.");
return 0; // Avoid compiler warning.
}
bool QScriptValuePrivate::toBool() const
{
switch (m_state) {
case JSValue:
case JSPrimitive:
return JSValueToBoolean(*m_engine, *this);
case JSObject:
return true;
case CNumber:
return !(qIsNaN(m_number) || !m_number);
case CBool:
return m_number;
case Invalid:
case CNull:
case CUndefined:
return false;
case CString:
return m_string.length();
}
Q_ASSERT_X(false, "toBool()", "Not all states are included in the previous switch statement.");
return false; // Avoid compiler warning.
}
qsreal QScriptValuePrivate::toInteger() const
{
qsreal result = toNumber();
if (qIsNaN(result))
return 0;
if (qIsInf(result))
return result;
return (result > 0) ? qFloor(result) : -1 * qFloor(-result);
}
qint32 QScriptValuePrivate::toInt32() const
{
qsreal result = toInteger();
// Orginaly it should look like that (result == 0 || qIsInf(result) || qIsNaN(result)), but
// some of these operation are invoked in toInteger subcall.
if (qIsInf(result))
return 0;
return result;
}
quint32 QScriptValuePrivate::toUInt32() const
{
qsreal result = toInteger();
// Orginaly it should look like that (result == 0 || qIsInf(result) || qIsNaN(result)), but
// some of these operation are invoked in toInteger subcall.
if (qIsInf(result))
return 0;
return result;
}
quint16 QScriptValuePrivate::toUInt16() const
{
return toInt32();
}
/*!
Creates a copy of this value and converts it to an object. If this value is an object
then pointer to this value will be returned.
\attention it should not happen but if this value is bounded to a different engine that the given, the first
one will be used.
\internal
*/
QScriptValuePrivate* QScriptValuePrivate::toObject(QScriptEnginePrivate* engine)
{
switch (m_state) {
case Invalid:
case CNull:
case CUndefined:
return new QScriptValuePrivate;
case CString:
{
// Exception can't occur here.
JSObjectRef object = JSValueToObject(*engine, engine->makeJSValue(m_string), /* exception */ 0);
Q_ASSERT(object);
return new QScriptValuePrivate(engine, object, object);
}
case CNumber:
{
// Exception can't occur here.
JSObjectRef object = JSValueToObject(*engine, engine->makeJSValue(m_number), /* exception */ 0);
Q_ASSERT(object);
return new QScriptValuePrivate(engine, object, object);
}
case CBool:
{
// Exception can't occure here.
JSObjectRef object = JSValueToObject(*engine, engine->makeJSValue(static_cast<bool>(m_number)), /* exception */ 0);
Q_ASSERT(object);
return new QScriptValuePrivate(engine, object, object);
}
case JSValue:
if (refinedJSValue() != JSPrimitive)
break;
// Fall-through.
case JSPrimitive:
{
if (engine != this->engine())
qWarning("QScriptEngine::toObject: cannot convert value created in a different engine");
JSObjectRef object = JSValueToObject(*m_engine, *this, /* exception */ 0);
if (object)
return new QScriptValuePrivate(m_engine.constData(), object);
}
return new QScriptValuePrivate;
case JSObject:
break;
}
if (engine != this->engine())
qWarning("QScriptEngine::toObject: cannot convert value created in a different engine");
Q_ASSERT(m_state == JSObject);
return this;
}
/*!
This method is created only for QScriptValue::toObject() purpose which is obsolete.
\internal
*/
QScriptValuePrivate* QScriptValuePrivate::toObject()
{
if (isJSBased())
return toObject(m_engine.data());
// Without an engine there is not much we can do.
return new QScriptValuePrivate;
}
bool QScriptValuePrivate::equals(QScriptValuePrivate* other)
{
if (!isValid())
return !other->isValid();
if (!other->isValid())
return false;
if ((m_state == other->m_state) && !isJSBased()) {
if (isNumberBased())
return m_number == other->m_number;
Q_ASSERT(isStringBased());
return m_string == other->m_string;
}
if (!isJSBased() && !other->isJSBased())
return false;
if (isJSBased() && !other->isJSBased()) {
if (!other->assignEngine(engine())) {
qWarning("equals(): Cannot compare to a value created in a different engine");
return false;
}
} else if (!isJSBased() && other->isJSBased()) {
if (!assignEngine(other->engine())) {
qWarning("equals(): Cannot compare to a value created in a different engine");
return false;
}
}
return JSValueIsEqual(*m_engine, *this, *other, /* exception */ 0);
}
bool QScriptValuePrivate::strictlyEquals(QScriptValuePrivate* other)
{
if (isJSBased()) {
// We can't compare these two values without binding to the same engine.
if (!other->isJSBased()) {
if (other->assignEngine(engine()))
return JSValueIsStrictEqual(*m_engine, *this, *other);
return false;
}
if (other->engine() != engine()) {
qWarning("strictlyEquals(): Cannot compare to a value created in a different engine");
return false;
}
return JSValueIsStrictEqual(*m_engine, *this, *other);
}
if (isStringBased()) {
if (other->isStringBased())
return m_string == other->m_string;
if (other->isJSBased()) {
assignEngine(other->engine());
return JSValueIsStrictEqual(*m_engine, *this, *other);
}
}
if (isNumberBased()) {
if (other->isNumberBased())
return m_number == other->m_number;
if (other->isJSBased()) {
assignEngine(other->engine());
return JSValueIsStrictEqual(*m_engine, *this, *other);
}
}
if (!isValid() && !other->isValid())
return true;
return false;
}
inline bool QScriptValuePrivate::instanceOf(QScriptValuePrivate* other)
{
if (!isJSBased() || !other->isObject())
return false;
return JSValueIsInstanceOfConstructor(*m_engine, *this, *other, /* exception */ 0);
}
/*!
Tries to assign \a engine to this value. Returns true on success; otherwise returns false.
*/
bool QScriptValuePrivate::assignEngine(QScriptEnginePrivate* engine)
{
JSValueRef value;
switch (m_state) {
case CBool:
value = engine->makeJSValue(static_cast<bool>(m_number));
break;
case CString:
value = engine->makeJSValue(m_string);
break;
case CNumber:
value = engine->makeJSValue(m_number);
break;
case CNull:
value = engine->makeJSValue(QScriptValue::NullValue);
break;
case CUndefined:
value = engine->makeJSValue(QScriptValue::UndefinedValue);
break;
default:
if (!isJSBased())
Q_ASSERT_X(!isJSBased(), "assignEngine()", "Not all states are included in the previous switch statement.");
else
qWarning("JSValue can't be rassigned to an another engine.");
return false;
}
m_engine = engine;
m_state = JSPrimitive;
setValue(value);
return true;
}
QScriptValuePrivate* QScriptValuePrivate::call(const QScriptValuePrivate*, const QScriptValueList& args)
{
switch (m_state) {
case JSValue:
if (refinedJSValue() != JSObject)
return new QScriptValuePrivate;
// Fall-through.
case JSObject:
{
// Convert all arguments and bind to the engine.
int argc = args.size();
QVarLengthArray<JSValueRef, 8> argv(argc);
QScriptValueList::const_iterator i = args.constBegin();
for (int j = 0; i != args.constEnd(); j++, i++) {
QScriptValuePrivate* value = QScriptValuePrivate::get(*i);
if (!value->assignEngine(engine())) {
qWarning("QScriptValue::call() failed: cannot call function with values created in a different engine");
return new QScriptValuePrivate;
}
argv[j] = *value;
}
// Make the call
JSValueRef exception = 0;
JSValueRef result = JSObjectCallAsFunction(*m_engine, *this, /* thisObject */ 0, argc, argv.constData(), &exception);
if (!result && exception)
return new QScriptValuePrivate(engine(), exception);
if (result && !exception)
return new QScriptValuePrivate(engine(), result);
}
// this QSV is not a function <-- !result && !exception. Fall-through.
default:
return new QScriptValuePrivate;
}
}
QScriptEnginePrivate* QScriptValuePrivate::engine() const
{
// As long as m_engine is an autoinitializated pointer we can safely return it without
// checking current state.
return m_engine.data();
}
QScriptValuePrivate::operator JSValueRef() const
{
Q_ASSERT(isJSBased());
return m_value;
}
QScriptValuePrivate::operator JSObjectRef() const
{
Q_ASSERT(m_state == JSObject);
return m_object;
}
void QScriptValuePrivate::setValue(JSValueRef value)
{
if (m_value)
JSValueUnprotect(*m_engine, m_value);
if (value)
JSValueProtect(*m_engine, value);
m_value = value;
}
/*!
\internal
Returns true if QSV is created from constructor with the given \a name, it has to be a
built-in type.
*/
bool QScriptValuePrivate::inherits(const char* name)
{
Q_ASSERT(isJSBased());
JSObjectRef globalObject = JSContextGetGlobalObject(*m_engine);
JSStringRef errorAttrName = QScriptConverter::toString(name);
JSValueRef error = JSObjectGetProperty(*m_engine, globalObject, errorAttrName, /* exception */ 0);
JSStringRelease(errorAttrName);
return JSValueIsInstanceOfConstructor(*m_engine, *this, JSValueToObject(*m_engine, error, /* exception */ 0), /* exception */ 0);
}
/*!
\internal
Refines the state of this QScriptValuePrivate. Returns the new state.
*/
QScriptValuePrivate::State QScriptValuePrivate::refinedJSValue()
{
Q_ASSERT(m_state == JSValue);
if (!JSValueIsObject(*m_engine, *this)) {
m_state = JSPrimitive;
} else {
m_state = JSObject;
// We are sure that value is an JSObject, so we can const_cast safely without
// calling JSC C API (JSValueToObject(*m_engine, *this, /* exceptions */ 0)).
m_object = const_cast<JSObjectRef>(m_value);
}
return m_state;
}
/*!
\internal
Returns true if QSV have an engine associated.
*/
bool QScriptValuePrivate::isJSBased() const { return m_state >= JSValue; }
/*!
\internal
Returns true if current value of QSV is placed in m_number.
*/
bool QScriptValuePrivate::isNumberBased() const { return m_state == CNumber || m_state == CBool; }
/*!
\internal
Returns true if current value of QSV is placed in m_string.
*/
bool QScriptValuePrivate::isStringBased() const { return m_state == CString; }
#endif // qscriptvalue_p_h
|