@@ -74,6 +74,7 @@ using v8::HandleScope;
74
74
using v8::Isolate;
75
75
using v8::Local;
76
76
using v8::Maybe;
77
+ using v8::MaybeLocal;
77
78
using v8::Number;
78
79
using v8::Object;
79
80
using v8::Persistent;
@@ -234,7 +235,9 @@ size_t Length(Handle<Object> obj) {
234
235
}
235
236
236
237
237
- Local<Object> New (Isolate* isolate, Handle <String> string, enum encoding enc) {
238
+ MaybeLocal<Object> New (Isolate* isolate,
239
+ Local<String> string,
240
+ enum encoding enc) {
238
241
EscapableHandleScope scope (isolate);
239
242
240
243
size_t length = StringBytes::Size (isolate, string, enc);
@@ -251,19 +254,26 @@ Local<Object> New(Isolate* isolate, Handle<String> string, enum encoding enc) {
251
254
CHECK_NE (data, nullptr );
252
255
}
253
256
254
- Local<Object> buf = Use (isolate, data, actual);
255
- return scope.Escape (buf);
257
+ Local<Object> buf;
258
+ if (Use (isolate, data, actual).ToLocal (&buf))
259
+ return scope.Escape (buf);
260
+
261
+ // Object failed to be created. Clean up resources.
262
+ free (data);
263
+ return Local<Object>();
256
264
}
257
265
258
266
259
- Local <Object> New (Isolate* isolate, size_t length) {
267
+ MaybeLocal <Object> New (Isolate* isolate, size_t length) {
260
268
EscapableHandleScope handle_scope (isolate);
261
- Local<Object> obj = Buffer::New (Environment::GetCurrent (isolate), length);
262
- return handle_scope.Escape (obj);
269
+ Local<Object> obj;
270
+ if (Buffer::New (Environment::GetCurrent (isolate), length).ToLocal (&obj))
271
+ return handle_scope.Escape (obj);
272
+ return Local<Object>();
263
273
}
264
274
265
275
266
- Local <Object> New (Environment* env, size_t length) {
276
+ MaybeLocal <Object> New (Environment* env, size_t length) {
267
277
EscapableHandleScope scope (env->isolate ());
268
278
269
279
if (using_old_buffer) {
@@ -286,13 +296,12 @@ Local<Object> New(Environment* env, size_t length) {
286
296
void * data;
287
297
if (length > 0 ) {
288
298
data = malloc (length);
289
- // NOTE: API change. Must check .IsEmpty() on the return object to see if
290
- // the data was able to be allocated.
291
299
if (data == nullptr )
292
300
return Local<Object>();
293
301
} else {
294
302
data = nullptr ;
295
303
}
304
+
296
305
Local<ArrayBuffer> ab =
297
306
ArrayBuffer::New (env->isolate (),
298
307
data,
@@ -301,25 +310,27 @@ Local<Object> New(Environment* env, size_t length) {
301
310
Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
302
311
Maybe<bool > mb =
303
312
ui->SetPrototype (env->context (), env->buffer_prototype_object ());
304
- if (! mb.FromMaybe (false )) {
305
- FatalError ( " node::Buffer::New(Environment*, size_t) " ,
306
- " Could not set Object prototype " );
307
- UNREACHABLE ();
308
- }
309
- return scope. Escape (ui );
313
+ if (mb.FromMaybe (false ))
314
+ return scope. Escape (ui);
315
+
316
+ // Object failed to be created. Clean up resources.
317
+ free (data);
318
+ return Local<Object>( );
310
319
}
311
320
312
321
313
- Local <Object> New (Isolate* isolate, const char * data, size_t length) {
322
+ MaybeLocal <Object> New (Isolate* isolate, const char * data, size_t length) {
314
323
Environment* env = Environment::GetCurrent (isolate);
315
324
EscapableHandleScope handle_scope (env->isolate ());
316
- Local<Object> obj = Buffer::New (env, data, length);
317
- return handle_scope.Escape (obj);
325
+ Local<Object> obj;
326
+ if (Buffer::New (env, data, length).ToLocal (&obj))
327
+ return handle_scope.Escape (obj);
328
+ return Local<Object>();
318
329
}
319
330
320
331
321
332
// Make a copy of "data". Why this isn't called "Copy", we'll never know.
322
- Local <Object> New (Environment* env, const char * data, size_t length) {
333
+ MaybeLocal <Object> New (Environment* env, const char * data, size_t length) {
323
334
EscapableHandleScope scope (env->isolate ());
324
335
325
336
if (using_old_buffer) {
@@ -353,8 +364,6 @@ Local<Object> New(Environment* env, const char* data, size_t length) {
353
364
if (length > 0 ) {
354
365
CHECK_NE (data, nullptr );
355
366
new_data = malloc (length);
356
- // NOTE: API change. Must check .IsEmpty() on the return object to see if
357
- // the data was able to be allocated.
358
367
if (new_data == nullptr )
359
368
return Local<Object>();
360
369
memcpy (new_data, data, length);
@@ -370,33 +379,34 @@ Local<Object> New(Environment* env, const char* data, size_t length) {
370
379
Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
371
380
Maybe<bool > mb =
372
381
ui->SetPrototype (env->context (), env->buffer_prototype_object ());
373
- if (!mb.FromMaybe (false )) {
374
- FatalError (" node::Buffer::New(Environment*, char*, size_t)" ,
375
- " Could not set Object prototype" );
376
- UNREACHABLE ();
377
- }
382
+ if (mb.FromMaybe (false ))
383
+ return scope.Escape (ui);
378
384
379
- return scope.Escape (ui);
385
+ // Object failed to be created. Clean up resources.
386
+ free (new_data);
387
+ return Local<Object>();
380
388
}
381
389
382
390
383
- Local <Object> New (Isolate* isolate,
384
- char * data,
385
- size_t length,
386
- FreeCallback callback,
387
- void * hint) {
391
+ MaybeLocal <Object> New (Isolate* isolate,
392
+ char * data,
393
+ size_t length,
394
+ FreeCallback callback,
395
+ void * hint) {
388
396
Environment* env = Environment::GetCurrent (isolate);
389
397
EscapableHandleScope handle_scope (env->isolate ());
390
- Local<Object> obj = Buffer::New (env, data, length, callback, hint);
391
- return handle_scope.Escape (obj);
398
+ Local<Object> obj;
399
+ if (Buffer::New (env, data, length, callback, hint).ToLocal (&obj))
400
+ return handle_scope.Escape (obj);
401
+ return Local<Object>();
392
402
}
393
403
394
404
395
- Local <Object> New (Environment* env,
396
- char * data,
397
- size_t length,
398
- FreeCallback callback,
399
- void * hint) {
405
+ MaybeLocal <Object> New (Environment* env,
406
+ char * data,
407
+ size_t length,
408
+ FreeCallback callback,
409
+ void * hint) {
400
410
EscapableHandleScope scope (env->isolate ());
401
411
402
412
if (using_old_buffer) {
@@ -416,26 +426,26 @@ Local<Object> New(Environment* env,
416
426
Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
417
427
Maybe<bool > mb =
418
428
ui->SetPrototype (env->context (), env->buffer_prototype_object ());
419
- if (!mb.FromMaybe (false )) {
420
- FatalError (" node::Buffer::New(Environment*, char*, size_t,"
421
- " FreeCallback, void*)" ,
422
- " Could not set Object prototype" );
423
- UNREACHABLE ();
424
- }
429
+
430
+ if (!mb.FromMaybe (false ))
431
+ return Local<Object>();
432
+
425
433
CallbackInfo::New (env->isolate (), ui, callback, hint);
426
434
return scope.Escape (ui);
427
435
}
428
436
429
437
430
- Local <Object> Use (Isolate* isolate, char * data, size_t length) {
438
+ MaybeLocal <Object> Use (Isolate* isolate, char * data, size_t length) {
431
439
Environment* env = Environment::GetCurrent (isolate);
432
440
EscapableHandleScope handle_scope (env->isolate ());
433
- Local<Object> obj = Buffer::Use (env, data, length);
434
- return handle_scope.Escape (obj);
441
+ Local<Object> obj;
442
+ if (Buffer::Use (env, data, length).ToLocal (&obj))
443
+ return handle_scope.Escape (obj);
444
+ return Local<Object>();
435
445
}
436
446
437
447
438
- Local <Object> Use (Environment* env, char * data, size_t length) {
448
+ MaybeLocal <Object> Use (Environment* env, char * data, size_t length) {
439
449
EscapableHandleScope scope (env->isolate ());
440
450
441
451
if (using_old_buffer) {
@@ -463,12 +473,9 @@ Local<Object> Use(Environment* env, char* data, size_t length) {
463
473
Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
464
474
Maybe<bool > mb =
465
475
ui->SetPrototype (env->context (), env->buffer_prototype_object ());
466
- if (!mb.FromMaybe (false )) {
467
- FatalError (" node::Buffer::Use(Environment*, char*, size_t)" ,
468
- " Could not set Object prototype" );
469
- UNREACHABLE ();
470
- }
471
- return scope.Escape (ui);
476
+ if (mb.FromMaybe (false ))
477
+ return scope.Escape (ui);
478
+ return Local<Object>();
472
479
}
473
480
474
481
@@ -501,8 +508,9 @@ void Create(const FunctionCallbackInfo<Value>& args) {
501
508
Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
502
509
Maybe<bool > mb =
503
510
ui->SetPrototype (env->context (), env->buffer_prototype_object ());
504
- if (mb.FromMaybe (false ))
505
- args.GetReturnValue ().Set (ui);
511
+ if (!mb.FromMaybe (false ))
512
+ return env->ThrowError (" Unable to set Object prototype" );
513
+ args.GetReturnValue ().Set (ui);
506
514
}
507
515
508
516
@@ -513,8 +521,9 @@ void CreateFromString(const FunctionCallbackInfo<Value>& args) {
513
521
enum encoding enc = ParseEncoding (args.GetIsolate (),
514
522
args[1 ].As <String>(),
515
523
UTF8);
516
- Local<Object> buf = New (args.GetIsolate (), args[0 ].As <String>(), enc);
517
- args.GetReturnValue ().Set (buf);
524
+ Local<Object> buf;
525
+ if (New (args.GetIsolate (), args[0 ].As <String>(), enc).ToLocal (&buf))
526
+ args.GetReturnValue ().Set (buf);
518
527
}
519
528
520
529
@@ -535,8 +544,9 @@ void Slice(const FunctionCallbackInfo<Value>& args) {
535
544
Local<Uint8Array> ui = Uint8Array::New (ab, start, size);
536
545
Maybe<bool > mb =
537
546
ui->SetPrototype (env->context (), env->buffer_prototype_object ());
538
- if (mb.FromMaybe (false ))
539
- args.GetReturnValue ().Set (ui);
547
+ if (!mb.FromMaybe (false ))
548
+ env->ThrowError (" Unable to set Object prototype" );
549
+ args.GetReturnValue ().Set (ui);
540
550
}
541
551
542
552
0 commit comments