File tree 4 files changed +48
-7
lines changed 4 files changed +48
-7
lines changed Original file line number Diff line number Diff line change @@ -1415,6 +1415,20 @@ inline size_t ArrayBuffer::ByteLength() {
1415
1415
return length;
1416
1416
}
1417
1417
1418
+ #ifdef NAPI_EXPERIMENTAL
1419
+ inline bool ArrayBuffer::IsDetached () const {
1420
+ bool detached;
1421
+ napi_status status = napi_is_detached_arraybuffer (_env, _value, &detached);
1422
+ NAPI_THROW_IF_FAILED (_env, status, false );
1423
+ return detached;
1424
+ }
1425
+
1426
+ inline void ArrayBuffer::Detach () {
1427
+ napi_status status = napi_detach_arraybuffer (_env, _value);
1428
+ NAPI_THROW_IF_FAILED_VOID (_env, status);
1429
+ }
1430
+ #endif // NAPI_EXPERIMENTAL
1431
+
1418
1432
// //////////////////////////////////////////////////////////////////////////////
1419
1433
// DataView class
1420
1434
// //////////////////////////////////////////////////////////////////////////////
Original file line number Diff line number Diff line change @@ -813,6 +813,11 @@ namespace Napi {
813
813
814
814
void * Data (); // /< Gets a pointer to the data buffer.
815
815
size_t ByteLength (); // /< Gets the length of the array buffer in bytes.
816
+
817
+ #ifdef NAPI_EXPERIMENTAL
818
+ bool IsDetached () const ;
819
+ void Detach ();
820
+ #endif // NAPI_EXPERIMENTAL
816
821
};
817
822
818
823
// / A JavaScript typed-array value with unknown array type.
Original file line number Diff line number Diff line change
1
+ #define NAPI_EXPERIMENTAL
1
2
#include " napi.h"
2
3
3
4
using namespace Napi ;
@@ -157,19 +158,35 @@ void CheckDetachUpdatesData(const CallbackInfo& info) {
157
158
return ;
158
159
}
159
160
160
- if (!info[1 ].IsFunction ()) {
161
- Error::New (info.Env (), " A function was expected." ).ThrowAsJavaScriptException ();
162
- return ;
163
- }
164
-
165
161
ArrayBuffer buffer = info[0 ].As <ArrayBuffer>();
166
- Function detach = info[1 ].As <Function>();
167
162
168
163
// This potentially causes the buffer to cache its data pointer and length.
169
164
buffer.Data ();
170
165
buffer.ByteLength ();
171
166
172
- detach.Call ({});
167
+ if (buffer.IsDetached ()) {
168
+ Error::New (info.Env (), " Buffer should not be detached." ).ThrowAsJavaScriptException ();
169
+ return ;
170
+ }
171
+
172
+ if (info.Length () == 2 ) {
173
+ // Detach externally (in JavaScript).
174
+ if (!info[1 ].IsFunction ()) {
175
+ Error::New (info.Env (), " A function was expected." ).ThrowAsJavaScriptException ();
176
+ return ;
177
+ }
178
+
179
+ Function detach = info[1 ].As <Function>();
180
+ detach.Call ({});
181
+ } else {
182
+ // Detach directly.
183
+ buffer.Detach ();
184
+ }
185
+
186
+ if (!buffer.IsDetached ()) {
187
+ Error::New (info.Env (), " Buffer should be detached." ).ThrowAsJavaScriptException ();
188
+ return ;
189
+ }
173
190
174
191
if (buffer.Data () != nullptr ) {
175
192
Error::New (info.Env (), " Incorrect data pointer." ).ThrowAsJavaScriptException ();
Original file line number Diff line number Diff line change @@ -64,8 +64,13 @@ function test(binding) {
64
64
65
65
'ArrayBuffer updates data pointer and length when detached' ,
66
66
( ) => {
67
+ // Detach the ArrayBuffer in JavaScript.
67
68
const mem = new WebAssembly . Memory ( { initial : 1 } ) ;
68
69
binding . arraybuffer . checkDetachUpdatesData ( mem . buffer , ( ) => mem . grow ( 1 ) ) ;
70
+
71
+ // Let C++ detach the ArrayBuffer.
72
+ const extBuffer = binding . arraybuffer . createExternalBuffer ( ) ;
73
+ binding . arraybuffer . checkDetachUpdatesData ( extBuffer ) ;
69
74
} ,
70
75
] ) ;
71
76
}
You can’t perform that action at this time.
0 commit comments