@@ -352,16 +352,31 @@ MaybeLocal<Object> New(Isolate* isolate, size_t length) {
352352
353353
354354MaybeLocal<Object> New (Environment* env, size_t length) {
355- EscapableHandleScope scope (env->isolate ());
355+ Isolate* isolate (env->isolate ());
356+ EscapableHandleScope scope (isolate);
356357
357358 // V8 currently only allows a maximum Typed Array index of max Smi.
358359 if (length > kMaxLength ) {
359- env-> isolate () ->ThrowException (ERR_BUFFER_TOO_LARGE (env-> isolate () ));
360+ isolate->ThrowException (ERR_BUFFER_TOO_LARGE (isolate));
360361 return Local<Object>();
361362 }
362363
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);
365380}
366381
367382
@@ -380,20 +395,33 @@ MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
380395
381396
382397MaybeLocal<Object> Copy (Environment* env, const char * data, size_t length) {
383- EscapableHandleScope scope (env->isolate ());
398+ Isolate* isolate (env->isolate ());
399+ EscapableHandleScope scope (isolate);
384400
385401 // V8 currently only allows a maximum Typed Array index of max Smi.
386402 if (length > kMaxLength ) {
387- env-> isolate () ->ThrowException (ERR_BUFFER_TOO_LARGE (env-> isolate () ));
403+ isolate->ThrowException (ERR_BUFFER_TOO_LARGE (isolate));
388404 return Local<Object>();
389405 }
390406
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));
394418 }
395419
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);
397425}
398426
399427
@@ -1077,13 +1105,25 @@ static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
10771105
10781106 Local<String> str = args[0 ].As <String>();
10791107 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);
10871127 args.GetReturnValue ().Set (array);
10881128}
10891129
0 commit comments