Skip to content

src: deprecate root and GLOBAL variables - #1838

Closed
JacksonTian wants to merge 1 commit into
nodejs:masterfrom
JacksonTian:remove_root
Closed

src: deprecate root and GLOBAL variables#1838
JacksonTian wants to merge 1 commit into
nodejs:masterfrom
JacksonTian:remove_root

Conversation

@JacksonTian

Copy link
Copy Markdown
Contributor

The root and GLOBAL never be documented.

@silverwindsilverwind added the semver-major PRs that contain breaking changes and should be released in the next major version. label May 29, 2015
@silverwind

Copy link
Copy Markdown
Contributor

I think these should be pretty low risk to remove. If someone has an idea on how to print a deprecation warning when accessing the global or its properties, we could also go that route.

@Fishrock123

Copy link
Copy Markdown
Contributor

If someone has an idea on how to print a deprecation warning when accessing the global or its properties

Use a Getter?

@brendanashworth

Copy link
Copy Markdown
Contributor

There shouldn't be much code relying on this but there may be some so we do need a deprecation message.

edit: btw the commit log should be src: not lib:

@silverwind

Copy link
Copy Markdown
Contributor

Use a Getter?

Won't work for usage like GLOBAL.setTimeout() is suppose.

@Fishrock123

Copy link
Copy Markdown
Contributor

@silverwind isn't that still accessing GLOBAL from global?

@silverwind

Copy link
Copy Markdown
Contributor

@Fishrock123 right, something like this should work:

Object.defineProperty(global,'GLOBAL',{get: function(){util.deprecate(function(){},"'GLOBAL' is deprecated, use 'global'");returnglobal;}});

@JacksonTian could you try incorporating such a print to both globals instead of removing them?

@silverwind

Copy link
Copy Markdown
Contributor

Better one:

Object.defineProperty(global,'GLOBAL',{get: util.deprecate(function(){returnglobal;},"'GLOBAL' is deprecated, use 'global'")});

@targos

Copy link
Copy Markdown
Member

maybe also make the property writable ?

@silverwind

Copy link
Copy Markdown
Contributor

maybe also make the property writable ?

Right, this is getting interesting now. Below code should only print once on first read/write access:

varg=r=global;varaccessGlobal=util.deprecate(function(val){if(val)g=val;returng;},"'GLOBAL' is deprecated, use 'global'");varaccessRoot=util.deprecate(function(val){if(val)r=val;returnr;},"'root' is deprecated, use 'global'");Object.defineProperty(global,'GLOBAL',{get: accessGlobal,set: accessGlobal});Object.defineProperty(global,'root',{get: accessRoot,set: accessRoot});

@silverwind

Copy link
Copy Markdown
Contributor

One more revision:

constholders={GLOBAL: global,root: global};constaccessGlobal=util.deprecate(function(val){if(val)holders.GLOBAL=val;returnholders.GLOBAL;},"'GLOBAL' is deprecated, use 'global'");constaccessRoot=util.deprecate(function(val){if(val)holders.root=val;returnholders.root;},"'root' is deprecated, use 'global'");Object.defineProperty(global,'GLOBAL',{get: accessGlobal,set: accessGlobal});Object.defineProperty(global,'root',{get: accessRoot,set: accessRoot});

@JacksonTian feel free to use something like that. Otherwise, I could do a PR that succeeds this one :)

@JacksonTian
JacksonTianforce-pushed the remove_root branch 2 times, most recently from acbdbcf to 233d4f8CompareMay 30, 2015 08:20
@JacksonTian

Copy link
Copy Markdown
ContributorAuthor

@silverwind The PR is updated, please review it again.

@brendanashworthbrendanashworth added 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 May 30, 2015
@bnoordhuis

Copy link
Copy Markdown
Member

GLOBAL === global and root === global so you can just return this inside the setter, no need to create a separate object.

Apropos assignment, I'd replace the getter/setter inside the setter, e.g.:

Object.defineProperty(global,'GLOBAL',{configurable: true,// but not enumerableget: util.deprecate(function(){returnthis;},`'GLOBAL' is deprecated, use 'global'`),set: function(value){constdesc={configurable: true,enumerable: true,value: value};Object.defineProperty(this,'GLOBAL',desc);},});

Ditto for root. You can even go one step further and redefine the property inside the getter as well if you change it to:

get: util.deprecate(function(){constdesc={configurable: true,enumerable: true,value: this};Object.defineProperty(this,'GLOBAL',desc);returnthis;},`'GLOBAL' is deprecated, use 'global'`)

That prints a warning on the first access and then gets out of the way. It won't regress code that uses GLOBAL or root in performance-sensitive code.

@silverwind

Copy link
Copy Markdown
Contributor

+1 for using this.

@bnoordhuis I have a bit of a hard time understanding why running defineProperty each access can be faster, but you're probably right :)

@silverwindsilverwind changed the title lib: remove variables root and GLOBALsrc: deprecate root and GLOBAL variablesMay 30, 2015
@bnoordhuis

Copy link
Copy Markdown
Member

@silverwind The getter calls defineProperty on the first access. After that the property is no longer a getter, just a regular property.

@silverwind

Copy link
Copy Markdown
Contributor

@bnoordhuis oh right, thats quite genius!

@targos

Copy link
Copy Markdown
Member

@bnoordhuis 👍 very clever

The setter can be simplified using shorthand property:

set(value){constdesc={configurable: true,enumerable: true,value: value};Object.defineProperty(this,'GLOBAL',desc);}

@targos

Copy link
Copy Markdown
Member

and maybe it should be writable: true as well ?

@bnoordhuis

Copy link
Copy Markdown
Member

Good point, it should be writable when setting the value (but not when setting the getter/setter pair.)

@JacksonTian

Copy link
Copy Markdown
ContributorAuthor

Hi everyone, I updated the PR by your comments, thanks.

Comment threadsrc/node.js Outdated

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.

Should be Object.defineProperty(this, name, desc);

Comment threadsrc/node.js Outdated

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.

Tiny suggestion: the code might look a little better if you pulled the NativeModule.require('util') out into a variable. Feel free to ignore if you want.

@cjihrig

Copy link
Copy Markdown
Contributor

LGTM with the same suggestions. And +1 to this being semver-major.

@ChALkeR

Copy link
Copy Markdown
Member

@jasnell

Copy link
Copy Markdown
Member

LGTM as semver-major

@silverwindsilverwind removed the stalled Issues and PRs that are stalled. label Feb 4, 2016
@silverwind

Copy link
Copy Markdown
Contributor

ping @JacksonTian

@brendanashworth

Copy link
Copy Markdown
Contributor

@bnoordhuis back then, root and GLOBAL were just outright removed, but when they were deprecated I changed it to semver-minor. +1 for major though.

The `root` and `GLOBAL` never be documented.
@JacksonTian

Copy link
Copy Markdown
ContributorAuthor

Updated. Thanks all.

@thefourtheye

Copy link
Copy Markdown
Contributor

LGTM

@ChALkeR

Copy link
Copy Markdown
Member

LGTM (if passes CI)

@silverwind

Copy link
Copy Markdown
Contributor

bnoordhuis pushed a commit that referenced this pull request Feb 10, 2016
The `root` and `GLOBAL` were never documented.
PR-URL: #1838
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
@bnoordhuis

Copy link
Copy Markdown
Member

Unrelated lint error addressed by #5161. Landed in 4e46931, thanks @JacksonTian!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

12 participants

@JacksonTian@silverwind@Fishrock123@brendanashworth@targos@bnoordhuis@evanlucas@chrisdickinson@ChALkeR@jasnell@cjihrig@thefourtheye