Skip to content

uv: binding improvements - #14933

Closed
jasnell wants to merge 4 commits into
nodejs:masterfrom
jasnell:uv-binding-improvements
Closed

uv: binding improvements#14933
jasnell wants to merge 4 commits into
nodejs:masterfrom
jasnell:uv-binding-improvements

Conversation

@jasnell

Copy link
Copy Markdown
Member

Few improvements to process.binding('uv')

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

uv

The error message when passing in `err >= 0` to `util._errnoException()`
was less than useful.
@jasnelljasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Aug 18, 2017
@nodejs-github-botnodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Aug 18, 2017
Comment threadlib/_stream_wrap.js Outdated
else
errCode = uv.UV_EPIPE;
const code = uv[`UV_${err.code}`];
errCode = err.code && code ? code : uv.UV_EPIPE;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

teeny tiny suggestion: err.code && code in parentheses might be more readable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions:

  • This closes over err, why not hoist it out of the callback, so only errCode gets closed.
  • if !err.code then `UV_${err.code}` == somethinglike('UV_undefined') so uv[`UV_${err.code}`] == false
  • IIFE can be nice:
consterrCode=(()=>{if(!err)return0;returnuv[`UV_${err.code}`]||uv.UV_EPIPE;})();

@addaleax

Copy link
Copy Markdown
Member

I don’t think this needs to be semver-major.

refack
refack previously requested changes Aug 19, 2017
Comment threadlib/_stream_wrap.js Outdated
else
errCode = uv.UV_EPIPE;
const code = uv[`UV_${err.code}`];
errCode = err.code && code ? code : uv.UV_EPIPE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions:

  • This closes over err, why not hoist it out of the callback, so only errCode gets closed.
  • if !err.code then `UV_${err.code}` == somethinglike('UV_undefined') so uv[`UV_${err.code}`] == false
  • IIFE can be nice:
consterrCode=(()=>{if(!err)return0;returnuv[`UV_${err.code}`]||uv.UV_EPIPE;})();

// cluster modules for stuff like this.
const errno = uv['UV_' + err.errno];
send(errno, null);
send(uv[`UV_${err.errno}`], null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-0 on this.
I like one expression per line...

Comment threadtest/parallel/test-uv-errno.js Outdated
});

[0, 1, 'test', {}, [], Infinity, -Infinity, NaN].forEach((key) => {
assert.throws(() => util._errnoException(key),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the new expectsError syntex:

common.expectsError(()=>util._errnoException(key),{code: 'ERR_INVALID_ARG_TYPE',type: TypeError,message: 'The "err" argument must be of type negative number'});

if (key === 'errname')
return;

assert.doesNotThrow(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the assert.doesNotThrow is necessary...

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly necessary but not a problem either

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it mangles the actual error a little bit:

{
letthrew=false;
try{
assert.doesNotThrow(makeBlock(thrower,Error));
}catch(e){
threw=true;
common.expectsError({
code: 'ERR_ASSERTION',
message: /Gotunwantedexception\.\n\[objectObject\]/
})(e);
}
assert.ok(threw);
}

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware of what the function does. These cases do not throw

const util = require('util');
const uv = process.binding('uv');

const keys = Object.keys(uv);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd go:

constkeys=Object.keys(uv).filter(k=>k!=='errname');

and drop L10-11

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not particularly a fan of using filter

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

if (key === 'errname')
return; // skip this
const val = uv[key];
assert.throws(() => uv[key] = 1,

@refackrefackAug 19, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT assignment is not a valid lambda expression, you need to () => { uv[key] = 1; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what am I thinking us 😕? I remember an old debate that ended up with the invention of Object.assign().
Ohh it return the LHS, not the object.

@refack

Copy link
Copy Markdown
Contributor

Not all comments are blocking, but PTAL.

@jasnell

Copy link
Copy Markdown
MemberAuthor

It has to be semver-major because of the error message change.

Comment threadlib/util.js
var e = new Error(message);
e.code = name;
e.errno = name;
const e = new Error(message);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO a new type something like errors.UVError or errors.PlatformError would be nice, but then we'd need to expose it somehow.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not something I want to do in this pr

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

@refack
refack dismissed their stale reviewAugust 19, 2017 02:38

Comments have been discussed.

@mscdexmscdex added the libuv Issues and PRs related to the libuv dependency or the uv binding. label Aug 19, 2017
@jasnell

Copy link
Copy Markdown
MemberAuthor

Comment threadlib/util.js
var name = errname(err);
if (typeof err !== 'number' || err >= 0 || !Number.isSafeInteger(err)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'err',
'negative number');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to add the actual argument?

@jasnelljasnellAug 20, 2017

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not particularly useful. The ERR_INVALID_ARG_TYPE actual prints out the type of the actual, not the value itself. So if I passed in _errnoException(1), the error message would be The "err" argument must be of type negative number. Received type number, which is just odd. We could turn this into a RangeError if the err is a number and isn't negative, but that adds a second if-check that is not strictly necessary for what is supposed to be an internal API.

@jasnell

Copy link
Copy Markdown
MemberAuthor

CI run was a bit too red, although all of the failures were unrelated. Running again just to be safe: https://ci.nodejs.org/job/node-test-pull-request/9778/

jasnell added a commit that referenced this pull request Aug 23, 2017
* ensure that UV_... props are constants
* improve error message ... the error message when passing
in `err >= 0` to `util._errnoException()` was less than
useful.
* refine uses of process.binding('uv')
PR-URL: #14933
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@jasnell

Copy link
Copy Markdown
MemberAuthor

Landed in 58831b2

@jasnelljasnell closed this Aug 23, 2017
addaleax pushed a commit to addaleax/ayo that referenced this pull request Aug 25, 2017
* ensure that UV_... props are constants
* improve error message ... the error message when passing
in `err >= 0` to `util._errnoException()` was less than
useful.
* refine uses of process.binding('uv')
PR-URL: nodejs/node#14933
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
addaleax pushed a commit to ayojs/ayo that referenced this pull request Aug 28, 2017
* ensure that UV_... props are constants
* improve error message ... the error message when passing
in `err >= 0` to `util._errnoException()` was less than
useful.
* refine uses of process.binding('uv')
PR-URL: nodejs/node#14933
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / srcIssues and PRs related to general changes in the lib or src directory.libuvIssues and PRs related to the libuv dependency or the uv binding.semver-majorPRs that contain breaking changes and should be released in the next major version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@jasnell@addaleax@refack@lpinca@cjihrig@mscdex@nodejs-github-bot