summaryrefslogtreecommitdiff
path: root/src/node_buffer.cc
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-12-31 16:11:01 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2019-01-10 16:59:24 +0800
commit97f59b95675b80a8d8df1d0e9c179c06c0c86a7b (patch)
treef895a5b2b53b7098f779daf769188a4ba6f4331c /src/node_buffer.cc
parentfa5af0d50846083fcd0bb9de6006bb57424a17cf (diff)
downloadandroid-node-v8-97f59b95675b80a8d8df1d0e9c179c06c0c86a7b.tar.gz
android-node-v8-97f59b95675b80a8d8df1d0e9c179c06c0c86a7b.tar.bz2
android-node-v8-97f59b95675b80a8d8df1d0e9c179c06c0c86a7b.zip
buffer: move initialization of buffer prototype into node.js
Instead of exposing it in `lib/internal/buffer.js` after deleting it from the binding and then do the initialization in `lib/buffer.js`, which results in an implicit dependency on the order in which these modules are loaded. PR-URL: https://github.com/nodejs/node/pull/25292 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r--src/node_buffer.cc56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index ae2d1a06a7..2f4a4c6661 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -1057,38 +1057,12 @@ static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) {
}
-// pass Buffer object to load prototype methods
-void SetupBufferJS(const FunctionCallbackInfo<Value>& args) {
+void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsObject());
Local<Object> proto = args[0].As<Object>();
env->set_buffer_prototype_object(proto);
-
- env->SetMethodNoSideEffect(proto, "asciiSlice", StringSlice<ASCII>);
- env->SetMethodNoSideEffect(proto, "base64Slice", StringSlice<BASE64>);
- env->SetMethodNoSideEffect(proto, "latin1Slice", StringSlice<LATIN1>);
- env->SetMethodNoSideEffect(proto, "hexSlice", StringSlice<HEX>);
- env->SetMethodNoSideEffect(proto, "ucs2Slice", StringSlice<UCS2>);
- env->SetMethodNoSideEffect(proto, "utf8Slice", StringSlice<UTF8>);
-
- env->SetMethod(proto, "asciiWrite", StringWrite<ASCII>);
- env->SetMethod(proto, "base64Write", StringWrite<BASE64>);
- env->SetMethod(proto, "latin1Write", StringWrite<LATIN1>);
- env->SetMethod(proto, "hexWrite", StringWrite<HEX>);
- env->SetMethod(proto, "ucs2Write", StringWrite<UCS2>);
- env->SetMethod(proto, "utf8Write", StringWrite<UTF8>);
-
- if (auto zero_fill_field = env->isolate_data()->zero_fill_field()) {
- CHECK(args[1]->IsObject());
- auto binding_object = args[1].As<Object>();
- auto array_buffer = ArrayBuffer::New(env->isolate(),
- zero_fill_field,
- sizeof(*zero_fill_field));
- auto name = FIXED_ONE_BYTE_STRING(env->isolate(), "zeroFill");
- auto value = Uint32Array::New(array_buffer, 0, 1);
- CHECK(binding_object->Set(env->context(), name, value).FromJust());
- }
}
@@ -1098,7 +1072,7 @@ void Initialize(Local<Object> target,
void* priv) {
Environment* env = Environment::GetCurrent(context);
- env->SetMethod(target, "setupBufferJS", SetupBufferJS);
+ env->SetMethod(target, "setBufferPrototype", SetBufferPrototype);
env->SetMethodNoSideEffect(target, "createFromString", CreateFromString);
env->SetMethodNoSideEffect(target, "byteLengthUtf8", ByteLengthUtf8);
@@ -1123,6 +1097,32 @@ void Initialize(Local<Object> target,
target->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "kStringMaxLength"),
Integer::New(env->isolate(), String::kMaxLength)).FromJust();
+
+ env->SetMethodNoSideEffect(target, "asciiSlice", StringSlice<ASCII>);
+ env->SetMethodNoSideEffect(target, "base64Slice", StringSlice<BASE64>);
+ env->SetMethodNoSideEffect(target, "latin1Slice", StringSlice<LATIN1>);
+ env->SetMethodNoSideEffect(target, "hexSlice", StringSlice<HEX>);
+ env->SetMethodNoSideEffect(target, "ucs2Slice", StringSlice<UCS2>);
+ env->SetMethodNoSideEffect(target, "utf8Slice", StringSlice<UTF8>);
+
+ env->SetMethod(target, "asciiWrite", StringWrite<ASCII>);
+ env->SetMethod(target, "base64Write", StringWrite<BASE64>);
+ env->SetMethod(target, "latin1Write", StringWrite<LATIN1>);
+ env->SetMethod(target, "hexWrite", StringWrite<HEX>);
+ env->SetMethod(target, "ucs2Write", StringWrite<UCS2>);
+ env->SetMethod(target, "utf8Write", StringWrite<UTF8>);
+
+ // It can be a nullptr when running inside an isolate where we
+ // do not own the ArrayBuffer allocator.
+ if (uint32_t* zero_fill_field = env->isolate_data()->zero_fill_field()) {
+ Local<ArrayBuffer> array_buffer = ArrayBuffer::New(
+ env->isolate(), zero_fill_field, sizeof(*zero_fill_field));
+ CHECK(target
+ ->Set(env->context(),
+ FIXED_ONE_BYTE_STRING(env->isolate(), "zeroFill"),
+ Uint32Array::New(array_buffer, 0, 1))
+ .FromJust());
+ }
}
} // anonymous namespace