Importing files with evaluate() function



  • When I run this code, I get a SyntaxError as a result. AFAICT, there's nothing wrong with the syntax. Why wouldn't this work?

    @
    QQmlEngine engine;
    QJSValue res = engine.evaluate("import "test.js" as Test");
    @


  • Moderators

    Hi,

    The QML engine supports ECMAScript 5. Import statements do not exist in this version of JavaScript; they are a new feature of ECMAScript 6, which hasn't been released yet.

    However, the QML engine lets you do import in JavaScript files in the following ways:
    @
    // Write a dot before "import"
    .import "test.js" as Test
    @
    @
    Qt.include("test.js")
    @

    See also:



  • This still doesn't work from with this code:

    @
    QQmlEngine engine;
    QJSValue res = engine.evaluate(".import "test.js" as Test");
    if(res.isError()) {
    std::cout << "uncaught exception: \n" << res.toString().toStdString() << "\n";;
    std::cout << res.property("lineNumber").toInt() << "\n";
    std::cout << res.property("fileName").toString().toStdString() << "\n";
    }
    @

    I get the following error:

    uncaught exception:
    SyntaxError: Syntax error
    1

    I have two comments about this:

    Why doesn't this work?

    Why don't I get a better error message?


  • Moderators

    Hi,

    Sorry for being unclear before. The imports statements I wrote in the previous post are QML extensions to JavaScript documents. However, they are not valid JavaScript statements.

    The function you are calling is QJSEngine::evaluate(). It only works with "pure" JavaScript. It only works with code that you can put in the "[INSERT JS CODE HERE]" block below:

    @
    Rectangle {
    MouseArea {
    anshors.fill: parent
    onClicked: {
    // [INSERT JS CODE HERE]
    }
    }
    }
    @

    You cannot put import statements in that block.

    [quote]I have two comments about this:

    Why doesn’t this work?

    Why don’t I get a better error message?

    [/quote]

    Because .import "test.js" as Test is not valid JavaScript code, as explained above.

    How can the message be improved?

    Anyway, what are you trying to accomplish with that evaluation?



  • bq. How can the message be improved?

    Well, it could give a line and column number somehow. I can get the line number from the Error object. Is there any way to get the column number of the error? Also, it just says SyntaxError. There's no mention of "an invalid expression .import" or something that would actually tell me which part of the code was bad. It could've been a missing quote mark on a string from a typo and I wouldn't know the difference because the error message was so vague.

    My goal is to figure out the best way to incorporate a number of JS libs into my project. I was hoping to use some kind of import/require mechanism to do this.

    Since the import syntax only works for QML, I'm wondering if there's a way to push a C function into the JSEngine. I want to write a function in C/C++ that can lookup JS resources and push them back into the JSEngine for evaluation. I know you can do this with V8, but I see no public API for doing this with Qt's JSEngine.


  • Moderators

    [quote author="weshoke" date="1412312965"]Well, it could give a line and column number somehow. I can get the line number from the Error object. Is there any way to get the column number of the error? Also, it just says SyntaxError. There's no mention of "an invalid expression .import" or something that would actually tell me which part of the code was bad. It could've been a missing quote mark on a string from a typo and I wouldn't know the difference because the error message was so vague.[/quote]You're right, there's much room for improvement. If you wish, you can submit a request for more detailed messages at http://bugreports.qt-project.org/

    [quote]My goal is to figure out the best way to incorporate a number of JS libs into my project. I was hoping to use some kind of import/require mechanism to do this.

    Since the import syntax only works for QML, I'm wondering if there's a way to push a C function into the JSEngine. I want to write a function in C/C++ that can lookup JS resources and push them back into the JSEngine for evaluation. I know you can do this with V8, but I see no public API for doing this with Qt's JSEngine.[/quote]What kind of project is it?

    • If it's a web-based application, use Qt WebKit (or even better, Qt WebEngine).
    • If it's a QML application, write the import statements directly in your *.js files.
    • If it's a standalone JavaScript application à la Node.js, or if you're looking to use Node.js modules with C++, then QJSEngine might (currently?) be a poor fit for your needs.

    Some history: Qt 5.0 and Qt 5.1 used the V8 engine, but it was a "poor fit for QML applications":http://blog.qt.digia.com/blog/2013/04/15/evolution-of-the-qml-engine-part-1/ because the target use cases are very different. Thus, Qt 5.2 came with a custom engine, designed specifically for QML applications in mind. As a result, the performance of QML applications improved dramatically, but the performance of "pure" JavaScript code was sacrificed.

    Nonetheless, the Qt engine does have a mechanism for importing resources. I don't know how to access that mechanism from C++, but I recommend subscribing to the "Interest mailing list":http://lists.qt-project.org/mailman/listinfo/interest and asking there. The Qt devs might be able to help you out.



  • Thanks for the pointers. The app I'm working on is heavily GPU oriented. I tried to use QML, but it doesn't play well with OpenGL 3 or 4 right now. Supposedly this will change in Qt 5.4?

    I'm trying to use JavaScript as a string manipulation engine. I have some JS libs from another project that I use to run templates through to generate GLSL. WebKit is out of the question for this project, so I'm trying to use the JSEngine as my text processor in concert with a QGLWidget.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.