1 /*
2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #ifndef qscriptstring_p_h
21 #define qscriptstring_p_h
22
23 #include "qscriptconverter_p.h"
24 #include "qscriptstring.h"
25 #include <JavaScriptCore/JavaScript.h>
26 #include <QtCore/qnumeric.h>
27 #include <QtCore/qshareddata.h>
28
29 class QScriptStringPrivate : public QSharedData {
30 public:
31 inline QScriptStringPrivate();
32 inline QScriptStringPrivate(const QString& qtstring);
33 inline ~QScriptStringPrivate();
34
35 static inline QScriptString get(QScriptStringPrivate* d);
36 static inline QScriptStringPtr get(const QScriptString& p);
37
38 inline bool isValid() const;
39
40 inline bool operator==(const QScriptStringPrivate& other) const;
41 inline bool operator!=(const QScriptStringPrivate& other) const;
42
43 inline quint32 toArrayIndex(bool* ok = 0) const;
44
45 inline QString toString() const;
46
47 inline quint64 id() const;
48
49 inline operator JSStringRef() const;
50
51 private:
52 JSStringRef m_string;
53 };
54
55
QScriptStringPrivate()56 QScriptStringPrivate::QScriptStringPrivate()
57 : m_string(0)
58 {}
59
QScriptStringPrivate(const QString & qtstring)60 QScriptStringPrivate::QScriptStringPrivate(const QString& qtstring)
61 : m_string(QScriptConverter::toString(qtstring))
62 {}
63
~QScriptStringPrivate()64 QScriptStringPrivate::~QScriptStringPrivate()
65 {
66 if (isValid())
67 JSStringRelease(m_string);
68 }
69
get(QScriptStringPrivate * d)70 QScriptString QScriptStringPrivate::get(QScriptStringPrivate* d)
71 {
72 Q_ASSERT(d);
73 return QScriptString(d);
74 }
75
get(const QScriptString & p)76 QScriptStringPtr QScriptStringPrivate::get(const QScriptString& p)
77 {
78 return p.d_ptr;
79 }
80
isValid()81 bool QScriptStringPrivate::isValid() const
82 {
83 return m_string;
84 }
85
86 bool QScriptStringPrivate::operator==(const QScriptStringPrivate& other) const
87 {
88 return isValid() && other.isValid() && JSStringIsEqual(m_string, other.m_string);
89 }
90
91 bool QScriptStringPrivate::operator!=(const QScriptStringPrivate& other) const
92 {
93 return isValid() && other.isValid() && !JSStringIsEqual(m_string, other.m_string);
94 }
95
toArrayIndex(bool * ok)96 quint32 QScriptStringPrivate::toArrayIndex(bool* ok) const
97 {
98 quint32 idx = QScriptConverter::toArrayIndex(m_string);
99 if (ok)
100 *ok = (idx != 0xffffffff);
101 return idx;
102 }
103
toString()104 QString QScriptStringPrivate::toString() const
105 {
106 return QScriptConverter::toString(m_string);
107 }
108
id()109 quint64 QScriptStringPrivate::id() const
110 {
111 return reinterpret_cast<quint32>(m_string);
112 }
113
114 /*!
115 \internal
116 This method should be used for invoking JSC functions.
117 \note This method keeps ownership of an internal JSStringRef.
118 */
JSStringRef()119 QScriptStringPrivate::operator JSStringRef() const
120 {
121 return m_string;
122 }
123
124 #endif // qscriptstring_p_h
125