Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJSEngine does not support JavaScript lambdas as expected
QtWS25 Last Chance

QJSEngine does not support JavaScript lambdas as expected

Scheduled Pinned Locked Moved Unsolved General and Desktop
javascriptqjsengineqjsvaluecallback
1 Posts 1 Posters 242 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    plaristote
    wrote on last edited by
    #1

    Hi !

    I'm getting a bit worried at the way QJSEngine handles lambdas using the new () => {} syntax.

    In some cases which are pretty rare and not completely consistent, the value of this from within one of this lambda will evaluate to undefined. This happens when passing said lambda to C++ using QJSValue.

    Imagine the following QObject:

    // header
    class MyObject : public QObject
    {
      Q_OBJECT
    public:
      MyObject(QObject* parent = nullptr) : QObject(parent) {}
    
      Q_INVOKABLE void pushTask(QJSValue value) { taskList << value; }
      Q_INVOKABLE void runTasks();
    
      QList<QJSValue> taskList;
    };
    
    // source
    void MyObject::runTasks()
    {
      for (QJSValue task : taskList)
        task.call();
    }
    

    Then if we interact with this QObject from JavaScript:

    export class MyObject {
      doStuffOn(qobject) {
        qobject.pushTask(() => {
          console.log("Doing stuff from", this);
        });
      qobject.runTasks();
      }
    }
    

    While you'd expect the output to be something such as:
    Doing stuff from [object Object]
    You may actually end up with:
    Doing stuff from undefined

    The example is simplified: in truth, you will never have this issue if runTasks is called right after pushTask. However, it will happen SOMETIMES if runTasks only gets called from another context. Say, if you would add a QTimer to this example, and trigger runTasks periodically rather than directly from JavaScript, you'd have a good chance to run in this issue.

    Up until now, I've been fixing the issue by using this form in JavaScript:

    export class MyObject {
      doStuffOn(qobject) {
        const self = this;
        qobject.pushTask(() => {
          console.log("Doing stuff from", self);
        });
      qobject.runTasks();
      }
    }
    

    Using a temporary variable to store this works. But I'm not completely satisfied with that solution.

    Any idea what's up with that ?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved