Skip to content

Commit 8f3e8bb

Browse files
theanarkhjuanarbol
authored andcommitted
os: add machine method
PR-URL: #44416 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 89c837a commit 8f3e8bb

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
@@ -443,6 +443,22 @@ On POSIX systems, the operating system release is determined by calling
443443
available, `GetVersionExW()` will be used. See
444444
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.
445445

446+
## `os.machine()`
447+
448+
<!-- YAML
449+
added: REPLACEME
450+
-->
451+
452+
* Returns {string}
453+
454+
Returns the machine type as a string, such as `arm`, `aarch64`, `mips`,
455+
`mips64`, `ppc64`, `ppc64le`, `s390`, `s390x`, `i386`, `i686`, `x86_64`.
456+
457+
On POSIX systems, the machine type is determined by calling
458+
[`uname(3)`][]. On Windows, `RtlGetVersion()` is used, and if it is not
459+
available, `GetVersionExW()` will be used. See
460+
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.
461+
446462
## OS constants
447463

448464
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);
@@ -94,12 +95,17 @@ const getOSType = () => type;
9495
* @returns {string}
9596
*/
9697
const getOSVersion = () => version;
98+
/**
99+
* @returns {string}
100+
*/
101+
const getMachine = () => machine;
97102

98103
getFreeMem[SymbolToPrimitive] = () => getFreeMem();
99104
getHostname[SymbolToPrimitive] = () => getHostname();
100105
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
101106
getOSType[SymbolToPrimitive] = () => getOSType();
102107
getOSRelease[SymbolToPrimitive] = () => getOSRelease();
108+
getMachine[SymbolToPrimitive] = () => getMachine();
103109
getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory();
104110
getTotalMem[SymbolToPrimitive] = () => getTotalMem();
105111
getUptime[SymbolToPrimitive] = () => getUptime();
@@ -369,7 +375,8 @@ module.exports = {
369375
type: getOSType,
370376
userInfo,
371377
uptime: getUptime,
372-
version: getOSVersion
378+
version: getOSVersion,
379+
machine: getMachine,
373380
};
374381

375382
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)