Skip to content

Commit 5e4e87a

Browse files
committed
os: add os.endianness() function
1 parent 3c91a7a commit 5e4e87a

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

doc/api/os.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Use `require('os')` to access this module.
1010

1111
Returns the operating system's default directory for temp files.
1212

13+
## os.endianness()
14+
15+
Returns the endianness of the CPU. Possible values are `"BE"` or `"LE"`.
16+
1317
## os.hostname()
1418

1519
Returns the hostname of the operating system.

lib/os.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
var binding = process.binding('os');
2323
var util = require('util');
2424

25+
exports.endianness = binding.getEndianness;
2526
exports.hostname = binding.getHostname;
2627
exports.loadavg = binding.getLoadAvg;
2728
exports.uptime = binding.getUptime;

src/node_os.cc

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ namespace node {
4141

4242
using namespace v8;
4343

44+
static Handle<Value> GetEndianness(const Arguments& args) {
45+
HandleScope scope;
46+
int i = 1;
47+
bool big = (*(char *)&i) == 0;
48+
Local<String> endianness = String::New(big ? "BE" : "LE");
49+
return scope.Close(endianness);
50+
}
51+
4452
static Handle<Value> GetHostname(const Arguments& args) {
4553
HandleScope scope;
4654
char s[255];
@@ -242,6 +250,7 @@ static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
242250
void OS::Initialize(v8::Handle<v8::Object> target) {
243251
HandleScope scope;
244252

253+
NODE_SET_METHOD(target, "getEndianness", GetEndianness);
245254
NODE_SET_METHOD(target, "getHostname", GetHostname);
246255
NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg);
247256
NODE_SET_METHOD(target, "getUptime", GetUptime);

test/simple/test-os.js

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ assert.equal(os.tmpDir(), '/temp');
3939
process.env.TEMP = '';
4040
assert.equal(os.tmpDir(), t);
4141

42+
var endianness = os.endianness();
43+
console.log('endianness = %s', endianness);
44+
assert.ok(/[BL]E/.test(endianness));
45+
4246
var hostname = os.hostname();
4347
console.log('hostname = %s', hostname);
4448
assert.ok(hostname.length > 0);

0 commit comments

Comments
 (0)