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: refactor structure of the os module #12654

Closed
wants to merge 2 commits into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Apr 25, 2017

Refactoring the structure of the os module to use the more efficient module.exports = {} pattern.

Also adds simple Symbol.toPrimitive support for each of the os methods that return simple primitives.

Lastly, removes extraneous console output from the test-os.js

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)

os

@jasnell jasnell added dont-land-on-v4.x semver-minor PRs that contain new features and should be released in the next minor version. labels Apr 25, 2017
@nodejs-github-bot nodejs-github-bot added the os Issues and PRs related to the os subsystem. label Apr 25, 2017
@jasnell jasnell changed the title Refactor os os: refactor structure of the os module Apr 25, 2017
@mscdex
Copy link
Contributor

mscdex commented Apr 25, 2017

What is the reason for the Symbol.toPrimitive changes?

@jasnell
Copy link
Member Author

jasnell commented Apr 25, 2017

Symbol.toPrimitive is an extremely simple convenience. For instance, os.endianness() always returns a constant string value. With the Symbol.toPrimitive, we can do:

// No parens at the end
`${os.endianess}`

instead of

`${os.endianness()}`

Likewise with the other functions that always return primitives.

It's extremely minor and quite cheap for us to support.

Another, example...

 `${os.platform}-${os.release}-${os.arch}-${os.hostname}`

Instead of

 `${os.platform()}-${os.release()}-${os.arch()}-${os.hostname()}`

/* eslint-disable no-restricted-properties */
[

[assert.equal, `${os.freemem}`, os.freemem()],
Copy link
Member

@Trott Trott Apr 25, 2017

Choose a reason for hiding this comment

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

Nit: Maybe better as

assert.strictEqual, `${os.freemem}`, `${os.freemem()}`]

Matter of style preference, I suppose.

Copy link
Member Author

Choose a reason for hiding this comment

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

This was intentional, recognizing that the differences in the values should still be equal.

Copy link
Member

Choose a reason for hiding this comment

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

This was intentional, recognizing that the differences in the values should still be equal.

You might be misunderstanding my suggestion. I know it's intentional. Explicitly converting the number to a string and using strictEqual() makes it more clear that it's intentional, at least in my opinion. OK by me, though, if you prefer the way it is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I know what you meant, just wasn't being very clear. I'm using assert.equal() intentionally because the return value of the methods themselves are numbers and I want to ensure that the conversion to string after the Symbol.toPrimitive call returns an equal (but not strict equal) result. Either way I think gets the job done but to answer the question: yes, I prefer it this way :-)

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

Oops, I rebased while the CI was checking things out and goofed it up.
New New CI: https://ci.nodejs.org/job/node-test-pull-request/7717/

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

Once more with gusto: https://ci.nodejs.org/job/node-test-pull-request/7718/

@jasnell
Copy link
Member Author

jasnell commented Apr 27, 2017

CI is green

@jasnell
Copy link
Member Author

jasnell commented Apr 29, 2017

ping @nodejs/collaborators

Copy link
Member

@watilde watilde left a comment

Choose a reason for hiding this comment

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

Looks similar to #12717. LGTM.

Copy link
Contributor

@aqrln aqrln left a comment

Choose a reason for hiding this comment

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

LGTM

@aqrln
Copy link
Contributor

aqrln commented Apr 29, 2017

Is it really semver-minor? Even though I don't think someone really used these functions in template literals as values themselves (as it doesn't make much sense), strictly speaking, the behavior has changed.

Before:

> `${os.endianness}`
'function () { return \'LE\'; }'

After:

> `${os.endianness}`
'LE'

It might be better to make this a major change just to be safe and strictly adhering to semver, especially given the fact that v8.0 is coming soon. It might be also worth to split this into two commits (just refactoring and Symbol.toPrimitive functionality) and have only one of them landed on v7.x.

However, a huge +1 to the change itself, about a week ago I really wished these functions were getters when I used them in template literals, while this PR solves it even more elegantly :)

@addaleax
Copy link
Member

@jasnell
Copy link
Member Author

jasnell commented Apr 29, 2017

I'm good with defensively labeling this as semver-major.

