From 5e4e87ade5ad2dbf525865a3a94694dabb9b1d7f Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 8 Nov 2012 12:31:45 -0800 Subject: [PATCH] os: add os.endianness() function --- doc/api/os.markdown | 4 ++++ lib/os.js | 1 + src/node_os.cc | 9 +++++++++ test/simple/test-os.js | 4 ++++ 4 files changed, 18 insertions(+) diff --git a/doc/api/os.markdown b/doc/api/os.markdown index b5fffcf1cb..fac97d391b 100644 --- a/doc/api/os.markdown +++ b/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. +## os.endianness() + +Returns the endianness of the CPU. Possible values are `"BE"` or `"LE"`. + ## os.hostname() Returns the hostname of the operating system. diff --git a/lib/os.js b/lib/os.js index 1fe8731033..612a0e43d5 100644 --- a/lib/os.js +++ b/lib/os.js @@ -22,6 +22,7 @@ var binding = process.binding('os'); var util = require('util'); +exports.endianness = binding.getEndianness; exports.hostname = binding.getHostname; exports.loadavg = binding.getLoadAvg; exports.uptime = binding.getUptime; diff --git a/src/node_os.cc b/src/node_os.cc index c09458e60d..af363d33a5 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -41,6 +41,14 @@ namespace node { using namespace v8; +static Handle GetEndianness(const Arguments& args) { + HandleScope scope; + int i = 1; + bool big = (*(char *)&i) == 0; + Local endianness = String::New(big ? "BE" : "LE"); + return scope.Close(endianness); +} + static Handle GetHostname(const Arguments& args) { HandleScope scope; char s[255]; @@ -242,6 +250,7 @@ static Handle GetInterfaceAddresses(const Arguments& args) { void OS::Initialize(v8::Handle target) { HandleScope scope; + NODE_SET_METHOD(target, "getEndianness", GetEndianness); NODE_SET_METHOD(target, "getHostname", GetHostname); NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg); NODE_SET_METHOD(target, "getUptime", GetUptime); diff --git a/test/simple/test-os.js b/test/simple/test-os.js index b141f00089..59278329cc 100644 --- a/test/simple/test-os.js +++ b/test/simple/test-os.js @@ -39,6 +39,10 @@ assert.equal(os.tmpDir(), '/temp'); process.env.TEMP = ''; assert.equal(os.tmpDir(), t); +var endianness = os.endianness(); +console.log('endianness = %s', endianness); +assert.ok(/[BL]E/.test(endianness)); + var hostname = os.hostname(); console.log('hostname = %s', hostname); assert.ok(hostname.length > 0);