Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: add machine method #44416

Merged
merged 1 commit into from Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/api/os.md
Expand Up @@ -452,6 +452,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
Expand Up @@ -75,6 +75,7 @@ const {
0: type,
1: version,
2: release,
3: machine,
} = _getOSInformation();

const getHomeDirectory = getCheckedFunction(_getHomeDirectory);
Expand All @@ -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();
Expand Down Expand Up @@ -367,7 +373,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
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
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