diff --git a/libraries/script-engine/src/ArrayBuffer.cpp b/libraries/script-engine/src/ArrayBuffer.cpp new file mode 100644 index 0000000000..561a8cd5bf --- /dev/null +++ b/libraries/script-engine/src/ArrayBuffer.cpp @@ -0,0 +1,105 @@ +// +// ArrayBuffer.cpp +// +// +// Created by Clement on 7/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +#include "ArrayBufferPrototype.h" + +#include "ArrayBuffer.h" + +Q_DECLARE_METATYPE(QByteArray*) +Q_DECLARE_METATYPE(ArrayBuffer*) + +ArrayBuffer::ArrayBuffer(QScriptEngine* engine) : QObject(engine), QScriptClass(engine) { + qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue); + + _byteLength = engine->toStringHandle(QLatin1String("byteLength")); + +// _proto = engine->newQObject(new ArrayBufferPrototype(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)); +} + +QScriptValue ArrayBuffer::newInstance(unsigned long size) { + engine()->reportAdditionalMemoryCost(size); + QScriptValue data = engine()->newVariant(QVariant::fromValue(QByteArray(size, 0))); + return engine()->newObject(this, data); +} + +QScriptValue ArrayBuffer::newInstance(const QByteArray& ba) { + QScriptValue data = engine()->newVariant(QVariant::fromValue(ba)); + return engine()->newObject(this, data); +} + +QScriptValue ArrayBuffer::construct(QScriptContext* ctx, QScriptEngine* eng) { + ArrayBuffer* cls = qscriptvalue_cast(ctx->callee().data()); + QScriptValue arg = ctx->argument(0); + int size = arg.toInt32(); + return cls ? cls->newInstance(size) : QScriptValue(); +} + +QScriptClass::QueryFlags ArrayBuffer::queryProperty(const QScriptValue& object, + const QScriptString& name, + QueryFlags flags, uint* id) { + QByteArray* ba = qscriptvalue_cast(object.data()); + if (ba && name == _byteLength) { + // if the property queried is byteLength, only handle read access + return flags &= HandlesReadAccess; + } + return 0; // No access +} + +QScriptValue ArrayBuffer::property(const QScriptValue &object, + const QScriptString &name, uint id) { + QByteArray* ba = qscriptvalue_cast(object.data()); + if (ba && name == _byteLength) { + return ba->length(); + } + return QScriptValue(); +} + +QScriptValue::PropertyFlags ArrayBuffer::propertyFlags(const QScriptValue& object, + const QScriptString& name, uint id) { + return QScriptValue::Undeletable; +} + +QString ArrayBuffer::name() const { + return QLatin1String("ArrayBuffer"); +} + +QScriptValue ArrayBuffer::prototype() const { + return _proto; +} + +QScriptValue ArrayBuffer::constructor() const { + return _ctor; +} + +QScriptValue ArrayBuffer::toScriptValue(QScriptEngine* eng, const QByteArray& ba) +{ + QScriptValue ctor = eng->globalObject().property("ArrayBuffer"); + ArrayBuffer *cls = qscriptvalue_cast(ctor.data()); + if (!cls) { + return eng->newVariant(QVariant::fromValue(ba)); + } + return cls->newInstance(ba); +} + +void ArrayBuffer::fromScriptValue(const QScriptValue& obj, QByteArray& ba) { + ba = qvariant_cast(obj.data().toVariant()); +} + diff --git a/libraries/script-engine/src/ArrayBuffer.h b/libraries/script-engine/src/ArrayBuffer.h new file mode 100644 index 0000000000..f951a1248a --- /dev/null +++ b/libraries/script-engine/src/ArrayBuffer.h @@ -0,0 +1,56 @@ +// +// ArrayBuffer.h +// +// +// Created by Clement on 7/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_ArrayBuffer_h +#define hifi_ArrayBuffer_h + +#include +#include +#include +#include +#include +#include +#include + +class ArrayBuffer : public QObject, public QScriptClass { +public: + ArrayBuffer(QScriptEngine* engine); + QScriptValue newInstance(unsigned long size = 0); + QScriptValue newInstance(const QByteArray& ba); + + QueryFlags queryProperty(const QScriptValue& object, + const QScriptString& name, + QueryFlags flags, uint* id); + QScriptValue property(const QScriptValue& object, + const QScriptString& name, + uint id); + QScriptValue::PropertyFlags propertyFlags(const QScriptValue& object, + const QScriptString& name, + uint id); + + QString name() const; + QScriptValue prototype() const; + QScriptValue constructor() const; + +private: + static QScriptValue construct(QScriptContext* ctx, QScriptEngine* eng); + + static QScriptValue toScriptValue(QScriptEngine* eng, const QByteArray& ba); + static void fromScriptValue(const QScriptValue& obj, QByteArray& ba); + + QScriptValue _proto; + QScriptValue _ctor; + + // JS Object attributes + QScriptString _byteLength; +}; + +#endif // hifi_ArrayBuffer_h \ No newline at end of file diff --git a/libraries/script-engine/src/ArrayBufferPrototype.cpp b/libraries/script-engine/src/ArrayBufferPrototype.cpp new file mode 100644 index 0000000000..01736cc511 --- /dev/null +++ b/libraries/script-engine/src/ArrayBufferPrototype.cpp @@ -0,0 +1,29 @@ +// +// ArrayBufferPrototype.cpp +// +// +// Created by Clement on 7/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include + +#include "ArrayBufferPrototype.h" + +ArrayBufferPrototype::ArrayBufferPrototype(QObject* parent) : QObject(parent) { +} + +QByteArray ArrayBufferPrototype::slice(long begin, long end) const { + if (end == -1) { + return thisArrayBuffer().mid(begin); + } else { + return thisArrayBuffer().mid(begin, end); + } +} + +QByteArray ArrayBufferPrototype::thisArrayBuffer() const { + return qscriptvalue_cast(thisObject().data()); +} diff --git a/libraries/script-engine/src/ArrayBufferPrototype.h b/libraries/script-engine/src/ArrayBufferPrototype.h new file mode 100644 index 0000000000..829fb8e3ec --- /dev/null +++ b/libraries/script-engine/src/ArrayBufferPrototype.h @@ -0,0 +1,32 @@ +// +// ArrayBufferPrototype.h +// +// +// Created by Clement on 7/3/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_ArrayBufferPrototype_h +#define hifi_ArrayBufferPrototype_h + +#include +#include +#include +#include + +class ArrayBufferPrototype : public QObject, public QScriptable { + Q_OBJECT +public: + ArrayBufferPrototype(QObject* parent = NULL); + +public slots: + QByteArray slice(long begin, long end = -1) const; + +private: + QByteArray thisArrayBuffer() const; +}; + +#endif // hifi_ArrayBufferPrototype_h \ No newline at end of file