1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtScript module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file.  Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt.io/contact-us/.
19 **
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23 
24 #ifndef QSCRIPTVALUE_P_H
25 #define QSCRIPTVALUE_P_H
26 
27 //
28 //  W A R N I N G
29 //  -------------
30 //
31 // This file is not part of the Qt API.  It exists purely as an
32 // implementation detail.  This header file may change from version to
33 // version without notice, or even be removed.
34 //
35 // We mean it.
36 //
37 
38 #include <QtCore/qobjectdefs.h>
39 
40 #include "wtf/Platform.h"
41 #include "JSValue.h"
42 
43 QT_BEGIN_NAMESPACE
44 
45 class QString;
46 class QScriptEnginePrivate;
47 
48 class QScriptValue;
49 class QScriptValuePrivate
50 {
51     Q_DISABLE_COPY(QScriptValuePrivate)
52 public:
53     inline void* operator new(size_t, QScriptEnginePrivate*);
54     inline void operator delete(void*);
55 
56     enum Type {
57         JavaScriptCore,
58         Number,
59         String
60     };
61 
62     inline QScriptValuePrivate(QScriptEnginePrivate*);
63     inline ~QScriptValuePrivate();
64 
65     inline void initFrom(JSC::JSValue value);
66     inline void initFrom(qsreal value);
67     inline void initFrom(const QString &value);
68 
69     inline bool isJSC() const;
70     inline bool isObject() const;
71 
get(const QScriptValue & q)72     static inline QScriptValuePrivate *get(const QScriptValue &q)
73     {
74         return q.d_ptr.data();
75     }
76 
toPublic(QScriptValuePrivate * d)77     static inline QScriptValue toPublic(QScriptValuePrivate *d)
78     {
79         return QScriptValue(d);
80     }
81 
getEngine(const QScriptValue & q)82     static inline QScriptEnginePrivate *getEngine(const QScriptValue &q)
83     {
84         if (!q.d_ptr)
85             return 0;
86         return q.d_ptr->engine;
87     }
88 
89     inline JSC::JSValue property(const JSC::Identifier &id,
90                                  const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const;
91     inline JSC::JSValue property(quint32 index, const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const;
92     inline JSC::JSValue property(const JSC::UString &, const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const;
93     inline void setProperty(const JSC::UString &name, const JSC::JSValue &value,
94                             const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags);
95     inline void setProperty(const JSC::Identifier &id, const JSC::JSValue &value,
96                             const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags);
97     inline void setProperty(quint32 index, const JSC::JSValue &value,
98                             const QScriptValue::PropertyFlags &flags = QScriptValue::KeepExistingFlags);
99     inline QScriptValue::PropertyFlags propertyFlags(
100         const JSC::Identifier &id, const QScriptValue::ResolveFlags &mode = QScriptValue::ResolvePrototype) const;
101 
102     void detachFromEngine();
103 
objectId()104     qint64 objectId()
105     {
106         if ( (type == JavaScriptCore) && (engine) && jscValue.isCell() )
107             return (qint64)jscValue.asCell();
108         else
109             return -1;
110     }
111 
112     QScriptEnginePrivate *engine;
113     Type type;
114     JSC::JSValue jscValue;
115     qsreal numberValue;
116     QString stringValue;
117 
118     // linked list of engine's script values
119     QScriptValuePrivate *prev;
120     QScriptValuePrivate *next;
121 
122     QBasicAtomicInt ref;
123 };
124 
QScriptValuePrivate(QScriptEnginePrivate * e)125 inline QScriptValuePrivate::QScriptValuePrivate(QScriptEnginePrivate *e)
126     : engine(e), prev(0), next(0)
127 {
128     ref = 0;
129 }
130 
isJSC()131 inline bool QScriptValuePrivate::isJSC() const
132 {
133     return (type == JavaScriptCore);
134 }
135 
isObject()136 inline bool QScriptValuePrivate::isObject() const
137 {
138     return isJSC() && jscValue && jscValue.isObject();
139 }
140 
141 // Rest of inline functions implemented in qscriptengine_p.h
142 
143 QT_END_NAMESPACE
144 
145 #endif
146