File tree Expand file tree Collapse file tree 5 files changed +71
-7
lines changed Expand file tree Collapse file tree 5 files changed +71
-7
lines changed Original file line number Diff line number Diff line change @@ -130,4 +130,20 @@ void* Napi::ArrayBuffer::Data() const;
130
130
131
131
Returns a pointer the wrapped data.
132
132
133
+ ### Detach
134
+
135
+ ``` cpp
136
+ void Napi::ArrayBuffer::Detach ();
137
+ ```
138
+
139
+ Invokes the ` ArrayBuffer ` detach operation on a detachable ` ArrayBuffer ` .
140
+
141
+ ### IsDetached
142
+
143
+ ``` cpp
144
+ bool Napi::ArrayBuffer::IsDetached () const ;
145
+ ```
146
+
147
+ Returns ` true ` if this ` ArrayBuffer ` has been detached.
148
+
133
149
[ `Napi::Object` ] : ./object.md
Original file line number Diff line number Diff line change @@ -1530,6 +1530,20 @@ inline size_t ArrayBuffer::ByteLength() {
1530
1530
return length;
1531
1531
}
1532
1532
1533
+ #if NAPI_VERSION >= 7
1534
+ inline bool ArrayBuffer::IsDetached () const {
1535
+ bool detached;
1536
+ napi_status status = napi_is_detached_arraybuffer (_env, _value, &detached);
1537
+ NAPI_THROW_IF_FAILED (_env, status, false );
1538
+ return detached;
1539
+ }
1540
+
1541
+ inline void ArrayBuffer::Detach () {
1542
+ napi_status status = napi_detach_arraybuffer (_env, _value);
1543
+ NAPI_THROW_IF_FAILED_VOID (_env, status);
1544
+ }
1545
+ #endif // NAPI_VERSION >= 7
1546
+
1533
1547
// //////////////////////////////////////////////////////////////////////////////
1534
1548
// DataView class
1535
1549
// //////////////////////////////////////////////////////////////////////////////
Original file line number Diff line number Diff line change @@ -822,6 +822,11 @@ namespace Napi {
822
822
823
823
void * Data (); // /< Gets a pointer to the data buffer.
824
824
size_t ByteLength (); // /< Gets the length of the array buffer in bytes.
825
+
826
+ #if NAPI_VERSION >= 7
827
+ bool IsDetached () const ;
828
+ void Detach ();
829
+ #endif // NAPI_VERSION >= 7
825
830
};
826
831
827
832
// / A JavaScript typed-array value with unknown array type.
Original file line number Diff line number Diff line change @@ -157,19 +157,43 @@ void CheckDetachUpdatesData(const CallbackInfo& info) {
157
157
return ;
158
158
}
159
159
160
- if (!info[1 ].IsFunction ()) {
161
- Error::New (info.Env (), " A function was expected." ).ThrowAsJavaScriptException ();
162
- return ;
163
- }
164
-
165
160
ArrayBuffer buffer = info[0 ].As <ArrayBuffer>();
166
- Function detach = info[1 ].As <Function>();
167
161
168
162
// This potentially causes the buffer to cache its data pointer and length.
169
163
buffer.Data ();
170
164
buffer.ByteLength ();
171
165
172
- detach.Call ({});
166
+ #if NAPI_VERSION >= 7
167
+ if (buffer.IsDetached ()) {
168
+ Error::New (info.Env (), " Buffer should not be detached." ).ThrowAsJavaScriptException ();
169
+ return ;
170
+ }
171
+ #endif
172
+
173
+ if (info.Length () == 2 ) {
174
+ // Detach externally (in JavaScript).
175
+ if (!info[1 ].IsFunction ()) {
176
+ Error::New (info.Env (), " A function was expected." ).ThrowAsJavaScriptException ();
177
+ return ;
178
+ }
179
+
180
+ Function detach = info[1 ].As <Function>();
181
+ detach.Call ({});
182
+ } else {
183
+ #if NAPI_VERSION >= 7
184
+ // Detach directly.
185
+ buffer.Detach ();
186
+ #else
187
+ return ;
188
+ #endif
189
+ }
190
+
191
+ #if NAPI_VERSION >= 7
192
+ if (!buffer.IsDetached ()) {
193
+ Error::New (info.Env (), " Buffer should be detached." ).ThrowAsJavaScriptException ();
194
+ return ;
195
+ }
196
+ #endif
173
197
174
198
if (buffer.Data () != nullptr ) {
175
199
Error::New (info.Env (), " Incorrect data pointer." ).ThrowAsJavaScriptException ();
Original file line number Diff line number Diff line change @@ -58,8 +58,13 @@ function test(binding) {
58
58
59
59
'ArrayBuffer updates data pointer and length when detached' ,
60
60
( ) => {
61
+ // Detach the ArrayBuffer in JavaScript.
61
62
const mem = new WebAssembly . Memory ( { initial : 1 } ) ;
62
63
binding . arraybuffer . checkDetachUpdatesData ( mem . buffer , ( ) => mem . grow ( 1 ) ) ;
64
+
65
+ // Let C++ detach the ArrayBuffer.
66
+ const extBuffer = binding . arraybuffer . createExternalBuffer ( ) ;
67
+ binding . arraybuffer . checkDetachUpdatesData ( extBuffer ) ;
63
68
} ,
64
69
] ) ;
65
70
}
You can’t perform that action at this time.
0 commit comments