Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-136797

QJSEngine::throwError(QJSValue) with a string value becomes "undefined" in JS

    XMLWordPrintable

Details

    • Linux/Wayland
    • 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"

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ulherman Ulf Hermann
            peat-psuwit Ratchanan Srirattanamet
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes