@@ -2635,7 +2635,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, size_t length) {
2635
2635
napi_status status =
2636
2636
napi_create_buffer (env, length * sizeof (T), &data, &value);
2637
2637
NAPI_THROW_IF_FAILED (env, status, Buffer<T>());
2638
- return Buffer (env, value, length, static_cast <T*>(data) );
2638
+ return Buffer (env, value);
2639
2639
}
2640
2640
2641
2641
#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
@@ -2645,7 +2645,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length) {
2645
2645
napi_status status = napi_create_external_buffer (
2646
2646
env, length * sizeof (T), data, nullptr , nullptr , &value);
2647
2647
NAPI_THROW_IF_FAILED (env, status, Buffer<T>());
2648
- return Buffer (env, value, length, data );
2648
+ return Buffer (env, value);
2649
2649
}
2650
2650
2651
2651
template <typename T>
@@ -2669,7 +2669,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
2669
2669
delete finalizeData;
2670
2670
NAPI_THROW_IF_FAILED (env, status, Buffer ());
2671
2671
}
2672
- return Buffer (env, value, length, data );
2672
+ return Buffer (env, value);
2673
2673
}
2674
2674
2675
2675
template <typename T>
@@ -2694,7 +2694,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
2694
2694
delete finalizeData;
2695
2695
NAPI_THROW_IF_FAILED (env, status, Buffer ());
2696
2696
}
2697
- return Buffer (env, value, length, data );
2697
+ return Buffer (env, value);
2698
2698
}
2699
2699
#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2700
2700
@@ -2711,7 +2711,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env, T* data, size_t length) {
2711
2711
#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2712
2712
}
2713
2713
NAPI_THROW_IF_FAILED (env, status, Buffer<T>());
2714
- return Buffer (env, value, length, data );
2714
+ return Buffer (env, value);
2715
2715
#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2716
2716
}
2717
2717
@@ -2745,7 +2745,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
2745
2745
delete finalizeData;
2746
2746
NAPI_THROW_IF_FAILED (env, status, Buffer ());
2747
2747
}
2748
- return Buffer (env, value, length, data );
2748
+ return Buffer (env, value);
2749
2749
#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
2750
2750
}
2751
2751
@@ -2781,7 +2781,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
2781
2781
delete finalizeData;
2782
2782
NAPI_THROW_IF_FAILED (env, status, Buffer ());
2783
2783
}
2784
- return Buffer (env, value, length, data );
2784
+ return Buffer (env, value);
2785
2785
#endif
2786
2786
}
2787
2787
@@ -2805,42 +2805,20 @@ inline void Buffer<T>::CheckCast(napi_env env, napi_value value) {
2805
2805
}
2806
2806
2807
2807
template <typename T>
2808
- inline Buffer<T>::Buffer () : Uint8Array (), _length ( 0 ), _data ( nullptr ) {}
2808
+ inline Buffer<T>::Buffer () : Uint8Array () {}
2809
2809
2810
2810
template <typename T>
2811
2811
inline Buffer<T>::Buffer (napi_env env, napi_value value)
2812
- : Uint8Array (env, value), _length (0 ), _data (nullptr ) {}
2813
-
2814
- template <typename T>
2815
- inline Buffer<T>::Buffer (napi_env env, napi_value value, size_t length, T* data)
2816
- : Uint8Array (env, value), _length (length), _data (data) {}
2812
+ : Uint8Array (env, value) {}
2817
2813
2818
2814
template <typename T>
2819
2815
inline size_t Buffer<T>::Length () const {
2820
- EnsureInfo ();
2821
- return _length;
2816
+ return ByteLength () / sizeof (T);
2822
2817
}
2823
2818
2824
2819
template <typename T>
2825
2820
inline T* Buffer<T>::Data () const {
2826
- EnsureInfo ();
2827
- return _data;
2828
- }
2829
-
2830
- template <typename T>
2831
- inline void Buffer<T>::EnsureInfo () const {
2832
- // The Buffer instance may have been constructed from a napi_value whose
2833
- // length/data are not yet known. Fetch and cache these values just once,
2834
- // since they can never change during the lifetime of the Buffer.
2835
- if (_data == nullptr ) {
2836
- size_t byteLength;
2837
- void * voidData;
2838
- napi_status status =
2839
- napi_get_buffer_info (_env, _value, &voidData, &byteLength);
2840
- NAPI_THROW_IF_FAILED_VOID (_env, status);
2841
- _length = byteLength / sizeof (T);
2842
- _data = static_cast <T*>(voidData);
2843
- }
2821
+ return reinterpret_cast <T*>(const_cast <uint8_t *>(Uint8Array::Data ()));
2844
2822
}
2845
2823
2846
2824
// //////////////////////////////////////////////////////////////////////////////
0 commit comments