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

Accessing `byteLength` on an ArrayBuffer constructed from an empty QByteArray throws error

    XMLWordPrintable

Details

    • b201b1d0b3 (qt/qtdeclarative/dev)

    Description

      I have a QObject with:

      Q_PROPERTY(QByteArray foo READ foo WRITE setFoo NOTIFY fooChanged)
      Q_PROPERTY(QByteArray bar READ bar WRITE setBar NOTIFY barChanged)
      

      where foo() returns QByteArray("Some Text") and bar() returns a default constructed QByteArray.

      In QML accessing bar.byteLength throws TypeError: Type error.

      For instance the following QML code:

      console.log(obj);
      console.log(obj.foo);
      console.log(obj.foo.byteLength);
      
      console.log(obj.bar);
      console.log(typeof obj.bar);
      console.log(obj.bar.constructor.name);
      console.log('byteLength' in obj.bar);
      try {
          console.log(obj.bar.byteLength);
      } catch (error) {
          console.error(error);
      }
      try {
          console.log(typeof obj.bar['byteLength']);
      } catch (error) {
          console.error(error);
      }
      

      generates the following output:

      qml: MyObject(0x5640bdc0a0e0)
      qml: Some Text
      qml: 9
      qml: 
      qml: object
      qml: ArrayBuffer
      aml: true
      qml: TypeError: Type error
      qml: TypeError: Type error
      

      Edit

      I have made a simpler example using only QJSEngine:

      void test(QJSEngine *engine, const QByteArray &ba) {
        qDebug() << "TEST with" << ba;
        auto value = engine->toScriptValue(ba);
        engine->globalObject().setProperty("myarray", value);
      
        QStringList trace;
        const auto result = engine->evaluate(R"(
      (function(){
          return myarray.byteLength;
      })()
      )",
      "script.js", 1, &trace);
      
        if (result.isError()) {
          qDebug() << "ERROR" << result.toString();
          qDebug() << "TRACE:";
          qDebug().noquote() << trace.join("\n");
        } else {
          qDebug() << "OK:" << result.toNumber();
        }
        qDebug() << "";
      }
      

      Then calling

      test(&engine, "Some Text");
      test(&engine, {});
      

      gives:

      TEST with "Some Text"
       OK: 9
      
      TEST with ""
       ERROR "TypeError: Type error"
       TRACE:
       :3:-1:[file:script.js|file:///script.js]
       %entry:2:-1:[file:script.js|file:///script.js]
       

      Attachments

        For Gerrit Dashboard: QTBUG-103925
        # Subject Branch Project Status CR V

        Activity

          People

            bterrier Benjamin Terrier
            bterrier Benjamin Terrier
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes