| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qscriptvalue.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_SCRIPT
|
|---|
| 45 |
|
|---|
| 46 | #include "qscriptvalue_p.h"
|
|---|
| 47 | #include "qscriptengine_p.h"
|
|---|
| 48 | #include "qscriptvalueimpl_p.h"
|
|---|
| 49 | #include "qscriptcontext_p.h"
|
|---|
| 50 | #include "qscriptmember_p.h"
|
|---|
| 51 | #include "qscriptobject_p.h"
|
|---|
| 52 | #include "qscriptclass.h"
|
|---|
| 53 | #include "qscriptclass_p.h"
|
|---|
| 54 |
|
|---|
| 55 | #include <QtCore/QDateTime>
|
|---|
| 56 | #include <QtCore/QRegExp>
|
|---|
| 57 |
|
|---|
| 58 | QT_BEGIN_NAMESPACE
|
|---|
| 59 |
|
|---|
| 60 | /*!
|
|---|
| 61 | \since 4.3
|
|---|
| 62 | \class QScriptValue
|
|---|
| 63 |
|
|---|
| 64 | \brief The QScriptValue class acts as a container for the Qt Script data types.
|
|---|
| 65 |
|
|---|
| 66 | \ingroup script
|
|---|
| 67 | \mainclass
|
|---|
| 68 |
|
|---|
| 69 | QScriptValue supports the types defined in the \l{ECMA-262}
|
|---|
| 70 | standard: The primitive types, which are Undefined, Null, Boolean,
|
|---|
| 71 | Number, and String; and the Object type. Additionally, Qt Script
|
|---|
| 72 | has built-in support for QVariant, QObject and QMetaObject.
|
|---|
| 73 |
|
|---|
| 74 | For the object-based types (including Date and RegExp), use the
|
|---|
| 75 | newT() functions in QScriptEngine (e.g. QScriptEngine::newObject())
|
|---|
| 76 | to create a QScriptValue of the desired type. For the primitive types,
|
|---|
| 77 | use one of the QScriptValue constructor overloads.
|
|---|
| 78 |
|
|---|
| 79 | The methods named isT() (e.g. isBool(), isUndefined()) can be
|
|---|
| 80 | used to test if a value is of a certain type. The methods named
|
|---|
| 81 | toT() (e.g. toBool(), toString()) can be used to convert a
|
|---|
| 82 | QScriptValue to another type. You can also use the generic
|
|---|
| 83 | qscriptvalue_cast() function.
|
|---|
| 84 |
|
|---|
| 85 | Object values have zero or more properties which are themselves
|
|---|
| 86 | QScriptValues. Use setProperty() to set a property of an object, and
|
|---|
| 87 | call property() to retrieve the value of a property.
|
|---|
| 88 |
|
|---|
| 89 | \snippet doc/src/snippets/code/src_script_qscriptvalue.cpp 0
|
|---|
| 90 |
|
|---|
| 91 | Each property can have a set of attributes; these are specified as
|
|---|
| 92 | the third (optional) argument to setProperty(). The attributes of a
|
|---|
| 93 | property can be queried by calling the propertyFlags() function. The
|
|---|
| 94 | following code snippet creates a property that cannot be modified by
|
|---|
| 95 | script code:
|
|---|
| 96 |
|
|---|
| 97 | \snippet doc/src/snippets/code/src_script_qscriptvalue.cpp 1
|
|---|
| 98 |
|
|---|
| 99 | If you want to iterate over the properties of a script object, use
|
|---|
| 100 | the QScriptValueIterator class.
|
|---|
| 101 |
|
|---|
| 102 | Object values have an internal \c{prototype} property, which can be
|
|---|
| 103 | accessed with prototype() and setPrototype(). Properties added to a
|
|---|
| 104 | prototype are shared by all objects having that prototype; this is
|
|---|
| 105 | referred to as prototype-based inheritance. In practice, it means
|
|---|
| 106 | that (by default) the property() function will automatically attempt
|
|---|
| 107 | to look up look the property in the prototype() (and in the
|
|---|
| 108 | prototype of the prototype(), and so on), if the object itself does
|
|---|
| 109 | not have the requested property. Note that this prototype-based
|
|---|
| 110 | lookup is not performed by setProperty(); setProperty() will always
|
|---|
| 111 | create the property in the script object itself. For more
|
|---|
| 112 | information, see the \l{QtScript} documentation.
|
|---|
| 113 |
|
|---|
| 114 | Function objects (objects for which isFunction() returns true) can
|
|---|
| 115 | be invoked by calling call(). Constructor functions can be used to
|
|---|
| 116 | construct new objects by calling construct().
|
|---|
| 117 |
|
|---|
| 118 | Use equals(), strictlyEquals() and lessThan() to compare a QScriptValue
|
|---|
| 119 | to another.
|
|---|
| 120 |
|
|---|
| 121 | Object values can have custom data associated with them; see the
|
|---|
| 122 | setData() and data() functions. By default, this data is not
|
|---|
| 123 | accessible to scripts; it can be used to store any data you want to
|
|---|
| 124 | associate with the script object. Typically this is used by custom
|
|---|
| 125 | class objects (see QScriptClass) to store a C++ type that contains
|
|---|
| 126 | the "native" object data.
|
|---|
| 127 |
|
|---|
| 128 | Note that a QScriptValue for which isObject() is true only carries a
|
|---|
| 129 | reference to an actual object; copying the QScriptValue will only
|
|---|
| 130 | copy the object reference, not the object itself. If you want to
|
|---|
| 131 | clone an object (i.e. copy an object's properties to another
|
|---|
| 132 | object), you can do so with the help of a \c{for-in} statement in
|
|---|
| 133 | script code, or QScriptValueIterator in C++.
|
|---|
| 134 |
|
|---|
| 135 | \sa QScriptEngine, QScriptValueIterator
|
|---|
| 136 | */
|
|---|
| 137 |
|
|---|
| 138 | /*!
|
|---|
| 139 | \enum QScriptValue::SpecialValue
|
|---|
| 140 |
|
|---|
| 141 | This enum is used to specify a single-valued type.
|
|---|
| 142 |
|
|---|
| 143 | \value UndefinedValue An undefined value.
|
|---|
| 144 |
|
|---|
| 145 | \value NullValue A null value.
|
|---|
| 146 | */
|
|---|
| 147 |
|
|---|
| 148 | /*!
|
|---|
| 149 | \enum QScriptValue::PropertyFlag
|
|---|
| 150 |
|
|---|
| 151 | This enum describes the attributes of a property.
|
|---|
| 152 |
|
|---|
| 153 | \value ReadOnly The property is read-only. Attempts by Qt Script code to write to the property will be ignored.
|
|---|
| 154 |
|
|---|
| 155 | \value Undeletable Attempts by Qt Script code to \c{delete} the property will be ignored.
|
|---|
| 156 |
|
|---|
| 157 | \value SkipInEnumeration The property is not to be enumerated by a \c{for-in} enumeration.
|
|---|
| 158 |
|
|---|
| 159 | \value PropertyGetter The property is defined by a function which will be called to get the property value.
|
|---|
| 160 |
|
|---|
| 161 | \value PropertySetter The property is defined by a function which will be called to set the property value.
|
|---|
| 162 |
|
|---|
| 163 | \value QObjectMember This flag is used to indicate that an existing property is a QObject member (a property or method).
|
|---|
| 164 |
|
|---|
| 165 | \value KeepExistingFlags This value is used to indicate to setProperty() that the property's flags should be left unchanged. If the property doesn't exist, the default flags (0) will be used.
|
|---|
| 166 |
|
|---|
| 167 | \value UserRange Flags in this range are not used by Qt Script, and can be used for custom purposes.
|
|---|
| 168 | */
|
|---|
| 169 |
|
|---|
| 170 | /*!
|
|---|
| 171 | \enum QScriptValue::ResolveFlag
|
|---|
| 172 |
|
|---|
| 173 | This enum specifies how to look up a property of an object.
|
|---|
| 174 |
|
|---|
| 175 | \value ResolveLocal Only check the object's own properties.
|
|---|
| 176 |
|
|---|
| 177 | \value ResolvePrototype Check the object's own properties first, then search the prototype chain. This is the default.
|
|---|
| 178 |
|
|---|
| 179 | \value ResolveScope Check the object's own properties first, then search the scope chain.
|
|---|
| 180 |
|
|---|
| 181 | \value ResolveFull Check the object's own properties first, then search the prototype chain, and finally search the scope chain.
|
|---|
| 182 | */
|
|---|
| 183 |
|
|---|
| 184 | /*!
|
|---|
| 185 | Constructs an invalid QScriptValue.
|
|---|
| 186 | */
|
|---|
| 187 | QScriptValue::QScriptValue()
|
|---|
| 188 | : d_ptr(0)
|
|---|
| 189 | {
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /*!
|
|---|
| 193 | Destroys this QScriptValue.
|
|---|
| 194 | */
|
|---|
| 195 | QScriptValue::~QScriptValue()
|
|---|
| 196 | {
|
|---|
| 197 | if (d_ptr && !d_ptr->ref.deref()) {
|
|---|
| 198 | if (engine()) {
|
|---|
| 199 | QScriptEnginePrivate::get(engine())->unregisterValue(d_ptr);
|
|---|
| 200 | } else {
|
|---|
| 201 | delete d_ptr;
|
|---|
| 202 | }
|
|---|
| 203 | d_ptr = 0;
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /*!
|
|---|
| 208 | Constructs a new QScriptValue that is a copy of \a other.
|
|---|
| 209 |
|
|---|
| 210 | Note that if \a other is an object (i.e., isObject() would return
|
|---|
| 211 | true), then only a reference to the underlying object is copied into
|
|---|
| 212 | the new script value (i.e., the object itself is not copied).
|
|---|
| 213 | */
|
|---|
| 214 | QScriptValue::QScriptValue(const QScriptValue &other)
|
|---|
| 215 | : d_ptr(other.d_ptr)
|
|---|
| 216 | {
|
|---|
| 217 | if (d_ptr)
|
|---|
| 218 | d_ptr->ref.ref();
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /*!
|
|---|
| 222 | \obsolete
|
|---|
| 223 |
|
|---|
| 224 | Constructs a new QScriptValue with the special \a value and
|
|---|
| 225 | registers it with the script \a engine.
|
|---|
| 226 | */
|
|---|
| 227 | QScriptValue::QScriptValue(QScriptEngine *engine, QScriptValue::SpecialValue value)
|
|---|
| 228 | {
|
|---|
| 229 | if (engine) {
|
|---|
| 230 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 231 | d_ptr = eng_p->registerValue(QScriptValueImpl(value));
|
|---|
| 232 | d_ptr->ref.ref();
|
|---|
| 233 | } else {
|
|---|
| 234 | d_ptr = 0;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /*!
|
|---|
| 239 | \obsolete
|
|---|
| 240 |
|
|---|
| 241 | \fn QScriptValue::QScriptValue(QScriptEngine *engine, bool value)
|
|---|
| 242 |
|
|---|
| 243 | Constructs a new QScriptValue with the boolean \a value and
|
|---|
| 244 | registers it with the script \a engine.
|
|---|
| 245 | */
|
|---|
| 246 | QScriptValue::QScriptValue(QScriptEngine *engine, bool val)
|
|---|
| 247 | {
|
|---|
| 248 | if (engine) {
|
|---|
| 249 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 250 | d_ptr = eng_p->registerValue(QScriptValueImpl(val));
|
|---|
| 251 | d_ptr->ref.ref();
|
|---|
| 252 | } else {
|
|---|
| 253 | d_ptr = 0;
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /*!
|
|---|
| 258 | \fn QScriptValue::QScriptValue(QScriptEngine *engine, int value)
|
|---|
| 259 | \obsolete
|
|---|
| 260 |
|
|---|
| 261 | Constructs a new QScriptValue with the integer \a value and
|
|---|
| 262 | registers it with the script \a engine.
|
|---|
| 263 | */
|
|---|
| 264 | QScriptValue::QScriptValue(QScriptEngine *engine, int val)
|
|---|
| 265 | {
|
|---|
| 266 | if (engine) {
|
|---|
| 267 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 268 | d_ptr = eng_p->registerValue(QScriptValueImpl(val));
|
|---|
| 269 | d_ptr->ref.ref();
|
|---|
| 270 | } else {
|
|---|
| 271 | d_ptr = 0;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /*!
|
|---|
| 276 | \fn QScriptValue::QScriptValue(QScriptEngine *engine, uint value)
|
|---|
| 277 | \obsolete
|
|---|
| 278 |
|
|---|
| 279 | Constructs a new QScriptValue with the unsigned integer \a value and
|
|---|
| 280 | registers it with the script \a engine.
|
|---|
| 281 | */
|
|---|
| 282 | QScriptValue::QScriptValue(QScriptEngine *engine, uint val)
|
|---|
| 283 | {
|
|---|
| 284 | if (engine) {
|
|---|
| 285 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 286 | d_ptr = eng_p->registerValue(QScriptValueImpl(val));
|
|---|
| 287 | d_ptr->ref.ref();
|
|---|
| 288 | } else {
|
|---|
| 289 | d_ptr = 0;
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /*!
|
|---|
| 294 | \fn QScriptValue::QScriptValue(QScriptEngine *engine, qsreal value)
|
|---|
| 295 | \obsolete
|
|---|
| 296 |
|
|---|
| 297 | Constructs a new QScriptValue with the qsreal \a value and
|
|---|
| 298 | registers it with the script \a engine.
|
|---|
| 299 | */
|
|---|
| 300 | QScriptValue::QScriptValue(QScriptEngine *engine, qsreal val)
|
|---|
| 301 | {
|
|---|
| 302 | if (engine) {
|
|---|
| 303 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 304 | d_ptr = eng_p->registerValue(QScriptValueImpl(val));
|
|---|
| 305 | d_ptr->ref.ref();
|
|---|
| 306 | } else {
|
|---|
| 307 | d_ptr = 0;
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /*!
|
|---|
| 312 | \fn QScriptValue::QScriptValue(QScriptEngine *engine, const QString &value)
|
|---|
| 313 | \obsolete
|
|---|
| 314 |
|
|---|
| 315 | Constructs a new QScriptValue with the string \a value and
|
|---|
| 316 | registers it with the script \a engine.
|
|---|
| 317 | */
|
|---|
| 318 | QScriptValue::QScriptValue(QScriptEngine *engine, const QString &val)
|
|---|
| 319 | {
|
|---|
| 320 | if (engine) {
|
|---|
| 321 | QScriptValueImpl v;
|
|---|
| 322 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 323 | eng_p->newString(&v, val);
|
|---|
| 324 | d_ptr = eng_p->registerValue(v);
|
|---|
| 325 | d_ptr->ref.ref();
|
|---|
| 326 | } else {
|
|---|
| 327 | d_ptr = 0;
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | /*!
|
|---|
| 332 | \fn QScriptValue::QScriptValue(QScriptEngine *engine, const char *value)
|
|---|
| 333 | \obsolete
|
|---|
| 334 |
|
|---|
| 335 | Constructs a new QScriptValue with the string \a value and
|
|---|
| 336 | registers it with the script \a engine.
|
|---|
| 337 | */
|
|---|
| 338 |
|
|---|
| 339 | #ifndef QT_NO_CAST_FROM_ASCII
|
|---|
| 340 | QScriptValue::QScriptValue(QScriptEngine *engine, const char *val)
|
|---|
| 341 | {
|
|---|
| 342 | if (engine) {
|
|---|
| 343 | QScriptValueImpl v;
|
|---|
| 344 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 345 | eng_p->newString(&v, QString::fromAscii(val));
|
|---|
| 346 | d_ptr = eng_p->registerValue(v);
|
|---|
| 347 | d_ptr->ref.ref();
|
|---|
| 348 | } else {
|
|---|
| 349 | d_ptr = 0;
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 | #endif
|
|---|
| 353 |
|
|---|
| 354 | /*!
|
|---|
| 355 | \since 4.5
|
|---|
| 356 |
|
|---|
| 357 | Constructs a new QScriptValue with a special \a value.
|
|---|
| 358 | */
|
|---|
| 359 | QScriptValue::QScriptValue(SpecialValue value)
|
|---|
| 360 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 361 | {
|
|---|
| 362 | d_ptr->value = QScriptValueImpl(value);
|
|---|
| 363 | d_ptr->ref.ref();
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | /*!
|
|---|
| 367 | \since 4.5
|
|---|
| 368 |
|
|---|
| 369 | Constructs a new QScriptValue with a boolean \a value.
|
|---|
| 370 | */
|
|---|
| 371 | QScriptValue::QScriptValue(bool value)
|
|---|
| 372 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 373 | {
|
|---|
| 374 | d_ptr->value = QScriptValueImpl(value);
|
|---|
| 375 | d_ptr->ref.ref();
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | /*!
|
|---|
| 379 | \since 4.5
|
|---|
| 380 |
|
|---|
| 381 | Constructs a new QScriptValue with a number \a value.
|
|---|
| 382 | */
|
|---|
| 383 | QScriptValue::QScriptValue(int value)
|
|---|
| 384 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 385 | {
|
|---|
| 386 | d_ptr->value = QScriptValueImpl(value);
|
|---|
| 387 | d_ptr->ref.ref();
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | /*!
|
|---|
| 391 | \since 4.5
|
|---|
| 392 |
|
|---|
| 393 | Constructs a new QScriptValue with a number \a value.
|
|---|
| 394 | */
|
|---|
| 395 | QScriptValue::QScriptValue(uint value)
|
|---|
| 396 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 397 | {
|
|---|
| 398 | d_ptr->value = QScriptValueImpl(value);
|
|---|
| 399 | d_ptr->ref.ref();
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | /*!
|
|---|
| 403 | \since 4.5
|
|---|
| 404 |
|
|---|
| 405 | Constructs a new QScriptValue with a number \a value.
|
|---|
| 406 | */
|
|---|
| 407 | QScriptValue::QScriptValue(qsreal value)
|
|---|
| 408 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 409 | {
|
|---|
| 410 | d_ptr->value = QScriptValueImpl(value);
|
|---|
| 411 | d_ptr->ref.ref();
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | /*!
|
|---|
| 415 | \since 4.5
|
|---|
| 416 |
|
|---|
| 417 | Constructs a new QScriptValue with a string \a value.
|
|---|
| 418 | */
|
|---|
| 419 | QScriptValue::QScriptValue(const QString &value)
|
|---|
| 420 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 421 | {
|
|---|
| 422 | d_ptr->value.m_type = QScript::LazyStringType;
|
|---|
| 423 | d_ptr->value.m_lazy_string_value = new QString(value);
|
|---|
| 424 | d_ptr->ref.ref();
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | /*!
|
|---|
| 428 | \since 4.5
|
|---|
| 429 |
|
|---|
| 430 | Constructs a new QScriptValue with a string \a value.
|
|---|
| 431 | */
|
|---|
| 432 | QScriptValue::QScriptValue(const QLatin1String &value)
|
|---|
| 433 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 434 | {
|
|---|
| 435 | d_ptr->value.m_type = QScript::LazyStringType;
|
|---|
| 436 | d_ptr->value.m_lazy_string_value = new QString(value);
|
|---|
| 437 | d_ptr->ref.ref();
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /*!
|
|---|
| 441 | \since 4.5
|
|---|
| 442 |
|
|---|
| 443 | Constructs a new QScriptValue with a string \a value.
|
|---|
| 444 | */
|
|---|
| 445 |
|
|---|
| 446 | #ifndef QT_NO_CAST_FROM_ASCII
|
|---|
| 447 | QScriptValue::QScriptValue(const char *value)
|
|---|
| 448 | : d_ptr(new QScriptValuePrivate)
|
|---|
| 449 | {
|
|---|
| 450 | d_ptr->value.m_type = QScript::LazyStringType;
|
|---|
| 451 | d_ptr->value.m_lazy_string_value = new QString(QString::fromAscii(value));
|
|---|
| 452 | d_ptr->ref.ref();
|
|---|
| 453 | }
|
|---|
| 454 | #endif
|
|---|
| 455 |
|
|---|
| 456 | /*!
|
|---|
| 457 | Assigns the \a other value to this QScriptValue.
|
|---|
| 458 |
|
|---|
| 459 | Note that if \a other is an object (isObject() returns true),
|
|---|
| 460 | only a reference to the underlying object will be assigned;
|
|---|
| 461 | the object itself will not be copied.
|
|---|
| 462 | */
|
|---|
| 463 | QScriptValue &QScriptValue::operator=(const QScriptValue &other)
|
|---|
| 464 | {
|
|---|
| 465 | if (d_ptr == other.d_ptr)
|
|---|
| 466 | return *this;
|
|---|
| 467 | if (d_ptr && !d_ptr->ref.deref()) {
|
|---|
| 468 | if (engine()) {
|
|---|
| 469 | QScriptEnginePrivate::get(engine())->unregisterValue(d_ptr);
|
|---|
| 470 | } else {
|
|---|
| 471 | delete d_ptr;
|
|---|
| 472 | }
|
|---|
| 473 | }
|
|---|
| 474 | d_ptr = other.d_ptr;
|
|---|
| 475 | if (d_ptr)
|
|---|
| 476 | d_ptr->ref.ref();
|
|---|
| 477 | return *this;
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | /*!
|
|---|
| 481 | Returns true if this QScriptValue is an object of the Error class;
|
|---|
| 482 | otherwise returns false.
|
|---|
| 483 |
|
|---|
| 484 | \sa QScriptContext::throwError()
|
|---|
| 485 | */
|
|---|
| 486 | bool QScriptValue::isError() const
|
|---|
| 487 | {
|
|---|
| 488 | Q_D(const QScriptValue);
|
|---|
| 489 | return d && d->value.isError();
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | /*!
|
|---|
| 493 | Returns true if this QScriptValue is an object of the Array class;
|
|---|
| 494 | otherwise returns false.
|
|---|
| 495 |
|
|---|
| 496 | \sa QScriptEngine::newArray()
|
|---|
| 497 | */
|
|---|
| 498 | bool QScriptValue::isArray() const
|
|---|
| 499 | {
|
|---|
| 500 | Q_D(const QScriptValue);
|
|---|
| 501 | return d && d->value.isArray();
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | /*!
|
|---|
| 505 | Returns true if this QScriptValue is an object of the Date class;
|
|---|
| 506 | otherwise returns false.
|
|---|
| 507 |
|
|---|
| 508 | \sa QScriptEngine::newDate()
|
|---|
| 509 | */
|
|---|
| 510 | bool QScriptValue::isDate() const
|
|---|
| 511 | {
|
|---|
| 512 | Q_D(const QScriptValue);
|
|---|
| 513 | return d && d->value.isDate();
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | /*!
|
|---|
| 517 | Returns true if this QScriptValue is an object of the RegExp class;
|
|---|
| 518 | otherwise returns false.
|
|---|
| 519 |
|
|---|
| 520 | \sa QScriptEngine::newRegExp()
|
|---|
| 521 | */
|
|---|
| 522 | bool QScriptValue::isRegExp() const
|
|---|
| 523 | {
|
|---|
| 524 | Q_D(const QScriptValue);
|
|---|
| 525 | return d && d->value.isRegExp();
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | /*!
|
|---|
| 529 | If this QScriptValue is an object, returns the internal prototype
|
|---|
| 530 | (\c{__proto__} property) of this object; otherwise returns an
|
|---|
| 531 | invalid QScriptValue.
|
|---|
| 532 |
|
|---|
| 533 | \sa setPrototype(), isObject()
|
|---|
| 534 | */
|
|---|
| 535 | QScriptValue QScriptValue::prototype() const
|
|---|
| 536 | {
|
|---|
| 537 | Q_D(const QScriptValue);
|
|---|
| 538 | if (!d || !d->value.isObject())
|
|---|
| 539 | return QScriptValue();
|
|---|
| 540 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 541 | return eng->toPublic(d->value.prototype());
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | /*!
|
|---|
| 545 | If this QScriptValue is an object, sets the internal prototype
|
|---|
| 546 | (\c{__proto__} property) of this object to be \a prototype;
|
|---|
| 547 | otherwise does nothing.
|
|---|
| 548 |
|
|---|
| 549 | The internal prototype should not be confused with the public
|
|---|
| 550 | property with name "prototype"; the public prototype is usually
|
|---|
| 551 | only set on functions that act as constructors.
|
|---|
| 552 |
|
|---|
| 553 | \sa prototype(), isObject()
|
|---|
| 554 | */
|
|---|
| 555 | void QScriptValue::setPrototype(const QScriptValue &prototype)
|
|---|
| 556 | {
|
|---|
| 557 | Q_D(QScriptValue);
|
|---|
| 558 | if (!d || !d->value.isObject())
|
|---|
| 559 | return;
|
|---|
| 560 | if (prototype.isValid() && prototype.engine()
|
|---|
| 561 | && (prototype.engine() != engine())) {
|
|---|
| 562 | qWarning("QScriptValue::setPrototype() failed: "
|
|---|
| 563 | "cannot set a prototype created in "
|
|---|
| 564 | "a different engine");
|
|---|
| 565 | return;
|
|---|
| 566 | }
|
|---|
| 567 | QScriptValueImpl was = d->value.prototype();
|
|---|
| 568 | d->value.setPrototype(d->value.engine()->toImpl(prototype));
|
|---|
| 569 | if (d->value.detectedCycle()) {
|
|---|
| 570 | qWarning("QScriptValue::setPrototype() failed: "
|
|---|
| 571 | "cyclic prototype value");
|
|---|
| 572 | d->value.setPrototype(was);
|
|---|
| 573 | }
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | /*!
|
|---|
| 577 | Returns the scope object of this QScriptValue. This function is only
|
|---|
| 578 | relevant for function objects. The scope determines how variables are
|
|---|
| 579 | resolved when the function is invoked.
|
|---|
| 580 | */
|
|---|
| 581 | QScriptValue QScriptValue::scope() const
|
|---|
| 582 | {
|
|---|
| 583 | Q_D(const QScriptValue);
|
|---|
| 584 | if (!d || !d->value.isObject())
|
|---|
| 585 | return QScriptValue();
|
|---|
| 586 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 587 | return eng->toPublic(d->value.scope());
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | /*!
|
|---|
| 591 | Sets the \a scope object of this QScriptValue. This function is only
|
|---|
| 592 | relevant for function objects. Changing the scope is useful when creating
|
|---|
| 593 | closures; see \l{Nested Functions and the Scope Chain}.
|
|---|
| 594 | */
|
|---|
| 595 | void QScriptValue::setScope(const QScriptValue &scope)
|
|---|
| 596 | {
|
|---|
| 597 | Q_D(QScriptValue);
|
|---|
| 598 | if (!d || !d->value.isObject())
|
|---|
| 599 | return;
|
|---|
| 600 | if (scope.isValid() && scope.engine()
|
|---|
| 601 | && (scope.engine() != engine())) {
|
|---|
| 602 | qWarning("QScriptValue::setScope() failed: "
|
|---|
| 603 | "cannot set a scope object created in "
|
|---|
| 604 | "a different engine");
|
|---|
| 605 | return;
|
|---|
| 606 | }
|
|---|
| 607 | d->value.setScope(d->value.engine()->toImpl(scope));
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /*!
|
|---|
| 611 | Returns true if this QScriptValue is an instance of
|
|---|
| 612 | \a other; otherwise returns false.
|
|---|
| 613 |
|
|---|
| 614 | This QScriptValue is considered to be an instance of \a other if
|
|---|
| 615 | \a other is a function and the value of the \c{prototype}
|
|---|
| 616 | property of \a other is in the prototype chain of this
|
|---|
| 617 | QScriptValue.
|
|---|
| 618 | */
|
|---|
| 619 | bool QScriptValue::instanceOf(const QScriptValue &other) const
|
|---|
| 620 | {
|
|---|
| 621 | Q_D(const QScriptValue);
|
|---|
| 622 | if (!isObject() || !other.isObject())
|
|---|
| 623 | return false;
|
|---|
| 624 | if (other.engine() != engine()) {
|
|---|
| 625 | qWarning("QScriptValue::instanceof: "
|
|---|
| 626 | "cannot perform operation on a value created in "
|
|---|
| 627 | "a different engine");
|
|---|
| 628 | return false;
|
|---|
| 629 | }
|
|---|
| 630 | return d->value.engine()->toImpl(*this)
|
|---|
| 631 | .instanceOf(d->value.engine()->toImpl(other));
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | /*!
|
|---|
| 635 | Returns true if this QScriptValue is less than \a other, otherwise
|
|---|
| 636 | returns false. The comparison follows the behavior described in
|
|---|
| 637 | \l{ECMA-262} section 11.8.5, "The Abstract Relational Comparison
|
|---|
| 638 | Algorithm".
|
|---|
| 639 |
|
|---|
| 640 | Note that if this QScriptValue or the \a other value are objects,
|
|---|
| 641 | calling this function has side effects on the script engine, since
|
|---|
| 642 | the engine will call the object's valueOf() function (and possibly
|
|---|
| 643 | toString()) in an attempt to convert the object to a primitive value
|
|---|
| 644 | (possibly resulting in an uncaught script exception).
|
|---|
| 645 |
|
|---|
| 646 | \sa equals()
|
|---|
| 647 | */
|
|---|
| 648 | bool QScriptValue::lessThan(const QScriptValue &other) const
|
|---|
| 649 | {
|
|---|
| 650 | if (!isValid() || !other.isValid())
|
|---|
| 651 | return false;
|
|---|
| 652 | if (other.engine() && engine() && (other.engine() != engine())) {
|
|---|
| 653 | qWarning("QScriptValue::lessThan: "
|
|---|
| 654 | "cannot compare to a value created in "
|
|---|
| 655 | "a different engine");
|
|---|
| 656 | return false;
|
|---|
| 657 | }
|
|---|
| 658 | return QScriptEnginePrivate::lessThan(QScriptValuePrivate::valueOf(*this),
|
|---|
| 659 | QScriptValuePrivate::valueOf(other));
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | /*!
|
|---|
| 663 | Returns true if this QScriptValue is equal to \a other, otherwise
|
|---|
| 664 | returns false. The comparison follows the behavior described in
|
|---|
| 665 | \l{ECMA-262} section 11.9.3, "The Abstract Equality Comparison
|
|---|
| 666 | Algorithm".
|
|---|
| 667 |
|
|---|
| 668 | This function can return true even if the type of this QScriptValue
|
|---|
| 669 | is different from the type of the \a other value; i.e. the
|
|---|
| 670 | comparison is not strict. For example, comparing the number 9 to
|
|---|
| 671 | the string "9" returns true; comparing an undefined value to a null
|
|---|
| 672 | value returns true; comparing a \c{Number} object whose primitive
|
|---|
| 673 | value is 6 to a \c{String} object whose primitive value is "6"
|
|---|
| 674 | returns true; and comparing the number 1 to the boolean value
|
|---|
| 675 | \c{true} returns true. If you want to perform a comparison
|
|---|
| 676 | without such implicit value conversion, use strictlyEquals().
|
|---|
| 677 |
|
|---|
| 678 | Note that if this QScriptValue or the \a other value are objects,
|
|---|
| 679 | calling this function has side effects on the script engine, since
|
|---|
| 680 | the engine will call the object's valueOf() function (and possibly
|
|---|
| 681 | toString()) in an attempt to convert the object to a primitive value
|
|---|
| 682 | (possibly resulting in an uncaught script exception).
|
|---|
| 683 |
|
|---|
| 684 | \sa strictlyEquals(), lessThan()
|
|---|
| 685 | */
|
|---|
| 686 | bool QScriptValue::equals(const QScriptValue &other) const
|
|---|
| 687 | {
|
|---|
| 688 | if (!isValid() || !other.isValid())
|
|---|
| 689 | return isValid() == other.isValid();
|
|---|
| 690 | if (other.engine() && engine() && (other.engine() != engine())) {
|
|---|
| 691 | qWarning("QScriptValue::equals: "
|
|---|
| 692 | "cannot compare to a value created in "
|
|---|
| 693 | "a different engine");
|
|---|
| 694 | return false;
|
|---|
| 695 | }
|
|---|
| 696 | return QScriptEnginePrivate::equals(QScriptValuePrivate::valueOf(*this),
|
|---|
| 697 | QScriptValuePrivate::valueOf(other));
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | /*!
|
|---|
| 701 | Returns true if this QScriptValue is equal to \a other using strict
|
|---|
| 702 | comparison (no conversion), otherwise returns false. The comparison
|
|---|
| 703 | follows the behavior described in \l{ECMA-262} section 11.9.6, "The
|
|---|
| 704 | Strict Equality Comparison Algorithm".
|
|---|
| 705 |
|
|---|
| 706 | If the type of this QScriptValue is different from the type of the
|
|---|
| 707 | \a other value, this function returns false. If the types are equal,
|
|---|
| 708 | the result depends on the type, as shown in the following table:
|
|---|
| 709 |
|
|---|
| 710 | \table
|
|---|
| 711 | \header \o Type \o Result
|
|---|
| 712 | \row \o Undefined \o true
|
|---|
| 713 | \row \o Null \o true
|
|---|
| 714 | \row \o Boolean \o true if both values are true, false otherwise
|
|---|
| 715 | \row \o Number \o false if either value is NaN (Not-a-Number); true if values are equal, false otherwise
|
|---|
| 716 | \row \o String \o true if both values are exactly the same sequence of characters, false otherwise
|
|---|
| 717 | \row \o Object \o true if both values refer to the same object, false otherwise
|
|---|
| 718 | \endtable
|
|---|
| 719 |
|
|---|
| 720 | \sa equals()
|
|---|
| 721 | */
|
|---|
| 722 | bool QScriptValue::strictlyEquals(const QScriptValue &other) const
|
|---|
| 723 | {
|
|---|
| 724 | if (!isValid() || !other.isValid())
|
|---|
| 725 | return isValid() == other.isValid();
|
|---|
| 726 | if (other.engine() && engine() && (other.engine() != engine())) {
|
|---|
| 727 | qWarning("QScriptValue::strictlyEquals: "
|
|---|
| 728 | "cannot compare to a value created in "
|
|---|
| 729 | "a different engine");
|
|---|
| 730 | return false;
|
|---|
| 731 | }
|
|---|
| 732 | return QScriptEnginePrivate::strictlyEquals(QScriptValuePrivate::valueOf(*this),
|
|---|
| 733 | QScriptValuePrivate::valueOf(other));
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | /*!
|
|---|
| 737 | Returns the string value of this QScriptValue, as defined in
|
|---|
| 738 | \l{ECMA-262} section 9.8, "ToString".
|
|---|
| 739 |
|
|---|
| 740 | Note that if this QScriptValue is an object, calling this function
|
|---|
| 741 | has side effects on the script engine, since the engine will call
|
|---|
| 742 | the object's toString() function (and possibly valueOf()) in an
|
|---|
| 743 | attempt to convert the object to a primitive value (possibly
|
|---|
| 744 | resulting in an uncaught script exception).
|
|---|
| 745 |
|
|---|
| 746 | \sa isString()
|
|---|
| 747 | */
|
|---|
| 748 | QString QScriptValue::toString() const
|
|---|
| 749 | {
|
|---|
| 750 | Q_D(const QScriptValue);
|
|---|
| 751 | if (!d)
|
|---|
| 752 | return QString();
|
|---|
| 753 | return d->value.toString();
|
|---|
| 754 | }
|
|---|
| 755 |
|
|---|
| 756 | /*!
|
|---|
| 757 | Returns the number value of this QScriptValue, as defined in
|
|---|
| 758 | \l{ECMA-262} section 9.3, "ToNumber".
|
|---|
| 759 |
|
|---|
| 760 | Note that if this QScriptValue is an object, calling this function
|
|---|
| 761 | has side effects on the script engine, since the engine will call
|
|---|
| 762 | the object's valueOf() function (and possibly toString()) in an
|
|---|
| 763 | attempt to convert the object to a primitive value (possibly
|
|---|
| 764 | resulting in an uncaught script exception).
|
|---|
| 765 |
|
|---|
| 766 | \sa isNumber(), toInteger(), toInt32(), toUInt32(), toUInt16()
|
|---|
| 767 | */
|
|---|
| 768 | qsreal QScriptValue::toNumber() const
|
|---|
| 769 | {
|
|---|
| 770 | Q_D(const QScriptValue);
|
|---|
| 771 | if (!d)
|
|---|
| 772 | return 0;
|
|---|
| 773 | return d->value.toNumber();
|
|---|
| 774 | }
|
|---|
| 775 |
|
|---|
| 776 | /*!
|
|---|
| 777 | \obsolete
|
|---|
| 778 |
|
|---|
| 779 | Use toBool() instead.
|
|---|
| 780 | */
|
|---|
| 781 | bool QScriptValue::toBoolean() const
|
|---|
| 782 | {
|
|---|
| 783 | Q_D(const QScriptValue);
|
|---|
| 784 | if (!d)
|
|---|
| 785 | return false;
|
|---|
| 786 | return d->value.toBoolean();
|
|---|
| 787 | }
|
|---|
| 788 |
|
|---|
| 789 | /*!
|
|---|
| 790 | \since 4.5
|
|---|
| 791 |
|
|---|
| 792 | Returns the boolean value of this QScriptValue, using the conversion
|
|---|
| 793 | rules described in \l{ECMA-262} section 9.2, "ToBoolean".
|
|---|
| 794 |
|
|---|
| 795 | Note that if this QScriptValue is an object, calling this function
|
|---|
| 796 | has side effects on the script engine, since the engine will call
|
|---|
| 797 | the object's valueOf() function (and possibly toString()) in an
|
|---|
| 798 | attempt to convert the object to a primitive value (possibly
|
|---|
| 799 | resulting in an uncaught script exception).
|
|---|
| 800 |
|
|---|
| 801 | \sa isBool()
|
|---|
| 802 | */
|
|---|
| 803 | bool QScriptValue::toBool() const
|
|---|
| 804 | {
|
|---|
| 805 | Q_D(const QScriptValue);
|
|---|
| 806 | if (!d)
|
|---|
| 807 | return false;
|
|---|
| 808 | return d->value.toBoolean();
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 | /*!
|
|---|
| 812 | Returns the signed 32-bit integer value of this QScriptValue, using
|
|---|
| 813 | the conversion rules described in \l{ECMA-262} section 9.5, "ToInt32".
|
|---|
| 814 |
|
|---|
| 815 | Note that if this QScriptValue is an object, calling this function
|
|---|
| 816 | has side effects on the script engine, since the engine will call
|
|---|
| 817 | the object's valueOf() function (and possibly toString()) in an
|
|---|
| 818 | attempt to convert the object to a primitive value (possibly
|
|---|
| 819 | resulting in an uncaught script exception).
|
|---|
| 820 |
|
|---|
| 821 | \sa toNumber(), toUInt32()
|
|---|
| 822 | */
|
|---|
| 823 | qint32 QScriptValue::toInt32() const
|
|---|
| 824 | {
|
|---|
| 825 | Q_D(const QScriptValue);
|
|---|
| 826 | if (!d)
|
|---|
| 827 | return 0;
|
|---|
| 828 | return d->value.toInt32();
|
|---|
| 829 | }
|
|---|
| 830 |
|
|---|
| 831 | /*!
|
|---|
| 832 | Returns the unsigned 32-bit integer value of this QScriptValue, using
|
|---|
| 833 | the conversion rules described in \l{ECMA-262} section 9.6, "ToUint32".
|
|---|
| 834 |
|
|---|
| 835 | Note that if this QScriptValue is an object, calling this function
|
|---|
| 836 | has side effects on the script engine, since the engine will call
|
|---|
| 837 | the object's valueOf() function (and possibly toString()) in an
|
|---|
| 838 | attempt to convert the object to a primitive value (possibly
|
|---|
| 839 | resulting in an uncaught script exception).
|
|---|
| 840 |
|
|---|
| 841 | \sa toNumber(), toInt32()
|
|---|
| 842 | */
|
|---|
| 843 | quint32 QScriptValue::toUInt32() const
|
|---|
| 844 | {
|
|---|
| 845 | Q_D(const QScriptValue);
|
|---|
| 846 | if (!d)
|
|---|
| 847 | return 0;
|
|---|
| 848 | return d->value.toUInt32();
|
|---|
| 849 | }
|
|---|
| 850 |
|
|---|
| 851 | /*!
|
|---|
| 852 | Returns the unsigned 16-bit integer value of this QScriptValue, using
|
|---|
| 853 | the conversion rules described in \l{ECMA-262} section 9.7, "ToUint16".
|
|---|
| 854 |
|
|---|
| 855 | Note that if this QScriptValue is an object, calling this function
|
|---|
| 856 | has side effects on the script engine, since the engine will call
|
|---|
| 857 | the object's valueOf() function (and possibly toString()) in an
|
|---|
| 858 | attempt to convert the object to a primitive value (possibly
|
|---|
| 859 | resulting in an uncaught script exception).
|
|---|
| 860 |
|
|---|
| 861 | \sa toNumber()
|
|---|
| 862 | */
|
|---|
| 863 | quint16 QScriptValue::toUInt16() const
|
|---|
| 864 | {
|
|---|
| 865 | Q_D(const QScriptValue);
|
|---|
| 866 | if (!d)
|
|---|
| 867 | return 0;
|
|---|
| 868 | return d->value.toUInt16();
|
|---|
| 869 | }
|
|---|
| 870 |
|
|---|
| 871 | /*!
|
|---|
| 872 | Returns the integer value of this QScriptValue, using the conversion
|
|---|
| 873 | rules described in \l{ECMA-262} section 9.4, "ToInteger".
|
|---|
| 874 |
|
|---|
| 875 | Note that if this QScriptValue is an object, calling this function
|
|---|
| 876 | has side effects on the script engine, since the engine will call
|
|---|
| 877 | the object's valueOf() function (and possibly toString()) in an
|
|---|
| 878 | attempt to convert the object to a primitive value (possibly
|
|---|
| 879 | resulting in an uncaught script exception).
|
|---|
| 880 |
|
|---|
| 881 | \sa toNumber()
|
|---|
| 882 | */
|
|---|
| 883 | qsreal QScriptValue::toInteger() const
|
|---|
| 884 | {
|
|---|
| 885 | Q_D(const QScriptValue);
|
|---|
| 886 | if (!d)
|
|---|
| 887 | return 0;
|
|---|
| 888 | return d->value.toInteger();
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | /*!
|
|---|
| 892 | Returns the QVariant value of this QScriptValue, if it can be
|
|---|
| 893 | converted to a QVariant; otherwise returns an invalid QVariant.
|
|---|
| 894 | The conversion is performed according to the following table:
|
|---|
| 895 |
|
|---|
| 896 | \table
|
|---|
| 897 | \header \o Input Type \o Result
|
|---|
| 898 | \row \o Undefined \o An invalid QVariant.
|
|---|
| 899 | \row \o Null \o An invalid QVariant.
|
|---|
| 900 | \row \o Boolean \o A QVariant containing the value of the boolean.
|
|---|
| 901 | \row \o Number \o A QVariant containing the value of the number.
|
|---|
| 902 | \row \o String \o A QVariant containing the value of the string.
|
|---|
| 903 | \row \o QVariant Object \o The result is the QVariant value of the object (no conversion).
|
|---|
| 904 | \row \o QObject Object \o A QVariant containing a pointer to the QObject.
|
|---|
| 905 | \row \o Date Object \o A QVariant containing the date value (toDateTime()).
|
|---|
| 906 | \row \o RegExp Object \o A QVariant containing the regular expression value (toRegExp()).
|
|---|
| 907 | \row \o Object \o If the value is primitive, then the result is converted to a QVariant according to the above rules; otherwise, an invalid QVariant is returned.
|
|---|
| 908 | \endtable
|
|---|
| 909 |
|
|---|
| 910 | \sa isVariant()
|
|---|
| 911 | */
|
|---|
| 912 | QVariant QScriptValue::toVariant() const
|
|---|
| 913 | {
|
|---|
| 914 | Q_D(const QScriptValue);
|
|---|
| 915 | if (!d)
|
|---|
| 916 | return QVariant();
|
|---|
| 917 | return d->value.toVariant();
|
|---|
| 918 | }
|
|---|
| 919 |
|
|---|
| 920 | /*!
|
|---|
| 921 | \obsolete
|
|---|
| 922 |
|
|---|
| 923 | This function is obsolete; use QScriptEngine::toObject() instead.
|
|---|
| 924 | */
|
|---|
| 925 | QScriptValue QScriptValue::toObject() const
|
|---|
| 926 | {
|
|---|
| 927 | Q_D(const QScriptValue);
|
|---|
| 928 | if (!d)
|
|---|
| 929 | return QScriptValue();
|
|---|
| 930 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 931 | if (!eng)
|
|---|
| 932 | return QScriptValue();
|
|---|
| 933 | return eng->toPublic(eng->toObject(d->value));
|
|---|
| 934 | }
|
|---|
| 935 |
|
|---|
| 936 | /*!
|
|---|
| 937 | Returns a QDateTime representation of this value, in local time.
|
|---|
| 938 | If this QScriptValue is not a date, or the value of the date is NaN
|
|---|
| 939 | (Not-a-Number), an invalid QDateTime is returned.
|
|---|
| 940 |
|
|---|
| 941 | \sa isDate()
|
|---|
| 942 | */
|
|---|
| 943 | QDateTime QScriptValue::toDateTime() const
|
|---|
| 944 | {
|
|---|
| 945 | Q_D(const QScriptValue);
|
|---|
| 946 | if (!d)
|
|---|
| 947 | return QDateTime();
|
|---|
| 948 | return d->value.toDateTime();
|
|---|
| 949 | }
|
|---|
| 950 |
|
|---|
| 951 | #ifndef QT_NO_REGEXP
|
|---|
| 952 | /*!
|
|---|
| 953 | Returns the QRegExp representation of this value.
|
|---|
| 954 | If this QScriptValue is not a regular expression, an empty
|
|---|
| 955 | QRegExp is returned.
|
|---|
| 956 |
|
|---|
| 957 | \sa isRegExp()
|
|---|
| 958 | */
|
|---|
| 959 | QRegExp QScriptValue::toRegExp() const
|
|---|
| 960 | {
|
|---|
| 961 | Q_D(const QScriptValue);
|
|---|
| 962 | if (!d)
|
|---|
| 963 | return QRegExp();
|
|---|
| 964 | return d->value.toRegExp();
|
|---|
| 965 | }
|
|---|
| 966 | #endif // QT_NO_REGEXP
|
|---|
| 967 |
|
|---|
| 968 | /*!
|
|---|
| 969 | If this QScriptValue is a QObject, returns the QObject pointer
|
|---|
| 970 | that the QScriptValue represents; otherwise, returns 0.
|
|---|
| 971 |
|
|---|
| 972 | If the QObject that this QScriptValue wraps has been deleted,
|
|---|
| 973 | this function returns 0 (i.e. it is possible for toQObject()
|
|---|
| 974 | to return 0 even when isQObject() returns true).
|
|---|
| 975 |
|
|---|
| 976 | \sa isQObject()
|
|---|
| 977 | */
|
|---|
| 978 | QObject *QScriptValue::toQObject() const
|
|---|
| 979 | {
|
|---|
| 980 | Q_D(const QScriptValue);
|
|---|
| 981 | if (!d)
|
|---|
| 982 | return 0;
|
|---|
| 983 | return d->value.toQObject();
|
|---|
| 984 | }
|
|---|
| 985 |
|
|---|
| 986 | /*!
|
|---|
| 987 | If this QScriptValue is a QMetaObject, returns the QMetaObject pointer
|
|---|
| 988 | that the QScriptValue represents; otherwise, returns 0.
|
|---|
| 989 |
|
|---|
| 990 | \sa isQMetaObject()
|
|---|
| 991 | */
|
|---|
| 992 | const QMetaObject *QScriptValue::toQMetaObject() const
|
|---|
| 993 | {
|
|---|
| 994 | Q_D(const QScriptValue);
|
|---|
| 995 | if (!d)
|
|---|
| 996 | return 0;
|
|---|
| 997 | return d->value.toQMetaObject();
|
|---|
| 998 | }
|
|---|
| 999 |
|
|---|
| 1000 | /*!
|
|---|
| 1001 | Sets the value of this QScriptValue's property with the given \a name to
|
|---|
| 1002 | the given \a value.
|
|---|
| 1003 |
|
|---|
| 1004 | If this QScriptValue is not an object, this function does nothing.
|
|---|
| 1005 |
|
|---|
| 1006 | If this QScriptValue does not already have a property with name \a name,
|
|---|
| 1007 | a new property is created; the given \a flags then specify how this
|
|---|
| 1008 | property may be accessed by script code.
|
|---|
| 1009 |
|
|---|
| 1010 | If \a value is invalid, the property is removed.
|
|---|
| 1011 |
|
|---|
| 1012 | If the property is implemented using a setter function (i.e. has the
|
|---|
| 1013 | PropertySetter flag set), calling setProperty() has side-effects on
|
|---|
| 1014 | the script engine, since the setter function will be called with the
|
|---|
| 1015 | given \a value as argument (possibly resulting in an uncaught script
|
|---|
| 1016 | exception).
|
|---|
| 1017 |
|
|---|
| 1018 | Note that you cannot specify custom getter or setter functions for
|
|---|
| 1019 | built-in properties, such as the \c{length} property of Array objects
|
|---|
| 1020 | or meta properties of QObject objects.
|
|---|
| 1021 |
|
|---|
| 1022 | \sa property()
|
|---|
| 1023 | */
|
|---|
| 1024 | void QScriptValue::setProperty(const QString &name, const QScriptValue &value,
|
|---|
| 1025 | const PropertyFlags &flags)
|
|---|
| 1026 | {
|
|---|
| 1027 | Q_D(QScriptValue);
|
|---|
| 1028 | if (!d || !d->value.isObject())
|
|---|
| 1029 | return;
|
|---|
| 1030 | if (value.engine() && (value.engine() != engine())) {
|
|---|
| 1031 | qWarning("QScriptValue::setProperty(%s) failed: "
|
|---|
| 1032 | "cannot set value created in a different engine",
|
|---|
| 1033 | qPrintable(name));
|
|---|
| 1034 | return;
|
|---|
| 1035 | }
|
|---|
| 1036 | d->value.setProperty(name, d->value.engine()->toImpl(value), flags);
|
|---|
| 1037 | }
|
|---|
| 1038 |
|
|---|
| 1039 | /*!
|
|---|
| 1040 | Returns the value of this QScriptValue's property with the given \a name,
|
|---|
| 1041 | using the given \a mode to resolve the property.
|
|---|
| 1042 |
|
|---|
| 1043 | If no such property exists, an invalid QScriptValue is returned.
|
|---|
| 1044 |
|
|---|
| 1045 | If the property is implemented using a getter function (i.e. has the
|
|---|
| 1046 | PropertyGetter flag set), calling property() has side-effects on the
|
|---|
| 1047 | script engine, since the getter function will be called (possibly
|
|---|
| 1048 | resulting in an uncaught script exception). If an exception
|
|---|
| 1049 | occurred, property() returns the value that was thrown (typically
|
|---|
| 1050 | an \c{Error} object).
|
|---|
| 1051 |
|
|---|
| 1052 | \sa setProperty(), propertyFlags(), QScriptValueIterator
|
|---|
| 1053 | */
|
|---|
| 1054 | QScriptValue QScriptValue::property(const QString &name,
|
|---|
| 1055 | const ResolveFlags &mode) const
|
|---|
| 1056 | {
|
|---|
| 1057 | Q_D(const QScriptValue);
|
|---|
| 1058 | if (!d || !d->value.isObject())
|
|---|
| 1059 | return QScriptValue();
|
|---|
| 1060 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1061 | return eng->toPublic(d->value.property(name, mode));
|
|---|
| 1062 | }
|
|---|
| 1063 |
|
|---|
| 1064 | /*!
|
|---|
| 1065 | \overload
|
|---|
| 1066 |
|
|---|
| 1067 | Returns the property at the given \a arrayIndex, using the given \a
|
|---|
| 1068 | mode to resolve the property.
|
|---|
| 1069 |
|
|---|
| 1070 | This function is provided for convenience and performance when
|
|---|
| 1071 | working with array objects.
|
|---|
| 1072 |
|
|---|
| 1073 | If this QScriptValue is not an Array object, this function behaves
|
|---|
| 1074 | as if property() was called with the string representation of \a
|
|---|
| 1075 | arrayIndex.
|
|---|
| 1076 | */
|
|---|
| 1077 | QScriptValue QScriptValue::property(quint32 arrayIndex,
|
|---|
| 1078 | const ResolveFlags &mode) const
|
|---|
| 1079 | {
|
|---|
| 1080 | Q_D(const QScriptValue);
|
|---|
| 1081 | if (!d || !d->value.isObject())
|
|---|
| 1082 | return QScriptValue();
|
|---|
| 1083 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1084 | return eng->toPublic(d->value.property(arrayIndex, mode));
|
|---|
| 1085 | }
|
|---|
| 1086 |
|
|---|
| 1087 | /*!
|
|---|
| 1088 | \overload
|
|---|
| 1089 |
|
|---|
| 1090 | Sets the property at the given \a arrayIndex to the given \a value.
|
|---|
| 1091 |
|
|---|
| 1092 | This function is provided for convenience and performance when
|
|---|
| 1093 | working with array objects.
|
|---|
| 1094 |
|
|---|
| 1095 | If this QScriptValue is not an Array object, this function behaves
|
|---|
| 1096 | as if setProperty() was called with the string representation of \a
|
|---|
| 1097 | arrayIndex.
|
|---|
| 1098 | */
|
|---|
| 1099 | void QScriptValue::setProperty(quint32 arrayIndex, const QScriptValue &value,
|
|---|
| 1100 | const PropertyFlags &flags)
|
|---|
| 1101 | {
|
|---|
| 1102 | Q_D(QScriptValue);
|
|---|
| 1103 | if (!d || !d->value.isObject())
|
|---|
| 1104 | return;
|
|---|
| 1105 | if (value.engine() && (value.engine() != engine())) {
|
|---|
| 1106 | qWarning("QScriptValue::setProperty() failed: "
|
|---|
| 1107 | "cannot set value created in a different engine");
|
|---|
| 1108 | return;
|
|---|
| 1109 | }
|
|---|
| 1110 | d->value.setProperty(arrayIndex, d->value.engine()->toImpl(value), flags);
|
|---|
| 1111 | }
|
|---|
| 1112 |
|
|---|
| 1113 | /*!
|
|---|
| 1114 | \since 4.4
|
|---|
| 1115 |
|
|---|
| 1116 | Returns the value of this QScriptValue's property with the given \a name,
|
|---|
| 1117 | using the given \a mode to resolve the property.
|
|---|
| 1118 |
|
|---|
| 1119 | This overload of property() is useful when you need to look up the
|
|---|
| 1120 | same property repeatedly, since the lookup can be performed faster
|
|---|
| 1121 | when the name is represented as an interned string.
|
|---|
| 1122 |
|
|---|
| 1123 | \sa QScriptEngine::toStringHandle(), setProperty()
|
|---|
| 1124 | */
|
|---|
| 1125 | QScriptValue QScriptValue::property(const QScriptString &name,
|
|---|
| 1126 | const ResolveFlags &mode) const
|
|---|
| 1127 | {
|
|---|
| 1128 | Q_D(const QScriptValue);
|
|---|
| 1129 | if (!d || !d->value.isObject())
|
|---|
| 1130 | return QScriptValue();
|
|---|
| 1131 | if (!name.isValid())
|
|---|
| 1132 | return QScriptValue();
|
|---|
| 1133 | QScriptStringPrivate *s = QScriptStringPrivate::get(name);
|
|---|
| 1134 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1135 | return eng->toPublic(d->value.property(s->nameId, mode));
|
|---|
| 1136 | }
|
|---|
| 1137 |
|
|---|
| 1138 | /*!
|
|---|
| 1139 | \since 4.4
|
|---|
| 1140 |
|
|---|
| 1141 | Sets the value of this QScriptValue's property with the given \a
|
|---|
| 1142 | name to the given \a value. The given \a flags specify how this
|
|---|
| 1143 | property may be accessed by script code.
|
|---|
| 1144 |
|
|---|
| 1145 | This overload of setProperty() is useful when you need to set the
|
|---|
| 1146 | same property repeatedly, since the operation can be performed
|
|---|
| 1147 | faster when the name is represented as an interned string.
|
|---|
| 1148 |
|
|---|
| 1149 | \sa QScriptEngine::toStringHandle()
|
|---|
| 1150 | */
|
|---|
| 1151 | void QScriptValue::setProperty(const QScriptString &name,
|
|---|
| 1152 | const QScriptValue &value,
|
|---|
| 1153 | const PropertyFlags &flags)
|
|---|
| 1154 | {
|
|---|
| 1155 | Q_D(QScriptValue);
|
|---|
| 1156 | if (!d || !d->value.isObject() || !name.isValid())
|
|---|
| 1157 | return;
|
|---|
| 1158 | if (value.engine() && (value.engine() != engine())) {
|
|---|
| 1159 | qWarning("QScriptValue::setProperty() failed: "
|
|---|
| 1160 | "cannot set value created in a different engine");
|
|---|
| 1161 | return;
|
|---|
| 1162 | }
|
|---|
| 1163 | QScriptStringPrivate *s = QScriptStringPrivate::get(name);
|
|---|
| 1164 | d->value.setProperty(s->nameId, d->value.engine()->toImpl(value), flags);
|
|---|
| 1165 | }
|
|---|
| 1166 |
|
|---|
| 1167 | /*!
|
|---|
| 1168 | Returns the flags of the property with the given \a name, using the
|
|---|
| 1169 | given \a mode to resolve the property.
|
|---|
| 1170 |
|
|---|
| 1171 | \sa property()
|
|---|
| 1172 | */
|
|---|
| 1173 | QScriptValue::PropertyFlags QScriptValue::propertyFlags(const QString &name,
|
|---|
| 1174 | const ResolveFlags &mode) const
|
|---|
| 1175 | {
|
|---|
| 1176 | Q_D(const QScriptValue);
|
|---|
| 1177 | if (!d)
|
|---|
| 1178 | return 0;
|
|---|
| 1179 | return d->value.propertyFlags(name, mode);
|
|---|
| 1180 | }
|
|---|
| 1181 |
|
|---|
| 1182 | /*!
|
|---|
| 1183 | \since 4.4
|
|---|
| 1184 |
|
|---|
| 1185 | Returns the flags of the property with the given \a name, using the
|
|---|
| 1186 | given \a mode to resolve the property.
|
|---|
| 1187 |
|
|---|
| 1188 | \sa property()
|
|---|
| 1189 | */
|
|---|
| 1190 | QScriptValue::PropertyFlags QScriptValue::propertyFlags(const QScriptString &name,
|
|---|
| 1191 | const ResolveFlags &mode) const
|
|---|
| 1192 | {
|
|---|
| 1193 | Q_D(const QScriptValue);
|
|---|
| 1194 | if (!d)
|
|---|
| 1195 | return 0;
|
|---|
| 1196 | if (!name.isValid())
|
|---|
| 1197 | return 0;
|
|---|
| 1198 | QScriptStringPrivate *s = QScriptStringPrivate::get(name);
|
|---|
| 1199 | return d->value.propertyFlags(s->nameId, mode);
|
|---|
| 1200 | }
|
|---|
| 1201 |
|
|---|
| 1202 | /*!
|
|---|
| 1203 | Calls this QScriptValue as a function, using \a thisObject as
|
|---|
| 1204 | the `this' object in the function call, and passing \a args
|
|---|
| 1205 | as arguments to the function. Returns the value returned from
|
|---|
| 1206 | the function.
|
|---|
| 1207 |
|
|---|
| 1208 | If this QScriptValue is not a function, call() does nothing
|
|---|
| 1209 | and returns an invalid QScriptValue.
|
|---|
| 1210 |
|
|---|
| 1211 | Note that if \a thisObject is not an object, the global object
|
|---|
| 1212 | (see \l{QScriptEngine::globalObject()}) will be used as the
|
|---|
| 1213 | `this' object.
|
|---|
| 1214 |
|
|---|
| 1215 | Calling call() can cause an exception to occur in the script engine;
|
|---|
| 1216 | in that case, call() returns the value that was thrown (typically an
|
|---|
| 1217 | \c{Error} object). You can call
|
|---|
| 1218 | QScriptEngine::hasUncaughtException() to determine if an exception
|
|---|
| 1219 | occurred.
|
|---|
| 1220 |
|
|---|
| 1221 | \snippet doc/src/snippets/code/src_script_qscriptvalue.cpp 2
|
|---|
| 1222 |
|
|---|
| 1223 | \sa construct()
|
|---|
| 1224 | */
|
|---|
| 1225 | QScriptValue QScriptValue::call(const QScriptValue &thisObject,
|
|---|
| 1226 | const QScriptValueList &args)
|
|---|
| 1227 | {
|
|---|
| 1228 | Q_D(QScriptValue);
|
|---|
| 1229 | if (!d || !d->value.isObject())
|
|---|
| 1230 | return QScriptValue();
|
|---|
| 1231 | if (isFunction() && thisObject.isValid() && thisObject.engine() &&
|
|---|
| 1232 | engine() && (thisObject.engine() != engine())) {
|
|---|
| 1233 | qWarning("QScriptValue::call() failed: "
|
|---|
| 1234 | "cannot call function with thisObject created in "
|
|---|
| 1235 | "a different engine");
|
|---|
| 1236 | return QScriptValue();
|
|---|
| 1237 | }
|
|---|
| 1238 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1239 | return eng->toPublic(d->value.call(eng->toImpl(thisObject),
|
|---|
| 1240 | eng->toImplList(args)));
|
|---|
| 1241 | }
|
|---|
| 1242 |
|
|---|
| 1243 | /*!
|
|---|
| 1244 | Calls this QScriptValue as a function, using \a thisObject as
|
|---|
| 1245 | the `this' object in the function call, and passing \a arguments
|
|---|
| 1246 | as arguments to the function. Returns the value returned from
|
|---|
| 1247 | the function.
|
|---|
| 1248 |
|
|---|
| 1249 | If this QScriptValue is not a function, call() does nothing
|
|---|
| 1250 | and returns an invalid QScriptValue.
|
|---|
| 1251 |
|
|---|
| 1252 | \a arguments can be an arguments object, an array, null or
|
|---|
| 1253 | undefined; any other type will cause a TypeError to be thrown.
|
|---|
| 1254 |
|
|---|
| 1255 | Note that if \a thisObject is not an object, the global object
|
|---|
| 1256 | (see \l{QScriptEngine::globalObject()}) will be used as the
|
|---|
| 1257 | `this' object.
|
|---|
| 1258 |
|
|---|
| 1259 | One common usage of this function is to forward native function
|
|---|
| 1260 | calls to another function:
|
|---|
| 1261 |
|
|---|
| 1262 | \snippet doc/src/snippets/code/src_script_qscriptvalue.cpp 3
|
|---|
| 1263 |
|
|---|
| 1264 | \sa construct(), QScriptContext::argumentsObject()
|
|---|
| 1265 | */
|
|---|
| 1266 | QScriptValue QScriptValue::call(const QScriptValue &thisObject,
|
|---|
| 1267 | const QScriptValue &arguments)
|
|---|
| 1268 | {
|
|---|
| 1269 | Q_D(QScriptValue);
|
|---|
| 1270 | if (!d || !d->value.isObject())
|
|---|
| 1271 | return QScriptValue();
|
|---|
| 1272 | if (isFunction() && thisObject.isValid() && thisObject.engine()
|
|---|
| 1273 | && (thisObject.engine() != engine())) {
|
|---|
| 1274 | qWarning("QScriptValue::call() failed: "
|
|---|
| 1275 | "cannot call function with thisObject created in "
|
|---|
| 1276 | "a different engine");
|
|---|
| 1277 | return QScriptValue();
|
|---|
| 1278 | }
|
|---|
| 1279 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1280 | return eng->toPublic(d->value.call(eng->toImpl(thisObject),
|
|---|
| 1281 | eng->toImpl(arguments)));
|
|---|
| 1282 | }
|
|---|
| 1283 |
|
|---|
| 1284 | /*!
|
|---|
| 1285 | Creates a new \c{Object} and calls this QScriptValue as a
|
|---|
| 1286 | constructor, using the created object as the `this' object and
|
|---|
| 1287 | passing \a args as arguments. If the return value from the
|
|---|
| 1288 | constructor call is an object, then that object is returned;
|
|---|
| 1289 | otherwise the default constructed object is returned.
|
|---|
| 1290 |
|
|---|
| 1291 | If this QScriptValue is not a function, construct() does nothing
|
|---|
| 1292 | and returns an invalid QScriptValue.
|
|---|
| 1293 |
|
|---|
| 1294 | Calling construct() can cause an exception to occur in the script
|
|---|
| 1295 | engine; in that case, construct() returns the value that was thrown
|
|---|
| 1296 | (typically an \c{Error} object). You can call
|
|---|
| 1297 | QScriptEngine::hasUncaughtException() to determine if an exception
|
|---|
| 1298 | occurred.
|
|---|
| 1299 |
|
|---|
| 1300 | \sa call(), QScriptEngine::newObject()
|
|---|
| 1301 | */
|
|---|
| 1302 | QScriptValue QScriptValue::construct(const QScriptValueList &args)
|
|---|
| 1303 | {
|
|---|
| 1304 | Q_D(QScriptValue);
|
|---|
| 1305 | if (!d || !d->value.isObject())
|
|---|
| 1306 | return QScriptValue();
|
|---|
| 1307 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1308 | return eng->toPublic(d->value.construct(eng->toImplList(args)));
|
|---|
| 1309 | }
|
|---|
| 1310 |
|
|---|
| 1311 | /*!
|
|---|
| 1312 | Creates a new \c{Object} and calls this QScriptValue as a
|
|---|
| 1313 | constructor, using the created object as the `this' object and
|
|---|
| 1314 | passing \a arguments as arguments. If the return value from the
|
|---|
| 1315 | constructor call is an object, then that object is returned;
|
|---|
| 1316 | otherwise the default constructed object is returned.
|
|---|
| 1317 |
|
|---|
| 1318 | If this QScriptValue is not a function, construct() does nothing
|
|---|
| 1319 | and returns an invalid QScriptValue.
|
|---|
| 1320 |
|
|---|
| 1321 | \a arguments can be an arguments object, an array, null or
|
|---|
| 1322 | undefined. Any other type will cause a TypeError to be thrown.
|
|---|
| 1323 |
|
|---|
| 1324 | \sa call(), QScriptEngine::newObject(), QScriptContext::argumentsObject()
|
|---|
| 1325 | */
|
|---|
| 1326 | QScriptValue QScriptValue::construct(const QScriptValue &arguments)
|
|---|
| 1327 | {
|
|---|
| 1328 | Q_D(QScriptValue);
|
|---|
| 1329 | if (!d || !d->value.isObject())
|
|---|
| 1330 | return QScriptValue();
|
|---|
| 1331 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1332 | return eng->toPublic(d->value.construct(eng->toImpl(arguments)));
|
|---|
| 1333 | }
|
|---|
| 1334 |
|
|---|
| 1335 | /*!
|
|---|
| 1336 | Returns the QScriptEngine that created this QScriptValue,
|
|---|
| 1337 | or 0 if this QScriptValue is invalid or the value is not
|
|---|
| 1338 | associated with a particular engine.
|
|---|
| 1339 | */
|
|---|
| 1340 | QScriptEngine *QScriptValue::engine() const
|
|---|
| 1341 | {
|
|---|
| 1342 | Q_D(const QScriptValue);
|
|---|
| 1343 | if (!d)
|
|---|
| 1344 | return 0;
|
|---|
| 1345 | return d->engine;
|
|---|
| 1346 | }
|
|---|
| 1347 |
|
|---|
| 1348 | /*!
|
|---|
| 1349 | \obsolete
|
|---|
| 1350 |
|
|---|
| 1351 | Use isBool() instead.
|
|---|
| 1352 | */
|
|---|
| 1353 | bool QScriptValue::isBoolean() const
|
|---|
| 1354 | {
|
|---|
| 1355 | Q_D(const QScriptValue);
|
|---|
| 1356 | return d && d->value.isBoolean();
|
|---|
| 1357 | }
|
|---|
| 1358 |
|
|---|
| 1359 | /*!
|
|---|
| 1360 | \since 4.5
|
|---|
| 1361 |
|
|---|
| 1362 | Returns true if this QScriptValue is of the primitive type Boolean;
|
|---|
| 1363 | otherwise returns false.
|
|---|
| 1364 |
|
|---|
| 1365 | \sa toBool()
|
|---|
| 1366 | */
|
|---|
| 1367 | bool QScriptValue::isBool() const
|
|---|
| 1368 | {
|
|---|
| 1369 | Q_D(const QScriptValue);
|
|---|
| 1370 | return d && d->value.isBoolean();
|
|---|
| 1371 | }
|
|---|
| 1372 |
|
|---|
| 1373 | /*!
|
|---|
| 1374 | Returns true if this QScriptValue is of the primitive type Number;
|
|---|
| 1375 | otherwise returns false.
|
|---|
| 1376 |
|
|---|
| 1377 | \sa toNumber()
|
|---|
| 1378 | */
|
|---|
| 1379 | bool QScriptValue::isNumber() const
|
|---|
| 1380 | {
|
|---|
| 1381 | Q_D(const QScriptValue);
|
|---|
| 1382 | return d && d->value.isNumber();
|
|---|
| 1383 | }
|
|---|
| 1384 |
|
|---|
| 1385 | /*!
|
|---|
| 1386 | Returns true if this QScriptValue is of the primitive type String;
|
|---|
| 1387 | otherwise returns false.
|
|---|
| 1388 |
|
|---|
| 1389 | \sa toString()
|
|---|
| 1390 | */
|
|---|
| 1391 | bool QScriptValue::isString() const
|
|---|
| 1392 | {
|
|---|
| 1393 | Q_D(const QScriptValue);
|
|---|
| 1394 | return d && d->value.isString();
|
|---|
| 1395 | }
|
|---|
| 1396 |
|
|---|
| 1397 | /*!
|
|---|
| 1398 | Returns true if this QScriptValue is a function; otherwise returns
|
|---|
| 1399 | false.
|
|---|
| 1400 |
|
|---|
| 1401 | \sa call()
|
|---|
| 1402 | */
|
|---|
| 1403 | bool QScriptValue::isFunction() const
|
|---|
| 1404 | {
|
|---|
| 1405 | Q_D(const QScriptValue);
|
|---|
| 1406 | return d && d->value.isFunction();
|
|---|
| 1407 | }
|
|---|
| 1408 |
|
|---|
| 1409 | /*!
|
|---|
| 1410 | Returns true if this QScriptValue is of the primitive type Null;
|
|---|
| 1411 | otherwise returns false.
|
|---|
| 1412 |
|
|---|
| 1413 | \sa QScriptEngine::nullValue()
|
|---|
| 1414 | */
|
|---|
| 1415 | bool QScriptValue::isNull() const
|
|---|
| 1416 | {
|
|---|
| 1417 | Q_D(const QScriptValue);
|
|---|
| 1418 | return d && d->value.isNull();
|
|---|
| 1419 | }
|
|---|
| 1420 |
|
|---|
| 1421 | /*!
|
|---|
| 1422 | Returns true if this QScriptValue is of the primitive type Undefined;
|
|---|
| 1423 | otherwise returns false.
|
|---|
| 1424 |
|
|---|
| 1425 | \sa QScriptEngine::undefinedValue()
|
|---|
| 1426 | */
|
|---|
| 1427 | bool QScriptValue::isUndefined() const
|
|---|
| 1428 | {
|
|---|
| 1429 | Q_D(const QScriptValue);
|
|---|
| 1430 | return d && d->value.isUndefined();
|
|---|
| 1431 | }
|
|---|
| 1432 |
|
|---|
| 1433 | /*!
|
|---|
| 1434 | Returns true if this QScriptValue is of the Object type; otherwise
|
|---|
| 1435 | returns false.
|
|---|
| 1436 |
|
|---|
| 1437 | Note that function values, variant values, and QObject values are
|
|---|
| 1438 | objects, so this function returns true for such values.
|
|---|
| 1439 |
|
|---|
| 1440 | \sa toObject(), QScriptEngine::newObject()
|
|---|
| 1441 | */
|
|---|
| 1442 | bool QScriptValue::isObject() const
|
|---|
| 1443 | {
|
|---|
| 1444 | Q_D(const QScriptValue);
|
|---|
| 1445 | return d && d->value.isObject();
|
|---|
| 1446 | }
|
|---|
| 1447 |
|
|---|
| 1448 | /*!
|
|---|
| 1449 | Returns true if this QScriptValue is a variant value;
|
|---|
| 1450 | otherwise returns false.
|
|---|
| 1451 |
|
|---|
| 1452 | \sa toVariant(), QScriptEngine::newVariant()
|
|---|
| 1453 | */
|
|---|
| 1454 | bool QScriptValue::isVariant() const
|
|---|
| 1455 | {
|
|---|
| 1456 | Q_D(const QScriptValue);
|
|---|
| 1457 | return d && d->value.isVariant();
|
|---|
| 1458 | }
|
|---|
| 1459 |
|
|---|
| 1460 | /*!
|
|---|
| 1461 | Returns true if this QScriptValue is a QObject; otherwise returns
|
|---|
| 1462 | false.
|
|---|
| 1463 |
|
|---|
| 1464 | Note: This function returns true even if the QObject that this
|
|---|
| 1465 | QScriptValue wraps has been deleted.
|
|---|
| 1466 |
|
|---|
| 1467 | \sa toQObject(), QScriptEngine::newQObject()
|
|---|
| 1468 | */
|
|---|
| 1469 | bool QScriptValue::isQObject() const
|
|---|
| 1470 | {
|
|---|
| 1471 | Q_D(const QScriptValue);
|
|---|
| 1472 | return d && d->value.isQObject();
|
|---|
| 1473 | }
|
|---|
| 1474 |
|
|---|
| 1475 | /*!
|
|---|
| 1476 | Returns true if this QScriptValue is a QMetaObject; otherwise returns
|
|---|
| 1477 | false.
|
|---|
| 1478 |
|
|---|
| 1479 | \sa toQMetaObject(), QScriptEngine::newQMetaObject()
|
|---|
| 1480 | */
|
|---|
| 1481 | bool QScriptValue::isQMetaObject() const
|
|---|
| 1482 | {
|
|---|
| 1483 | Q_D(const QScriptValue);
|
|---|
| 1484 | return d && d->value.isQMetaObject();
|
|---|
| 1485 | }
|
|---|
| 1486 |
|
|---|
| 1487 | /*!
|
|---|
| 1488 | Returns true if this QScriptValue is valid; otherwise returns
|
|---|
| 1489 | false.
|
|---|
| 1490 | */
|
|---|
| 1491 | bool QScriptValue::isValid() const
|
|---|
| 1492 | {
|
|---|
| 1493 | Q_D(const QScriptValue);
|
|---|
| 1494 | return d && d->value.isValid();
|
|---|
| 1495 | }
|
|---|
| 1496 |
|
|---|
| 1497 | /*!
|
|---|
| 1498 | \since 4.4
|
|---|
| 1499 |
|
|---|
| 1500 | Returns the internal data of this QScriptValue object. QtScript uses
|
|---|
| 1501 | this property to store the primitive value of Date, String, Number
|
|---|
| 1502 | and Boolean objects. For other types of object, custom data may be
|
|---|
| 1503 | stored using setData().
|
|---|
| 1504 | */
|
|---|
| 1505 | QScriptValue QScriptValue::data() const
|
|---|
| 1506 | {
|
|---|
| 1507 | Q_D(const QScriptValue);
|
|---|
| 1508 | if (!d || !d->value.isObject())
|
|---|
| 1509 | return QScriptValue();
|
|---|
| 1510 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1511 | return eng->toPublic(d->value.internalValue());
|
|---|
| 1512 | }
|
|---|
| 1513 |
|
|---|
| 1514 | /*!
|
|---|
| 1515 | \since 4.4
|
|---|
| 1516 |
|
|---|
| 1517 | Sets the internal \a data of this QScriptValue object. You can use
|
|---|
| 1518 | this function to set object-specific data that won't be directly
|
|---|
| 1519 | accessible to scripts, but may be retrieved in C++ using the data()
|
|---|
| 1520 | function.
|
|---|
| 1521 | */
|
|---|
| 1522 | void QScriptValue::setData(const QScriptValue &data)
|
|---|
| 1523 | {
|
|---|
| 1524 | Q_D(QScriptValue);
|
|---|
| 1525 | if (!d || !d->value.isObject())
|
|---|
| 1526 | return;
|
|---|
| 1527 | QScriptEnginePrivate *eng = QScriptEnginePrivate::get(engine());
|
|---|
| 1528 | d->value.setInternalValue(eng->toImpl(data));
|
|---|
| 1529 | }
|
|---|
| 1530 |
|
|---|
| 1531 | /*!
|
|---|
| 1532 | \since 4.4
|
|---|
| 1533 |
|
|---|
| 1534 | Returns the custom script class that this script object is an
|
|---|
| 1535 | instance of, or 0 if the object is not of a custom class.
|
|---|
| 1536 |
|
|---|
| 1537 | \sa setScriptClass()
|
|---|
| 1538 | */
|
|---|
| 1539 | QScriptClass *QScriptValue::scriptClass() const
|
|---|
| 1540 | {
|
|---|
| 1541 | Q_D(const QScriptValue);
|
|---|
| 1542 | if (!d || !d->value.isObject())
|
|---|
| 1543 | return 0;
|
|---|
| 1544 | QScriptClassInfo *info = d->value.classInfo();
|
|---|
| 1545 | if ((info->type() & QScriptClassInfo::TypeMask) < QScriptClassInfo::CustomType)
|
|---|
| 1546 | return 0;
|
|---|
| 1547 | return QScriptClassPrivate::classFromInfo(info);
|
|---|
| 1548 | }
|
|---|
| 1549 |
|
|---|
| 1550 | /*!
|
|---|
| 1551 | \since 4.4
|
|---|
| 1552 |
|
|---|
| 1553 | Sets the custom script class of this script object to \a scriptClass.
|
|---|
| 1554 | This can be used to "promote" a plain script object (e.g. created
|
|---|
| 1555 | by the "new" operator in a script, or by QScriptEngine::newObject() in C++)
|
|---|
| 1556 | to an object of a custom type.
|
|---|
| 1557 |
|
|---|
| 1558 | If \a scriptClass is 0, the object will be demoted to a plain
|
|---|
| 1559 | script object.
|
|---|
| 1560 |
|
|---|
| 1561 | \sa scriptClass(), setData()
|
|---|
| 1562 | */
|
|---|
| 1563 | void QScriptValue::setScriptClass(QScriptClass *scriptClass)
|
|---|
| 1564 | {
|
|---|
| 1565 | Q_D(QScriptValue);
|
|---|
| 1566 | if (!d || !d->value.isObject())
|
|---|
| 1567 | return;
|
|---|
| 1568 | if (!scriptClass) {
|
|---|
| 1569 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine());
|
|---|
| 1570 | d->value.setClassInfo(eng_p->m_class_object);
|
|---|
| 1571 | } else {
|
|---|
| 1572 | QScriptClassPrivate *cls_p = QScriptClassPrivate::get(scriptClass);
|
|---|
| 1573 | d->value.setClassInfo(cls_p->classInfo());
|
|---|
| 1574 | }
|
|---|
| 1575 | }
|
|---|
| 1576 |
|
|---|
| 1577 | /*!
|
|---|
| 1578 | \internal
|
|---|
| 1579 |
|
|---|
| 1580 | Returns the ID of this object, or -1 if this QScriptValue is not an
|
|---|
| 1581 | object.
|
|---|
| 1582 |
|
|---|
| 1583 | \sa QScriptEngine::objectById()
|
|---|
| 1584 | */
|
|---|
| 1585 | qint64 QScriptValue::objectId() const
|
|---|
| 1586 | {
|
|---|
| 1587 | Q_D(const QScriptValue);
|
|---|
| 1588 | if (!d || !d->value.isObject())
|
|---|
| 1589 | return -1;
|
|---|
| 1590 | return d->value.m_object_value->m_id;
|
|---|
| 1591 | }
|
|---|
| 1592 |
|
|---|
| 1593 | QT_END_NAMESPACE
|
|---|
| 1594 |
|
|---|
| 1595 | #endif // QT_NO_SCRIPT
|
|---|