Added Uint8ClampedArray

This commit is contained in:
Atlante45 2014-07-11 18:27:26 -07:00
parent 5a8569fcd5
commit 6d4bb0f4fd
5 changed files with 56 additions and 2 deletions

View file

@ -80,7 +80,7 @@ UnitTest.prototype.arrayEqual = function(array1, array2, message) {
}
for (var i = 0; i < array1.length; ++i) {
if (array1[i] !== array2[i]) {
throw new AssertionException(array1[i], array2[i], message);
throw new AssertionException(array1[i], array2[i], i + " " + message);
}
}
}

View file

@ -687,3 +687,22 @@ test('Typed Array getters/setters', function () {
this.assertEquals(bytes[1], 0xff);
});
test('Uint8ClampedArray', function () {
this.assertEquals(Uint8ClampedArray.BYTES_PER_ELEMENT, 1, 'Uint8ClampedArray.BYTES_PER_ELEMENT');
var a = new Uint8ClampedArray([-Infinity, -Number.MAX_VALUE, -1, -Number.MIN_VALUE, -0,
0, Number.MIN_VALUE, 1, 1.1, 1.9, 255, 255.1, 255.9, 256, Number.MAX_VALUE, Infinity,
NaN]);
this.assertEquals(a.BYTES_PER_ELEMENT, 1);
this.assertEquals(a.byteOffset, 0);
this.assertEquals(a.byteLength, 17);
this.arrayEqual(a, [0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0], "array test");
});
test('Regression Tests', function() {
// Bug: https://github.com/inexorabletash/polyfill/issues/16
var minFloat32 = 1.401298464324817e-45;
var truncated = new Float32Array([-minFloat32 / 2 - Math.pow(2, -202)]).get(0);
this.assertEquals(truncated, -minFloat32, 'smallest 32 bit float should not truncate to zero');
});

View file

@ -100,6 +100,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam
new DataViewClass(this);
new Int8ArrayClass(this);
new Uint8ArrayClass(this);
new Uint8ClampedArrayClass(this);
new Int16ArrayClass(this);
new Uint16ArrayClass(this);
new Int32ArrayClass(this);
@ -141,6 +142,7 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL,
new DataViewClass(this);
new Int8ArrayClass(this);
new Uint8ArrayClass(this);
new Uint8ClampedArrayClass(this);
new Int16ArrayClass(this);
new Uint16ArrayClass(this);
new Int32ArrayClass(this);

View file

@ -342,6 +342,33 @@ void Uint8ArrayClass::setProperty(QScriptValue &object, const QScriptString &nam
setPropertyHelper<quint8>(ba, name, id, value);
}
Uint8ClampedArrayClass::Uint8ClampedArrayClass(ScriptEngine* scriptEngine) : TypedArray(scriptEngine, UINT_8_CLAMPED_ARRAY_CLASS_NAME) {
setBytesPerElement(sizeof(quint8));
}
QScriptValue Uint8ClampedArrayClass::property(const QScriptValue &object, const QScriptString &name, uint id) {
QByteArray* arrayBuffer = qscriptvalue_cast<QByteArray*>(object.data().property(_bufferName).data());
QScriptValue result = propertyHelper<quint8>(arrayBuffer, name, id);
return (result.isValid()) ? result : TypedArray::property(object, name, id);
}
void Uint8ClampedArrayClass::setProperty(QScriptValue &object, const QScriptString &name,
uint id, const QScriptValue &value) {
QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data().property(_bufferName).data());
if (ba && value.isNumber()) {
QDataStream stream(ba, QIODevice::ReadWrite);
stream.skipRawData(id);
if (value.toNumber() > 255) {
stream << (quint8)255;
} else if (value.toNumber() < 0) {
stream << (quint8)0;
} else {
stream << (quint8)glm::clamp(qRound(value.toNumber()), 0, 255);
}
}
}
Int16ArrayClass::Int16ArrayClass(ScriptEngine* scriptEngine) : TypedArray(scriptEngine, INT_16_ARRAY_CLASS_NAME) {
setBytesPerElement(sizeof(qint16));
}

View file

@ -19,6 +19,7 @@ static const QString LENGTH_PROPERTY_NAME = "length";
static const QString INT_8_ARRAY_CLASS_NAME = "Int8Array";
static const QString UINT_8_ARRAY_CLASS_NAME = "Uint8Array";
static const QString UINT_8_CLAMPED_ARRAY_CLASS_NAME = "Uint8ClampedArray";
static const QString INT_16_ARRAY_CLASS_NAME = "Int16Array";
static const QString UINT_16_ARRAY_CLASS_NAME = "Uint16Array";
static const QString INT_32_ARRAY_CLASS_NAME = "Int32Array";
@ -98,7 +99,12 @@ public:
};
class Uint8ClampedArrayClass : public TypedArray {
// TODO
Q_OBJECT
public:
Uint8ClampedArrayClass(ScriptEngine* scriptEngine);
QScriptValue property(const QScriptValue &object, const QScriptString &name, uint id);
void setProperty(QScriptValue &object, const QScriptString &name, uint id, const QScriptValue &value);
};
class Int16ArrayClass : public TypedArray {