[Development] QJSEngine replacement for QScriptEngine misses newFunction

Walter Stefan Stefan.Walter at lisec.com
Tue Feb 23 06:24:17 CET 2016


Hi,

I am looking into migrating my code to QJSEngine, because of the deprecation of the QScriptEngine (QtScript). As we have used the functionality of newFunction very extensive and all related scripts are depending on this, the newFunction in QJSEngine is for us mandatory or something that creates and equivalent result.

How do I currently map a native C++ class member function to the engine:
QScriptValue func_sqrt = m_engine->newFunction(sqrt, liEngine);
m_engine->globalObject().setProperty("sqrt", func_sqrt, QScriptValue::ReadOnly | QScriptValue::Undeletable);

This is an example of a native function:
QScriptValue ScriptModuleMath::sqrt(QScriptContext *context, QScriptEngine *engine, void *arg)
{
    Q_UNUSED(engine)
    Q_UNUSED(arg)
    if (context->argumentCount()!=1)
    {
        return context->throwError( QScriptContext::SyntaxError, "sqrt(...): illegal number of arguments.");
    } else if (!context->argument(0).isNumber())
    {
        return context->throwError( QScriptContext::SyntaxError, "sqrt(...): argument 1 is not a number.");
    }

    return qSqrt(context->argument(0).toNumber());
}

And in this way. I can use it then in the script:
value = sqrt(4);

I actually don't want to map a whole QObject, because that would require a call like this:
value = MyMath.sqrt(4);

Is there any way to achive in QJSEngine the same result as I do within the QScriptEngine?

Best Regards,
Stefan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20160223/39315bd6/attachment.html>


More information about the Development mailing list