Qt-interest Archive, June 2007
QtScript: Bug in PropertyGetter?
Message 1 in thread
I have this small testcase:
#include <QCoreApplication>
#include <QScriptEngine>
static int foo;
static QScriptValue f_getSetFoo(QScriptContext* context, QScriptEngine* engine)
{
if (context->argumentCount() == 1)
foo = context->argument(0).toInt32();
return engine->toScriptValue(foo);
} // f_getSetFoo
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QScriptEngine e;
e.globalObject().setProperty(
"foo", e.newFunction(f_getSetFoo),
QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
foo = 10;
e.evaluate(
"print(++foo);\n"
"print(++foo);\n"
"print(--foo);\n"
"print(foo);\n"
);
}
I would expect the output to be:
11
12
11
11
However in fact it is:
11
12
11
function () { [native] }
Seems the PropertyGetter is not called consistently?
--
[ signature omitted ]
Message 2 in thread
Seneca wrote:
> I have this small testcase:
>
> #include <QCoreApplication>
> #include <QScriptEngine>
>
> static int foo;
>
> static QScriptValue f_getSetFoo(QScriptContext* context, QScriptEngine* engine)
> {
> if (context->argumentCount() == 1)
> foo = context->argument(0).toInt32();
> return engine->toScriptValue(foo);
> } // f_getSetFoo
>
> int main(int argc, char *argv[])
> {
> QCoreApplication a(argc, argv);
> QScriptEngine e;
> e.globalObject().setProperty(
> "foo", e.newFunction(f_getSetFoo),
> QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
> foo = 10;
> e.evaluate(
> "print(++foo);\n"
> "print(++foo);\n"
> "print(--foo);\n"
> "print(foo);\n"
> );
> }
>
> I would expect the output to be:
>
> 11
> 12
> 11
> 11
>
> However in fact it is:
>
> 11
> 12
> 11
> function () { [native] }
>
> Seems the PropertyGetter is not called consistently?
>
Hi Seneca,
Thanks for your report. Yes, you've found a bug. When requesting the
value of a getter/setter-based global variable from a global context
(i.e. not within a function, and without using any prefix or postfix
operator), the value isn't resolved correctly. It's been fixed for
4.3.1. You can work around it by using the expression "this.foo" (in the
global context, the "this" object is the Global Object).
Regards,
Kent
--
[ signature omitted ]