@@ -245,7 +245,13 @@ extern "C" uintptr_t node_ffi_fast_buffer_data(v8::Local<v8::Value> value, |
245 | 245 | // returns zero after throwing, preventing the native target from seeing an |
246 | 246 | // invalid pointer value. |
247 | 247 | constexpr uintptr_t kInvalidBuffer = std::numeric_limits<uintptr_t>::max(); |
248 | | - v8::Isolate* isolate = options != nullptr ? options->isolate : nullptr; |
| 248 | + // The trampoline always passes the FastApiCallbackOptions declared in the |
| 249 | + // CFunctionInfo. No HandleScope is active during a Fast API call, and both |
| 250 | + // ArrayBufferView::Buffer() and the error paths below create handles, so |
| 251 | + // open one for the whole helper. Otherwise every call leaks handles into |
| 252 | + // the caller's scope. |
| 253 | + v8::Isolate* isolate = options->isolate; |
| 254 | + v8::HandleScope scope(isolate); |
249 | 255 |
|
250 | 256 | // Accept only the memory-backed JS values supported by ToFFIArgument in the |
251 | 257 | // native helper. Other pointer conversions, including strings and direct |
@@ -254,41 +260,26 @@ extern "C" uintptr_t node_ffi_fast_buffer_data(v8::Local<v8::Value> value, |
254 | 260 | if (value->IsArrayBufferView()) { |
255 | 261 | v8::Local<v8::ArrayBufferView> view = value.As<v8::ArrayBufferView>(); |
256 | 262 | if (view->Buffer()->WasDetached()) { |
257 | | - if (isolate != nullptr) { |
258 | | - // No HandleScope is active during a Fast API call, so open one before |
259 | | - // creating the error object. |
260 | | - v8::HandleScope scope(isolate); |
261 | | - THROW_ERR_INVALID_ARG_VALUE( |
262 | | - isolate, |
263 | | - "Argument %u is an ArrayBufferView backed by a detached " |
264 | | - "ArrayBuffer", |
265 | | - index); |
266 | | - } |
| 263 | + THROW_ERR_INVALID_ARG_VALUE( |
| 264 | + isolate, |
| 265 | + "Argument %u is an ArrayBufferView backed by a detached " |
| 266 | + "ArrayBuffer", |
| 267 | + index); |
267 | 268 | return kInvalidBuffer; |
268 | 269 | } |
269 | 270 | return PointerFromValue(value); |
270 | 271 | } |
271 | 272 | if (value->IsArrayBuffer()) { |
272 | 273 | if (value.As<v8::ArrayBuffer>()->WasDetached()) { |
273 | | - if (isolate != nullptr) { |
274 | | - // No HandleScope is active during a Fast API call, so open one before |
275 | | - // creating the error object. |
276 | | - v8::HandleScope scope(isolate); |
277 | | - THROW_ERR_INVALID_ARG_VALUE( |
278 | | - isolate, "Argument %u is a detached ArrayBuffer", index); |
279 | | - } |
| 274 | + THROW_ERR_INVALID_ARG_VALUE( |
| 275 | + isolate, "Argument %u is a detached ArrayBuffer", index); |
280 | 276 | return kInvalidBuffer; |
281 | 277 | } |
282 | 278 | return PointerFromValue(value); |
283 | 279 | } |
284 | 280 |
|
285 | | - if (isolate != nullptr) { |
286 | | - // No HandleScope is active during a Fast API call, so open one before |
287 | | - // creating the error object. |
288 | | - v8::HandleScope scope(isolate); |
289 | | - THROW_ERR_INVALID_ARG_VALUE( |
290 | | - isolate, "Argument %u must be a buffer or an ArrayBuffer", index); |
291 | | - } |
| 281 | + THROW_ERR_INVALID_ARG_VALUE( |
| 282 | + isolate, "Argument %u must be a buffer or an ArrayBuffer", index); |
292 | 283 | return kInvalidBuffer; |
293 | 284 | } |
294 | 285 |
|
|