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 QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qscriptisolate_p.h"
43 #include "qjsengine.h"
44 #include "qv8engine_p.h"
46 #include "qjsvalue_p.h"
47 #include "qscript_impl_p.h"
48 #include "qscriptshareddata_p.h"
49 #include <QtCore/qstring.h>
55 \brief The QJSValue class acts as a container for Qt/JavaScript data types.
60 QJSValue supports the types defined in the \l{ECMA-262}
61 standard: The primitive types, which are Undefined, Null, Boolean,
62 Number, and String; and the Object type. Additionally, built-in
63 support is provided for Qt/C++ types such as QVariant and QObject.
65 For the object-based types (including Date and RegExp), use the
66 newT() functions in QJSEngine (e.g. QJSEngine::newObject())
67 to create a QJSValue of the desired type. For the primitive types,
68 use one of the QJSValue constructor overloads.
70 The methods named isT() (e.g. isBool(), isUndefined()) can be
71 used to test if a value is of a certain type. The methods named
72 toT() (e.g. toBool(), toString()) can be used to convert a
73 QJSValue to another type. You can also use the generic
74 QJSValue_cast() function.
76 Object values have zero or more properties which are themselves
77 QJSValues. Use setProperty() to set a property of an object, and
78 call property() to retrieve the value of a property.
80 \snippet code/src_script_qjsvalue.cpp 0
82 If you want to iterate over the properties of a script object, use
83 the QJSValueIterator class.
85 Object values have an internal \c{prototype} property, which can be
86 accessed with prototype() and setPrototype().
88 Function objects (objects for which isCallable()) returns true) can
89 be invoked by calling call(). Constructor functions can be used to
90 construct new objects by calling callAsConstructor().
92 Use equals() or strictlyEquals() to compare a QJSValue to another.
94 Note that a QJSValue for which isObject() is true only carries a
95 reference to an actual object; copying the QJSValue will only
96 copy the object reference, not the object itself. If you want to
97 clone an object (i.e. copy an object's properties to another
98 object), you can do so with the help of a \c{for-in} statement in
99 script code, or QJSValueIterator in C++.
101 \sa QJSEngine, QJSValueIterator
105 \enum QJSValue::SpecialValue
107 This enum is used to specify a single-valued type.
109 \value UndefinedValue An undefined value.
111 \value NullValue A null value.
117 Constructs a new QJSValue with a boolean \a value.
119 QJSValue::QJSValue(bool value)
120 : d_ptr(new QJSValuePrivate(value))
125 Constructs a new QJSValue with a number \a value.
127 QJSValue::QJSValue(int value)
128 : d_ptr(new QJSValuePrivate(value))
133 Constructs a new QJSValue with a number \a value.
135 QJSValue::QJSValue(uint value)
136 : d_ptr(new QJSValuePrivate(value))
141 Constructs a new QJSValue with a number \a value.
143 QJSValue::QJSValue(double value)
144 : d_ptr(new QJSValuePrivate(value))
149 Constructs a new QJSValue with a string \a value.
151 QJSValue::QJSValue(const QString& value)
152 : d_ptr(new QJSValuePrivate(value))
157 Constructs a new QJSValue with a special \a value.
159 QJSValue::QJSValue(SpecialValue value)
160 : d_ptr(new QJSValuePrivate(value))
165 Constructs a new QJSValue with a string \a value.
167 QJSValue::QJSValue(const QLatin1String &value)
168 : d_ptr(new QJSValuePrivate(value))
173 Constructs a new QJSValue with a string \a value.
175 #ifndef QT_NO_CAST_FROM_ASCII
176 QJSValue::QJSValue(const char *value)
177 : d_ptr(new QJSValuePrivate(QString::fromLatin1(value)))
183 Constructs a new QJSValue from private
186 QJSValue::QJSValue(QJSValuePrivate* d)
192 Constructs a new QJSValue from private
195 QJSValue::QJSValue(QScriptPassPointer<QJSValuePrivate> d)
201 Constructs a new QJSValue that is a copy of \a other.
203 Note that if \a other is an object (i.e., isObject() would return
204 true), then only a reference to the underlying object is copied into
205 the new script value (i.e., the object itself is not copied).
207 QJSValue::QJSValue(const QJSValue& other)
213 Destroys this QJSValue.
215 QJSValue::~QJSValue()
220 Returns true if this QJSValue is of the primitive type Boolean;
221 otherwise returns false.
225 bool QJSValue::isBool() const
228 QScriptIsolate api(d->engine());
233 Returns true if this QJSValue is of the primitive type Number;
234 otherwise returns false.
238 bool QJSValue::isNumber() const
241 QScriptIsolate api(d->engine());
242 return d->isNumber();
246 Returns true if this QJSValue is of the primitive type Null;
247 otherwise returns false.
249 bool QJSValue::isNull() const
252 QScriptIsolate api(d->engine());
257 Returns true if this QJSValue is of the primitive type String;
258 otherwise returns false.
262 bool QJSValue::isString() const
265 QScriptIsolate api(d->engine());
266 return d->isString();
270 Returns true if this QJSValue is of the primitive type Undefined;
271 otherwise returns false.
273 bool QJSValue::isUndefined() const
276 QScriptIsolate api(d->engine());
277 return d->isUndefined();
281 Returns true if this QJSValue is an object of the Error class;
282 otherwise returns false.
284 bool QJSValue::isError() const
287 QScriptIsolate api(d->engine());
292 Returns true if this QJSValue is an object of the Array class;
293 otherwise returns false.
295 \sa QJSEngine::newArray()
297 bool QJSValue::isArray() const
300 QScriptIsolate api(d->engine());
305 Returns true if this QJSValue is of the Object type; otherwise
308 Note that function values, variant values, and QObject values are
309 objects, so this function returns true for such values.
311 \sa QJSEngine::newObject()
313 bool QJSValue::isObject() const
316 QScriptIsolate api(d->engine());
317 return d->isObject();
321 Returns true if this QJSValue can be called a function, otherwise
326 bool QJSValue::isCallable() const
329 QScriptIsolate api(d->engine());
330 return d->isCallable();
334 Returns true if this QJSValue is a variant value;
335 otherwise returns false.
339 bool QJSValue::isVariant() const
342 QScriptIsolate api(d->engine());
343 return d->isVariant();
347 Returns the string value of this QJSValue, as defined in
348 \l{ECMA-262} section 9.8, "ToString".
350 Note that if this QJSValue is an object, calling this function
351 has side effects on the script engine, since the engine will call
352 the object's toString() function (and possibly valueOf()) in an
353 attempt to convert the object to a primitive value (possibly
354 resulting in an uncaught script exception).
358 QString QJSValue::toString() const
361 QScriptIsolate api(d->engine());
362 return d->toString();
366 Returns the number value of this QJSValue, as defined in
367 \l{ECMA-262} section 9.3, "ToNumber".
369 Note that if this QJSValue is an object, calling this function
370 has side effects on the script engine, since the engine will call
371 the object's valueOf() function (and possibly toString()) in an
372 attempt to convert the object to a primitive value (possibly
373 resulting in an uncaught script exception).
375 \sa isNumber(), toInt(), toUInt()
377 double QJSValue::toNumber() const
380 QScriptIsolate api(d->engine());
381 return d->toNumber();
385 Returns the boolean value of this QJSValue, using the conversion
386 rules described in \l{ECMA-262} section 9.2, "ToBoolean".
388 Note that if this QJSValue is an object, calling this function
389 has side effects on the script engine, since the engine will call
390 the object's valueOf() function (and possibly toString()) in an
391 attempt to convert the object to a primitive value (possibly
392 resulting in an uncaught script exception).
396 bool QJSValue::toBool() const
399 QScriptIsolate api(d->engine());
404 Returns the signed 32-bit integer value of this QJSValue, using
405 the conversion rules described in \l{ECMA-262} section 9.5, "ToInt32".
407 Note that if this QJSValue is an object, calling this function
408 has side effects on the script engine, since the engine will call
409 the object's valueOf() function (and possibly toString()) in an
410 attempt to convert the object to a primitive value (possibly
411 resulting in an uncaught script exception).
413 \sa toNumber(), toUInt()
415 qint32 QJSValue::toInt() const
418 QScriptIsolate api(d->engine());
423 Returns the unsigned 32-bit integer value of this QJSValue, using
424 the conversion rules described in \l{ECMA-262} section 9.6, "ToUint32".
426 Note that if this QJSValue is an object, calling this function
427 has side effects on the script engine, since the engine will call
428 the object's valueOf() function (and possibly toString()) in an
429 attempt to convert the object to a primitive value (possibly
430 resulting in an uncaught script exception).
432 \sa toNumber(), toInt()
434 quint32 QJSValue::toUInt() const
437 QScriptIsolate api(d->engine());
438 return d->toUInt32();
442 Returns the QVariant value of this QJSValue, if it can be
443 converted to a QVariant; otherwise returns an invalid QVariant.
444 The conversion is performed according to the following table:
447 \header \li Input Type \li Result
448 \row \li Undefined \li An invalid QVariant.
449 \row \li Null \li A QVariant containing a null pointer (QMetaType::VoidStar).
450 \row \li Boolean \li A QVariant containing the value of the boolean.
451 \row \li Number \li A QVariant containing the value of the number.
452 \row \li String \li A QVariant containing the value of the string.
453 \row \li QVariant Object \li The result is the QVariant value of the object (no conversion).
454 \row \li QObject Object \li A QVariant containing a pointer to the QObject.
455 \row \li Date Object \li A QVariant containing the date value (toDateTime()).
456 \row \li RegExp Object \li A QVariant containing the regular expression value.
457 \row \li Array Object \li The array is converted to a QVariantList. Each element is converted to a QVariant, recursively; cyclic references are not followed.
458 \row \li Object \li The object is converted to a QVariantMap. Each property is converted to a QVariant, recursively; cyclic references are not followed.
463 QVariant QJSValue::toVariant() const
466 QScriptIsolate api(d->engine());
467 return d->toVariant();
471 Calls this QJSValue as a function, passing \a args as arguments
472 to the function, and using the globalObject() as the "this"-object.
473 Returns the value returned from the function.
475 If this QJSValue is not callable, call() does nothing and
476 returns an undefined QJSValue.
478 Calling call() can cause an exception to occur in the script engine;
479 in that case, call() returns the value that was thrown (typically an
480 \c{Error} object). You can call isError() on the return value to
481 determine whether an exception occurred.
483 \sa isCallable(), callWithInstance(), callAsConstructor()
485 QJSValue QJSValue::call(const QJSValueList &args)
488 QScriptIsolate api(d->engine());
489 return d->call(/*thisObject=*/0, args);
493 Calls this QJSValue as a function, using \a instance as
494 the `this' object in the function call, and passing \a args
495 as arguments to the function. Returns the value returned from
498 If this QJSValue is not a function, call() does nothing
499 and returns an undefined QJSValue.
501 Note that if \a instance is not an object, the global object
502 (see \l{QJSEngine::globalObject()}) will be used as the
505 Calling call() can cause an exception to occur in the script engine;
506 in that case, call() returns the value that was thrown (typically an
507 \c{Error} object). You can call isError() on the return value to
508 determine whether an exception occurred.
512 QJSValue QJSValue::callWithInstance(const QJSValue &instance, const QJSValueList &args)
515 QScriptIsolate api(d->engine());
516 return d->call(QJSValuePrivate::get(instance), args);
520 Creates a new \c{Object} and calls this QJSValue as a
521 constructor, using the created object as the `this' object and
522 passing \a args as arguments. If the return value from the
523 constructor call is an object, then that object is returned;
524 otherwise the default constructed object is returned.
526 If this QJSValue is not a function, callAsConstructor() does
527 nothing and returns an undefined QJSValue.
529 Calling this function can cause an exception to occur in the
530 script engine; in that case, the value that was thrown
531 (typically an \c{Error} object) is returned. You can call
532 isError() on the return value to determine whether an
535 \sa call(), QJSEngine::newObject()
537 QJSValue QJSValue::callAsConstructor(const QJSValueList &args)
540 QScriptIsolate api(d->engine());
541 return QJSValuePrivate::get(d->callAsConstructor(args));
549 Returns the QJSEngine that created this QJSValue,
550 or 0 if this QJSValue is invalid or the value is not
551 associated with a particular engine.
553 QJSEngine* QJSValue::engine() const
556 QScriptIsolate api(d->engine());
557 QV8Engine* engine = d->engine();
559 return QV8Engine::get(engine);
563 #endif // QT_DEPRECATED
566 If this QJSValue is an object, returns the internal prototype
567 (\c{__proto__} property) of this object; otherwise returns an
570 \sa setPrototype(), isObject()
572 QJSValue QJSValue::prototype() const
575 QScriptIsolate api(d->engine());
576 return QJSValuePrivate::get(d->prototype());
580 If this QJSValue is an object, sets the internal prototype
581 (\c{__proto__} property) of this object to be \a prototype;
582 otherwise does nothing.
584 The internal prototype should not be confused with the public
585 property with name "prototype"; the public prototype is usually
586 only set on functions that act as constructors.
588 \sa prototype(), isObject()
590 void QJSValue::setPrototype(const QJSValue& prototype)
593 QScriptIsolate api(d->engine());
594 d->setPrototype(QJSValuePrivate::get(prototype));
598 Assigns the \a other value to this QJSValue.
600 Note that if \a other is an object (isObject() returns true),
601 only a reference to the underlying object will be assigned;
602 the object itself will not be copied.
604 QJSValue& QJSValue::operator=(const QJSValue& other)
611 Returns true if this QJSValue is equal to \a other, otherwise
612 returns false. The comparison follows the behavior described in
613 \l{ECMA-262} section 11.9.3, "The Abstract Equality Comparison
616 This function can return true even if the type of this QJSValue
617 is different from the type of the \a other value; i.e. the
618 comparison is not strict. For example, comparing the number 9 to
619 the string "9" returns true; comparing an undefined value to a null
620 value returns true; comparing a \c{Number} object whose primitive
621 value is 6 to a \c{String} object whose primitive value is "6"
622 returns true; and comparing the number 1 to the boolean value
623 \c{true} returns true. If you want to perform a comparison
624 without such implicit value conversion, use strictlyEquals().
626 Note that if this QJSValue or the \a other value are objects,
627 calling this function has side effects on the script engine, since
628 the engine will call the object's valueOf() function (and possibly
629 toString()) in an attempt to convert the object to a primitive value
630 (possibly resulting in an uncaught script exception).
634 bool QJSValue::equals(const QJSValue& other) const
637 QJSValuePrivate* otherValue = QJSValuePrivate::get(other);
638 QScriptIsolate api(d->engine() ? d->engine() : otherValue->engine());
639 return d_ptr->equals(otherValue);
643 Returns true if this QJSValue is equal to \a other using strict
644 comparison (no conversion), otherwise returns false. The comparison
645 follows the behavior described in \l{ECMA-262} section 11.9.6, "The
646 Strict Equality Comparison Algorithm".
648 If the type of this QJSValue is different from the type of the
649 \a other value, this function returns false. If the types are equal,
650 the result depends on the type, as shown in the following table:
653 \header \li Type \li Result
654 \row \li Undefined \li true
655 \row \li Null \li true
656 \row \li Boolean \li true if both values are true, false otherwise
657 \row \li Number \li false if either value is NaN (Not-a-Number); true if values are equal, false otherwise
658 \row \li String \li true if both values are exactly the same sequence of characters, false otherwise
659 \row \li Object \li true if both values refer to the same object, false otherwise
664 bool QJSValue::strictlyEquals(const QJSValue& other) const
667 QJSValuePrivate* o = QJSValuePrivate::get(other);
668 QScriptIsolate api(d->engine() ? d->engine() : o->engine());
669 return d_ptr->strictlyEquals(o);
673 Returns the value of this QJSValue's property with the given \a name.
674 If no such property exists, an undefined QJSValue is returned.
676 If the property is implemented using a getter function (i.e. has the
677 PropertyGetter flag set), calling property() has side-effects on the
678 script engine, since the getter function will be called (possibly
679 resulting in an uncaught script exception). If an exception
680 occurred, property() returns the value that was thrown (typically
681 an \c{Error} object).
683 \sa setProperty(), hasProperty(), QJSValueIterator
685 QJSValue QJSValue::property(const QString& name) const
688 QScriptIsolate api(d->engine());
689 return QJSValuePrivate::get(d->property(name));
695 Returns the property at the given \a arrayIndex.
697 This function is provided for convenience and performance when
698 working with array objects.
700 If this QJSValue is not an Array object, this function behaves
701 as if property() was called with the string representation of \a
704 QJSValue QJSValue::property(quint32 arrayIndex) const
707 QScriptIsolate api(d->engine());
708 return QJSValuePrivate::get(d->property(arrayIndex));
712 Sets the value of this QJSValue's property with the given \a name to
715 If this QJSValue is not an object, this function does nothing.
717 If this QJSValue does not already have a property with name \a name,
718 a new property is created.
720 \sa property(), deleteProperty()
722 void QJSValue::setProperty(const QString& name, const QJSValue& value)
725 QScriptIsolate api(d->engine());
726 d->setProperty(name, QJSValuePrivate::get(value));
732 Sets the property at the given \a arrayIndex to the given \a value.
734 This function is provided for convenience and performance when
735 working with array objects.
737 If this QJSValue is not an Array object, this function behaves
738 as if setProperty() was called with the string representation of \a
741 void QJSValue::setProperty(quint32 arrayIndex, const QJSValue& value)
744 QScriptIsolate api(d->engine());
745 d->setProperty(arrayIndex, QJSValuePrivate::get(value));
749 Attempts to delete this object's property of the given \a name.
750 Returns true if the property was deleted, otherwise returns false.
752 The behavior of this function is consistent with the JavaScript
753 delete operator. In particular:
756 \li Non-configurable properties cannot be deleted.
757 \li This function will return true even if this object doesn't
758 have a property of the given \a name (i.e., non-existent
759 properties are "trivially deletable").
760 \li If this object doesn't have an own property of the given
761 \a name, but an object in the prototype() chain does, the
762 prototype object's property is not deleted, and this function
766 \sa setProperty(), hasOwnProperty()
768 bool QJSValue::deleteProperty(const QString &name)
771 QScriptIsolate api(d->engine());
772 return d->deleteProperty(name);
776 Returns true if this object has a property of the given \a name,
777 otherwise returns false.
779 \sa property(), hasOwnProperty()
781 bool QJSValue::hasProperty(const QString &name) const
784 QScriptIsolate api(d->engine());
785 return d->hasProperty(name);
789 Returns true if this object has an own (not prototype-inherited)
790 property of the given \a name, otherwise returns false.
792 \sa property(), hasProperty()
794 bool QJSValue::hasOwnProperty(const QString &name) const
797 QScriptIsolate api(d->engine());
798 return d->hasOwnProperty(name);
802 * If this QJSValue is a QObject, returns the QObject pointer
803 * that the QJSValue represents; otherwise, returns 0.
805 * If the QObject that this QJSValue wraps has been deleted,
806 * this function returns 0 (i.e. it is possible for toQObject()
807 * to return 0 even when isQObject() returns true).
811 QObject *QJSValue::toQObject() const
814 QScriptIsolate api(d->engine());
815 return d->toQObject();
819 Returns a QDateTime representation of this value, in local time.
820 If this QJSValue is not a date, or the value of the date is NaN
821 (Not-a-Number), an invalid QDateTime is returned.
825 QDateTime QJSValue::toDateTime() const
828 QScriptIsolate api(d->engine());
829 return d->toDataTime();
833 Returns true if this QJSValue is an object of the Date class;
834 otherwise returns false.
836 \sa QJSEngine::newDate()
838 bool QJSValue::isDate() const
841 QScriptIsolate api(d->engine());
846 Returns true if this QJSValue is an object of the RegExp class;
847 otherwise returns false.
849 bool QJSValue::isRegExp() const
852 QScriptIsolate api(d->engine());
853 return d->isRegExp();
857 Returns true if this QJSValue is a QObject; otherwise returns
860 Note: This function returns true even if the QObject that this
861 QJSValue wraps has been deleted.
863 \sa toQObject(), QJSEngine::newQObject()
865 bool QJSValue::isQObject() const
868 QScriptIsolate api(d->engine());
869 return d->isQObject();
875 Returns this value's internal V8 value, or an empty handle if
876 the QJSValue isn't bound to a QJSEngine.
878 The V8 value enables direct use of the V8 API.
879 The caller is responsible for ensuring that a handle scope is in place.
883 QJSValue value = ...;
884 v8::HandleScope handleScope;
885 v8::Local<v8::Value> v8value = qt_QJSValueV8Value(value);
887 // Do something with the V8 value (e.g., call v8::Value::IsInt32()) ...
890 \sa qt_QJSEngineV8Context()
892 Q_QML_EXPORT v8::Local<v8::Value> qt_QJSValueV8Value(const QJSValue &value)
894 QJSValuePrivate *d = QJSValuePrivate::get(value);
895 return v8::Local<v8::Value>::New(d->handle());