Details
-
Bug
-
Resolution: Fixed
-
P3: Somewhat important
-
6.9.0
-
None
-
-
08ac22ab6 (dev)
Description
In the code below, JS code calls into a QObject, which call QJSEngine::throwError(QJSValue) and then return. The JS code then catch the error, log the thrown value and return the type of throw value (which is then logged using qDebug()).
If the thrown value is a string QJSValue, then the JS code will catch an "undefined" instead of the passed-in string. So instead of logging:
js: throw a string "string"
It instead logs:
js: undefined "undefined"
This doesn't happen if the QJSValue is any other type (number, symbol, object).
#include <QCoreApplication> #include <QJSEngine> #include <QDebug> class Wrapper: public QObject { Q_OBJECT public: Wrapper(QJSEngine * engine, QObject * parent = nullptr) : QObject(parent) , m_engine(engine) {} Q_INVOKABLE QJSValue callWrapped() { m_engine->throwError(QJSValue("throw a string")); return QJSValue(); } private: QJSEngine *m_engine; }; int main(int argc, char **argv) { QCoreApplication app(argc, argv); QJSEngine myEngine; myEngine.installExtensions(QJSEngine::ConsoleExtension); QJSValue fun = myEngine.evaluate(QStringLiteral( "(function (w) { try { w.callWrapped(); return '(no exception)'; }" "catch (e) { console.log(e); return typeof e; } })" )); QJSValue wrapper = myEngine.newQObject(new Wrapper(&myEngine)); QJSValue ret = fun.call({ wrapper }); qDebug() << ret.toString(); return 0; } #include "toy.moc"