diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-12-18 15:47:46 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-12-19 19:53:44 +0100 |
commit | bf89ec173492abc679ce71f41d9e8d49a68743f3 (patch) | |
tree | 84fc3002bbdebd05fcdf3e7c1a8bc467218c93dd /src/qml | |
parent | 0d835e42c105f32042a8be0fc91676cf6ff8e737 (diff) |
QJSManagedValue: Throw an exception when trying to call a non-callable
The JavaScript engine does this, so we should do the same here.
Change-Id: I011a60bc2c013f19306c843da7a9c22dc80a39cb
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/jsapi/qjsmanagedvalue.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/qml/jsapi/qjsmanagedvalue.cpp b/src/qml/jsapi/qjsmanagedvalue.cpp index fee2d9da10..31b28fd690 100644 --- a/src/qml/jsapi/qjsmanagedvalue.cpp +++ b/src/qml/jsapi/qjsmanagedvalue.cpp @@ -916,6 +916,21 @@ bool QJSManagedValue::isCallable() const return d && d->isFunctionObject(); } +static const QV4::FunctionObject *functionObjectForCall(QV4::Value *d) +{ + if (Q_UNLIKELY(!d)) { + qWarning("QJSManagedValue: Calling a default-constructed or moved-from managed value" + "should throw an exception, but there is no engine to receive it."); + return nullptr; + } + + if (const QV4::FunctionObject *f = d->as<QV4::FunctionObject>()) + return f; + + v4Engine(d)->throwTypeError(QStringLiteral("Value is not a function")); + return nullptr; +} + /*! * If this QJSManagedValue represents a JavaScript FunctionObject, calls it with * the given \a arguments, and returns the result. Otherwise returns a @@ -927,7 +942,7 @@ bool QJSManagedValue::isCallable() const */ QJSValue QJSManagedValue::call(const QJSValueList &arguments) const { - const QV4::FunctionObject *f = d ? d->as<QV4::FunctionObject>() : nullptr; + const QV4::FunctionObject *f = functionObjectForCall(d); if (!f) return QJSValue(); @@ -960,7 +975,7 @@ QJSValue QJSManagedValue::call(const QJSValueList &arguments) const QJSValue QJSManagedValue::callWithInstance(const QJSValue &instance, const QJSValueList &arguments) const { - const QV4::FunctionObject *f = d ? d->as<QV4::FunctionObject>() : nullptr; + const QV4::FunctionObject *f = functionObjectForCall(d); if (!f) return QJSValue(); @@ -999,7 +1014,7 @@ QJSValue QJSManagedValue::callWithInstance(const QJSValue &instance, */ QJSValue QJSManagedValue::callAsConstructor(const QJSValueList &arguments) const { - const QV4::FunctionObject *f = d ? d->as<QV4::FunctionObject>() : nullptr; + const QV4::FunctionObject *f = functionObjectForCall(d); if (!f) return QJSValue(); |