Ouch. No, I didn't get you. But now that I do, I can't think of a way to achieve this. If you were to implement your function like below you can access the very ScriptValue it was called from. But I don't know of a way to get the variable name. Sorry! QScriptValue doesn't feature a name property or something like that.
What I still don't get is your motivation to get the variable name. What do you want to do with it? You want to generate other scripts/code which access that variable? Well you can do that by accessing its QScriptValue passed as argument or as the this object like so:
That's the QScriptEngine::FunctionSignature.
QScriptValue CDBGetActionScript(QScriptContext *ctx, QScriptEngine *eng)
{
CDB* cdb = eng->fromScriptValue<CDB*>(ctx->thisObject());
return cdb->actionscript;
}
QScriptValue CDBGetActionScript(QScriptContext *ctx, QScriptEngine *eng)
{
CDB* cdb = eng->fromScriptValue<CDB*>(ctx->thisObject());
return cdb->actionscript;
}
To copy to clipboard, switch view to plain text mode
So here with the context's thisObject you can access the QScriptValue from which the function was called.
You need to register that function to each ScriptValue of that type.. upon its creation / conversion.
QScriptValue CDBToScriptValue(QScriptEngine *engine, CDB* const &in)
{
QScriptValue sv = engine->newQObject(in,QScriptEngine::QtOwnership);
sv.setProperty("Action",engine->newFunction(CDBGetActionScript),QScriptValue::PropertyGetter);
return sv;
}
void CDBFromScriptValue(const QScriptValue &object, CDB* &out)
{
out = qobject_cast<CDB*>(object.toQObject());
}
QScriptValue CDBToScriptValue(QScriptEngine *engine, CDB* const &in)
{
QScriptValue sv = engine->newQObject(in,QScriptEngine::QtOwnership);
sv.setProperty("Action",engine->newFunction(CDBGetActionScript),QScriptValue::PropertyGetter);
return sv;
}
void CDBFromScriptValue(const QScriptValue &object, CDB* &out)
{
out = qobject_cast<CDB*>(object.toQObject());
}
To copy to clipboard, switch view to plain text mode
As an alternative you could create a prototype for the type. And add the function to it only once.
Right direction?
Johannes
Bookmarks