@@ -1171,6 +1171,88 @@ inline void ArrayBuffer::EnsureInfo() const {
1171
1171
}
1172
1172
}
1173
1173
1174
+ #if NAPI_DATA_VIEW_FEATURE
1175
+ // //////////////////////////////////////////////////////////////////////////////
1176
+ // DataView class
1177
+ // //////////////////////////////////////////////////////////////////////////////
1178
+ inline DataView DataView::New (napi_env env,
1179
+ Napi::ArrayBuffer arrayBuffer) {
1180
+ return New (env, arrayBuffer, 0 , arrayBuffer.ByteLength ());
1181
+ }
1182
+
1183
+ inline DataView DataView::New (napi_env env,
1184
+ Napi::ArrayBuffer arrayBuffer,
1185
+ size_t byteOffset) {
1186
+ if (byteOffset > arrayBuffer.ByteLength ()) {
1187
+ NAPI_THROW (RangeError::New (env,
1188
+ " Start offset is outside the bounds of the buffer" ));
1189
+ return DataView ();
1190
+ }
1191
+ return New (env, arrayBuffer, byteOffset,
1192
+ arrayBuffer.ByteLength () - byteOffset);
1193
+ }
1194
+
1195
+ inline DataView DataView::New (napi_env env,
1196
+ Napi::ArrayBuffer arrayBuffer,
1197
+ size_t byteOffset,
1198
+ size_t byteLength) {
1199
+ if (byteOffset + byteLength > arrayBuffer.ByteLength ()) {
1200
+ NAPI_THROW (RangeError::New (env, " Invalid DataView length" ));
1201
+ return DataView ();
1202
+ }
1203
+ napi_value value;
1204
+ napi_status status = napi_create_dataview (
1205
+ env, byteLength, arrayBuffer, byteOffset, &value);
1206
+ NAPI_THROW_IF_FAILED (env, status, DataView ());
1207
+ return DataView (env, value);
1208
+ }
1209
+
1210
+ inline DataView::DataView () : Object() {
1211
+ }
1212
+
1213
+ inline DataView::DataView (napi_env env, napi_value value) : Object(env, value) {
1214
+ }
1215
+
1216
+ inline Napi::ArrayBuffer DataView::ArrayBuffer () const {
1217
+ napi_value arrayBuffer;
1218
+ napi_status status = napi_get_dataview_info (
1219
+ _env,
1220
+ _value /* dataView */ ,
1221
+ nullptr /* byteLength */ ,
1222
+ nullptr /* data */ ,
1223
+ &arrayBuffer /* arrayBuffer */ ,
1224
+ nullptr /* byteOffset */ );
1225
+ NAPI_THROW_IF_FAILED (_env, status, Napi::ArrayBuffer ());
1226
+ return Napi::ArrayBuffer (_env, arrayBuffer);
1227
+ }
1228
+
1229
+ inline size_t DataView::ByteOffset () const {
1230
+ size_t byteOffset;
1231
+ napi_status status = napi_get_dataview_info (
1232
+ _env,
1233
+ _value /* dataView */ ,
1234
+ nullptr /* byteLength */ ,
1235
+ nullptr /* data */ ,
1236
+ nullptr /* arrayBuffer */ ,
1237
+ &byteOffset /* byteOffset */ );
1238
+ NAPI_THROW_IF_FAILED (_env, status, 0 );
1239
+ return byteOffset;
1240
+ }
1241
+
1242
+ inline size_t DataView::ByteLength () const {
1243
+ size_t byteLength;
1244
+ napi_status status = napi_get_dataview_info (
1245
+ _env,
1246
+ _value /* dataView */ ,
1247
+ &byteLength /* byteLength */ ,
1248
+ nullptr /* data */ ,
1249
+ nullptr /* arrayBuffer */ ,
1250
+ nullptr /* byteOffset */ );
1251
+ NAPI_THROW_IF_FAILED (_env, status, 0 );
1252
+ return byteLength;
1253
+ }
1254
+ #endif
1255
+
1174
1256
// //////////////////////////////////////////////////////////////////////////////
1175
1257
// TypedArray class
1176
1258
// //////////////////////////////////////////////////////////////////////////////
0 commit comments