diff options
author | Yash Ladha <yashladhapankajladha123@gmail.com> | 2020-09-09 18:38:20 +0300 |
---|---|---|
committer | Node.js GitHub Bot <github-bot@iojs.org> | 2020-11-14 00:50:49 +0300 |
commit | 8656ab49e3b11c48ca245234b7a57720503fc18c (patch) | |
tree | 8e52c97877fb31dcd0032472aee5e24288b9aa41 /src/node_process_methods.cc | |
parent | 4b0308ad34005dfde83afe7e32eb39cdaca9a4a1 (diff) |
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.
PR-URL: https://github.com/nodejs/node/pull/34553
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/node_process_methods.cc')
-rw-r--r-- | src/node_process_methods.cc | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 6e7b1c92946..4a6d767f9b7 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -89,6 +89,16 @@ static void Chdir(const FunctionCallbackInfo<Value>& args) { } } +inline Local<ArrayBuffer> get_fields_array_buffer( + const FunctionCallbackInfo<Value>& args, + size_t index, + size_t array_length) { + CHECK(args[index]->IsFloat64Array()); + Local<Float64Array> arr = args[index].As<Float64Array>(); + 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). @@ -104,10 +114,7 @@ static void CPUUsage(const FunctionCallbackInfo<Value>& args) { return env->ThrowUVException(err, "uv_getrusage"); // Get the double array pointer from the Float64Array argument. - CHECK(args[0]->IsFloat64Array()); - Local<Float64Array> array = args[0].As<Float64Array>(); - CHECK_EQ(array->Length(), 2); - Local<ArrayBuffer> ab = array->Buffer(); + Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 2); double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); // Set the Float64Array elements to be user / system values in microseconds. @@ -174,10 +181,7 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) { env->isolate_data()->node_allocator(); // Get the double array pointer from the Float64Array argument. - CHECK(args[0]->IsFloat64Array()); - Local<Float64Array> array = args[0].As<Float64Array>(); - CHECK_EQ(array->Length(), 5); - Local<ArrayBuffer> ab = array->Buffer(); + Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5); double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); fields[0] = rss; @@ -263,10 +267,7 @@ static void ResourceUsage(const FunctionCallbackInfo<Value>& args) { if (err) return env->ThrowUVException(err, "uv_getrusage"); - CHECK(args[0]->IsFloat64Array()); - Local<Float64Array> array = args[0].As<Float64Array>(); - CHECK_EQ(array->Length(), 16); - Local<ArrayBuffer> ab = array->Buffer(); + Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 16); double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; |