Browse Source

typed arrays: add Uint8ClampedArray

v0.9.1-release
Mikael Bourges-Sevenier 13 years ago
committed by Ben Noordhuis
parent
commit
67fc1dafd0
  1. 13
      src/v8_typed_array.cc
  2. 1
      test/common.js
  3. 17
      test/simple/test-typed-arrays.js

13
src/v8_typed_array.cc

@ -339,6 +339,15 @@ class TypedArray {
reinterpret_cast<float*>(ptr)[index] = (float) args[1]->NumberValue(); reinterpret_cast<float*>(ptr)[index] = (float) args[1]->NumberValue();
else if (TEAType == v8::kExternalDoubleArray) else if (TEAType == v8::kExternalDoubleArray)
reinterpret_cast<double*>(ptr)[index] = (double) args[1]->NumberValue(); reinterpret_cast<double*>(ptr)[index] = (double) args[1]->NumberValue();
else if (TEAType == v8::kExternalPixelArray) {
int value = args[1]->Int32Value();
if (value < 0)
value = 0;
else if (value > 255)
value = 255;
reinterpret_cast<unsigned char*>(ptr)[index] =
(unsigned char) value;
}
} else if (args[0]->IsObject()) { } else if (args[0]->IsObject()) {
v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(args[0]); v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(args[0]);
@ -439,6 +448,7 @@ class TypedArray {
case v8::kExternalUnsignedIntArray: return "Uint32Array"; case v8::kExternalUnsignedIntArray: return "Uint32Array";
case v8::kExternalFloatArray: return "Float32Array"; case v8::kExternalFloatArray: return "Float32Array";
case v8::kExternalDoubleArray: return "Float64Array"; case v8::kExternalDoubleArray: return "Float64Array";
case v8::kExternalPixelArray: return "Uint8ClampedArray";
} }
abort(); abort();
} }
@ -446,6 +456,7 @@ class TypedArray {
class Int8Array : public TypedArray<1, v8::kExternalByteArray> { }; class Int8Array : public TypedArray<1, v8::kExternalByteArray> { };
class Uint8Array : public TypedArray<1, v8::kExternalUnsignedByteArray> { }; class Uint8Array : public TypedArray<1, v8::kExternalUnsignedByteArray> { };
class Uint8ClampedArray : public TypedArray<1, v8::kExternalPixelArray> { };
class Int16Array : public TypedArray<2, v8::kExternalShortArray> { }; class Int16Array : public TypedArray<2, v8::kExternalShortArray> { };
class Uint16Array : public TypedArray<2, v8::kExternalUnsignedShortArray> { }; class Uint16Array : public TypedArray<2, v8::kExternalUnsignedShortArray> { };
class Int32Array : public TypedArray<4, v8::kExternalIntArray> { }; class Int32Array : public TypedArray<4, v8::kExternalIntArray> { };
@ -799,6 +810,8 @@ void AttachBindings(v8::Handle<v8::Object> obj) {
Int8Array::GetTemplate()->GetFunction()); Int8Array::GetTemplate()->GetFunction());
obj->Set(v8::String::New("Uint8Array"), obj->Set(v8::String::New("Uint8Array"),
Uint8Array::GetTemplate()->GetFunction()); Uint8Array::GetTemplate()->GetFunction());
obj->Set(v8::String::New("Uint8ClampedArray"),
Uint8ClampedArray::GetTemplate()->GetFunction());
obj->Set(v8::String::New("Int16Array"), obj->Set(v8::String::New("Int16Array"),
Int16Array::GetTemplate()->GetFunction()); Int16Array::GetTemplate()->GetFunction());
obj->Set(v8::String::New("Uint16Array"), obj->Set(v8::String::New("Uint16Array"),

1
test/common.js

@ -111,6 +111,7 @@ process.on('exit', function() {
knownGlobals.push(ArrayBuffer); knownGlobals.push(ArrayBuffer);
knownGlobals.push(Int8Array); knownGlobals.push(Int8Array);
knownGlobals.push(Uint8Array); knownGlobals.push(Uint8Array);
knownGlobals.push(Uint8ClampedArray);
knownGlobals.push(Int16Array); knownGlobals.push(Int16Array);
knownGlobals.push(Uint16Array); knownGlobals.push(Uint16Array);
knownGlobals.push(Int32Array); knownGlobals.push(Int32Array);

17
test/simple/test-typed-arrays.js

@ -37,7 +37,8 @@ var assert = require('assert');
'Int32Array', 'Int32Array',
'Uint32Array', 'Uint32Array',
'Float32Array', 'Float32Array',
'Float64Array' 'Float64Array',
'Uint8ClampedArray'
].forEach(function(name) { ].forEach(function(name) {
var expected = '[object ' + name + ']'; var expected = '[object ' + name + ']';
var clazz = global[name]; var clazz = global[name];
@ -145,3 +146,17 @@ uint8.set([0x0a, 0x0b], 2);
assert.equal(uint8.get(1), 0x09); assert.equal(uint8.get(1), 0x09);
assert.equal(uint8.get(2), 0x0a); assert.equal(uint8.get(2), 0x0a);
assert.equal(uint8.get(3), 0x0b); assert.equal(uint8.get(3), 0x0b);
// test clamped array
var uint8c = new Uint8ClampedArray(buffer);
uint8c[0] = -1;
uint8c[1] = 257;
assert.equal(uint8c[0], 0);
assert.equal(uint8c[1], 255);
uint8c.set(0, -10);
uint8c.set(1, 260);
assert.equal(uint8c[0], 0);
assert.equal(uint8c[1], 255);

Loading…
Cancel
Save