| 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$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "config.h" // compile on Windows |
| 41 | #include "qscriptstring.h" |
| 42 | #include "qscriptstring_p.h" |
| 43 | #include "qscriptengine.h" |
| 44 | #include "qscriptengine_p.h" |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | /*! |
| 49 | \since 4.4 |
| 50 | \class QScriptString |
| 51 | \inmodule QtScript |
| 52 | \brief The QScriptString class acts as a handle to "interned" strings in a QScriptEngine. |
| 53 | |
| 54 | \ingroup script |
| 55 | |
| 56 | |
| 57 | QScriptString can be used to achieve faster (repeated) |
| 58 | property getting/setting, and comparison of property names, of |
| 59 | script objects. |
| 60 | |
| 61 | To get a QScriptString representation of a string, pass the string |
| 62 | to QScriptEngine::toStringHandle(). The typical usage pattern is to |
| 63 | register one or more pre-defined strings when setting up your script |
| 64 | environment, then subsequently use the relevant QScriptString as |
| 65 | argument to e.g. QScriptValue::property(). |
| 66 | |
| 67 | Call the toString() function to obtain the string that a |
| 68 | QScriptString represents. |
| 69 | |
| 70 | Call the toArrayIndex() function to convert a QScriptString to an |
| 71 | array index. This is useful when using QScriptClass to implement |
| 72 | array-like objects. |
| 73 | */ |
| 74 | |
| 75 | /*! |
| 76 | Constructs an invalid QScriptString. |
| 77 | */ |
| 78 | QScriptString::QScriptString() |
| 79 | : d_ptr(0) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | /*! |
| 84 | Constructs a new QScriptString that is a copy of \a other. |
| 85 | */ |
| 86 | QScriptString::QScriptString(const QScriptString &other) |
| 87 | : d_ptr(other.d_ptr) |
| 88 | { |
| 89 | if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) { |
| 90 | Q_ASSERT(d_func()->ref.load() != 1); |
| 91 | d_ptr.detach(); |
| 92 | d_func()->ref.store(newValue: 1); |
| 93 | d_func()->type = QScriptStringPrivate::HeapAllocated; |
| 94 | d_func()->engine->registerScriptString(value: d_func()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /*! |
| 99 | Destroys this QScriptString. |
| 100 | */ |
| 101 | QScriptString::~QScriptString() |
| 102 | { |
| 103 | Q_D(QScriptString); |
| 104 | if (d) { |
| 105 | switch (d->type) { |
| 106 | case QScriptStringPrivate::StackAllocated: |
| 107 | Q_ASSERT(d->ref.load() == 1); |
| 108 | d->ref.ref(); // avoid deletion |
| 109 | break; |
| 110 | case QScriptStringPrivate::HeapAllocated: |
| 111 | if (d->engine && (d->ref.load() == 1)) { |
| 112 | // Make sure the identifier is removed from the correct engine. |
| 113 | QScript::APIShim shim(d->engine); |
| 114 | d->identifier = JSC::Identifier(); |
| 115 | d->engine->unregisterScriptString(value: d); |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /*! |
| 123 | Assigns the \a other value to this QScriptString. |
| 124 | */ |
| 125 | QScriptString &QScriptString::operator=(const QScriptString &other) |
| 126 | { |
| 127 | if (d_func() && d_func()->engine && (d_func()->ref.load() == 1) && (d_func()->type == QScriptStringPrivate::HeapAllocated)) { |
| 128 | // current d_ptr will be deleted at the assignment below, so unregister it first |
| 129 | d_func()->engine->unregisterScriptString(value: d_func()); |
| 130 | } |
| 131 | d_ptr = other.d_ptr; |
| 132 | if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) { |
| 133 | Q_ASSERT(d_func()->ref.load() != 1); |
| 134 | d_ptr.detach(); |
| 135 | d_func()->ref.store(newValue: 1); |
| 136 | d_func()->type = QScriptStringPrivate::HeapAllocated; |
| 137 | d_func()->engine->registerScriptString(value: d_func()); |
| 138 | } |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | /*! |
| 143 | Returns true if this QScriptString is valid; otherwise |
| 144 | returns false. |
| 145 | */ |
| 146 | bool QScriptString::isValid() const |
| 147 | { |
| 148 | return QScriptStringPrivate::isValid(q: *this); |
| 149 | } |
| 150 | |
| 151 | /*! |
| 152 | Returns true if this QScriptString is equal to \a other; |
| 153 | otherwise returns false. |
| 154 | */ |
| 155 | bool QScriptString::operator==(const QScriptString &other) const |
| 156 | { |
| 157 | Q_D(const QScriptString); |
| 158 | if (!d || !other.d_func()) |
| 159 | return d == other.d_func(); |
| 160 | return d->identifier == other.d_func()->identifier; |
| 161 | } |
| 162 | |
| 163 | /*! |
| 164 | Returns true if this QScriptString is not equal to \a other; |
| 165 | otherwise returns false. |
| 166 | */ |
| 167 | bool QScriptString::operator!=(const QScriptString &other) const |
| 168 | { |
| 169 | return !operator==(other); |
| 170 | } |
| 171 | |
| 172 | /*! |
| 173 | \since 4.6 |
| 174 | |
| 175 | Attempts to convert this QScriptString to a Qt Script array index, |
| 176 | and returns the result. |
| 177 | |
| 178 | If a conversion error occurs, *\a{ok} is set to false; otherwise |
| 179 | *\a{ok} is set to true. |
| 180 | */ |
| 181 | quint32 QScriptString::toArrayIndex(bool *ok) const |
| 182 | { |
| 183 | Q_D(const QScriptString); |
| 184 | if (!d) { |
| 185 | if (ok) |
| 186 | *ok = false; |
| 187 | return -1; |
| 188 | } |
| 189 | bool tmp; |
| 190 | bool *okok = ok ? ok : &tmp; |
| 191 | quint32 result = d->identifier.toArrayIndex(ok: okok); |
| 192 | if (!*okok) |
| 193 | result = -1; |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | /*! |
| 198 | Returns the string that this QScriptString represents, or a |
| 199 | null string if this QScriptString is not valid. |
| 200 | |
| 201 | \sa isValid() |
| 202 | */ |
| 203 | QString QScriptString::toString() const |
| 204 | { |
| 205 | Q_D(const QScriptString); |
| 206 | if (!d || !d->engine) |
| 207 | return QString(); |
| 208 | return d->identifier.ustring(); |
| 209 | } |
| 210 | |
| 211 | /*! |
| 212 | Returns the string that this QScriptString represents, or a |
| 213 | null string if this QScriptString is not valid. |
| 214 | |
| 215 | \sa toString() |
| 216 | */ |
| 217 | QScriptString::operator QString() const |
| 218 | { |
| 219 | return toString(); |
| 220 | } |
| 221 | |
| 222 | uint qHash(const QScriptString &key) |
| 223 | { |
| 224 | QScriptStringPrivate *d = QScriptStringPrivate::get(q: key); |
| 225 | if (!d) |
| 226 | return 0; |
| 227 | return qHash(key: d->identifier.ustring().rep()); |
| 228 | } |
| 229 | |
| 230 | QT_END_NAMESPACE |
| 231 | |