From 18d5b99cabfad78e8acce3d840ef5e1e359bcceb Mon Sep 17 00:00:00 2001 From: Yash Ladha Date: Wed, 9 Sep 2020 21:08:20 +0530 Subject: [PATCH] src: remove duplicate logic for getting buffer We were fetching the buffer from the float array to send out the response in js land, however that logic is being duplicated in node_process.h. Now they will be using an inline to fetch the array buffers and making it more generic. --- src/node_process_methods.cc | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 105cbff151b151..ec6e25b49557a7 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -89,6 +89,16 @@ static void Chdir(const FunctionCallbackInfo& args) { } } +inline Local get_fields_array_buffer( + const FunctionCallbackInfo& args, + size_t index, + size_t array_length) { + CHECK(args[index]->IsFloat64Array()); + Local arr = args[index].As(); + CHECK_EQ(arr->Length(), array_length); + return arr->Buffer(); +} + // CPUUsage use libuv's uv_getrusage() this-process resource usage accessor, // to access ru_utime (user CPU time used) and ru_stime (system CPU time used), // which are uv_timeval_t structs (long tv_sec, long tv_usec). @@ -106,10 +116,7 @@ static void CPUUsage(const FunctionCallbackInfo& args) { } // Get the double array pointer from the Float64Array argument. - CHECK(args[0]->IsFloat64Array()); - Local array = args[0].As(); - CHECK_EQ(array->Length(), 2); - Local ab = array->Buffer(); + Local ab = get_fields_array_buffer(args, 0, 2); double* fields = static_cast(ab->GetBackingStore()->Data()); // Set the Float64Array elements to be user / system values in microseconds. @@ -175,10 +182,7 @@ static void MemoryUsage(const FunctionCallbackInfo& args) { env->isolate_data()->node_allocator(); // Get the double array pointer from the Float64Array argument. - CHECK(args[0]->IsFloat64Array()); - Local array = args[0].As(); - CHECK_EQ(array->Length(), 5); - Local ab = array->Buffer(); + Local ab = get_fields_array_buffer(args, 0, 5); double* fields = static_cast(ab->GetBackingStore()->Data()); fields[0] = rss; @@ -275,10 +279,7 @@ static void ResourceUsage(const FunctionCallbackInfo& args) { if (err) return env->ThrowUVException(err, "uv_getrusage"); - CHECK(args[0]->IsFloat64Array()); - Local array = args[0].As(); - CHECK_EQ(array->Length(), 16); - Local ab = array->Buffer(); + Local ab = get_fields_array_buffer(args, 0, 16); double* fields = static_cast(ab->GetBackingStore()->Data()); fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;