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

repl: Enable tab completion for global properties when useGlobal is false #7369

Closed
wants to merge 5 commits into from
Closed

repl: Enable tab completion for global properties when useGlobal is false #7369

wants to merge 5 commits into from

Conversation

lance
Copy link
Member

@lance lance commented Jun 22, 2016

Checklist
  • make -j4 test (UNIX) or vcbuild test nosign (Windows) passes
  • a test and/or benchmark is included
  • the commit message follows commit guidelines
Affected core subsystem(s)

repl

Description of change

When useGlobal is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

context[prop] = global[prop]

Use Object.defineProperty and the property descriptor found on
global for the new property in context.

Also addresses a previously unnoticed issue where console is writable
when useGlobal is false.

Ref: #7353

@nodejs-github-bot nodejs-github-bot added the repl Issues and PRs related to the REPL subsystem. label Jun 22, 2016
@Fishrock123
Copy link
Contributor

idea seems sound to me

for (var i in global) context[i] = global[i];
context.console = new Console(this.outputStream);
context.global = context;
context.global.global = context;
Copy link
Member

Choose a reason for hiding this comment

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

What does global in the inner context refer to now? I think it should still be context, right?

(The second line here also seems to have been superfluous?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Both lines should stay the same

Copy link
Member Author

Choose a reason for hiding this comment

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

@addaleax - yes, this was an oversight...

@princejwesley
Copy link
Contributor

@lance This solution has some side effects!

node 🙈  git:(master)  cat ~/Desktop/test.js
let repl = require('repl')
repl.start({useGlobal: false})
node 🙈  git:(master)  ./node !$
./node ~/Desktop/test.js
> [] instanceof Array
false
> /vm/ instanceof RegExp
false
>

@lance
Copy link
Member Author

lance commented Jun 23, 2016

@princejwesley hmm - this is strange. I will take a look at this.

@lance
Copy link
Member Author

lance commented Jun 23, 2016

@princejwesley this issue should be addressed with my latest push.
@addaleax your comments are addressed as well.

if (name === 'console' || name === 'global') return false;
return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined;
}).forEach((name) => {
Object.defineProperty(context, name,
Copy link
Contributor

Choose a reason for hiding this comment

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

@lance instanceof test for this filtered list may fail!. e.g Int16Array. Can you confirm that? Adding those missing properties to GLOBAL_OBJECT_PROPERTIES will solve this issue.

Copy link
Member Author

@lance lance Jun 23, 2016

Choose a reason for hiding this comment

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

I couldn't figure out the best way to test this programmatically, but the CLI behavior tests manually as expected.

~/s/node git:repl-useglobal-false-tab-completion ❯❯❯ ./node test-use-global-auto-complete.js
> const x = new Int16Array()
undefined
> x instanceof Int16Array
true
>

I think this is the case because there is no builtin shorthand for Int16Array and the like. For builtin objects that have a shorthand representation such as [] or /\w+/ that representation is an instanceofthe builtin language object no matter what. But there is no builtin shorthand for Int16Array.

@addaleax
Copy link
Member

nice work, LGTM with two questions

When `useGlobal` is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

    context[prop] = global[prop]

Use `Object.defineProperty` and the property descriptor found on
`global` for the new property in `context`.

Also addresses a previously unnoticed issue where `console` is writable
when `useGlobal` is false.

Ref: #7353
When `useGlobal` is false, all of the properties on `global` are copied
to the new context (excluding `console` and `global`). Since
`Object.getOwnPropertyNames()` returns all properties, not just
enumerable ones, builtin properties on `global` are returned. We need to
filter those out.
This existed in the original `repl.js` code, but it's not clear what the
purpose is. It appears to be redundant.
@@ -39,6 +39,16 @@ const debug = util.debuglog('repl');
const parentModule = module;
const replMap = new WeakMap();

const GLOBAL_OBJECT_PROPERTIES = ['NaN', 'Infinity', 'undefined',
Copy link
Member

Choose a reason for hiding this comment

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

I really wish we had a better way of tracking these... ah well.

@jasnell
Copy link
Member

jasnell commented Jun 27, 2016

LGTM with a couple of nits

If the binary has been built with `./configure --without-intl` then the
`Intl` builtin type will not be available in a repl runtime. Check for
this in the test.
@lance
Copy link
Member Author

lance commented Jun 27, 2016

CI: https://ci.nodejs.org/job/node-test-pull-request/3094/

@addaleax have I answered your questions well enough?
@jasnell nits addressed

@addaleax
Copy link
Member

CI is green & this still LGTM

@jasnell
Copy link
Member

jasnell commented Jun 27, 2016

LGTM

lance added a commit that referenced this pull request Jun 27, 2016
When `useGlobal` is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

    context[prop] = global[prop]

Use `Object.defineProperty` and the property descriptor found on
`global` for the new property in `context`.

Also addresses a previously unnoticed issue where `console` is writable
when `useGlobal` is false.

If the binary has been built with `./configure --without-intl` then the
`Intl` builtin type will not be available in a repl runtime. Check for
this in the test.

Fixes: #7353
PR-URL: #7369
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@lance
Copy link
Member Author

lance commented Jun 27, 2016

Landed in: c0e48bf

@lance lance closed this Jun 27, 2016
Fishrock123 pushed a commit that referenced this pull request Jul 5, 2016
When `useGlobal` is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

    context[prop] = global[prop]

Use `Object.defineProperty` and the property descriptor found on
`global` for the new property in `context`.

Also addresses a previously unnoticed issue where `console` is writable
when `useGlobal` is false.

If the binary has been built with `./configure --without-intl` then the
`Intl` builtin type will not be available in a repl runtime. Check for
this in the test.

Fixes: #7353
PR-URL: #7369
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@Fishrock123 Fishrock123 mentioned this pull request Jul 5, 2016
@MylesBorins
Copy link
Contributor

@lance @addaleax @nodejs/lts lts?

@addaleax
Copy link
Member

Yes, but I think I’d like to give it a bit more time in v6? @lance feel free to overrule me here. :)

@cjihrig
Copy link
Contributor

cjihrig commented Jul 12, 2016

I agree, this should be in a couple v6 releases first.

@lance
Copy link
Member Author

lance commented Jul 12, 2016

@addaleax @cjihrig wfm

MylesBorins pushed a commit that referenced this pull request Oct 10, 2016
When `useGlobal` is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

    context[prop] = global[prop]

Use `Object.defineProperty` and the property descriptor found on
`global` for the new property in `context`.

Also addresses a previously unnoticed issue where `console` is writable
when `useGlobal` is false.

If the binary has been built with `./configure --without-intl` then the
`Intl` builtin type will not be available in a repl runtime. Check for
this in the test.

Fixes: #7353
PR-URL: #7369
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this pull request Oct 10, 2016
When `useGlobal` is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

    context[prop] = global[prop]

Use `Object.defineProperty` and the property descriptor found on
`global` for the new property in `context`.

Also addresses a previously unnoticed issue where `console` is writable
when `useGlobal` is false.

If the binary has been built with `./configure --without-intl` then the
`Intl` builtin type will not be available in a repl runtime. Check for
this in the test.

Fixes: #7353
PR-URL: #7369
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
rvagg pushed a commit that referenced this pull request Oct 18, 2016
When `useGlobal` is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

    context[prop] = global[prop]

Use `Object.defineProperty` and the property descriptor found on
`global` for the new property in `context`.

Also addresses a previously unnoticed issue where `console` is writable
when `useGlobal` is false.

If the binary has been built with `./configure --without-intl` then the
`Intl` builtin type will not be available in a repl runtime. Check for
this in the test.

Fixes: #7353
PR-URL: #7369
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this pull request Oct 26, 2016
When `useGlobal` is false, tab completion in the repl does not enumerate
global properties. Instead of just setting these properties blindly on
the global context, e.g.

    context[prop] = global[prop]

Use `Object.defineProperty` and the property descriptor found on
`global` for the new property in `context`.

Also addresses a previously unnoticed issue where `console` is writable
when `useGlobal` is false.

If the binary has been built with `./configure --without-intl` then the
`Intl` builtin type will not be available in a repl runtime. Check for
this in the test.

Fixes: #7353
PR-URL: #7369
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@MylesBorins MylesBorins mentioned this pull request Oct 26, 2016
MylesBorins added a commit that referenced this pull request Oct 26, 2016
This LTS release comes with 219 commits. This includes 80 commits that
are docs related, 58 commits that are test related, 20 commits that
are build / tool related, and 9 commits that are updates to
dependencies.

Notable Changes

* build:
  - It is now possible to build the documentation from the release
    tarball (Anna Henningsen)
    #8413
* buffer:
  - Buffer will no longer incorrectly return a zero filled buffer when
    an encoding is passed (Teddy Katz)
    #9238
* deps:
  - upgrade npm in LTS to 2.15.11 (Kat Marchán)
    #8928
* repl:
  - Enable tab completion for global properties (Lance Ball)
    #7369
* url:
  - `url.format()` will now encode all `#` in `search` (Ilkka Myller)
    #8072

PR-URL: #9298
MylesBorins added a commit that referenced this pull request Oct 26, 2016
This LTS release comes with 219 commits. This includes 80 commits that
are docs related, 58 commits that are test related, 20 commits that
are build / tool related, and 9 commits that are updates to
dependencies.

Notable Changes

* build:
  - It is now possible to build the documentation from the release
    tarball (Anna Henningsen)
    #8413
* buffer:
  - Buffer.alloc() will no longer incorrectly return a zero filled
    buffer when an encoding is passed (Teddy Katz)
    #9238
* deps:
  - upgrade npm in LTS to 2.15.11 (Kat Marchán)
    #8928
* repl:
  - Enable tab completion for global properties (Lance Ball)
    #7369
* url:
  - `url.format()` will now encode all `#` in `search` (Ilkka Myller)
    #8072

PR-URL: #9298
MylesBorins added a commit that referenced this pull request Nov 7, 2016
This LTS release comes with 219 commits. This includes 80 commits that
are docs related, 58 commits that are test related, 20 commits that
are build / tool related, and 9 commits that are updates to
dependencies.

Notable Changes

* build:
  - It is now possible to build the documentation from the release
    tarball (Anna Henningsen)
    #8413
* buffer:
  - Buffer.alloc() will no longer incorrectly return a zero filled
    buffer when an encoding is passed (Teddy Katz)
    #9238
* deps:
  - upgrade npm in LTS to 2.15.11 (Kat Marchán)
    #8928
* repl:
  - Enable tab completion for global properties (Lance Ball)
    #7369
* url:
  - `url.format()` will now encode all `#` in `search` (Ilkka Myller)
    #8072

PR-URL: #9298
MylesBorins added a commit that referenced this pull request Nov 8, 2016
This LTS release comes with 219 commits. This includes 80 commits that
are docs related, 58 commits that are test related, 20 commits that
are build / tool related, and 9 commits that are updates to
dependencies.

Notable Changes

* build:
  - It is now possible to build the documentation from the release
    tarball (Anna Henningsen)
    #8413
* buffer:
  - Buffer.alloc() will no longer incorrectly return a zero filled
    buffer when an encoding is passed (Teddy Katz)
    #9238
* deps:
  - upgrade npm in LTS to 2.15.11 (Kat Marchán)
    #8928
* repl:
  - Enable tab completion for global properties (Lance Ball)
    #7369
* url:
  - `url.format()` will now encode all `#` in `search` (Ilkka Myller)
    #8072

PR-URL: #9298
imyller added a commit to imyller/meta-nodejs that referenced this pull request Nov 11, 2016
    This LTS release comes with 219 commits. This includes 80 commits that
    are docs related, 58 commits that are test related, 20 commits that
    are build / tool related, and 9 commits that are updates to
    dependencies.

    Notable Changes

    * build:
      - It is now possible to build the documentation from the release
        tarball (Anna Henningsen)
        nodejs/node#8413
    * buffer:
      - Buffer.alloc() will no longer incorrectly return a zero filled
        buffer when an encoding is passed (Teddy Katz)
        nodejs/node#9238
    * deps:
      - upgrade npm in LTS to 2.15.11 (Kat Marchan)
        nodejs/node#8928
    * repl:
      - Enable tab completion for global properties (Lance Ball)
        nodejs/node#7369
    * url:
      - `url.format()` will now encode all `#` in `search` (Ilkka Myller)
        nodejs/node#8072

    PR-URL: nodejs/node#9298

Signed-off-by: Ilkka Myller <ilkka.myller@nodefield.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants