QScriptContext thisObject in custom include function



  • Hi,
    I have a question regarding thisObject & activationObject in QScriptContext:
    I have written a custom include function to allow including other script files from custom script files, similar to the code in QScriptContext documentation: http://developer.qt.nokia.com/doc/qt-4.8/qscriptcontext.html#details

    This is the code from the doc:
    @QScriptValue myInclude(QScriptContext *ctx, QScriptEngine *eng)
    {
    QString fileName = ctx->argument(0).toString();
    QString contents = readTheFile(fileName);
    ctx->setActivationObject(ctx->parentContext()->activationObject());
    ctx->setThisObject(ctx->parentContext()->thisObject());
    return eng->evaluate(contents, fileName);
    }@

    However, after I set the thisObject and activationObject to the one of the parent context, they are undefined in the functions I call in the included script. After looking at the context backtrace, it turns out that a new <eval> context is added with the correct thisObject applied, but the top-most context has no thisObject and acitivationObject assigned.

    So to sum it up, if i set a QObject as thisObject for script A and include script B from A, I can NOT access the properties of my QObject from B. Instead I get an assertion stating the thisObject is not a qobject:

    ASSERT failure in QTJSC::JSValue QScript::QtPropertyFunction::execute(QTJSC::ExecState*, QTJSC::JSValue, const QTJSC::ArgList&): "this-object must be a QObject", file bridge\qscriptqobject.cpp, line 1085

    I hope anybody can help,
    Chris



  • After further investigating this issue, it gets even more unclear what its cause or solution might be. The following example demonstrates its effect:

    Suppose to have 2 script files: A.js and B.js. Script A includes B and calls functions of B:

    B.js:
    @function test1() {
    print("test1.this: " + this);
    test2();
    }

    function test2() {
    print("test2.this: " + this);
    }@

    A.js:
    @include("B.js");
    test2(); // outputs test2.this correctly
    test1(); // outputs test1.this correctly, BUT causes an exception in test2 because this is not known there!?!@

    Might this be a Qt bug?


Log in to reply
 

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