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

Improve import statement handling in QJSEngine::evaluate

    XMLWordPrintable

Details

    Description

      Evaluating a script that contains an import statement with QJSEngine::evaluate() does not work.

      import "file.js" as Test
      
      Test.someFunction('value')
      

      results in "SyntaxError: Unexpected token `import'"

      .import "file.js" as Test
      
      Test.someFunction('value')
      

      results in "ReferenceError: Test is not defined"

      With QQmlExpression, any import statement variant results in a syntax error. I have not loaded any QML document here, I just want to execute a plain JS script.

      #include <QCoreApplication>
      #include <QDebug>
      #include <QTimer>
      #include <QQmlEngine>
      
      QString script = "\
      .import 'file.js' as Test\n\
      \n\
      Test.someFunction('value')\n\
      ";
      
      void test() {
          QQmlEngine *engine = new QQmlEngine(QCoreApplication::instance());
      
          QJSValue value = engine->evaluate(script);
          if (value.isError()) {
              qDebug() << value.toString();
          }
          qDebug() << value.toVariant();
          QCoreApplication::quit();
      }
      
      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
          QTimer::singleShot(0, &app, &test);
          return app.exec();
      }
      
      
      // file.js
      
      export var someVar = "blabla";
      
      export function someFunction(someArg) {
          return someArg;
      }
      

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            rweickelt Richard Weickelt
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes