mirror of
https://github.com/Armored-Dragon/overte.git
synced 2025-03-11 16:13:16 +01:00
Base implementation of ArrayBuffer
This commit is contained in:
parent
12eeb50cbe
commit
c246205692
4 changed files with 222 additions and 0 deletions
105
libraries/script-engine/src/ArrayBuffer.cpp
Normal file
105
libraries/script-engine/src/ArrayBuffer.cpp
Normal file
|
@ -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<QByteArray>(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<ArrayBuffer*>(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<QByteArray*>(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<QByteArray*>(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<ArrayBuffer*>(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<QByteArray>(obj.data().toVariant());
|
||||
}
|
||||
|
56
libraries/script-engine/src/ArrayBuffer.h
Normal file
56
libraries/script-engine/src/ArrayBuffer.h
Normal file
|
@ -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 <QScriptClass>
|
||||
#include <QtCore/QObject>
|
||||
#include <QtScript/QScriptClass>
|
||||
#include <QtScript/QScriptContext>
|
||||
#include <QtScript/QScriptEngine>
|
||||
#include <QtScript/QScriptString>
|
||||
#include <QtScript/QScriptValue>
|
||||
|
||||
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
|
29
libraries/script-engine/src/ArrayBufferPrototype.cpp
Normal file
29
libraries/script-engine/src/ArrayBufferPrototype.cpp
Normal file
|
@ -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 <QScriptEngine>
|
||||
|
||||
#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<QByteArray>(thisObject().data());
|
||||
}
|
32
libraries/script-engine/src/ArrayBufferPrototype.h
Normal file
32
libraries/script-engine/src/ArrayBufferPrototype.h
Normal file
|
@ -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 <QtCore/QByteArray>
|
||||
#include <QtCore/QObject>
|
||||
#include <QtScript/QScriptable>
|
||||
#include <QtScript/QScriptValue>
|
||||
|
||||
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
|
Loading…
Reference in a new issue