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

TypeError error message doesn't give any clue as to what causes it

    XMLWordPrintable

Details

    Description

      The following JavaScript throws a type error:

      main.cpp
      #include <QGuiApplication>
      #include <QDebug>
      #include <QJSEngine>
      
      class EntityScriptFilter : public QObject
      {
          Q_OBJECT
      
      public:
          Q_INVOKABLE explicit EntityScriptFilter(QObject *parent = nullptr) : QObject(parent) {}
      
          void requireId(quint64 id) {
              qDebug() << Q_FUNC_INFO << id;
          }
      
          Q_INVOKABLE void requireName(const QString &name) {
              qDebug() << Q_FUNC_INFO << name;
          }
      };
      
      script.mjs
      export function narrate() {
          var equipperFilter = new EntityScriptFilter()
          equipperFilter.requireId(123)
      }
      

      The error says:

      "TypeError: Type error"
      

      Which isn't exactly helpful. When debugging the error, I see the following call stack:

      1 QV4::ExecutionEngine::throwTypeError()                                                                    qv4engine.cpp         1078 0x10400e362    
      2 QV4::Moth::VME::interpret(QV4::CppStackFrame *, QV4::ExecutionEngine *, const char *)                     qv4vme_moth.cpp       729  0x103d9debc    
      3 QV4::Moth::VME::exec(QV4::CppStackFrame *, QV4::ExecutionEngine *)                                        qv4vme_moth.cpp       441  0x103d8dede    
      4 QV4::ArrowFunction::virtualCall(QV4::FunctionObject const *, QV4::Value const *, QV4::Value const *, int) qv4functionobject.cpp 519  0x103bbf6bd    
      5 QV4::FunctionObject::call(QV4::Value const *, QV4::Value const *, int) const                              qv4functionobject_p.h 202  0x103aefc96    
      6 QV4::FunctionObject::call(QV4::JSCallData const&) const                                                   qv4jscall_p.h         112  0x103ae8cc2    
      7 QJSValue::call(QList<QJSValue> const&)                                                                    qjsvalue.cpp          709  0x103ae8459    
      8 main                                                                                                      main.cpp              33   0x1000048ef    
      9 start                                                                                                                                0x7fff63afa085 
      

      Looking at the call site, the problem is made clear:

          MOTH_BEGIN_INSTR(CallPropertyLookup)
              STORE_IP();
              Lookup *l = function->compilationUnit->runtimeLookups + lookupIndex;
              // ok to have the value on the stack here
              Value f = Value::fromReturnedValue(l->getter(l, engine, stack[base]));
      
              if (Q_UNLIKELY(!f.isFunctionObject())) {
                  acc = engine->throwTypeError();
                  goto handleUnwind;
              }
      
              acc = static_cast<FunctionObject &>(f).call(stack + base, stack + argv, argc);
              CHECK_EXCEPTION;
          MOTH_END_INSTR(CallPropertyLookup)
      

      The C++ function is not invokable.

      It seems that the engine has the context to print a more descriptive error message. For example:

      TypeError: requireId() is not a callable function (is it invokable?)
      

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            mitch_curtis Mitch Curtis
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes