@@ -32,6 +32,7 @@ using v8::ArrayBuffer;
32
32
using v8::Boolean ;
33
33
using v8::Context;
34
34
using v8::Float64Array;
35
+ using v8::Function;
35
36
using v8::FunctionCallbackInfo;
36
37
using v8::Integer;
37
38
using v8::Local;
@@ -122,36 +123,47 @@ static void GetOSRelease(const FunctionCallbackInfo<Value>& args) {
122
123
static void GetCPUInfo (const FunctionCallbackInfo<Value>& args) {
123
124
Environment* env = Environment::GetCurrent (args);
124
125
uv_cpu_info_t * cpu_infos;
125
- int count, i;
126
+ int count, i, field_idx ;
126
127
127
128
int err = uv_cpu_info (&cpu_infos, &count);
128
129
if (err)
129
130
return ;
130
131
131
- Local<Array> cpus = Array::New (env->isolate ());
132
- for (i = 0 ; i < count; i++) {
132
+ CHECK (args[0 ]->IsFunction ());
133
+ Local<Function> addfn = args[0 ].As <Function>();
134
+
135
+ CHECK (args[1 ]->IsFloat64Array ());
136
+ Local<Float64Array> array = args[1 ].As <Float64Array>();
137
+ CHECK_EQ (array->Length (), 6 * NODE_PUSH_VAL_TO_ARRAY_MAX);
138
+ Local<ArrayBuffer> ab = array->Buffer ();
139
+ double * fields = static_cast <double *>(ab->GetContents ().Data ());
140
+
141
+ CHECK (args[2 ]->IsArray ());
142
+ Local<Array> cpus = args[2 ].As <Array>();
143
+
144
+ Local<Value> model_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
145
+ int model_idx = 0 ;
146
+
147
+ for (i = 0 , field_idx = 0 ; i < count; i++) {
133
148
uv_cpu_info_t * ci = cpu_infos + i;
134
149
135
- Local<Object> times_info = Object::New (env->isolate ());
136
- times_info->Set (env->user_string (),
137
- Number::New (env->isolate (), ci->cpu_times .user ));
138
- times_info->Set (env->nice_string (),
139
- Number::New (env->isolate (), ci->cpu_times .nice ));
140
- times_info->Set (env->sys_string (),
141
- Number::New (env->isolate (), ci->cpu_times .sys ));
142
- times_info->Set (env->idle_string (),
143
- Number::New (env->isolate (), ci->cpu_times .idle ));
144
- times_info->Set (env->irq_string (),
145
- Number::New (env->isolate (), ci->cpu_times .irq ));
146
-
147
- Local<Object> cpu_info = Object::New (env->isolate ());
148
- cpu_info->Set (env->model_string (),
149
- OneByteString (env->isolate (), ci->model ));
150
- cpu_info->Set (env->speed_string (),
151
- Number::New (env->isolate (), ci->speed ));
152
- cpu_info->Set (env->times_string (), times_info);
153
-
154
- (*cpus)->Set (i, cpu_info);
150
+ fields[field_idx++] = ci->speed ;
151
+ fields[field_idx++] = ci->cpu_times .user ;
152
+ fields[field_idx++] = ci->cpu_times .nice ;
153
+ fields[field_idx++] = ci->cpu_times .sys ;
154
+ fields[field_idx++] = ci->cpu_times .idle ;
155
+ fields[field_idx++] = ci->cpu_times .irq ;
156
+ model_argv[model_idx++] = OneByteString (env->isolate (), ci->model );
157
+
158
+ if (model_idx >= NODE_PUSH_VAL_TO_ARRAY_MAX) {
159
+ addfn->Call (env->context (), cpus, model_idx, model_argv).ToLocalChecked ();
160
+ model_idx = 0 ;
161
+ field_idx = 0 ;
162
+ }
163
+ }
164
+
165
+ if (model_idx > 0 ) {
166
+ addfn->Call (env->context (), cpus, model_idx, model_argv).ToLocalChecked ();
155
167
}
156
168
157
169
uv_free_cpu_info (cpu_infos, count);
0 commit comments