summaryrefslogtreecommitdiff
path: root/examples/script/customclass/bytearrayclass.cpp
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-07-30 10:44:15 +0200
committerKent Hansen <khansen@trolltech.com>2009-07-30 10:48:45 +0200
commit776c4a5b8035b87d011b8e431c3d9e970ad98cd9 (patch)
tree1407bc256a512722e40fcb0b940c3880eb503e62 /examples/script/customclass/bytearrayclass.cpp
parentd2820e3a67812d31f45df282b378a75511bb1c4b (diff)
downloadqt4-tools-776c4a5b8035b87d011b8e431c3d9e970ad98cd9.tar.gz
fix two bugs in the custom script class example
1) fromScriptValue() needs to call qvariant_cast() on the data, in order to be symmetric with toScriptValue(). 2) use the overload of newFunction() that takes a prototype object, so that the prototype.constructor and constructor.prototype relationship is set up correctly; otherwise the instanceof operator won't work.
Diffstat (limited to 'examples/script/customclass/bytearrayclass.cpp')
-rw-r--r--examples/script/customclass/bytearrayclass.cpp9
1 files changed, 6 insertions, 3 deletions
diff --git a/examples/script/customclass/bytearrayclass.cpp b/examples/script/customclass/bytearrayclass.cpp
index 81a0b8aacd..2044a168a5 100644
--- a/examples/script/customclass/bytearrayclass.cpp
+++ b/examples/script/customclass/bytearrayclass.cpp
@@ -100,7 +100,7 @@ ByteArrayClass::ByteArrayClass(QScriptEngine *engine)
QScriptValue global = engine->globalObject();
proto.setPrototype(global.property("Object").property("prototype"));
- ctor = engine->newFunction(construct);
+ ctor = engine->newFunction(construct, proto);
ctor.setData(qScriptValueFromValue(engine, this));
}
//! [0]
@@ -224,7 +224,10 @@ QScriptValue ByteArrayClass::construct(QScriptContext *ctx, QScriptEngine *)
ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctx->callee().data());
if (!cls)
return QScriptValue();
- int size = ctx->argument(0).toInt32();
+ QScriptValue arg = ctx->argument(0);
+ if (arg.instanceOf(ctx->callee()))
+ return cls->newInstance(qscriptvalue_cast<QByteArray>(arg));
+ int size = arg.toInt32();
return cls->newInstance(size);
}
//! [2]
@@ -240,7 +243,7 @@ QScriptValue ByteArrayClass::toScriptValue(QScriptEngine *eng, const QByteArray
void ByteArrayClass::fromScriptValue(const QScriptValue &obj, QByteArray &ba)
{
- ba = qscriptvalue_cast<QByteArray>(obj.data());
+ ba = qvariant_cast<QByteArray>(obj.data().toVariant());
}