Skip to content

Commit 500c8f4

Browse files
author
Igor Zinkovsky
committed
Remove platform files, and use uv platform api
1 parent 88cc688 commit 500c8f4

18 files changed

Lines changed: 130 additions & 1952 deletions

node.gyp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@
109109
'src/node_string.h',
110110
'src/node_version.h',
111111
'src/pipe_wrap.h',
112-
'src/platform.h',
113112
'src/req_wrap.h',
114113
'src/stream_wrap.h',
115114
'src/v8_typed_array.h',
@@ -152,9 +151,6 @@
152151

153152
[ 'OS=="win"', {
154153
'sources': [
155-
'src/platform_win32.cc',
156-
# headers to make for a more pleasant IDE experience
157-
'src/platform_win32.h',
158154
'tools/msvs/res/node.rc',
159155
],
160156
'defines': [
@@ -172,25 +168,21 @@
172168
]
173169
}],
174170
[ 'OS=="mac"', {
175-
'sources': [ 'src/platform_darwin.cc' ],
176171
'libraries': [ '-framework Carbon' ],
177172
}],
178173
[ 'OS=="linux"', {
179-
'sources': [ 'src/platform_linux.cc' ],
180174
'libraries': [
181175
'-ldl',
182176
'-lutil' # needed for openpty
183177
],
184178
}],
185179
[ 'OS=="freebsd"', {
186-
'sources': [ 'src/platform_freebsd.cc' ],
187180
'libraries': [
188181
'-lutil',
189182
'-lkvm',
190183
],
191184
}],
192185
[ 'OS=="solaris"', {
193-
'sources': [ 'src/platform_sunos.cc' ],
194186
'libraries': [
195187
'-lkstat',
196188
],

src/node.cc

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,11 @@ typedef int mode_t;
5656
#include <errno.h>
5757
#include <sys/types.h>
5858

59-
#if defined(__MINGW32__) || defined(_MSC_VER)
60-
# include <platform_win32.h> /* winapi_perror() */
61-
#endif
62-
6359
#ifdef __POSIX__
6460
# include <pwd.h> /* getpwnam() */
6561
# include <grp.h> /* getgrnam() */
6662
#endif
6763

68-
#include "platform.h"
6964
#include <node_buffer.h>
7065
#ifdef __POSIX__
7166
# include <node_io_watcher.h>
@@ -137,7 +132,7 @@ extern char **environ;
137132
#define module_load_list NODE_VAR(module_load_list)
138133
#define node_isolate NODE_VAR(node_isolate)
139134
#define debugger_running NODE_VAR(debugger_running)
140-
135+
#define prog_start_time NODE_VAR(prog_start_time)
141136

142137
namespace node {
143138

@@ -854,6 +849,30 @@ Local<Value> UVException(int errorno,
854849

855850

856851
#ifdef _WIN32
852+
// Does about the same as strerror(),
853+
// but supports all windows error messages
854+
static const char *winapi_strerror(const int errorno) {
855+
char *errmsg = NULL;
856+
857+
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
858+
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
859+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
860+
861+
if (errmsg) {
862+
// Remove trailing newlines
863+
for (int i = strlen(errmsg) - 1;
864+
i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
865+
errmsg[i] = '\0';
866+
}
867+
868+
return errmsg;
869+
} else {
870+
// FormatMessage failed
871+
return "Unknown error";
872+
}
873+
}
874+
875+
857876
Local<Value> WinapiErrnoException(int errorno,
858877
const char* syscall,
859878
const char* msg,
@@ -1485,17 +1504,19 @@ static void CheckStatus(uv_timer_t* watcher, int status) {
14851504
}
14861505
}
14871506

1507+
14881508
static Handle<Value> Uptime(const Arguments& args) {
14891509
HandleScope scope;
14901510
assert(args.Length() == 0);
1511+
double uptime;
14911512

1492-
double uptime = Platform::GetUptime(true);
1513+
uv_err_t err = uv_uptime(&uptime);
14931514

1494-
if (uptime < 0) {
1515+
if (err.code != UV_OK) {
14951516
return Undefined();
14961517
}
14971518

1498-
return scope.Close(Number::New(uptime));
1519+
return scope.Close(Number::New(uptime - prog_start_time));
14991520
}
15001521

15011522

@@ -1537,10 +1558,10 @@ v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
15371558

15381559
size_t rss;
15391560

1540-
int r = Platform::GetMemory(&rss);
1561+
uv_err_t err = uv_resident_set_memory(&rss);
15411562

1542-
if (r != 0) {
1543-
return ThrowException(Exception::Error(String::New(strerror(errno))));
1563+
if (err.code != UV_OK) {
1564+
return ThrowException(UVException(err.code, "uv_resident_set_memory"));
15441565
}
15451566

15461567
Local<Object> info = Object::New();
@@ -1829,9 +1850,9 @@ static Handle<Value> Binding(const Arguments& args) {
18291850
static Handle<Value> ProcessTitleGetter(Local<String> property,
18301851
const AccessorInfo& info) {
18311852
HandleScope scope;
1832-
int len;
1833-
const char *s = Platform::GetProcessTitle(&len);
1834-
return scope.Close(s ? String::New(s, len) : String::Empty());
1853+
char buffer[512];
1854+
uv_get_process_title(buffer, sizeof(buffer));
1855+
return scope.Close(String::New(buffer));
18351856
}
18361857

18371858

@@ -1840,7 +1861,8 @@ static void ProcessTitleSetter(Local<String> property,
18401861
const AccessorInfo& info) {
18411862
HandleScope scope;
18421863
String::Utf8Value title(value->ToString());
1843-
Platform::SetProcessTitle(*title);
1864+
// TODO: protect with a lock
1865+
uv_set_process_title(*title);
18441866
}
18451867

18461868

@@ -2490,8 +2512,11 @@ static Handle<Value> DebugProcess(const Arguments& args) {
24902512

24912513

24922514
char** Init(int argc, char *argv[]) {
2515+
// Initialize prog_start_time to get relative uptime.
2516+
uv_uptime(&prog_start_time);
2517+
24932518
// Hack aroung with the argv pointer. Used for process.title = "blah".
2494-
argv = node::Platform::SetupArgs(argc, argv);
2519+
argv = uv_setup_args(argc, argv);
24952520

24962521
// Parse a few arguments which are specific to Node.
24972522
node::ParseArgs(argc, argv);

src/node_buffer.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
#include <stdlib.h> // malloc, free
3030
#include <string.h> // memcpy
3131

32-
#ifdef __MINGW32__
33-
# include "platform.h"
34-
#endif
35-
3632
#ifdef __POSIX__
3733
# include <arpa/inet.h> // htons, htonl
3834
#endif

src/node_constants.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
#include <sys/types.h>
3333
#include <sys/stat.h>
3434

35-
#if defined(__MINGW32__) || defined(_MSC_VER)
36-
# include <platform_win32.h>
37-
#endif
38-
3935
#if HAVE_OPENSSL
4036
# include <openssl/ssl.h>
4137
#endif

src/node_file.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
#if defined(__MINGW32__) || defined(_MSC_VER)
3939
# include <io.h>
40-
# include <platform_win32.h>
4140
#endif
4241

4342

src/node_os.cc

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#include <node.h>
2424
#include <node_os.h>
25-
#include "platform.h"
2625

2726
#include <v8.h>
2827

@@ -31,8 +30,6 @@
3130

3231
#ifdef __MINGW32__
3332
# include <io.h>
34-
35-
# include <platform_win32.h>
3633
#endif
3734

3835
#ifdef __POSIX__
@@ -105,13 +102,39 @@ static Handle<Value> GetOSRelease(const Arguments& args) {
105102

106103
static Handle<Value> GetCPUInfo(const Arguments& args) {
107104
HandleScope scope;
108-
Local<Array> cpus;
109-
int r = Platform::GetCPUInfo(&cpus);
105+
uv_cpu_info_t* cpu_infos;
106+
int count, i;
110107

111-
if (r < 0) {
108+
uv_err_t err = uv_cpu_info(&cpu_infos, &count);
109+
110+
if (err.code != UV_OK) {
112111
return Undefined();
113112
}
114113

114+
Local<Array> cpus = Array::New();
115+
116+
for (i = 0; i < count; i++) {
117+
Local<Object> times_info = Object::New();
118+
times_info->Set(String::New("user"),
119+
Integer::New(cpu_infos[i].cpu_times.user));
120+
times_info->Set(String::New("nice"),
121+
Integer::New(cpu_infos[i].cpu_times.nice));
122+
times_info->Set(String::New("sys"),
123+
Integer::New(cpu_infos[i].cpu_times.sys));
124+
times_info->Set(String::New("idle"),
125+
Integer::New(cpu_infos[i].cpu_times.idle));
126+
times_info->Set(String::New("irq"),
127+
Integer::New(cpu_infos[i].cpu_times.irq));
128+
129+
Local<Object> cpu_info = Object::New();
130+
cpu_info->Set(String::New("model"), String::New(cpu_infos[i].model));
131+
cpu_info->Set(String::New("speed"), Integer::New(cpu_infos[i].speed));
132+
cpu_info->Set(String::New("times"), times_info);
133+
(*cpus)->Set(i,cpu_info);
134+
}
135+
136+
uv_free_cpu_info(cpu_infos, count);
137+
115138
return scope.Close(cpus);
116139
}
117140

@@ -139,9 +162,11 @@ static Handle<Value> GetTotalMemory(const Arguments& args) {
139162

140163
static Handle<Value> GetUptime(const Arguments& args) {
141164
HandleScope scope;
142-
double uptime = Platform::GetUptime();
165+
double uptime;
143166

144-
if (uptime < 0) {
167+
uv_err_t err = uv_uptime(&uptime);
168+
169+
if (err.code != UV_OK) {
145170
return Undefined();
146171
}
147172

@@ -163,7 +188,54 @@ static Handle<Value> GetLoadAvg(const Arguments& args) {
163188

164189

165190
static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
166-
return Platform::GetInterfaceAddresses();
191+
HandleScope scope;
192+
uv_interface_address_t* interfaces;
193+
int count, i;
194+
char ip[INET6_ADDRSTRLEN];
195+
Local<Object> ret, o;
196+
Local<String> name, family;
197+
Local<Array> ifarr;
198+
199+
uv_err_t err = uv_interface_addresses(&interfaces, &count);
200+
201+
if (err.code != UV_OK) {
202+
return Undefined();
203+
}
204+
205+
ret = Object::New();
206+
207+
for (i = 0; i < count; i++) {
208+
name = String::New(interfaces[i].name);
209+
if (ret->Has(name)) {
210+
ifarr = Local<Array>::Cast(ret->Get(name));
211+
} else {
212+
ifarr = Array::New();
213+
ret->Set(name, ifarr);
214+
}
215+
216+
if (interfaces[i].address.address4.sin_family == AF_INET) {
217+
uv_ip4_name(&interfaces[i].address.address4,ip, sizeof(ip));
218+
family = String::New("IPv4");
219+
} else if (interfaces[i].address.address4.sin_family == AF_INET6) {
220+
uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
221+
family = String::New("IPv6");
222+
} else {
223+
strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
224+
family = String::New("<unknown>");
225+
}
226+
227+
o = Object::New();
228+
o->Set(String::New("address"), String::New(ip));
229+
o->Set(String::New("family"), family);
230+
o->Set(String::New("internal"), interfaces[i].is_internal ?
231+
True() : False());
232+
233+
ifarr->Set(ifarr->Length(), o);
234+
}
235+
236+
uv_free_interface_addresses(interfaces, count);
237+
238+
return scope.Close(ret);
167239
}
168240

169241

src/node_vars.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ struct globals {
7070
v8::Persistent<v8::Array> module_load_list;
7171
v8::Isolate* node_isolate;
7272
volatile bool debugger_running;
73-
74-
73+
double prog_start_time;
7574

7675
// stream_wrap.cc
7776
size_t slab_used;
@@ -176,13 +175,6 @@ struct globals {
176175
v8::Persistent<v8::String> write_sym;
177176
v8::Persistent<v8::FunctionTemplate> buffer_constructor_template;
178177

179-
// platform*.cc
180-
char* process_title;
181-
struct {
182-
char *str;
183-
size_t len;
184-
} linux_process_title;
185-
186178
// node_signal_watcher.cc
187179
v8::Persistent<v8::String> callback_symbol;
188180
v8::Persistent<v8::FunctionTemplate> signal_watcher_constructor_template;

0 commit comments

Comments
 (0)