Skip to content

Commit

Permalink
os: add machine method
Browse files Browse the repository at this point in the history
PR-URL: #44416
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
theanarkh authored and juanarbol committed Oct 4, 2022
1 parent b89049e commit be9b3e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
16 changes: 16 additions & 0 deletions doc/api/os.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ On POSIX systems, the operating system release is determined by calling
available, `GetVersionExW()` will be used. See
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.

## `os.machine()`

<!-- YAML
added: REPLACEME
-->

* 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
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.

## OS constants

The following constants are exported by `os.constants`.
Expand Down
9 changes: 8 additions & 1 deletion lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const {
0: type,
1: version,
2: release,
3: machine,
} = _getOSInformation();

const getHomeDirectory = getCheckedFunction(_getHomeDirectory);
Expand All @@ -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();
Expand Down Expand Up @@ -369,7 +375,8 @@ module.exports = {
type: getOSType,
userInfo,
uptime: getUptime,
version: getOSVersion
version: getOSVersion,
machine: getMachine,
};

ObjectDefineProperties(module.exports, {
Expand Down
10 changes: 5 additions & 5 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().SetUndefined();
}

// [sysname, version, release]
// [sysname, version, release, machine]
Local<Value> 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,
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-os.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit be9b3e6

Please sign in to comment.