QScriptEngine embedded evaluate's and functions.



  • First question, is there any harm calling QScriptEngine's evaluate from within an evaluate? For example if I have a script that can call back into a QObject that then can trigger another evaulate on the same QScriptEngine. This seems to be working ok but thought I should double check before I rely on it.

    Second question. I am importing .js files into my environment to extend my scripting environment before I evaluate my main script and this is working great, however one thing I do to evaluate my main script is wrap it in a function and when I try to "include" files from within this function I can not access them.

    A little background.

    I have a global object I have created available to scripts that let them do various things, lets call this object "Env", one of the things a script can do is call: Env.include("someFile.js"); and that will evaulate the file on its QScriptEngine.

    Now lets assume "someFile.js" has a simple that returns a number.

    someFile.js:
    @
    function getVal(){ return 100; };
    @

    As I mentioned my main script gets wrapped in a function so in the end it might look like this:

    @
    function main()
    {
    Env.include("someFile.js");
    var x = getVal();
    Env.print(x);
    return Env.success();
    }
    main();
    @

    The reason I wrap the script is because I want the user to return a success or failure and if I don't do that then "return" is unaccepted.

    Now, the above example fails however if I do this then it works:

    @
    function main()
    {
    function getVal(){ return 100; };
    var x = getVal();
    Env.print(x);
    return Env.success();
    }
    main();
    @

    Is there a way to coax QScriptEngine into evaluating an "include" script at a global level or somehow make it work as it does in this last example?

    for completeness here is what my Env.include function looks like:

    @
    bool Env::includeScript(const QString & file)
    {
    QString script = load file contents;
    QScriptValue v = _scriptEngine->evaluate(script, file);
    if( v.isError() )
    {
    display error
    return false;
    }
    return true;
    }
    @

    When it fails v is an error with the message "Can't find variable: getVal"

    Hopefully this is all making sense. :) Thanks!


Log in to reply
 

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