@@ -68,6 +68,7 @@ using v8::HandleScope;
6868using v8::Isolate;
6969using v8::Local;
7070using v8::Maybe;
71+ using v8::MaybeLocal;
7172using v8::Number;
7273using v8::Object;
7374using v8::Persistent;
@@ -228,7 +229,9 @@ size_t Length(Handle<Object> obj) {
228229}
229230
230231
231- Local<Object> New (Isolate* isolate, Handle<String> string, enum encoding enc) {
232+ MaybeLocal<Object> New (Isolate* isolate,
233+ Local<String> string,
234+ enum encoding enc) {
232235 EscapableHandleScope scope (isolate);
233236
234237 size_t length = StringBytes::Size (isolate, string, enc);
@@ -245,19 +248,26 @@ Local<Object> New(Isolate* isolate, Handle<String> string, enum encoding enc) {
245248 CHECK_NE (data, nullptr );
246249 }
247250
248- Local<Object> buf = Use (isolate, data, actual);
249- return scope.Escape (buf);
251+ Local<Object> buf;
252+ if (Use (isolate, data, actual).ToLocal (&buf))
253+ return scope.Escape (buf);
254+
255+ // Object failed to be created. Clean up resources.
256+ free (data);
257+ return Local<Object>();
250258}
251259
252260
253- Local <Object> New (Isolate* isolate, size_t length) {
261+ MaybeLocal <Object> New (Isolate* isolate, size_t length) {
254262 EscapableHandleScope handle_scope (isolate);
255- Local<Object> obj = Buffer::New (Environment::GetCurrent (isolate), length);
256- return handle_scope.Escape (obj);
263+ Local<Object> obj;
264+ if (Buffer::New (Environment::GetCurrent (isolate), length).ToLocal (&obj))
265+ return handle_scope.Escape (obj);
266+ return Local<Object>();
257267}
258268
259269
260- Local <Object> New (Environment* env, size_t length) {
270+ MaybeLocal <Object> New (Environment* env, size_t length) {
261271 EscapableHandleScope scope (env->isolate ());
262272
263273 if (using_old_buffer) {
@@ -280,13 +290,12 @@ Local<Object> New(Environment* env, size_t length) {
280290 void * data;
281291 if (length > 0 ) {
282292 data = malloc (length);
283- // NOTE: API change. Must check .IsEmpty() on the return object to see if
284- // the data was able to be allocated.
285293 if (data == nullptr )
286294 return Local<Object>();
287295 } else {
288296 data = nullptr ;
289297 }
298+
290299 Local<ArrayBuffer> ab =
291300 ArrayBuffer::New (env->isolate (),
292301 data,
@@ -295,25 +304,27 @@ Local<Object> New(Environment* env, size_t length) {
295304 Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
296305 Maybe<bool > mb =
297306 ui->SetPrototype (env->context (), env->buffer_prototype_object ());
298- if (! mb.FromMaybe (false )) {
299- FatalError ( " node::Buffer::New(Environment*, size_t) " ,
300- " Could not set Object prototype " );
301- UNREACHABLE ();
302- }
303- return scope. Escape (ui );
307+ if (mb.FromMaybe (false ))
308+ return scope. Escape (ui);
309+
310+ // Object failed to be created. Clean up resources.
311+ free (data);
312+ return Local<Object>( );
304313}
305314
306315
307- Local <Object> New (Isolate* isolate, const char * data, size_t length) {
316+ MaybeLocal <Object> New (Isolate* isolate, const char * data, size_t length) {
308317 Environment* env = Environment::GetCurrent (isolate);
309318 EscapableHandleScope handle_scope (env->isolate ());
310- Local<Object> obj = Buffer::New (env, data, length);
311- return handle_scope.Escape (obj);
319+ Local<Object> obj;
320+ if (Buffer::New (env, data, length).ToLocal (&obj))
321+ return handle_scope.Escape (obj);
322+ return Local<Object>();
312323}
313324
314325
315326// Make a copy of "data". Why this isn't called "Copy", we'll never know.
316- Local <Object> New (Environment* env, const char * data, size_t length) {
327+ MaybeLocal <Object> New (Environment* env, const char * data, size_t length) {
317328 EscapableHandleScope scope (env->isolate ());
318329
319330 if (using_old_buffer) {
@@ -347,8 +358,6 @@ Local<Object> New(Environment* env, const char* data, size_t length) {
347358 if (length > 0 ) {
348359 CHECK_NE (data, nullptr );
349360 new_data = malloc (length);
350- // NOTE: API change. Must check .IsEmpty() on the return object to see if
351- // the data was able to be allocated.
352361 if (new_data == nullptr )
353362 return Local<Object>();
354363 memcpy (new_data, data, length);
@@ -364,33 +373,34 @@ Local<Object> New(Environment* env, const char* data, size_t length) {
364373 Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
365374 Maybe<bool > mb =
366375 ui->SetPrototype (env->context (), env->buffer_prototype_object ());
367- if (!mb.FromMaybe (false )) {
368- FatalError (" node::Buffer::New(Environment*, char*, size_t)" ,
369- " Could not set Object prototype" );
370- UNREACHABLE ();
371- }
376+ if (mb.FromMaybe (false ))
377+ return scope.Escape (ui);
372378
373- return scope.Escape (ui);
379+ // Object failed to be created. Clean up resources.
380+ free (new_data);
381+ return Local<Object>();
374382}
375383
376384
377- Local <Object> New (Isolate* isolate,
378- char * data,
379- size_t length,
380- FreeCallback callback,
381- void * hint) {
385+ MaybeLocal <Object> New (Isolate* isolate,
386+ char * data,
387+ size_t length,
388+ FreeCallback callback,
389+ void * hint) {
382390 Environment* env = Environment::GetCurrent (isolate);
383391 EscapableHandleScope handle_scope (env->isolate ());
384- Local<Object> obj = Buffer::New (env, data, length, callback, hint);
385- return handle_scope.Escape (obj);
392+ Local<Object> obj;
393+ if (Buffer::New (env, data, length, callback, hint).ToLocal (&obj))
394+ return handle_scope.Escape (obj);
395+ return Local<Object>();
386396}
387397
388398
389- Local <Object> New (Environment* env,
390- char * data,
391- size_t length,
392- FreeCallback callback,
393- void * hint) {
399+ MaybeLocal <Object> New (Environment* env,
400+ char * data,
401+ size_t length,
402+ FreeCallback callback,
403+ void * hint) {
394404 EscapableHandleScope scope (env->isolate ());
395405
396406 if (using_old_buffer) {
@@ -410,26 +420,26 @@ Local<Object> New(Environment* env,
410420 Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
411421 Maybe<bool > mb =
412422 ui->SetPrototype (env->context (), env->buffer_prototype_object ());
413- if (!mb.FromMaybe (false )) {
414- FatalError (" node::Buffer::New(Environment*, char*, size_t,"
415- " FreeCallback, void*)" ,
416- " Could not set Object prototype" );
417- UNREACHABLE ();
418- }
423+
424+ if (!mb.FromMaybe (false ))
425+ return Local<Object>();
426+
419427 CallbackInfo::New (env->isolate (), ui, callback, hint);
420428 return scope.Escape (ui);
421429}
422430
423431
424- Local <Object> Use (Isolate* isolate, char * data, size_t length) {
432+ MaybeLocal <Object> Use (Isolate* isolate, char * data, size_t length) {
425433 Environment* env = Environment::GetCurrent (isolate);
426434 EscapableHandleScope handle_scope (env->isolate ());
427- Local<Object> obj = Buffer::Use (env, data, length);
428- return handle_scope.Escape (obj);
435+ Local<Object> obj;
436+ if (Buffer::Use (env, data, length).ToLocal (&obj))
437+ return handle_scope.Escape (obj);
438+ return Local<Object>();
429439}
430440
431441
432- Local <Object> Use (Environment* env, char * data, size_t length) {
442+ MaybeLocal <Object> Use (Environment* env, char * data, size_t length) {
433443 EscapableHandleScope scope (env->isolate ());
434444
435445 if (using_old_buffer) {
@@ -457,12 +467,9 @@ Local<Object> Use(Environment* env, char* data, size_t length) {
457467 Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
458468 Maybe<bool > mb =
459469 ui->SetPrototype (env->context (), env->buffer_prototype_object ());
460- if (!mb.FromMaybe (false )) {
461- FatalError (" node::Buffer::Use(Environment*, char*, size_t)" ,
462- " Could not set Object prototype" );
463- UNREACHABLE ();
464- }
465- return scope.Escape (ui);
470+ if (mb.FromMaybe (false ))
471+ return scope.Escape (ui);
472+ return Local<Object>();
466473}
467474
468475
@@ -495,8 +502,9 @@ void Create(const FunctionCallbackInfo<Value>& args) {
495502 Local<Uint8Array> ui = Uint8Array::New (ab, 0 , length);
496503 Maybe<bool > mb =
497504 ui->SetPrototype (env->context (), env->buffer_prototype_object ());
498- if (mb.FromMaybe (false ))
499- args.GetReturnValue ().Set (ui);
505+ if (!mb.FromMaybe (false ))
506+ return env->ThrowError (" Unable to set Object prototype" );
507+ args.GetReturnValue ().Set (ui);
500508}
501509
502510
@@ -507,8 +515,9 @@ void CreateFromString(const FunctionCallbackInfo<Value>& args) {
507515 enum encoding enc = ParseEncoding (args.GetIsolate (),
508516 args[1 ].As <String>(),
509517 UTF8 );
510- Local<Object> buf = New (args.GetIsolate (), args[0 ].As <String>(), enc);
511- args.GetReturnValue ().Set (buf);
518+ Local<Object> buf;
519+ if (New (args.GetIsolate (), args[0 ].As <String>(), enc).ToLocal (&buf))
520+ args.GetReturnValue ().Set (buf);
512521}
513522
514523
@@ -529,8 +538,9 @@ void Slice(const FunctionCallbackInfo<Value>& args) {
529538 Local<Uint8Array> ui = Uint8Array::New (ab, start, size);
530539 Maybe<bool > mb =
531540 ui->SetPrototype (env->context (), env->buffer_prototype_object ());
532- if (mb.FromMaybe (false ))
533- args.GetReturnValue ().Set (ui);
541+ if (!mb.FromMaybe (false ))
542+ env->ThrowError (" Unable to set Object prototype" );
543+ args.GetReturnValue ().Set (ui);
534544}
535545
536546
0 commit comments