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