Skip to content

Commit 122797e

Browse files
yashLadhacodebytere
authored andcommitted
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: #34553 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent fbe210b commit 122797e

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/node_process_methods.cc

+13-12
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ static void Chdir(const FunctionCallbackInfo<Value>& args) {
8989
}
9090
}
9191

92+
inline Local<ArrayBuffer> get_fields_array_buffer(
93+
const FunctionCallbackInfo<Value>& args,
94+
size_t index,
95+
size_t array_length) {
96+
CHECK(args[index]->IsFloat64Array());
97+
Local<Float64Array> arr = args[index].As<Float64Array>();
98+
CHECK_EQ(arr->Length(), array_length);
99+
return arr->Buffer();
100+
}
101+
92102
// CPUUsage use libuv's uv_getrusage() this-process resource usage accessor,
93103
// to access ru_utime (user CPU time used) and ru_stime (system CPU time used),
94104
// which are uv_timeval_t structs (long tv_sec, long tv_usec).
@@ -104,10 +114,7 @@ static void CPUUsage(const FunctionCallbackInfo<Value>& args) {
104114
return env->ThrowUVException(err, "uv_getrusage");
105115

106116
// Get the double array pointer from the Float64Array argument.
107-
CHECK(args[0]->IsFloat64Array());
108-
Local<Float64Array> array = args[0].As<Float64Array>();
109-
CHECK_EQ(array->Length(), 2);
110-
Local<ArrayBuffer> ab = array->Buffer();
117+
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 2);
111118
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
112119

113120
// Set the Float64Array elements to be user / system values in microseconds.
@@ -174,10 +181,7 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
174181
env->isolate_data()->node_allocator();
175182

176183
// Get the double array pointer from the Float64Array argument.
177-
CHECK(args[0]->IsFloat64Array());
178-
Local<Float64Array> array = args[0].As<Float64Array>();
179-
CHECK_EQ(array->Length(), 5);
180-
Local<ArrayBuffer> ab = array->Buffer();
184+
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5);
181185
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
182186

183187
fields[0] = rss;
@@ -263,10 +267,7 @@ static void ResourceUsage(const FunctionCallbackInfo<Value>& args) {
263267
if (err)
264268
return env->ThrowUVException(err, "uv_getrusage");
265269

266-
CHECK(args[0]->IsFloat64Array());
267-
Local<Float64Array> array = args[0].As<Float64Array>();
268-
CHECK_EQ(array->Length(), 16);
269-
Local<ArrayBuffer> ab = array->Buffer();
270+
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 16);
270271
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());
271272

272273
fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;

0 commit comments

Comments
 (0)