mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-14 12:53:14 +02:00
Modifications and improvments of the ArrayBuffers
This commit is contained in:
parent
05d74e2bb1
commit
1f6e1b9509
4 changed files with 16 additions and 16 deletions
|
@ -42,7 +42,7 @@ ArrayBufferClass::ArrayBufferClass(QScriptEngine* engine) : QObject(engine), QSc
|
|||
engine->globalObject().setProperty(name(), _ctor);
|
||||
}
|
||||
|
||||
QScriptValue ArrayBufferClass::newInstance(unsigned long size) {
|
||||
QScriptValue ArrayBufferClass::newInstance(quint32 size) {
|
||||
engine()->reportAdditionalMemoryCost(size);
|
||||
QScriptValue data = engine()->newVariant(QVariant::fromValue(QByteArray(size, 0)));
|
||||
return engine()->newObject(this, data);
|
||||
|
@ -60,7 +60,12 @@ QScriptValue ArrayBufferClass::construct(QScriptContext* context, QScriptEngine*
|
|||
}
|
||||
|
||||
QScriptValue arg = context->argument(0);
|
||||
unsigned long size = arg.toInt32();
|
||||
|
||||
if (!arg.isValid() || !arg.isNumber()) {
|
||||
return QScriptValue();
|
||||
}
|
||||
|
||||
quint32 size = arg.toInt32();
|
||||
QScriptValue newObject = cls->newInstance(size);
|
||||
|
||||
if (context->isCalledAsConstructor()) {
|
||||
|
|
|
@ -24,7 +24,7 @@ class ArrayBufferClass : public QObject, public QScriptClass {
|
|||
Q_OBJECT
|
||||
public:
|
||||
ArrayBufferClass(QScriptEngine* engine);
|
||||
QScriptValue newInstance(unsigned long size);
|
||||
QScriptValue newInstance(quint32 size);
|
||||
QScriptValue newInstance(const QByteArray& ba);
|
||||
|
||||
QueryFlags queryProperty(const QScriptValue& object,
|
||||
|
|
|
@ -9,9 +9,6 @@
|
|||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include <QDebug>
|
||||
#include <QScriptEngine>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "ArrayBufferClass.h"
|
||||
|
@ -23,26 +20,26 @@ Q_DECLARE_METATYPE(QByteArray*)
|
|||
ArrayBufferPrototype::ArrayBufferPrototype(QObject* parent) : QObject(parent) {
|
||||
}
|
||||
|
||||
QByteArray ArrayBufferPrototype::slice(long begin, long end) const {
|
||||
QByteArray ArrayBufferPrototype::slice(qint32 begin, qint32 end) const {
|
||||
QByteArray* ba = thisArrayBuffer();
|
||||
// if indices < 0 then they start from the end of the array
|
||||
begin = (begin < 0) ? ba->size() + begin : begin;
|
||||
end = (end < 0) ? ba->size() + end : end;
|
||||
|
||||
// here we clamp the indices to fit the array
|
||||
begin = glm::clamp(begin, 0l, (long)(ba->size() - 1));
|
||||
end = glm::clamp(end, 0l, (long)(ba->size() - 1));
|
||||
begin = glm::clamp(begin, 0, (ba->size() - 1));
|
||||
end = glm::clamp(end, 0, (ba->size() - 1));
|
||||
|
||||
return (end - begin > 0) ? ba->mid(begin, end - begin) : QByteArray();
|
||||
}
|
||||
|
||||
QByteArray ArrayBufferPrototype::slice(long begin) const {
|
||||
QByteArray ArrayBufferPrototype::slice(qint32 begin) const {
|
||||
QByteArray* ba = thisArrayBuffer();
|
||||
// if indices < 0 then they start from the end of the array
|
||||
begin = (begin < 0) ? ba->size() + begin : begin;
|
||||
|
||||
// here we clamp the indices to fit the array
|
||||
begin = glm::clamp(begin, 0l, (long)(ba->size() - 1));
|
||||
begin = glm::clamp(begin, 0, (ba->size() - 1));
|
||||
|
||||
return ba->mid(begin, -1);
|
||||
}
|
||||
|
|
|
@ -12,19 +12,17 @@
|
|||
#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) const;
|
||||
QByteArray slice(long begin) const;
|
||||
public slots:
|
||||
QByteArray slice(qint32 begin, qint32 end) const;
|
||||
QByteArray slice(qint32 begin) const;
|
||||
|
||||
private:
|
||||
QByteArray* thisArrayBuffer() const;
|
||||
|
|
Loading…
Reference in a new issue