@jasnell jasnell added semver-major PRs that contain breaking changes and should be released in the next major version. and removed semver-minor PRs that contain new features and should be released in the next minor version. labels Apr 29, 2017
[assert.strictEqual, `${os.type}`, os.type()],
[assert.strictEqual, `${os.endianness}`, os.endianness()]
].forEach((set) => {
set[0](set[1], set[2], `${set[1]}, ${set[2]}`);
Copy link
Member

Choose a reason for hiding this comment

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

I’m not actually sure whether the extra error message argument here is helpful, and maybe I would just inline assert.strictEqual here instead of passing it as part of set. (Just a suggestion, feel free to ignore me if you disagree)

Copy link
Member Author

Choose a reason for hiding this comment

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

Now that they're all strictEquals.. +1 :-)

jasnell added 2 commits April 29, 2017 12:32
Refactor the structure of the os module to use more efficient
module.exports = {} pattern.

Add Symbol.toPrimitive support to os methods that return simple
primitives. This is a minor tweak that makes using these slightly
more friendly when doing things like:

```js
var m = `${os.tmpdir}/foo`
```
@jasnell
Copy link
Member Author

jasnell commented Apr 29, 2017

jasnell added a commit that referenced this pull request May 1, 2017
Refactor the structure of the os module to use more efficient
module.exports = {} pattern.

Add Symbol.toPrimitive support to os methods that return simple
primitives. This is a minor tweak that makes using these slightly
more friendly when doing things like:

```js
var m = `${os.tmpdir}/foo`
```

PR-URL: #12654
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
jasnell added a commit that referenced this pull request May 1, 2017
PR-URL: #12654
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
@jasnell
Copy link
Member Author

jasnell commented May 1, 2017

Landed in 473572e and 23fc082

@jasnell jasnell closed this May 1, 2017
anchnk pushed a commit to anchnk/node that referenced this pull request May 6, 2017
Refactor the structure of the os module to use more efficient
module.exports = {} pattern.

Add Symbol.toPrimitive support to os methods that return simple
primitives. This is a minor tweak that makes using these slightly
more friendly when doing things like:

```js
var m = `${os.tmpdir}/foo`
```

PR-URL: nodejs#12654
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
anchnk pushed a commit to anchnk/node that referenced this pull request May 6, 2017
PR-URL: nodejs#12654
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
@jasnell jasnell mentioned this pull request May 11, 2017
@Fishrock123
Copy link
Contributor

Fishrock123 commented Jun 1, 2017

As far as I can tell, adding Symbol.toPrimitive to bare Functions not a incompatible API change.

I'm bringing this up because I don't know how to possibly word this as a breaking change for the 8.0.0 breaking changes doc.

Reading function bodies in any real, dependant way is already not supported. (Obviously. This is like process.binding('natives'), and anyone possibly depending on this knows function bodies can and will change at any time.)

Adding the symbol does not alter any existing behavior of the actual documented functions, in any way (including typeof), or the module overall.

The behavior before, for coercion to "primitive", is effectively a common "misuse" error that under normal circumstances would be akin to any of our TypeErrors for invalid arguments. That is, you forgot to add the parens to call the function when you meant to.

This either: adds behavior that was "missing", or "fixes" previously "broken" behavior.

@jasnell, @addaleax, @aqrln Thoughts? Would I be ok to remove the label? (We can add dont-land-on- if ya'll want.)

@addaleax
Copy link
Member

addaleax commented Jun 1, 2017

Would I be ok to remove the label?

I think so, yes. I don’t think this kind of change makes sense for LTS, so I’m adding the dont-land labels.

@addaleax addaleax added dont-land-on-v7.x semver-minor PRs that contain new features and should be released in the next minor version. and removed semver-major PRs that contain breaking changes and should be released in the next major version. labels Jun 1, 2017
@aqrln
Copy link
Contributor

aqrln commented Jun 2, 2017

@Fishrock123 yes, I'm fine with it. Thanks for bringing this up.

@jasnell
Copy link
Member Author

jasnell commented Jun 2, 2017

In this particular case, dropping down to semver-minor is ok. We need to be careful about it in general with these kinds of changes, tho. Adding the don't land labels is ++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
os Issues and PRs related to the os subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants