@@ -352,16 +352,31 @@ MaybeLocal<Object> New(Isolate* isolate, size_t length) {
352
352
353
353
354
354
MaybeLocal<Object> New (Environment* env, size_t length) {
355
- EscapableHandleScope scope (env->isolate ());
355
+ Isolate* isolate (env->isolate ());
356
+ EscapableHandleScope scope (isolate);
356
357
357
358
// V8 currently only allows a maximum Typed Array index of max Smi.
358
359
if (length > kMaxLength ) {
359
- env-> isolate () ->ThrowException (ERR_BUFFER_TOO_LARGE (env-> isolate () ));
360
+ isolate->ThrowException (ERR_BUFFER_TOO_LARGE (isolate));
360
361
return Local<Object>();
361
362
}
362
363
363
- return scope.EscapeMaybe (
364
- AllocatedBuffer::AllocateManaged (env, length).ToBuffer ());
364
+ Local<ArrayBuffer> ab;
365
+ {
366
+ NoArrayBufferZeroFillScope no_zero_fill_scope (env->isolate_data ());
367
+ std::unique_ptr<BackingStore> bs =
368
+ ArrayBuffer::NewBackingStore (isolate, length);
369
+
370
+ CHECK (bs);
371
+
372
+ ab = ArrayBuffer::New (isolate, std::move (bs));
373
+ }
374
+
375
+ MaybeLocal<Object> obj =
376
+ New (env, ab, 0 , ab->ByteLength ())
377
+ .FromMaybe (Local<Uint8Array>());
378
+
379
+ return scope.EscapeMaybe (obj);
365
380
}
366
381
367
382
@@ -380,20 +395,33 @@ MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
380
395
381
396
382
397
MaybeLocal<Object> Copy (Environment* env, const char * data, size_t length) {
383
- EscapableHandleScope scope (env->isolate ());
398
+ Isolate* isolate (env->isolate ());
399
+ EscapableHandleScope scope (isolate);
384
400
385
401
// V8 currently only allows a maximum Typed Array index of max Smi.
386
402
if (length > kMaxLength ) {
387
- env-> isolate () ->ThrowException (ERR_BUFFER_TOO_LARGE (env-> isolate () ));
403
+ isolate->ThrowException (ERR_BUFFER_TOO_LARGE (isolate));
388
404
return Local<Object>();
389
405
}
390
406
391
- AllocatedBuffer ret = AllocatedBuffer::AllocateManaged (env, length);
392
- if (length > 0 ) {
393
- memcpy (ret.data (), data, length);
407
+ Local<ArrayBuffer> ab;
408
+ {
409
+ NoArrayBufferZeroFillScope no_zero_fill_scope (env->isolate_data ());
410
+ std::unique_ptr<BackingStore> bs =
411
+ ArrayBuffer::NewBackingStore (isolate, length);
412
+
413
+ CHECK (bs);
414
+
415
+ memcpy (bs->Data (), data, length);
416
+
417
+ ab = ArrayBuffer::New (isolate, std::move (bs));
394
418
}
395
419
396
- return scope.EscapeMaybe (ret.ToBuffer ());
420
+ MaybeLocal<Object> obj =
421
+ New (env, ab, 0 , ab->ByteLength ())
422
+ .FromMaybe (Local<Uint8Array>());
423
+
424
+ return scope.EscapeMaybe (obj);
397
425
}
398
426
399
427
@@ -1077,13 +1105,25 @@ static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
1077
1105
1078
1106
Local<String> str = args[0 ].As <String>();
1079
1107
size_t length = str->Utf8Length (isolate);
1080
- AllocatedBuffer buf = AllocatedBuffer::AllocateManaged (env, length);
1081
- str->WriteUtf8 (isolate,
1082
- buf.data (),
1083
- -1 , // We are certain that `data` is sufficiently large
1084
- nullptr ,
1085
- String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
1086
- auto array = Uint8Array::New (buf.ToArrayBuffer (), 0 , length);
1108
+
1109
+ Local<ArrayBuffer> ab;
1110
+ {
1111
+ NoArrayBufferZeroFillScope no_zero_fill_scope (env->isolate_data ());
1112
+ std::unique_ptr<BackingStore> bs =
1113
+ ArrayBuffer::NewBackingStore (isolate, length);
1114
+
1115
+ CHECK (bs);
1116
+
1117
+ str->WriteUtf8 (isolate,
1118
+ static_cast <char *>(bs->Data ()),
1119
+ -1 , // We are certain that `data` is sufficiently large
1120
+ nullptr ,
1121
+ String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);
1122
+
1123
+ ab = ArrayBuffer::New (isolate, std::move (bs));
1124
+ }
1125
+
1126
+ auto array = Uint8Array::New (ab, 0 , length);
1087
1127
args.GetReturnValue ().Set (array);
1088
1128
}
1089
1129
0 commit comments