From a1e78d4d85396fbf61e0b689a329c7deef4f5272 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Sat, 27 Aug 2022 14:29:28 +0800 Subject: [PATCH] os: add machine method --- 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 e4b9d93b41bac4..327cbd5b3a1e4f 100644 --- a/doc/api/os.md +++ b/doc/api/os.md @@ -452,6 +452,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 81bbe15a9d2a42..c3bb404b2d776b 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); @@ -92,12 +93,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(); @@ -367,7 +373,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 23218310974b7b..9079faf887dc7a 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.