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

QJSEngine/QQmlEngine: Support manipulating variadic JS functions from C++

    XMLWordPrintable

Details

    Description

      Qt Script lets us do things like this; it would be helpful to have a similar ability in QJSEngine:

      QScriptValue ScriptContext::scriptAbortAfter(QScriptContext* ctx, QScriptEngine& eng)
      {
          try
          {
              if (ctx->argumentCount() < 2)
                  throw Exception(L"abortAfter(timeout, callback[, callback parameters...]) takes at least 2 parameters.");
      
              if (!ctx->argument(0).isNumber())
                  throw Exception(L"Parameter 1 (timeout) must be an integer specifying the timeout in milliseconds.");
      
              if (!ctx->argument(1).isFunction())
                  throw Exception(L"Parameter 2 (callback) must be a function.");
      
              int timeout = ctx->argument(0).toInteger();
              QScriptValue function = ctx->argument(1);
      
              QScriptValueList callbackParameters;
              for (int i = 2; i < ctx->argumentCount(); ++i)
              callbackParameters.push_back(ctx->argument(i));
      
              auto future = std::async(std::launch::async, [&]() {
                  QScriptValue value;
                  value = function.call(ctx->thisObject(), callbackParameters);
                  return value;
              });
          }
          ...
      }
      
      

       

      Side note

      For this particular example, an alternative solution might be to add support for ECMAScript 2017. Then, this example C++ code can be replaced by native JS async functions

      Attachments

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

        Activity

          People

            qtqmlteam Qt Qml Team User
            skoh-qt Sze Howe Koh
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes