From 8f3e8bb1fdf9d14c68a0c763cfe99d7dcddeaffb Mon Sep 17 00:00:00 2001 From: theanarkh Date: Mon, 29 Aug 2022 15:49:32 +0800 Subject: [PATCH] os: add machine method PR-URL: https://github.com/nodejs/node/pull/44416 Reviewed-By: Luigi Pinca Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- doc/api/os.md | 16 ++++++++++++++++ lib/os.js | 9 ++++++++- src/node_os.cc | 10 +++++----- test/parallel/test-os.js | 2 +- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/doc/api/os.md b/doc/api/os.md index 23467112487f57..2b474a82dbda09 100644 --- a/doc/api/os.md +++ b/doc/api/os.md @@ -443,6 +443,22 @@ On POSIX systems, the operating system release is determined by calling available, `GetVersionExW()` will be used. See for more information. +## `os.machine()` + + + +* Returns {string} + +Returns the machine type as a string, such as `arm`, `aarch64`, `mips`, +`mips64`, `ppc64`, `ppc64le`, `s390`, `s390x`, `i386`, `i686`, `x86_64`. + +On POSIX systems, the machine type is determined by calling +[`uname(3)`][]. On Windows, `RtlGetVersion()` is used, and if it is not +available, `GetVersionExW()` will be used. See + for more information. + ## OS constants The following constants are exported by `os.constants`. diff --git a/lib/os.js b/lib/os.js index b8948633cddfa4..860348a472f325 100644 --- a/lib/os.js +++ b/lib/os.js @@ -75,6 +75,7 @@ const { 0: type, 1: version, 2: release, + 3: machine, } = _getOSInformation(); const getHomeDirectory = getCheckedFunction(_getHomeDirectory); @@ -94,12 +95,17 @@ const getOSType = () => type; * @returns {string} */ const getOSVersion = () => version; +/** + * @returns {string} + */ +const getMachine = () => machine; getFreeMem[SymbolToPrimitive] = () => getFreeMem(); getHostname[SymbolToPrimitive] = () => getHostname(); getOSVersion[SymbolToPrimitive] = () => getOSVersion(); getOSType[SymbolToPrimitive] = () => getOSType(); getOSRelease[SymbolToPrimitive] = () => getOSRelease(); +getMachine[SymbolToPrimitive] = () => getMachine(); getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory(); getTotalMem[SymbolToPrimitive] = () => getTotalMem(); getUptime[SymbolToPrimitive] = () => getUptime(); @@ -369,7 +375,8 @@ module.exports = { type: getOSType, userInfo, uptime: getUptime, - version: getOSVersion + version: getOSVersion, + machine: getMachine, }; ObjectDefineProperties(module.exports, { diff --git a/src/node_os.cc b/src/node_os.cc index 79a50692afc458..52bb82a2b684d8 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -86,12 +86,12 @@ static void GetOSInformation(const FunctionCallbackInfo& args) { return args.GetReturnValue().SetUndefined(); } - // [sysname, version, release] + // [sysname, version, release, machine] Local osInformation[] = { - String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(), - String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(), - String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked() - }; + String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(), + String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(), + String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked(), + String::NewFromUtf8(env->isolate(), info.machine).ToLocalChecked()}; args.GetReturnValue().Set(Array::New(env->isolate(), osInformation, diff --git a/test/parallel/test-os.js b/test/parallel/test-os.js index 80c32ea2a996c0..cceebe1a6f19d0 100644 --- a/test/parallel/test-os.js +++ b/test/parallel/test-os.js @@ -245,7 +245,7 @@ assert.strictEqual(`${os.tmpdir}`, os.tmpdir()); assert.strictEqual(`${os.arch}`, os.arch()); assert.strictEqual(`${os.platform}`, os.platform()); assert.strictEqual(`${os.version}`, os.version()); - +assert.strictEqual(`${os.machine}`, os.machine()); assert.strictEqual(+os.totalmem, os.totalmem()); // Assert that the following values are coercible to numbers.