Browse Source

os: add os.endianness() function

v0.9.4-release
Nathan Rajlich 12 years ago
parent
commit
5e4e87ade5
  1. 4
      doc/api/os.markdown
  2. 1
      lib/os.js
  3. 9
      src/node_os.cc
  4. 4
      test/simple/test-os.js

4
doc/api/os.markdown

@ -10,6 +10,10 @@ Use `require('os')` to access this module.
Returns the operating system's default directory for temp files. Returns the operating system's default directory for temp files.
## os.endianness()
Returns the endianness of the CPU. Possible values are `"BE"` or `"LE"`.
## os.hostname() ## os.hostname()
Returns the hostname of the operating system. Returns the hostname of the operating system.

1
lib/os.js

@ -22,6 +22,7 @@
var binding = process.binding('os'); var binding = process.binding('os');
var util = require('util'); var util = require('util');
exports.endianness = binding.getEndianness;
exports.hostname = binding.getHostname; exports.hostname = binding.getHostname;
exports.loadavg = binding.getLoadAvg; exports.loadavg = binding.getLoadAvg;
exports.uptime = binding.getUptime; exports.uptime = binding.getUptime;

9
src/node_os.cc

@ -41,6 +41,14 @@ namespace node {
using namespace v8; using namespace v8;
static Handle<Value> GetEndianness(const Arguments& args) {
HandleScope scope;
int i = 1;
bool big = (*(char *)&i) == 0;
Local<String> endianness = String::New(big ? "BE" : "LE");
return scope.Close(endianness);
}
static Handle<Value> GetHostname(const Arguments& args) { static Handle<Value> GetHostname(const Arguments& args) {
HandleScope scope; HandleScope scope;
char s[255]; char s[255];
@ -242,6 +250,7 @@ static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
void OS::Initialize(v8::Handle<v8::Object> target) { void OS::Initialize(v8::Handle<v8::Object> target) {
HandleScope scope; HandleScope scope;
NODE_SET_METHOD(target, "getEndianness", GetEndianness);
NODE_SET_METHOD(target, "getHostname", GetHostname); NODE_SET_METHOD(target, "getHostname", GetHostname);
NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg); NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg);
NODE_SET_METHOD(target, "getUptime", GetUptime); NODE_SET_METHOD(target, "getUptime", GetUptime);

4
test/simple/test-os.js

@ -39,6 +39,10 @@ assert.equal(os.tmpDir(), '/temp');
process.env.TEMP = ''; process.env.TEMP = '';
assert.equal(os.tmpDir(), t); assert.equal(os.tmpDir(), t);
var endianness = os.endianness();
console.log('endianness = %s', endianness);
assert.ok(/[BL]E/.test(endianness));
var hostname = os.hostname(); var hostname = os.hostname();
console.log('hostname = %s', hostname); console.log('hostname = %s', hostname);
assert.ok(hostname.length > 0); assert.ok(hostname.length > 0);

Loading…
Cancel
Save