@@ -1055,38 +1055,12 @@ static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
10551055}
10561056
10571057
1058- // pass Buffer object to load prototype methods
1059- void SetupBufferJS (const FunctionCallbackInfo<Value>& args) {
1058+ void SetBufferPrototype (const FunctionCallbackInfo<Value>& args) {
10601059 Environment* env = Environment::GetCurrent (args);
10611060
10621061 CHECK (args[0 ]->IsObject ());
10631062 Local<Object> proto = args[0 ].As <Object>();
10641063 env->set_buffer_prototype_object (proto);
1065-
1066- env->SetMethodNoSideEffect (proto, " asciiSlice" , StringSlice<ASCII>);
1067- env->SetMethodNoSideEffect (proto, " base64Slice" , StringSlice<BASE64>);
1068- env->SetMethodNoSideEffect (proto, " latin1Slice" , StringSlice<LATIN1>);
1069- env->SetMethodNoSideEffect (proto, " hexSlice" , StringSlice<HEX>);
1070- env->SetMethodNoSideEffect (proto, " ucs2Slice" , StringSlice<UCS2>);
1071- env->SetMethodNoSideEffect (proto, " utf8Slice" , StringSlice<UTF8>);
1072-
1073- env->SetMethod (proto, " asciiWrite" , StringWrite<ASCII>);
1074- env->SetMethod (proto, " base64Write" , StringWrite<BASE64>);
1075- env->SetMethod (proto, " latin1Write" , StringWrite<LATIN1>);
1076- env->SetMethod (proto, " hexWrite" , StringWrite<HEX>);
1077- env->SetMethod (proto, " ucs2Write" , StringWrite<UCS2>);
1078- env->SetMethod (proto, " utf8Write" , StringWrite<UTF8>);
1079-
1080- if (auto zero_fill_field = env->isolate_data ()->zero_fill_field ()) {
1081- CHECK (args[1 ]->IsObject ());
1082- auto binding_object = args[1 ].As <Object>();
1083- auto array_buffer = ArrayBuffer::New (env->isolate (),
1084- zero_fill_field,
1085- sizeof (*zero_fill_field));
1086- auto name = FIXED_ONE_BYTE_STRING (env->isolate (), " zeroFill" );
1087- auto value = Uint32Array::New (array_buffer, 0 , 1 );
1088- CHECK (binding_object->Set (env->context (), name, value).FromJust ());
1089- }
10901064}
10911065
10921066
@@ -1096,7 +1070,7 @@ void Initialize(Local<Object> target,
10961070 void * priv) {
10971071 Environment* env = Environment::GetCurrent (context);
10981072
1099- env->SetMethod (target, " setupBufferJS " , SetupBufferJS );
1073+ env->SetMethod (target, " setBufferPrototype " , SetBufferPrototype );
11001074 env->SetMethodNoSideEffect (target, " createFromString" , CreateFromString);
11011075
11021076 env->SetMethodNoSideEffect (target, " byteLengthUtf8" , ByteLengthUtf8);
@@ -1121,6 +1095,32 @@ void Initialize(Local<Object> target,
11211095 target->Set (env->context (),
11221096 FIXED_ONE_BYTE_STRING (env->isolate (), " kStringMaxLength" ),
11231097 Integer::New (env->isolate (), String::kMaxLength )).FromJust ();
1098+
1099+ env->SetMethodNoSideEffect (target, " asciiSlice" , StringSlice<ASCII>);
1100+ env->SetMethodNoSideEffect (target, " base64Slice" , StringSlice<BASE64>);
1101+ env->SetMethodNoSideEffect (target, " latin1Slice" , StringSlice<LATIN1>);
1102+ env->SetMethodNoSideEffect (target, " hexSlice" , StringSlice<HEX>);
1103+ env->SetMethodNoSideEffect (target, " ucs2Slice" , StringSlice<UCS2>);
1104+ env->SetMethodNoSideEffect (target, " utf8Slice" , StringSlice<UTF8>);
1105+
1106+ env->SetMethod (target, " asciiWrite" , StringWrite<ASCII>);
1107+ env->SetMethod (target, " base64Write" , StringWrite<BASE64>);
1108+ env->SetMethod (target, " latin1Write" , StringWrite<LATIN1>);
1109+ env->SetMethod (target, " hexWrite" , StringWrite<HEX>);
1110+ env->SetMethod (target, " ucs2Write" , StringWrite<UCS2>);
1111+ env->SetMethod (target, " utf8Write" , StringWrite<UTF8>);
1112+
1113+ // It can be a nullptr when running inside an isolate where we
1114+ // do not own the ArrayBuffer allocator.
1115+ if (uint32_t * zero_fill_field = env->isolate_data ()->zero_fill_field ()) {
1116+ Local<ArrayBuffer> array_buffer = ArrayBuffer::New (
1117+ env->isolate (), zero_fill_field, sizeof (*zero_fill_field));
1118+ CHECK (target
1119+ ->Set (env->context (),
1120+ FIXED_ONE_BYTE_STRING (env->isolate (), " zeroFill" ),
1121+ Uint32Array::New (array_buffer, 0 , 1 ))
1122+ .FromJust ());
1123+ }
11241124}
11251125
11261126} // anonymous namespace
0 commit comments