@@ -979,21 +979,23 @@ inline void ArrayBuffer::EnsureInfo() const {
979
979
// TypedArray class
980
980
// //////////////////////////////////////////////////////////////////////////////
981
981
982
- inline TypedArray::TypedArray () : Object(), _type(TypedArray::unknown_type), _length(0 ) {
982
+ inline TypedArray::TypedArray ()
983
+ : Object(), _type(TypedArray::unknown_array_type), _length(0 ) {
983
984
}
984
985
985
986
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 ) {
987
988
}
988
989
989
990
inline TypedArray::TypedArray (napi_env env,
990
991
napi_value value,
991
992
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) {
993
995
}
994
996
995
997
inline napi_typedarray_type TypedArray::TypedArrayType () const {
996
- if (_type == TypedArray::unknown_type ) {
998
+ if (_type == TypedArray::unknown_array_type ) {
997
999
napi_status status = napi_get_typedarray_info (_env, _value,
998
1000
&const_cast <TypedArray*>(this )->_type , &const_cast <TypedArray*>(this )->_length ,
999
1001
nullptr , nullptr , nullptr );
@@ -1024,7 +1026,7 @@ inline uint8_t TypedArray::ElementSize() const {
1024
1026
}
1025
1027
1026
1028
inline size_t TypedArray::ElementLength () const {
1027
- if (_type == TypedArray::unknown_type ) {
1029
+ if (_type == TypedArray::unknown_array_type ) {
1028
1030
napi_status status = napi_get_typedarray_info (_env, _value,
1029
1031
&const_cast <TypedArray*>(this )->_type , &const_cast <TypedArray*>(this )->_length ,
1030
1032
nullptr , nullptr , nullptr );
@@ -1054,99 +1056,75 @@ inline Napi::ArrayBuffer TypedArray::ArrayBuffer() const {
1054
1056
return Napi::ArrayBuffer (_env, arrayBuffer);
1055
1057
}
1056
1058
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
-
1093
1059
// //////////////////////////////////////////////////////////////////////////////
1094
- // TypedArray_<T,A > class
1060
+ // TypedArrayOf<T > class
1095
1061
// //////////////////////////////////////////////////////////////////////////////
1096
1062
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) {
1099
1067
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 );
1101
1069
}
1102
1070
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) {
1108
1077
napi_value value;
1109
1078
napi_status status = napi_create_typedarray (
1110
- env, A , elementLength, arrayBuffer, bufferOffset, &value);
1079
+ env, type , elementLength, arrayBuffer, bufferOffset, &value);
1111
1080
if (status != napi_ok) throw Error::New (env);
1112
1081
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 ()));
1114
1083
}
1115
1084
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 ) {
1118
1087
}
1119
1088
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 ) {
1123
1092
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 );
1125
1094
if (status != napi_ok) throw Error::New (_env);
1126
1095
}
1127
1096
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
+ }
1131
1109
}
1132
1110
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) {
1135
1113
return _data[index];
1136
1114
}
1137
1115
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 {
1140
1118
return _data[index];
1141
1119
}
1142
1120
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() {
1145
1123
return _data;
1146
1124
}
1147
1125
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 {
1150
1128
return _data;
1151
1129
}
1152
1130
0 commit comments