summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/script/customclass/bytearrayclass.cpp9
-rw-r--r--examples/script/customclass/main.cpp4
2 files changed, 10 insertions, 3 deletions
diff --git a/examples/script/customclass/bytearrayclass.cpp b/examples/script/customclass/bytearrayclass.cpp
index 81a0b8a..2044a16 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());
}
diff --git a/examples/script/customclass/main.cpp b/examples/script/customclass/main.cpp
index 05aefd5..3b98f5c 100644
--- a/examples/script/customclass/main.cpp
+++ b/examples/script/customclass/main.cpp
@@ -52,6 +52,7 @@ int main(int argc, char **argv)
eng.globalObject().setProperty("ByteArray", baClass->constructor());
qDebug() << "ba = new ByteArray(4):" << eng.evaluate("ba = new ByteArray(4)").toString();
+ qDebug() << "ba instanceof ByteArray:" << eng.evaluate("ba instanceof ByteArray").toBool();
qDebug() << "ba.length:" << eng.evaluate("ba.length").toNumber();
qDebug() << "ba[1] = 123; ba[1]:" << eng.evaluate("ba[1] = 123; ba[1]").toNumber();
qDebug() << "ba[7] = 224; ba.length:" << eng.evaluate("ba[7] = 224; ba.length").toNumber();
@@ -65,6 +66,9 @@ int main(int argc, char **argv)
qDebug() << "ba.toBase64().toLatin1String():" << eng.evaluate("b64.toLatin1String()").toString();
qDebug() << "ba.valueOf():" << eng.evaluate("ba.valueOf()").toString();
qDebug() << "ba.chop(2); ba.length:" << eng.evaluate("ba.chop(2); ba.length").toNumber();
+ qDebug() << "ba2 = new ByteArray(ba):" << eng.evaluate("ba2 = new ByteArray(ba)").toString();
+ qDebug() << "ba2.equals(ba):" << eng.evaluate("ba2.equals(ba)").toBool();
+ qDebug() << "ba2.equals(new ByteArray()):" << eng.evaluate("ba2.equals(new ByteArray())").toBool();
return 0;
}