#include <QtScript/QScriptClassPropertyIterator>
#include <QtScript/QScriptContext>
#include <QtScript/QScriptEngine>
#include "bytearrayclass.h"
#include "bytearrayprototype.h"
#include <stdlib.h>
Q_DECLARE_METATYPE(QByteArray*)
Q_DECLARE_METATYPE(ByteArrayClass*)
class ByteArrayClassPropertyIterator : public QScriptClassPropertyIterator
{
public:
ByteArrayClassPropertyIterator(const QScriptValue &object);
~ByteArrayClassPropertyIterator();
bool hasNext() const;
void next();
bool hasPrevious() const;
void previous();
void toFront();
void toBack();
QScriptString name() const;
uint id() const;
private:
int m_index;
int m_last;
};
ByteArrayClass::ByteArrayClass(QScriptEngine *engine)
: QObject(engine), QScriptClass(engine)
{
qScriptRegisterMetaType<QByteArray>(engine, toScriptValue, fromScriptValue);
length = engine->toStringHandle(QLatin1String("length"));
proto = engine->newQObject(new ByteArrayPrototype(this),
QScriptEngine::QtOwnership,
QScriptEngine::SkipMethodsInEnumeration
| QScriptEngine::ExcludeSuperClassMethods
| QScriptEngine::ExcludeSuperClassProperties);
QScriptValue global = engine->globalObject();
proto.setPrototype(global.property("Object").property("prototype"));
ctor = engine->newFunction(construct, proto);
ctor.setData(engine->toScriptValue(this));
}
ByteArrayClass::~ByteArrayClass()
{
}
QScriptClass::QueryFlags ByteArrayClass::queryProperty(const QScriptValue &object,
const QScriptString &name,
QueryFlags flags, uint *id)
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
if (!ba)
return 0;
if (name == length) {
return flags;
} else {
bool isArrayIndex;
qint32 pos = name.toArrayIndex(&isArrayIndex);
if (!isArrayIndex)
return 0;
*id = pos;
if ((flags & HandlesReadAccess) && (pos >= ba->size()))
flags &= ~HandlesReadAccess;
return flags;
}
}
QScriptValue ByteArrayClass::property(const QScriptValue &object,
const QScriptString &name, uint id)
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
if (!ba)
return QScriptValue();
if (name == length) {
return ba->length();
} else {
qint32 pos = id;
if ((pos < 0) || (pos >= ba->size()))
return QScriptValue();
return uint(ba->at(pos)) & 255;
}
return QScriptValue();
}
void ByteArrayClass::setProperty(QScriptValue &object,
const QScriptString &name,
uint id, const QScriptValue &value)
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
if (!ba)
return;
if (name == length) {
resize(*ba, value.toInt32());
} else {
qint32 pos = id;
if (pos < 0)
return;
if (ba->size() <= pos)
resize(*ba, pos + 1);
(*ba)[pos] = char(value.toInt32());
}
}
QScriptValue::PropertyFlags ByteArrayClass::propertyFlags(
const QScriptValue &, const QScriptString &name, uint )
{
if (name == length) {
return QScriptValue::Undeletable
| QScriptValue::SkipInEnumeration;
}
return QScriptValue::Undeletable;
}
QScriptClassPropertyIterator *ByteArrayClass::newIterator(const QScriptValue &object)
{
return new ByteArrayClassPropertyIterator(object);
}
QString ByteArrayClass::name() const
{
return QLatin1String("ByteArray");
}
QScriptValue ByteArrayClass::prototype() const
{
return proto;
}
QScriptValue ByteArrayClass::constructor()
{
return ctor;
}
QScriptValue ByteArrayClass::newInstance(int size)
{
engine()->reportAdditionalMemoryCost(size);
return newInstance(QByteArray(size, 0));
}
QScriptValue ByteArrayClass::newInstance(const QByteArray &ba)
{
QScriptValue data = engine()->newVariant(QVariant::fromValue(ba));
return engine()->newObject(this, data);
}
QScriptValue ByteArrayClass::construct(QScriptContext *ctx, QScriptEngine *)
{
ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctx->callee().data());
if (!cls)
return QScriptValue();
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);
}
QScriptValue ByteArrayClass::toScriptValue(QScriptEngine *eng, const QByteArray &ba)
{
QScriptValue ctor = eng->globalObject().property("ByteArray");
ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctor.data());
if (!cls)
return eng->newVariant(QVariant::fromValue(ba));
return cls->newInstance(ba);
}
void ByteArrayClass::fromScriptValue(const QScriptValue &obj, QByteArray &ba)
{
ba = qvariant_cast<QByteArray>(obj.data().toVariant());
}
void ByteArrayClass::resize(QByteArray &ba, int newSize)
{
int oldSize = ba.size();
ba.resize(newSize);
if (newSize > oldSize)
engine()->reportAdditionalMemoryCost(newSize - oldSize);
}
ByteArrayClassPropertyIterator::ByteArrayClassPropertyIterator(const QScriptValue &object)
: QScriptClassPropertyIterator(object)
{
toFront();
}
ByteArrayClassPropertyIterator::~ByteArrayClassPropertyIterator()
{
}
bool ByteArrayClassPropertyIterator::hasNext() const
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
return m_index < ba->size();
}
void ByteArrayClassPropertyIterator::next()
{
m_last = m_index;
++m_index;
}
bool ByteArrayClassPropertyIterator::hasPrevious() const
{
return (m_index > 0);
}
void ByteArrayClassPropertyIterator::previous()
{
--m_index;
m_last = m_index;
}
void ByteArrayClassPropertyIterator::toFront()
{
m_index = 0;
m_last = -1;
}
void ByteArrayClassPropertyIterator::toBack()
{
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
m_index = ba->size();
m_last = -1;
}
QScriptString ByteArrayClassPropertyIterator::name() const
{
return object().engine()->toStringHandle(QString::number(m_last));
}
uint ByteArrayClassPropertyIterator::id() const
{
return m_last;
}