QJSManagedValue Class

QJSManagedValue represents a value on the JavaScript heap belonging to a QJSEngine . 更多...

头: #include <QJSManagedValue>
CMake: find_package(Qt6 COMPONENTS Qml REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml
Since: Qt 6.1

公共类型

enum Type { Undefined, Boolean, Number, String, Object, …, Function }

公共函数

  QJSManagedValue (QJSManagedValue && other )
  QJSManagedValue (const QString & string , QJSEngine * engine )
  QJSManagedValue (const QVariant & variant , QJSEngine * engine )
  QJSManagedValue (const QJSPrimitiveValue & value , QJSEngine * engine )
  QJSManagedValue (QJSValue value , QJSEngine * engine )
  QJSManagedValue ()
QJSManagedValue & operator= (QJSManagedValue && other )
  ~QJSManagedValue ()
QJSValue call (const QJSValueList & arguments = {}) const
QJSValue callAsConstructor (const QJSValueList & arguments = {}) const
QJSValue callWithInstance (const QJSValue & instance , const QJSValueList & arguments = {}) const
bool deleteProperty (const QString & name )
bool deleteProperty (quint32 arrayIndex )
QJSEngine * engine () const
bool equals (const QJSManagedValue & other ) const
bool hasOwnProperty (const QString & name ) const
bool hasOwnProperty (quint32 arrayIndex ) const
bool hasProperty (const QString & name ) const
bool hasProperty (quint32 arrayIndex ) const
bool isArray () const
bool isBoolean () const
bool isDate () const
bool isError () const
bool isFunction () const
bool isInteger () const
bool isNull () const
bool isNumber () const
bool isObject () const
bool isQMetaObject () const
bool isQObject () const
bool isRegularExpression () const
bool isString () const
bool isSymbol () const
bool isUndefined () const
bool isUrl () const
bool isVariant () const
QJSValue property (const QString & name ) const
QJSValue property (quint32 arrayIndex ) const
QJSManagedValue prototype () const
void setProperty (const QString & name , const QJSValue & value )
void setProperty (quint32 arrayIndex , const QJSValue & value )
void setPrototype (const QJSManagedValue & prototype )
bool strictlyEquals (const QJSManagedValue & other ) const
bool toBoolean () const
QDateTime toDateTime () const
int toInteger () const
QJSValue toJSValue () const
double toNumber () const
QJSPrimitiveValue toPrimitive () const
const QMetaObject * toQMetaObject () const
QObject * toQObject () const
QRegularExpression toRegularExpression () const
QString toString () const
QUrl toUrl () const
QVariant toVariant () const
QJSManagedValue::Type type () const

详细描述

The QJSManagedValue class allows interaction with JavaScript values in most ways you can interact with them from JavaScript itself. You can get and set properties and prototypes, and you can access arrays. Additionally, you can transform the value into the Qt counterparts of JavaScript objects. For example, a Url object may be transformed into a QUrl .

A QJSManagedValue is always bound to a particular QJSEngine . You cannot use it independently. This means that you cannot have a QJSManagedValue from one engine be a property or a proptotype of a QJSManagedValue from a different engine.

In contrast to QJSValue , almost all values held by QJSManagedValue live on the JavaScript heap. There is no inline or unmanaged storage. Therefore, you can get the prototype of a primitive value, and you can get the length property of a string.

Only default-constructed or moved-from QJSManagedValues do not hold a value on the JavaScript heap. They represent undefined , which doesn't have any properties or prototypes.

Also in contrast to QJSValue , QJSManagedValue does not catch any JavaScript exceptions. If an operation on a QJSManagedValue causes an error, it will generally return an undefined value and QJSEngine::hasError () 会返回 true afterwards. You can then catch the exception using QJSEngine::catchError (), or pass it up the stack, at your own discretion.

注意: As the reference to the value on the JavaScript heap has to be freed on destruction, you cannot move a QJSManagedValue to a different thread. The destruction would take place in the new thread, which would create a race condition with the garbage collector on the original thread. This also means that you cannot hold a QJSManagedValue beyond the lifespan of its engine.

The recommended way of working with a QJSManagedValue is creating it on the stack, possibly by moving a QJSValue and adding an engine, then performing the necessary operations on it, and finally moving it back into a QJSValue for storage. Moving between QJSManagedValue and QJSValue is fast.

成员类型文档编制

enum QJSManagedValue:: Type

This enum represents the JavaScript native types, as specified by ECMA-262 .

常量 描述
QJSManagedValue::Undefined 0 undefined type
QJSManagedValue::Boolean 1 boolean type
QJSManagedValue::Number 2 编号 type
QJSManagedValue::String 3 string type
QJSManagedValue::Object 4 对象 type
QJSManagedValue::Symbol 5 symbol type
QJSManagedValue::Function 6 function type

注意, null value is not a type of itself but rather a special kind of object. You can query a QJSManagedValue for this condition using the isNull () method. Furthermore, JavaScript has no integer type, but it knows a special treatment of numbers in preparation for integer only operations. You can query a QJSManagedValue to find out whether it holds the result of such a treatment by using the isInteger () 方法。

成员函数文档编制

QJSManagedValue:: QJSManagedValue ( QJSManagedValue && other )

Move-constructs a QJSManagedValue from other . This leaves other in the default-constructed state where it represents undefined and does not belong to any engine.

QJSManagedValue:: QJSManagedValue (const QString & string , QJSEngine * engine )

Creates a QJSManagedValue from string using the heap of engine .

QJSManagedValue:: QJSManagedValue (const QVariant & variant , QJSEngine * engine )

Creates a QJSManagedValue from variant using the heap of engine .

QJSManagedValue:: QJSManagedValue (const QJSPrimitiveValue & value , QJSEngine * engine )

Creates a QJSManagedValue from value using the heap of engine .

QJSManagedValue:: QJSManagedValue ( QJSValue value , QJSEngine * engine )

Creates a QJSManagedValue from value , using the heap of engine 。若 value is itself managed and the engine it belongs to is not engine , the result is an undefined value, and a warning is generated.

QJSManagedValue:: QJSManagedValue ()

Creates a QJSManagedValue that represents the JavaScript undefined value. This is the only value not stored on the JavaScript heap. Calling engine () on a default-constructed QJSManagedValue will return nullptr.

QJSManagedValue &QJSManagedValue:: operator= ( QJSManagedValue && other )

Move-assigns a QJSManagedValue from other . This leaves other in the default-constructed state where it represents undefined and does not belong to any engine.

注意: This frees the memory slot this QJSManagedValue holds on the JavaScript heap. You must not move-assign a QJSManagedValue on a different thread than the one where the QJSEngine it belongs to lives.

QJSManagedValue:: ~QJSManagedValue ()

销毁 QJSManagedValue .

注意: This frees the memory slot it holds on the JavaScript heap. You must not destroy a QJSManagedValue from a different thread than the one where the QJSEngine it belongs to lives.

QJSValue QJSManagedValue:: call (const QJSValueList & arguments = {}) const

若此 QJSManagedValue represents a JavaScript FunctionObject, calls it with the given arguments , and returns the result. Otherwise returns a JavaScript undefined 值。

arguments have to be either primitive values or belong to the same QJSEngine as this QJSManagedValue . Otherwise the call is not carried out and a JavaScript undefined value is returned.

QJSValue QJSManagedValue:: callAsConstructor (const QJSValueList & arguments = {}) const

若此 QJSManagedValue represents a JavaScript FunctionObject, calls it as constructor with the given arguments , and returns the result. Otherwise returns a JavaScript undefined 值。

arguments have to be either primitive values or belong to the same QJSEngine as this QJSManagedValue . Otherwise the call is not carried out and a JavaScript undefined value is returned.

QJSValue QJSManagedValue:: callWithInstance (const QJSValue & instance , const QJSValueList & arguments = {}) const

若此 QJSManagedValue represents a JavaScript FunctionObject, calls it on instance 采用给定 arguments , and returns the result. Otherwise returns a JavaScript undefined 值。

arguments instance have to be either primitive values or belong to the same QJSEngine as this QJSManagedValue . Otherwise the call is not carried out and a JavaScript undefined value is returned.

bool QJSManagedValue:: deleteProperty (const QString & name )

Deletes the property name 从此 QJSManagedValue 。返回 true if the deletion succeeded, or false 否则。

bool QJSManagedValue:: deleteProperty ( quint32 arrayIndex )

Deletes the value stored at arrayIndex 从此 QJSManagedValue 。返回 true if the deletion succeeded, or false 否则。

QJSEngine *QJSManagedValue:: engine () const

返回 QJSEngine this QJSManagedValue belongs to. Mind that the engine is always valid, unless the QJSManagedValue is default-constructed or moved from. In the latter case a nullptr is returned.

bool QJSManagedValue:: equals (const QJSManagedValue & other ) const

Invokes the JavaScript '==' operator on this QJSManagedValue and other , and returns the result.

另请参阅 strictlyEquals .

bool QJSManagedValue:: hasOwnProperty (const QString & name ) const

返回 true 若此 QJSManagedValue has a property name ,否则返回 false . The properties of the prototype chain are not considered.

bool QJSManagedValue:: hasOwnProperty ( quint32 arrayIndex ) const

返回 true 若此 QJSManagedValue has an array index arrayIndex ,否则返回 false . The properties of the prototype chain are not considered.

bool QJSManagedValue:: hasProperty (const QString & name ) const

返回 true 若此 QJSManagedValue has a property name ,否则返回 false . The properties of the prototype chain are considered.

bool QJSManagedValue:: hasProperty ( quint32 arrayIndex ) const

返回 true 若此 QJSManagedValue has an array index arrayIndex ,否则返回 false . The properties of the prototype chain are considered.

bool QJSManagedValue:: isArray () const

返回 true if this value represents a JavaScript Array object, or false 否则。

bool QJSManagedValue:: isBoolean () const

返回 true if the type of this QJSManagedValue is boolean ,或 false 否则。

bool QJSManagedValue:: isDate () const

返回 true if this value represents a JavaScript Date object, or false 否则。

bool QJSManagedValue:: isError () const

返回 true if this value represents a JavaScript Error object, or false 否则。

bool QJSManagedValue:: isFunction () const

返回 true if the type of this QJSManagedValue is function , false 否则。

bool QJSManagedValue:: isInteger () const

返回 true 若此 QJSManagedValue holds an integer value, or false otherwise. The storage format of a number does not affect the result of any operations performed on it, but if an integer is stored, many operations are faster.

bool QJSManagedValue:: isNull () const

返回 true 若此 QJSManagedValue holds the JavaScript null value, or false 否则。

bool QJSManagedValue:: isNumber () const

返回 true if the type of this QJSManagedValue is 编号 ,或 false 否则。

bool QJSManagedValue:: isObject () const

返回 true if the type of this QJSManagedValue is 对象 ,或 false 否则。

bool QJSManagedValue:: isQMetaObject () const

返回 true if this value represents a QMetaObject pointer managed on the JavaScript heap, or false 否则。

bool QJSManagedValue:: isQObject () const

返回 true if this value represents a QObject pointer managed on the JavaScript heap, or false 否则。

bool QJSManagedValue:: isRegularExpression () const

返回 true if this value represents a JavaScript regular expression object, or false 否则。

bool QJSManagedValue:: isString () const

返回 true if the type of this QJSManagedValue is string ,或 false 否则。

bool QJSManagedValue:: isSymbol () const

返回 true if the type of this QJSManagedValue is symbol ,或 false 否则。

bool QJSManagedValue:: isUndefined () const

返回 true if the type of this QJSManagedValue is undefined ,或 false 否则。

bool QJSManagedValue:: isUrl () const

返回 true if this value represents a JavaScript Url object, or false 否则。

bool QJSManagedValue:: isVariant () const

返回 true if this value represents a QVariant managed on the JavaScript heap, or false 否则。

QJSValue QJSManagedValue:: property (const QString & name ) const

Returns the property name of this QJSManagedValue . The prototype chain is searched if the property is not found on the actual object.

另请参阅 setProperty ().

QJSValue QJSManagedValue:: property ( quint32 arrayIndex ) const

Returns the property stored at arrayIndex of this QJSManagedValue . The prototype chain is searched if the property is not found on the actual object.

QJSManagedValue QJSManagedValue:: prototype () const

Returns the prototype for this QJSManagedValue . This works on any value. You can, for example retrieve the JavaScript boolean prototype from a boolean 值。

另请参阅 setPrototype ().

void QJSManagedValue:: setProperty (const QString & name , const QJSValue & value )

设置特性 name to value on this QJSManagedValue . This can only be done on JavaScript values of type 对象 . Furhermore, value has to be either a primitive or belong to the same engine as this value.

另请参阅 property ().

void QJSManagedValue:: setProperty ( quint32 arrayIndex , const QJSValue & value )

Stores the value at arrayIndex 在此 QJSManagedValue . This can only be done on JavaScript values of type 对象 , and it's not recommended if the value is not an array. Furhermore, value has to be either a primitive or belong to the same engine as this value.

void QJSManagedValue:: setPrototype (const QJSManagedValue & prototype )

Sets the prototype of this QJSManagedValue to prototype . A precondition is that prototype belongs to the same QJSEngine as this QJSManagedValue and is an object (including null). Furthermore, this QJSManagedValue has to be an object (excluding null), too, and you cannot create prototype cycles.

另请参阅 prototype ().

bool QJSManagedValue:: strictlyEquals (const QJSManagedValue & other ) const

Invokes the JavaScript '===' operator on this QJSManagedValue and other , and returns the result.

另请参阅 equals .

bool QJSManagedValue:: toBoolean () const

Converts the manged value to a boolean. If the managed value holds a boolean, that one is returned. Otherwise a boolean coercion by JavaScript rules is performed.

QDateTime QJSManagedValue:: toDateTime () const

若此 QJSManagedValue holds a JavaScript Date object, returns an equivalent QDateTime . Otherwise returns an invalid one.

int QJSManagedValue:: toInteger () const

Converts the manged value to an integer. This first converts the value to a number by the rules of toNumber (), and then clamps it into the integer range by the rules given for coercing the arguments to JavaScript bit shift operators into 32bit integers.

Internally, the value may already be stored as an integer, in which case a fast path is taken.

注意: Conversion of a managed value to a number can throw an exception. In particular, symbols cannot be coerced into numbers, or a custom valueOf() method may throw. In this case the result is 0 and the engine carries an error after the conversion.

注意: The JavaScript rules for coercing numbers into 32bit integers are unintuitive.

QJSValue QJSManagedValue:: toJSValue () const

Copies this QJSManagedValue into a new QJSValue . This is less efficient than move-constructing a QJSValue QJSManagedValue , but retains the QJSManagedValue .

double QJSManagedValue:: toNumber () const

Converts the manged value to a number. If the managed value holds a number, that one is returned. Otherwise a number coercion by JavaScript rules is performed.

注意: Conversion of a managed value to a number can throw an exception. In particular, symbols cannot be coerced into numbers, or a custom valueOf() method may throw. In this case the result is 0 and the engine carries an error after the conversion.

QJSPrimitiveValue QJSManagedValue:: toPrimitive () const

Converts the manged value to a QJSPrimitiveValue . If the managed value holds a type supported by QJSPrimitiveValue , the value is copied. Otherwise the value is converted to a string, and the string is stored in QJSPrimitiveValue .

注意: Conversion of a managed value to a string can throw an exception. In particular, symbols cannot be coerced into strings, or a custom toString () method may throw. In this case the result is the undefined value and the engine carries an error after the conversion.

const QMetaObject *QJSManagedValue:: toQMetaObject () const

若此 QJSManagedValue holds a QMetaObject pointer, returns it. Otherwise returns nullptr.

QObject *QJSManagedValue:: toQObject () const

若此 QJSManagedValue holds a QObject pointer, returns it. Otherwise returns nullptr.

QRegularExpression QJSManagedValue:: toRegularExpression () const

若此 QJSManagedValue holds a JavaScript regular expression object, returns an equivalent QRegularExpression . Otherwise returns an invalid one.

QString QJSManagedValue:: toString () const

Converts the manged value to a string. If the managed value holds a string, that one is returned. Otherwise a string coercion by JavaScript rules is performed.

注意: Conversion of a managed value to a string can throw an exception. In particular, symbols cannot be coerced into strings, or a custom toString() method may throw. In this case the result is an empty string and the engine carries an error after the conversion.

QUrl QJSManagedValue:: toUrl () const

若此 QJSManagedValue holds a JavaScript Url object, returns an equivalent QUrl . Otherwise returns an invalid one.

QVariant QJSManagedValue:: toVariant () const

Copies this QJSManagedValue into a new QVariant . This also creates a useful QVariant if QJSManagedValue::isVariant () 返回 false。 QVariant can hold all types supported by QJSManagedValue .

QJSManagedValue::Type QJSManagedValue:: type () const

Returns the JavaScript type of this QJSManagedValue .