1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtScript module of the Qt Toolkit.
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.
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
22 ****************************************************************************/
24 #include "qscriptisolate_p.h"
25 #include "qjsengine.h"
26 #include "qv8engine_p.h"
28 #include "qjsvalue_p.h"
29 #include "qscript_impl_p.h"
30 #include "qscriptshareddata_p.h"
31 #include <QtCore/qstring.h>
37 \brief The QJSValue class acts as a container for Qt/JavaScript data types.
42 QJSValue supports the types defined in the \l{ECMA-262}
43 standard: The primitive types, which are Undefined, Null, Boolean,
44 Number, and String; and the Object type. Additionally, built-in
45 support is provided for Qt/C++ types such as QVariant and QObject.
47 For the object-based types (including Date and RegExp), use the
48 newT() functions in QJSEngine (e.g. QJSEngine::newObject())
49 to create a QJSValue of the desired type. For the primitive types,
50 use one of the QJSValue constructor overloads.
52 The methods named isT() (e.g. isBool(), isUndefined()) can be
53 used to test if a value is of a certain type. The methods named
54 toT() (e.g. toBool(), toString()) can be used to convert a
55 QJSValue to another type. You can also use the generic
56 QJSValue_cast() function.
58 Object values have zero or more properties which are themselves
59 QJSValues. Use setProperty() to set a property of an object, and
60 call property() to retrieve the value of a property.
62 \snippet doc/src/snippets/code/src_script_qjsvalue.cpp 0
64 If you want to iterate over the properties of a script object, use
65 the QJSValueIterator class.
67 Object values have an internal \c{prototype} property, which can be
68 accessed with prototype() and setPrototype().
70 Function objects (objects for which isCallable()) returns true) can
71 be invoked by calling call(). Constructor functions can be used to
72 construct new objects by calling callAsConstructor().
74 Use equals() or strictlyEquals() to compare a QJSValue to another.
76 Note that a QJSValue for which isObject() is true only carries a
77 reference to an actual object; copying the QJSValue will only
78 copy the object reference, not the object itself. If you want to
79 clone an object (i.e. copy an object's properties to another
80 object), you can do so with the help of a \c{for-in} statement in
81 script code, or QJSValueIterator in C++.
83 \sa QJSEngine, QJSValueIterator
87 \enum QJSValue::SpecialValue
89 This enum is used to specify a single-valued type.
91 \value UndefinedValue An undefined value.
93 \value NullValue A null value.
99 Constructs a new QJSValue with a boolean \a value.
101 QJSValue::QJSValue(bool value)
102 : d_ptr(new QJSValuePrivate(value))
107 Constructs a new QJSValue with a number \a value.
109 QJSValue::QJSValue(int value)
110 : d_ptr(new QJSValuePrivate(value))
115 Constructs a new QJSValue with a number \a value.
117 QJSValue::QJSValue(uint value)
118 : d_ptr(new QJSValuePrivate(value))
123 Constructs a new QJSValue with a number \a value.
125 QJSValue::QJSValue(double value)
126 : d_ptr(new QJSValuePrivate(value))
131 Constructs a new QJSValue with a string \a value.
133 QJSValue::QJSValue(const QString& value)
134 : d_ptr(new QJSValuePrivate(value))
139 Constructs a new QJSValue with a special \a value.
141 QJSValue::QJSValue(SpecialValue value)
142 : d_ptr(new QJSValuePrivate(value))
147 Constructs a new QJSValue with a string \a value.
149 QJSValue::QJSValue(const QLatin1String &value)
150 : d_ptr(new QJSValuePrivate(value))
155 Constructs a new QJSValue with a string \a value.
157 #ifndef QT_NO_CAST_FROM_ASCII
158 QJSValue::QJSValue(const char *value)
159 : d_ptr(new QJSValuePrivate(QString::fromAscii(value)))
165 Block automatic convertion to bool
168 QJSValue::QJSValue(void* d)
175 Constructs a new QJSValue from private
178 QJSValue::QJSValue(QJSValuePrivate* d)
184 Constructs a new QJSValue from private
187 QJSValue::QJSValue(QScriptPassPointer<QJSValuePrivate> d)
193 Constructs a new QJSValue that is a copy of \a other.
195 Note that if \a other is an object (i.e., isObject() would return
196 true), then only a reference to the underlying object is copied into
197 the new script value (i.e., the object itself is not copied).
199 QJSValue::QJSValue(const QJSValue& other)
205 Destroys this QJSValue.
207 QJSValue::~QJSValue()
212 Returns true if this QJSValue is of the primitive type Boolean;
213 otherwise returns false.
217 bool QJSValue::isBool() const
220 QScriptIsolate api(d->engine());
225 Returns true if this QJSValue is of the primitive type Number;
226 otherwise returns false.
230 bool QJSValue::isNumber() const
233 QScriptIsolate api(d->engine());
234 return d->isNumber();
238 Returns true if this QJSValue is of the primitive type Null;
239 otherwise returns false.
241 bool QJSValue::isNull() const
244 QScriptIsolate api(d->engine());
249 Returns true if this QJSValue is of the primitive type String;
250 otherwise returns false.
254 bool QJSValue::isString() const
257 QScriptIsolate api(d->engine());
258 return d->isString();
262 Returns true if this QJSValue is of the primitive type Undefined;
263 otherwise returns false.
265 \sa QJSEngine::undefinedValue()
267 bool QJSValue::isUndefined() const
270 QScriptIsolate api(d->engine());
271 return d->isUndefined();
275 Returns true if this QJSValue is an object of the Error class;
276 otherwise returns false.
278 bool QJSValue::isError() const
281 QScriptIsolate api(d->engine());
286 Returns true if this QJSValue is an object of the Array class;
287 otherwise returns false.
289 \sa QJSEngine::newArray()
291 bool QJSValue::isArray() const
294 QScriptIsolate api(d->engine());
299 Returns true if this QJSValue is of the Object type; otherwise
302 Note that function values, variant values, and QObject values are
303 objects, so this function returns true for such values.
305 \sa QJSEngine::newObject()
307 bool QJSValue::isObject() const
310 QScriptIsolate api(d->engine());
311 return d->isObject();
315 Returns true if this QJSValue can be called a function, otherwise
320 bool QJSValue::isCallable() const
323 QScriptIsolate api(d->engine());
324 return d->isCallable();
332 Use isCallable() instead.
334 bool QJSValue::isFunction() const
337 QScriptIsolate api(d->engine());
338 return d->isCallable();
341 #endif // QT_DEPRECATED
344 Returns true if this QJSValue is a variant value;
345 otherwise returns false.
349 bool QJSValue::isVariant() const
352 QScriptIsolate api(d->engine());
353 return d->isVariant();
357 Returns the string value of this QJSValue, as defined in
358 \l{ECMA-262} section 9.8, "ToString".
360 Note that if this QJSValue is an object, calling this function
361 has side effects on the script engine, since the engine will call
362 the object's toString() function (and possibly valueOf()) in an
363 attempt to convert the object to a primitive value (possibly
364 resulting in an uncaught script exception).
368 QString QJSValue::toString() const
371 QScriptIsolate api(d->engine());
372 return d->toString();
376 Returns the number value of this QJSValue, as defined in
377 \l{ECMA-262} section 9.3, "ToNumber".
379 Note that if this QJSValue is an object, calling this function
380 has side effects on the script engine, since the engine will call
381 the object's valueOf() function (and possibly toString()) in an
382 attempt to convert the object to a primitive value (possibly
383 resulting in an uncaught script exception).
385 \sa isNumber(), toInt(), toUInt()
387 double QJSValue::toNumber() const
390 QScriptIsolate api(d->engine());
391 return d->toNumber();
395 Returns the boolean value of this QJSValue, using the conversion
396 rules described in \l{ECMA-262} section 9.2, "ToBoolean".
398 Note that if this QJSValue is an object, calling this function
399 has side effects on the script engine, since the engine will call
400 the object's valueOf() function (and possibly toString()) in an
401 attempt to convert the object to a primitive value (possibly
402 resulting in an uncaught script exception).
406 bool QJSValue::toBool() const
409 QScriptIsolate api(d->engine());
414 Returns the signed 32-bit integer value of this QJSValue, using
415 the conversion rules described in \l{ECMA-262} section 9.5, "ToInt32".
417 Note that if this QJSValue is an object, calling this function
418 has side effects on the script engine, since the engine will call
419 the object's valueOf() function (and possibly toString()) in an
420 attempt to convert the object to a primitive value (possibly
421 resulting in an uncaught script exception).
423 \sa toNumber(), toUInt()
425 qint32 QJSValue::toInt() const
428 QScriptIsolate api(d->engine());
433 Returns the unsigned 32-bit integer value of this QJSValue, using
434 the conversion rules described in \l{ECMA-262} section 9.6, "ToUint32".
436 Note that if this QJSValue is an object, calling this function
437 has side effects on the script engine, since the engine will call
438 the object's valueOf() function (and possibly toString()) in an
439 attempt to convert the object to a primitive value (possibly
440 resulting in an uncaught script exception).
442 \sa toNumber(), toInt()
444 quint32 QJSValue::toUInt() const
447 QScriptIsolate api(d->engine());
448 return d->toUInt32();
458 qint32 QJSValue::toInt32() const
461 QScriptIsolate api(d->engine());
468 Use toUInt() instead.
470 quint32 QJSValue::toUInt32() const
473 QScriptIsolate api(d->engine());
474 return d->toUInt32();
477 #endif // QT_DEPRECATED
480 Returns the QVariant value of this QJSValue, if it can be
481 converted to a QVariant; otherwise returns an invalid QVariant.
482 The conversion is performed according to the following table:
485 \header \o Input Type \o Result
486 \row \o Undefined \o An invalid QVariant.
487 \row \o Null \o An invalid QVariant.
488 \row \o Boolean \o A QVariant containing the value of the boolean.
489 \row \o Number \o A QVariant containing the value of the number.
490 \row \o String \o A QVariant containing the value of the string.
491 \row \o QVariant Object \o The result is the QVariant value of the object (no conversion).
492 \row \o QObject Object \o A QVariant containing a pointer to the QObject.
493 \row \o Date Object \o A QVariant containing the date value (toDateTime()).
494 \row \o RegExp Object \o A QVariant containing the regular expression value.
495 \row \o Array Object \o The array is converted to a QVariantList. Each element is converted to a QVariant, recursively; cyclic references are not followed.
496 \row \o Object \o The object is converted to a QVariantMap. Each property is converted to a QVariant, recursively; cyclic references are not followed.
501 QVariant QJSValue::toVariant() const
504 QScriptIsolate api(d->engine());
505 return d->toVariant();
509 Calls this QJSValue as a function, passing \a args as arguments
510 to the function, and using the globalObject() as the "this"-object.
511 Returns the value returned from the function.
513 If this QJSValue is not callable, call() does nothing and
514 returns an undefined QJSValue.
516 Calling call() can cause an exception to occur in the script engine;
517 in that case, call() returns the value that was thrown (typically an
518 \c{Error} object). You can call
519 QJSEngine::hasUncaughtException() to determine if an exception
522 \sa isCallable(), callWithInstance(), callAsConstructor()
524 QJSValue QJSValue::call(const QJSValueList &args)
527 QScriptIsolate api(d->engine());
528 return d->call(/*thisObject=*/0, args);
532 Calls this QJSValue as a function, using \a instance as
533 the `this' object in the function call, and passing \a args
534 as arguments to the function. Returns the value returned from
537 If this QJSValue is not a function, call() does nothing
538 and returns an undefined QJSValue.
540 Note that if \a instance is not an object, the global object
541 (see \l{QJSEngine::globalObject()}) will be used as the
544 Calling call() can cause an exception to occur in the script engine;
545 in that case, call() returns the value that was thrown (typically an
546 \c{Error} object). You can call
547 QJSEngine::hasUncaughtException() to determine if an exception
552 QJSValue QJSValue::callWithInstance(const QJSValue &instance, const QJSValueList &args)
555 QScriptIsolate api(d->engine());
556 return d->call(QJSValuePrivate::get(instance), args);
560 Creates a new \c{Object} and calls this QJSValue as a
561 constructor, using the created object as the `this' object and
562 passing \a args as arguments. If the return value from the
563 constructor call is an object, then that object is returned;
564 otherwise the default constructed object is returned.
566 If this QJSValue is not a function, callAsConstructor() does
567 nothing and returns an undefined QJSValue.
569 Calling this function can cause an exception to occur in the
570 script engine; in that case, the value that was thrown
571 (typically an \c{Error} object) is returned. You can call
572 QJSEngine::hasUncaughtException() to determine if an exception
575 \sa call(), QJSEngine::newObject()
577 QJSValue QJSValue::callAsConstructor(const QJSValueList &args)
580 QScriptIsolate api(d->engine());
581 return QJSValuePrivate::get(d->callAsConstructor(args));
589 Use callWithInstance() instead.
591 QJSValue QJSValue::call(const QJSValue& thisObject, const QJSValueList& args)
594 QScriptIsolate api(d->engine());
595 return d->call(QJSValuePrivate::get(thisObject), args);
601 Use callAsConstructor() instead.
603 QJSValue QJSValue::construct(const QJSValueList &args)
606 QScriptIsolate api(d->engine());
607 return QJSValuePrivate::get(d->callAsConstructor(args));
613 Returns the QJSEngine that created this QJSValue,
614 or 0 if this QJSValue is invalid or the value is not
615 associated with a particular engine.
617 QJSEngine* QJSValue::engine() const
620 QScriptIsolate api(d->engine());
621 QV8Engine* engine = d->engine();
623 return QV8Engine::get(engine);
627 #endif // QT_DEPRECATED
630 If this QJSValue is an object, returns the internal prototype
631 (\c{__proto__} property) of this object; otherwise returns an
634 \sa setPrototype(), isObject()
636 QJSValue QJSValue::prototype() const
639 QScriptIsolate api(d->engine());
640 return QJSValuePrivate::get(d->prototype());
644 If this QJSValue is an object, sets the internal prototype
645 (\c{__proto__} property) of this object to be \a prototype;
646 otherwise does nothing.
648 The internal prototype should not be confused with the public
649 property with name "prototype"; the public prototype is usually
650 only set on functions that act as constructors.
652 \sa prototype(), isObject()
654 void QJSValue::setPrototype(const QJSValue& prototype)
657 QScriptIsolate api(d->engine());
658 d->setPrototype(QJSValuePrivate::get(prototype));
662 Assigns the \a other value to this QJSValue.
664 Note that if \a other is an object (isObject() returns true),
665 only a reference to the underlying object will be assigned;
666 the object itself will not be copied.
668 QJSValue& QJSValue::operator=(const QJSValue& other)
675 Returns true if this QJSValue is equal to \a other, otherwise
676 returns false. The comparison follows the behavior described in
677 \l{ECMA-262} section 11.9.3, "The Abstract Equality Comparison
680 This function can return true even if the type of this QJSValue
681 is different from the type of the \a other value; i.e. the
682 comparison is not strict. For example, comparing the number 9 to
683 the string "9" returns true; comparing an undefined value to a null
684 value returns true; comparing a \c{Number} object whose primitive
685 value is 6 to a \c{String} object whose primitive value is "6"
686 returns true; and comparing the number 1 to the boolean value
687 \c{true} returns true. If you want to perform a comparison
688 without such implicit value conversion, use strictlyEquals().
690 Note that if this QJSValue or the \a other value are objects,
691 calling this function has side effects on the script engine, since
692 the engine will call the object's valueOf() function (and possibly
693 toString()) in an attempt to convert the object to a primitive value
694 (possibly resulting in an uncaught script exception).
698 bool QJSValue::equals(const QJSValue& other) const
701 QJSValuePrivate* otherValue = QJSValuePrivate::get(other);
702 QScriptIsolate api(d->engine() ? d->engine() : otherValue->engine());
703 return d_ptr->equals(otherValue);
707 Returns true if this QJSValue is equal to \a other using strict
708 comparison (no conversion), otherwise returns false. The comparison
709 follows the behavior described in \l{ECMA-262} section 11.9.6, "The
710 Strict Equality Comparison Algorithm".
712 If the type of this QJSValue is different from the type of the
713 \a other value, this function returns false. If the types are equal,
714 the result depends on the type, as shown in the following table:
717 \header \o Type \o Result
718 \row \o Undefined \o true
720 \row \o Boolean \o true if both values are true, false otherwise
721 \row \o Number \o false if either value is NaN (Not-a-Number); true if values are equal, false otherwise
722 \row \o String \o true if both values are exactly the same sequence of characters, false otherwise
723 \row \o Object \o true if both values refer to the same object, false otherwise
728 bool QJSValue::strictlyEquals(const QJSValue& other) const
731 QJSValuePrivate* o = QJSValuePrivate::get(other);
732 QScriptIsolate api(d->engine() ? d->engine() : o->engine());
733 return d_ptr->strictlyEquals(o);
737 Returns the value of this QJSValue's property with the given \a name.
738 If no such property exists, an undefined QJSValue is returned.
740 If the property is implemented using a getter function (i.e. has the
741 PropertyGetter flag set), calling property() has side-effects on the
742 script engine, since the getter function will be called (possibly
743 resulting in an uncaught script exception). If an exception
744 occurred, property() returns the value that was thrown (typically
745 an \c{Error} object).
747 \sa setProperty(), hasProperty(), QJSValueIterator
749 QJSValue QJSValue::property(const QString& name) const
752 QScriptIsolate api(d->engine());
753 return QJSValuePrivate::get(d->property(name));
759 Returns the property at the given \a arrayIndex.
761 This function is provided for convenience and performance when
762 working with array objects.
764 If this QJSValue is not an Array object, this function behaves
765 as if property() was called with the string representation of \a
768 QJSValue QJSValue::property(quint32 arrayIndex) const
771 QScriptIsolate api(d->engine());
772 return QJSValuePrivate::get(d->property(arrayIndex));
776 Sets the value of this QJSValue's property with the given \a name to
779 If this QJSValue is not an object, this function does nothing.
781 If this QJSValue does not already have a property with name \a name,
782 a new property is created.
784 \sa property(), deleteProperty()
786 void QJSValue::setProperty(const QString& name, const QJSValue& value)
789 QScriptIsolate api(d->engine());
790 d->setProperty(name, QJSValuePrivate::get(value));
796 Sets the property at the given \a arrayIndex to the given \a value.
798 This function is provided for convenience and performance when
799 working with array objects.
801 If this QJSValue is not an Array object, this function behaves
802 as if setProperty() was called with the string representation of \a
805 void QJSValue::setProperty(quint32 arrayIndex, const QJSValue& value)
808 QScriptIsolate api(d->engine());
809 d->setProperty(arrayIndex, QJSValuePrivate::get(value));
813 Attempts to delete this object's property of the given \a name.
814 Returns true if the property was deleted, otherwise returns false.
816 The behavior of this function is consistent with the JavaScript
817 delete operator. In particular:
820 \o Non-configurable properties cannot be deleted.
821 \o This function will return true even if this object doesn't
822 have a property of the given \a name (i.e., non-existent
823 properties are "trivially deletable").
824 \o If this object doesn't have an own property of the given
825 \a name, but an object in the prototype() chain does, the
826 prototype object's property is not deleted, and this function
830 \sa setProperty(), hasOwnProperty()
832 bool QJSValue::deleteProperty(const QString &name)
835 QScriptIsolate api(d->engine());
836 return d->deleteProperty(name);
840 Returns true if this object has a property of the given \a name,
841 otherwise returns false.
843 \sa property(), hasOwnProperty()
845 bool QJSValue::hasProperty(const QString &name) const
848 QScriptIsolate api(d->engine());
849 return d->hasProperty(name);
853 Returns true if this object has an own (not prototype-inherited)
854 property of the given \a name, otherwise returns false.
856 \sa property(), hasProperty()
858 bool QJSValue::hasOwnProperty(const QString &name) const
861 QScriptIsolate api(d->engine());
862 return d->hasOwnProperty(name);
866 * If this QJSValue is a QObject, returns the QObject pointer
867 * that the QJSValue represents; otherwise, returns 0.
869 * If the QObject that this QJSValue wraps has been deleted,
870 * this function returns 0 (i.e. it is possible for toQObject()
871 * to return 0 even when isQObject() returns true).
875 QObject *QJSValue::toQObject() const
878 QScriptIsolate api(d->engine());
879 return d->toQObject();
883 Returns a QDateTime representation of this value, in local time.
884 If this QJSValue is not a date, or the value of the date is NaN
885 (Not-a-Number), an invalid QDateTime is returned.
889 QDateTime QJSValue::toDateTime() const
892 QScriptIsolate api(d->engine());
893 return d->toDataTime();
897 Returns true if this QJSValue is an object of the Date class;
898 otherwise returns false.
900 \sa QJSEngine::newDate()
902 bool QJSValue::isDate() const
905 QScriptIsolate api(d->engine());
910 Returns true if this QJSValue is an object of the RegExp class;
911 otherwise returns false.
913 bool QJSValue::isRegExp() const
916 QScriptIsolate api(d->engine());
917 return d->isRegExp();
921 Returns true if this QJSValue is a QObject; otherwise returns
924 Note: This function returns true even if the QObject that this
925 QJSValue wraps has been deleted.
927 \sa toQObject(), QJSEngine::newQObject()
929 bool QJSValue::isQObject() const
932 QScriptIsolate api(d->engine());
933 return d->isQObject();