diff options
author | Ulf Hermann <[email protected]> | 2021-05-20 14:09:08 +0200 |
---|---|---|
committer | Fabian Kosmale <[email protected]> | 2021-05-26 14:46:39 +0200 |
commit | 0472a7a4326cf9ac1abdcbc0910c3b13f5c4fb5a (patch) | |
tree | a6d6ca7ecebcd733d11997f23a7ef2114b078457 /src/qml/jsruntime/qv4runtime.cpp | |
parent | e2f247fafcd90c02e5db686c791ff4818be9cc50 (diff) |
Evaluate type assertions in QML
Type assertions actually check whether the expression matches the type,
and return null if it doesn't.
[ChangeLog][QtQml] You can use TypeScript-like type assertions using
"as" now. In contrast to TypeScript, QML's type assertions are enforced
at runtime. If the type doesn't match, null is returned for object
types. Also, type assertions can only cast to object types. There is no
way to create a value type or primitive type reference. As value types
and primitives cannot be polymorphic, this doesn't matter, though.
There are other ways of converting those.
Task-number: QTBUG-93662
Change-Id: I00fce3d4ea7a8c6b4631c580eaf6c113ac485813
Reviewed-by: Cristian Maureira-Fredes <[email protected]>
Reviewed-by: Paul Wicking <[email protected]>
Diffstat (limited to 'src/qml/jsruntime/qv4runtime.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4runtime.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp index a3b1a52535..01a7878dae 100644 --- a/src/qml/jsruntime/qv4runtime.cpp +++ b/src/qml/jsruntime/qv4runtime.cpp @@ -60,6 +60,7 @@ #include <private/qqmlengine_p.h> #include <private/qqmljavascriptexpression_p.h> #include <private/qqmljsast_p.h> +#include <private/qqmlvaluetypewrapper_p.h> #include "qv4qobjectwrapper_p.h" #include "qv4symbol_p.h" #include "qv4generatorobject_p.h" @@ -393,6 +394,19 @@ QV4::ReturnedValue Runtime::Instanceof::call(ExecutionEngine *engine, const Valu return scope.hasException() ? Encode::undefined() : Encode(result->toBoolean()); } +QV4::ReturnedValue Runtime::As::call(ExecutionEngine *engine, const Value &lval, const Value &rval) +{ + Scope scope(engine); + ScopedValue result(scope, Runtime::Instanceof::call(engine, lval, rval)); + + if (scope.hasException()) + engine->catchException(); + else if (result->toBoolean()) + return lval.asReturnedValue(); + + return Encode::null(); +} + QV4::ReturnedValue Runtime::In::call(ExecutionEngine *engine, const Value &left, const Value &right) { Object *ro = right.objectValue(); |