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();
452 Returns the QVariant value of this QJSValue, if it can be
453 converted to a QVariant; otherwise returns an invalid QVariant.
454 The conversion is performed according to the following table:
457 \header \o Input Type \o Result
458 \row \o Undefined \o An invalid QVariant.
459 \row \o Null \o An invalid QVariant.
460 \row \o Boolean \o A QVariant containing the value of the boolean.
461 \row \o Number \o A QVariant containing the value of the number.
462 \row \o String \o A QVariant containing the value of the string.
463 \row \o QVariant Object \o The result is the QVariant value of the object (no conversion).
464 \row \o QObject Object \o A QVariant containing a pointer to the QObject.
465 \row \o Date Object \o A QVariant containing the date value (toDateTime()).
466 \row \o RegExp Object \o A QVariant containing the regular expression value.
467 \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.
468 \row \o Object \o The object is converted to a QVariantMap. Each property is converted to a QVariant, recursively; cyclic references are not followed.
473 QVariant QJSValue::toVariant() const
476 QScriptIsolate api(d->engine());
477 return d->toVariant();
481 Calls this QJSValue as a function, passing \a args as arguments
482 to the function, and using the globalObject() as the "this"-object.
483 Returns the value returned from the function.
485 If this QJSValue is not callable, call() does nothing and
486 returns an undefined QJSValue.
488 Calling call() can cause an exception to occur in the script engine;
489 in that case, call() returns the value that was thrown (typically an
490 \c{Error} object). You can call
491 QJSEngine::hasUncaughtException() to determine if an exception
494 \sa isCallable(), callWithInstance(), callAsConstructor()
496 QJSValue QJSValue::call(const QJSValueList &args)
499 QScriptIsolate api(d->engine());
500 return d->call(/*thisObject=*/0, args);
504 Calls this QJSValue as a function, using \a instance as
505 the `this' object in the function call, and passing \a args
506 as arguments to the function. Returns the value returned from
509 If this QJSValue is not a function, call() does nothing
510 and returns an undefined QJSValue.
512 Note that if \a instance is not an object, the global object
513 (see \l{QJSEngine::globalObject()}) will be used as the
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
524 QJSValue QJSValue::callWithInstance(const QJSValue &instance, const QJSValueList &args)
527 QScriptIsolate api(d->engine());
528 return d->call(QJSValuePrivate::get(instance), args);
532 Creates a new \c{Object} and calls this QJSValue as a
533 constructor, using the created object as the `this' object and
534 passing \a args as arguments. If the return value from the
535 constructor call is an object, then that object is returned;
536 otherwise the default constructed object is returned.
538 If this QJSValue is not a function, callAsConstructor() does
539 nothing and returns an undefined QJSValue.
541 Calling this function can cause an exception to occur in the
542 script engine; in that case, the value that was thrown
543 (typically an \c{Error} object) is returned. You can call
544 QJSEngine::hasUncaughtException() to determine if an exception
547 \sa call(), QJSEngine::newObject()
549 QJSValue QJSValue::callAsConstructor(const QJSValueList &args)
552 QScriptIsolate api(d->engine());
553 return QJSValuePrivate::get(d->callAsConstructor(args));
561 Use callWithInstance() instead.
563 QJSValue QJSValue::call(const QJSValue& thisObject, const QJSValueList& args)
566 QScriptIsolate api(d->engine());
567 return d->call(QJSValuePrivate::get(thisObject), args);
573 Use callAsConstructor() instead.
575 QJSValue QJSValue::construct(const QJSValueList &args)
578 QScriptIsolate api(d->engine());
579 return QJSValuePrivate::get(d->callAsConstructor(args));
585 Returns the QJSEngine that created this QJSValue,
586 or 0 if this QJSValue is invalid or the value is not
587 associated with a particular engine.
589 QJSEngine* QJSValue::engine() const
592 QScriptIsolate api(d->engine());
593 QV8Engine* engine = d->engine();
595 return QV8Engine::get(engine);
599 #endif // QT_DEPRECATED
602 If this QJSValue is an object, returns the internal prototype
603 (\c{__proto__} property) of this object; otherwise returns an
606 \sa setPrototype(), isObject()
608 QJSValue QJSValue::prototype() const
611 QScriptIsolate api(d->engine());
612 return QJSValuePrivate::get(d->prototype());
616 If this QJSValue is an object, sets the internal prototype
617 (\c{__proto__} property) of this object to be \a prototype;
618 otherwise does nothing.
620 The internal prototype should not be confused with the public
621 property with name "prototype"; the public prototype is usually
622 only set on functions that act as constructors.
624 \sa prototype(), isObject()
626 void QJSValue::setPrototype(const QJSValue& prototype)
629 QScriptIsolate api(d->engine());
630 d->setPrototype(QJSValuePrivate::get(prototype));
634 Assigns the \a other value to this QJSValue.
636 Note that if \a other is an object (isObject() returns true),
637 only a reference to the underlying object will be assigned;
638 the object itself will not be copied.
640 QJSValue& QJSValue::operator=(const QJSValue& other)
647 Returns true if this QJSValue is equal to \a other, otherwise
648 returns false. The comparison follows the behavior described in
649 \l{ECMA-262} section 11.9.3, "The Abstract Equality Comparison
652 This function can return true even if the type of this QJSValue
653 is different from the type of the \a other value; i.e. the
654 comparison is not strict. For example, comparing the number 9 to
655 the string "9" returns true; comparing an undefined value to a null
656 value returns true; comparing a \c{Number} object whose primitive
657 value is 6 to a \c{String} object whose primitive value is "6"
658 returns true; and comparing the number 1 to the boolean value
659 \c{true} returns true. If you want to perform a comparison
660 without such implicit value conversion, use strictlyEquals().
662 Note that if this QJSValue or the \a other value are objects,
663 calling this function has side effects on the script engine, since
664 the engine will call the object's valueOf() function (and possibly
665 toString()) in an attempt to convert the object to a primitive value
666 (possibly resulting in an uncaught script exception).
670 bool QJSValue::equals(const QJSValue& other) const
673 QJSValuePrivate* otherValue = QJSValuePrivate::get(other);
674 QScriptIsolate api(d->engine() ? d->engine() : otherValue->engine());
675 return d_ptr->equals(otherValue);
679 Returns true if this QJSValue is equal to \a other using strict
680 comparison (no conversion), otherwise returns false. The comparison
681 follows the behavior described in \l{ECMA-262} section 11.9.6, "The
682 Strict Equality Comparison Algorithm".
684 If the type of this QJSValue is different from the type of the
685 \a other value, this function returns false. If the types are equal,
686 the result depends on the type, as shown in the following table:
689 \header \o Type \o Result
690 \row \o Undefined \o true
692 \row \o Boolean \o true if both values are true, false otherwise
693 \row \o Number \o false if either value is NaN (Not-a-Number); true if values are equal, false otherwise
694 \row \o String \o true if both values are exactly the same sequence of characters, false otherwise
695 \row \o Object \o true if both values refer to the same object, false otherwise
700 bool QJSValue::strictlyEquals(const QJSValue& other) const
703 QJSValuePrivate* o = QJSValuePrivate::get(other);
704 QScriptIsolate api(d->engine() ? d->engine() : o->engine());
705 return d_ptr->strictlyEquals(o);
709 Returns the value of this QJSValue's property with the given \a name.
710 If no such property exists, an undefined QJSValue is returned.
712 If the property is implemented using a getter function (i.e. has the
713 PropertyGetter flag set), calling property() has side-effects on the
714 script engine, since the getter function will be called (possibly
715 resulting in an uncaught script exception). If an exception
716 occurred, property() returns the value that was thrown (typically
717 an \c{Error} object).
719 \sa setProperty(), hasProperty(), QJSValueIterator
721 QJSValue QJSValue::property(const QString& name) const
724 QScriptIsolate api(d->engine());
725 return QJSValuePrivate::get(d->property(name));
731 Returns the property at the given \a arrayIndex.
733 This function is provided for convenience and performance when
734 working with array objects.
736 If this QJSValue is not an Array object, this function behaves
737 as if property() was called with the string representation of \a
740 QJSValue QJSValue::property(quint32 arrayIndex) const
743 QScriptIsolate api(d->engine());
744 return QJSValuePrivate::get(d->property(arrayIndex));
748 Sets the value of this QJSValue's property with the given \a name to
751 If this QJSValue is not an object, this function does nothing.
753 If this QJSValue does not already have a property with name \a name,
754 a new property is created.
756 \sa property(), deleteProperty()
758 void QJSValue::setProperty(const QString& name, const QJSValue& value)
761 QScriptIsolate api(d->engine());
762 d->setProperty(name, QJSValuePrivate::get(value));
768 Sets the property at the given \a arrayIndex to the given \a value.
770 This function is provided for convenience and performance when
771 working with array objects.
773 If this QJSValue is not an Array object, this function behaves
774 as if setProperty() was called with the string representation of \a
777 void QJSValue::setProperty(quint32 arrayIndex, const QJSValue& value)
780 QScriptIsolate api(d->engine());
781 d->setProperty(arrayIndex, QJSValuePrivate::get(value));
785 Attempts to delete this object's property of the given \a name.
786 Returns true if the property was deleted, otherwise returns false.
788 The behavior of this function is consistent with the JavaScript
789 delete operator. In particular:
792 \o Non-configurable properties cannot be deleted.
793 \o This function will return true even if this object doesn't
794 have a property of the given \a name (i.e., non-existent
795 properties are "trivially deletable").
796 \o If this object doesn't have an own property of the given
797 \a name, but an object in the prototype() chain does, the
798 prototype object's property is not deleted, and this function
802 \sa setProperty(), hasOwnProperty()
804 bool QJSValue::deleteProperty(const QString &name)
807 QScriptIsolate api(d->engine());
808 return d->deleteProperty(name);
812 Returns true if this object has a property of the given \a name,
813 otherwise returns false.
815 \sa property(), hasOwnProperty()
817 bool QJSValue::hasProperty(const QString &name) const
820 QScriptIsolate api(d->engine());
821 return d->hasProperty(name);
825 Returns true if this object has an own (not prototype-inherited)
826 property of the given \a name, otherwise returns false.
828 \sa property(), hasProperty()
830 bool QJSValue::hasOwnProperty(const QString &name) const
833 QScriptIsolate api(d->engine());
834 return d->hasOwnProperty(name);
838 * If this QJSValue is a QObject, returns the QObject pointer
839 * that the QJSValue represents; otherwise, returns 0.
841 * If the QObject that this QJSValue wraps has been deleted,
842 * this function returns 0 (i.e. it is possible for toQObject()
843 * to return 0 even when isQObject() returns true).
847 QObject *QJSValue::toQObject() const
850 QScriptIsolate api(d->engine());
851 return d->toQObject();
855 Returns a QDateTime representation of this value, in local time.
856 If this QJSValue is not a date, or the value of the date is NaN
857 (Not-a-Number), an invalid QDateTime is returned.
861 QDateTime QJSValue::toDateTime() const
864 QScriptIsolate api(d->engine());
865 return d->toDataTime();
869 Returns true if this QJSValue is an object of the Date class;
870 otherwise returns false.
872 \sa QJSEngine::newDate()
874 bool QJSValue::isDate() const
877 QScriptIsolate api(d->engine());
882 Returns true if this QJSValue is an object of the RegExp class;
883 otherwise returns false.
885 bool QJSValue::isRegExp() const
888 QScriptIsolate api(d->engine());
889 return d->isRegExp();
893 Returns true if this QJSValue is a QObject; otherwise returns
896 Note: This function returns true even if the QObject that this
897 QJSValue wraps has been deleted.
899 \sa toQObject(), QJSEngine::newQObject()
901 bool QJSValue::isQObject() const
904 QScriptIsolate api(d->engine());
905 return d->isQObject();