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

WebAssembly: Use Qt Meta-Object System to generate Emscripten bindings

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • None
    • QPA
    • None

    Description

      The Emscripten Embind module allow to bind C++ classes with Javascript, and use it in JS code as if it where native objects.

      The fact is Embind requires exactly the same informations as the ones provided by the Qt Meta-Object System, and both are faces of the logic of exposing C++ entities to other technologies (QML, Javascript, etc).

      So the suggestion is to allow the use of the Qt Meta-Object System (macros and/or MOC) to automatically generate bindings for classes when compiling with Emscripten.

      Here is some example of code to illustrate this proposal:

      class DBObject : public QObject
      {
          Q_OBJECT
          
          // Here is described a Qt meta-property.
          Q_PROPERTY(int id READ id WRITE setId)
      
      public:
          explicit DBObject(QObject *parent = nullptr);
      
          int id() const;
          void setId(int id);
      
      private:
          int m_id;
      };
      
      class User : public DBObject
      {
          Q_OBJECT
      
          // Here are described Qt meta-properties.
          Q_PROPERTY(QString name READ name WRITE setName)
          Q_PROPERTY(Status status READ status WRITE setStatus)
          
      public:
          explicit User(QObject *parent = nullptr);
      
          enum Status {
              INACTIVE,
              ACTIVE,
              OTHER
          };
          // Here is declared a Qt meta-enum.
          Q_ENUM(Status)
      
          QString name() const;
          void setName(const QString &name);
      
          Status status() const;
          void setStatus(const Status &status);
      
      private:
          QString m_name;
          Status m_status;
      };
      
      #ifdef __EMSCRIPTEN__
      #include <emscripten/bind.h>
      
      // And here we can see kind of copy of the informations already given to Qt meta-object system.
      EMSCRIPTEN_BINDINGS(my_example) {
      
          emscripten::class_<DBObject>("DBObject")
                  .property("id", &DBObject::id, &DBObject::setId);
      
          emscripten::enum_<User::Status>("UserStatus")
                  .value("INACTIVE", User::Status::INACTIVE)
                  .value("ACTIVE", User::Status::ACTIVE)
                  .value("OTHER", User::Status::OTHER);
          emscripten::class_<User, emscripten::base<DBObject>>("User")
                  .property("name", &User::name, &User::setName)
                  .property("status", &User::status, &User::setStatus);
      }
      
      #endif
      

      All the code in the EMSCRIPTEN_BINDINGS macro could be avoided when using Qt Meta-Object System.

      The whole (working) code is available in MetaExample.tar.gz.

      Emscripten reference:
      http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/embind.html
      http://kripken.github.io/emscripten-site/docs/api_reference/bind.h.html

      Attachments

        Issue Links

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

          Activity

            People

              Unassigned Unassigned
              qvignaud Quentin Vignaud
              Veli-Pekka Heinonen Veli-Pekka Heinonen
              Votes:
              3 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes