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
29class QScriptStringPrivate : public QSharedData {
30public:
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
51private:
52    JSStringRef m_string;
53};
54
55
56QScriptStringPrivate::QScriptStringPrivate()
57    : m_string(0)
58{}
59
60QScriptStringPrivate::QScriptStringPrivate(const QString& qtstring)
61    : m_string(QScriptConverter::toString(qtstring))
62{}
63
64QScriptStringPrivate::~QScriptStringPrivate()
65{
66    if (isValid())
67        JSStringRelease(m_string);
68}
69
70QScriptString QScriptStringPrivate::get(QScriptStringPrivate* d)
71{
72    Q_ASSERT(d);
73    return QScriptString(d);
74}
75
76QScriptStringPtr QScriptStringPrivate::get(const QScriptString& p)
77{
78    return p.d_ptr;
79}
80
81bool QScriptStringPrivate::isValid() const
82{
83    return m_string;
84}
85
86bool QScriptStringPrivate::operator==(const QScriptStringPrivate& other) const
87{
88    return isValid() && other.isValid() && JSStringIsEqual(m_string, other.m_string);
89}
90
91bool QScriptStringPrivate::operator!=(const QScriptStringPrivate& other) const
92{
93    return isValid() && other.isValid() && !JSStringIsEqual(m_string, other.m_string);
94}
95
96quint32 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
104QString QScriptStringPrivate::toString() const
105{
106    return QScriptConverter::toString(m_string);
107}
108
109quint64 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*/
119QScriptStringPrivate::operator JSStringRef() const
120{
121    return m_string;
122}
123
124#endif // qscriptstring_p_h
125