Skip to content

Commit bc683eb

Browse files
authored
TypedArray improvements and tests (#39)
- Rename TypedArray_<> to TypedArrayOf<> - Remove the second template parameter from TypedArrayOf<>, because it can be (usually) inferred from the first parameter and is only needed at object construction time. - Remove the TypedArray::As*Array() methods, which were redundant because they did the same thing as Value::As(). - Add doc-comments to ArrayBuffer, TypedArray, and TypedArrayOf<> classes - Add tests for typed arrays
1 parent f797d14 commit bc683eb

File tree

8 files changed

+412
-136
lines changed

8 files changed

+412
-136
lines changed

napi-inl.h

Lines changed: 47 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -979,21 +979,23 @@ inline void ArrayBuffer::EnsureInfo() const {
979979
// TypedArray class
980980
////////////////////////////////////////////////////////////////////////////////
981981

982-
inline TypedArray::TypedArray() : Object(), _type(TypedArray::unknown_type), _length(0) {
982+
inline TypedArray::TypedArray()
983+
: Object(), _type(TypedArray::unknown_array_type), _length(0) {
983984
}
984985

985986
inline TypedArray::TypedArray(napi_env env, napi_value value)
986-
: Object(env, value), _type(TypedArray::unknown_type), _length(0) {
987+
: Object(env, value), _type(TypedArray::unknown_array_type), _length(0) {
987988
}
988989

989990
inline TypedArray::TypedArray(napi_env env,
990991
napi_value value,
991992
napi_typedarray_type type,
992-
size_t length) : Object(env, value), _type(type), _length(length) {
993+
size_t length)
994+
: Object(env, value), _type(type), _length(length) {
993995
}
994996

995997
inline napi_typedarray_type TypedArray::TypedArrayType() const {
996-
if (_type == TypedArray::unknown_type) {
998+
if (_type == TypedArray::unknown_array_type) {
997999
napi_status status = napi_get_typedarray_info(_env, _value,
9981000
&const_cast<TypedArray*>(this)->_type, &const_cast<TypedArray*>(this)->_length,
9991001
nullptr, nullptr, nullptr);
@@ -1024,7 +1026,7 @@ inline uint8_t TypedArray::ElementSize() const {
10241026
}
10251027

10261028
inline size_t TypedArray::ElementLength() const {
1027-
if (_type == TypedArray::unknown_type) {
1029+
if (_type == TypedArray::unknown_array_type) {
10281030
napi_status status = napi_get_typedarray_info(_env, _value,
10291031
&const_cast<TypedArray*>(this)->_type, &const_cast<TypedArray*>(this)->_length,
10301032
nullptr, nullptr, nullptr);
@@ -1054,99 +1056,75 @@ inline Napi::ArrayBuffer TypedArray::ArrayBuffer() const {
10541056
return Napi::ArrayBuffer(_env, arrayBuffer);
10551057
}
10561058

1057-
inline Int8Array TypedArray::AsInt8Array() const {
1058-
return Int8Array(_env, _value);
1059-
}
1060-
1061-
inline Uint8Array TypedArray::AsUint8Array() const {
1062-
return Uint8Array(_env, _value);
1063-
}
1064-
1065-
inline Uint8ClampedArray TypedArray::AsUint8ClampedArray() const {
1066-
return Uint8ClampedArray(_env, _value);
1067-
}
1068-
1069-
inline Int16Array TypedArray::AsInt16Array() const {
1070-
return Int16Array(_env, _value);
1071-
}
1072-
1073-
inline Uint16Array TypedArray::AsUint16Array() const {
1074-
return Uint16Array(_env, _value);
1075-
}
1076-
1077-
inline Int32Array TypedArray::AsInt32Array() const {
1078-
return Int32Array(_env, _value);
1079-
}
1080-
1081-
inline Uint32Array TypedArray::AsUint32Array() const {
1082-
return Uint32Array(_env, _value);
1083-
}
1084-
1085-
inline Float32Array TypedArray::AsFloat32Array() const {
1086-
return Float32Array(_env, _value);
1087-
}
1088-
1089-
inline Float64Array TypedArray::AsFloat64Array() const {
1090-
return Float64Array(_env, _value);
1091-
}
1092-
10931059
////////////////////////////////////////////////////////////////////////////////
1094-
// TypedArray_<T,A> class
1060+
// TypedArrayOf<T> class
10951061
////////////////////////////////////////////////////////////////////////////////
10961062

1097-
template <typename T, napi_typedarray_type A>
1098-
inline TypedArray_<T,A> TypedArray_<T,A>::New(napi_env env, size_t elementLength) {
1063+
template <typename T>
1064+
inline TypedArrayOf<T> TypedArrayOf<T>::New(napi_env env,
1065+
size_t elementLength,
1066+
napi_typedarray_type type) {
10991067
Napi::ArrayBuffer arrayBuffer = Napi::ArrayBuffer::New(env, elementLength * sizeof (T));
1100-
return New(env, elementLength, arrayBuffer, 0);
1068+
return New(env, elementLength, arrayBuffer, 0, type);
11011069
}
11021070

1103-
template <typename T, napi_typedarray_type A>
1104-
inline TypedArray_<T,A> TypedArray_<T,A>::New(napi_env env,
1105-
size_t elementLength,
1106-
Napi::ArrayBuffer arrayBuffer,
1107-
size_t bufferOffset) {
1071+
template <typename T>
1072+
inline TypedArrayOf<T> TypedArrayOf<T>::New(napi_env env,
1073+
size_t elementLength,
1074+
Napi::ArrayBuffer arrayBuffer,
1075+
size_t bufferOffset,
1076+
napi_typedarray_type type) {
11081077
napi_value value;
11091078
napi_status status = napi_create_typedarray(
1110-
env, A, elementLength, arrayBuffer, bufferOffset, &value);
1079+
env, type, elementLength, arrayBuffer, bufferOffset, &value);
11111080
if (status != napi_ok) throw Error::New(env);
11121081

1113-
return TypedArray_<T,A>(env, value, elementLength, reinterpret_cast<T*>(arrayBuffer.Data()));
1082+
return TypedArrayOf<T>(env, value, type, elementLength, reinterpret_cast<T*>(arrayBuffer.Data()));
11141083
}
11151084

1116-
template <typename T, napi_typedarray_type A>
1117-
inline TypedArray_<T,A>::TypedArray_() : TypedArray(), _data(nullptr) {
1085+
template <typename T>
1086+
inline TypedArrayOf<T>::TypedArrayOf() : TypedArray(), _data(nullptr) {
11181087
}
11191088

1120-
template <typename T, napi_typedarray_type A>
1121-
inline TypedArray_<T,A>::TypedArray_(napi_env env, napi_value value)
1122-
: TypedArray(env, value, A, 0), _data(nullptr) {
1089+
template <typename T>
1090+
inline TypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value)
1091+
: TypedArray(env, value), _data(nullptr) {
11231092
napi_status status = napi_get_typedarray_info(
1124-
_env, _value, nullptr, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
1093+
_env, _value, &_type, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
11251094
if (status != napi_ok) throw Error::New(_env);
11261095
}
11271096

1128-
template <typename T, napi_typedarray_type A>
1129-
inline TypedArray_<T,A>::TypedArray_(napi_env env, napi_value value, size_t length, T* data)
1130-
: TypedArray(env, value, A, length), _data(data) {
1097+
template <typename T>
1098+
inline TypedArrayOf<T>::TypedArrayOf(napi_env env,
1099+
napi_value value,
1100+
napi_typedarray_type type,
1101+
size_t length,
1102+
T* data)
1103+
: TypedArray(env, value, type, length), _data(data) {
1104+
if (!(type == TypedArrayTypeForPrimitiveType<T>() ||
1105+
(type == napi_uint8_clamped_array && std::is_same<T, uint8_t>::value))) {
1106+
throw TypeError::New(env, "Array type must match the template parameter. "
1107+
"(Uint8 arrays may optionally have the \"clamped\" array type.)");
1108+
}
11311109
}
11321110

1133-
template <typename T, napi_typedarray_type A>
1134-
inline T& TypedArray_<T,A>::operator [](size_t index) {
1111+
template <typename T>
1112+
inline T& TypedArrayOf<T>::operator [](size_t index) {
11351113
return _data[index];
11361114
}
11371115

1138-
template <typename T, napi_typedarray_type A>
1139-
inline const T& TypedArray_<T,A>::operator [](size_t index) const {
1116+
template <typename T>
1117+
inline const T& TypedArrayOf<T>::operator [](size_t index) const {
11401118
return _data[index];
11411119
}
11421120

1143-
template <typename T, napi_typedarray_type A>
1144-
inline T* TypedArray_<T,A>::Data() {
1121+
template <typename T>
1122+
inline T* TypedArrayOf<T>::Data() {
11451123
return _data;
11461124
}
11471125

1148-
template <typename T, napi_typedarray_type A>
1149-
inline const T* TypedArray_<T,A>::Data() const {
1126+
template <typename T>
1127+
inline const T* TypedArrayOf<T>::Data() const {
11501128
return _data;
11511129
}
11521130

0 commit comments

Comments
 (0)