Skip to content

Commit 6714736

Browse files
theanarkhRafaelGSS
authored andcommitted
os: add machine method
PR-URL: #44416 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b660b74 commit 6714736

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

doc/api/os.md

+16
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,22 @@ On POSIX systems, the operating system release is determined by calling
452452
available, `GetVersionExW()` will be used. See
453453
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.
454454

455+
## `os.machine()`
456+
457+
<!-- YAML
458+
added: REPLACEME
459+
-->
460+
461+
* Returns {string}
462+
463+
Returns the machine type as a string, such as `arm`, `aarch64`, `mips`,
464+
`mips64`, `ppc64`, `ppc64le`, `s390`, `s390x`, `i386`, `i686`, `x86_64`.
465+
466+
On POSIX systems, the machine type is determined by calling
467+
[`uname(3)`][]. On Windows, `RtlGetVersion()` is used, and if it is not
468+
available, `GetVersionExW()` will be used. See
469+
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.
470+
455471
## OS constants
456472

457473
The following constants are exported by `os.constants`.

lib/os.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const {
7575
0: type,
7676
1: version,
7777
2: release,
78+
3: machine,
7879
} = _getOSInformation();
7980

8081
const getHomeDirectory = getCheckedFunction(_getHomeDirectory);
@@ -92,12 +93,17 @@ const getOSType = () => type;
9293
* @returns {string}
9394
*/
9495
const getOSVersion = () => version;
96+
/**
97+
* @returns {string}
98+
*/
99+
const getMachine = () => machine;
95100

96101
getFreeMem[SymbolToPrimitive] = () => getFreeMem();
97102
getHostname[SymbolToPrimitive] = () => getHostname();
98103
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
99104
getOSType[SymbolToPrimitive] = () => getOSType();
100105
getOSRelease[SymbolToPrimitive] = () => getOSRelease();
106+
getMachine[SymbolToPrimitive] = () => getMachine();
101107
getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory();
102108
getTotalMem[SymbolToPrimitive] = () => getTotalMem();
103109
getUptime[SymbolToPrimitive] = () => getUptime();
@@ -367,7 +373,8 @@ module.exports = {
367373
type: getOSType,
368374
userInfo,
369375
uptime: getUptime,
370-
version: getOSVersion
376+
version: getOSVersion,
377+
machine: getMachine,
371378
};
372379

373380
ObjectDefineProperties(module.exports, {

src/node_os.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
8686
return args.GetReturnValue().SetUndefined();
8787
}
8888

89-
// [sysname, version, release]
89+
// [sysname, version, release, machine]
9090
Local<Value> osInformation[] = {
91-
String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(),
92-
String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(),
93-
String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked()
94-
};
91+
String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(),
92+
String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(),
93+
String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked(),
94+
String::NewFromUtf8(env->isolate(), info.machine).ToLocalChecked()};
9595

9696
args.GetReturnValue().Set(Array::New(env->isolate(),
9797
osInformation,

test/parallel/test-os.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ assert.strictEqual(`${os.tmpdir}`, os.tmpdir());
245245
assert.strictEqual(`${os.arch}`, os.arch());
246246
assert.strictEqual(`${os.platform}`, os.platform());
247247
assert.strictEqual(`${os.version}`, os.version());
248-
248+
assert.strictEqual(`${os.machine}`, os.machine());
249249
assert.strictEqual(+os.totalmem, os.totalmem());
250250

251251
// Assert that the following values are coercible to numbers.

0 commit comments

Comments
 (0)