-
Notifications
You must be signed in to change notification settings - Fork 29.4k
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
process: add nice() function #21675
process: add nice() function #21675
Changes from all commits
c5aaa5a
a9f80d4
cc92503
bf86dcc
e1388f3
01b0643
aadb65b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,14 @@ const { | |
errnoException, | ||
codes: { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_UNKNOWN_CREDENTIAL | ||
ERR_UNKNOWN_CREDENTIAL, | ||
ERR_METHOD_NOT_IMPLEMENTED | ||
} | ||
} = require('internal/errors'); | ||
const { | ||
validateMode, | ||
validateUint32 | ||
validateUint32, | ||
validateInt32 | ||
} = require('internal/validators'); | ||
|
||
const { | ||
|
@@ -29,12 +31,23 @@ function setupStdio() { | |
// Non-POSIX platforms like Windows don't have certain methods. | ||
// Workers also lack these methods since they change process-global state. | ||
function setupProcessMethods(_chdir, _umask, _initgroups, _setegid, | ||
_seteuid, _setgid, _setuid, _setgroups) { | ||
_seteuid, _nice, _setgid, _setuid, | ||
_setgroups) { | ||
|
||
if (_setgid !== undefined) { | ||
setupPosixMethods(_initgroups, _setegid, _seteuid, | ||
_setgid, _setuid, _setgroups); | ||
} | ||
|
||
process.nice = function nice(inc) { | ||
if (process.platform === 'win32') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps change this so the platform check is only run once when this file is evaluated, since the process platform will not change during runtime. Example: if (process.platform === 'win32') {
process.nice = function nice(inc) {
throw new ERR_METHOD_NOT_IMPLEMENTED('nice()');
};
} else {
process.nice = function nice(inc) {
validateInc(inc);
return _nice(inc);
};
} or something similar |
||
throw new ERR_METHOD_NOT_IMPLEMENTED('nice()'); | ||
} | ||
|
||
validateInc(inc); | ||
return _nice(inc); | ||
}; | ||
|
||
process.chdir = function chdir(directory) { | ||
if (typeof directory !== 'string') { | ||
throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory); | ||
|
@@ -50,6 +63,14 @@ function setupProcessMethods(_chdir, _umask, _initgroups, _setegid, | |
mask = validateMode(mask, 'mask'); | ||
return _umask(mask); | ||
}; | ||
|
||
function validateInc(inc) { | ||
if (typeof inc === 'number') { | ||
validateInt32(inc, 'nice'); | ||
} else if (inc !== undefined) { | ||
throw new ERR_INVALID_ARG_TYPE('nice', ['number', 'undefined'], inc); | ||
} | ||
} | ||
} | ||
|
||
function setupPosixMethods(_initgroups, _setegid, _seteuid, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
if (common.isWindows || !common.isMainThread) { | ||
assert.strictEqual(process.nice, undefined); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer valid since we're now throwing. However, I feel like we should just call |
||
return; | ||
} | ||
|
||
assert.throws( | ||
() => { | ||
process.nice(NaN); | ||
}, | ||
{ | ||
code: 'ERR_OUT_OF_RANGE', | ||
name: 'RangeError [ERR_OUT_OF_RANGE]', | ||
message: 'The value of "nice" is out of range. It must be ' + | ||
'an integer. Received NaN' | ||
} | ||
); | ||
|
||
assert.throws( | ||
() => { | ||
process.nice(Infinity); | ||
}, | ||
{ | ||
code: 'ERR_OUT_OF_RANGE', | ||
name: 'RangeError [ERR_OUT_OF_RANGE]', | ||
message: 'The value of "nice" is out of range. It must be ' + | ||
'an integer. Received Infinity' | ||
} | ||
); | ||
|
||
[null, false, true, {}, [], () => {}, 'text'].forEach((val) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about also testing that passing '0' or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, we might want to test that something like |
||
assert.throws( | ||
() => { | ||
process.nice(val); | ||
}, | ||
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError [ERR_INVALID_ARG_TYPE]', | ||
message: 'The "nice" argument must be ' + | ||
'one of type number or undefined. ' + | ||
`Received type ${typeof val}` | ||
} | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think having this is necessary, especially since we already document Windows is not supported below this example.