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

Directly evaluated anonymous function is considered syntactically incorrect

    XMLWordPrintable

Details

    Description

      You cannot evaluate an anonymous function directly, it yields a syntax error but the expected result is true wrapped in a QJSValue. Note the function call directly after the function declaration.

      Code to reproduce the error:

      QJSEngine engine;
      QJSValue result = engine.evaluate("function(){return true}()");
      if (result.isError()) {
          qWarning("%s", qPrintable(result.toString()));
      }
      

      Output:

      SyntaxError: unexpected token
      

      It works fine if I surround the function declaration with parenthesis, but other engines support the other way, though:

      (function(){return true})();
      

      Attachments

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

        Activity

          People

            shausman Simon Hausmann
            blochberger Maximilian Blochberger (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes