Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Update how REPLServer uses contexts #851

Closed
wants to merge 1 commit into from

Conversation

weaver
Copy link

@weaver weaver commented Mar 29, 2011

Hi, I was trying to subclass REPLServer to not share a context across instances. It turns out there's no way to do this conveniently because the current REPL implementation refers to a global context variable in several places. This patch does the following:

  • Always use this.context or self.context instead of context.
  • Move resetContext to REPLServer.createContext.
  • Add REPLServer.resetContext, memoize context here.
  • Memoize exports.repl in start instead of REPLServer constructor.

My use case is a local "administration" REPL for a web service. If several people are connected to the REPL, I don't want their context shared because they may clobber each others bindings by defining variables with the same name or using ".clear".

With this patch, the following works as expected (each repl has a unique local context):

util.inherits(REPL, repl.REPLServer);
function REPL(prompt, stream) {
  repl.REPLServer.call(this, prompt, stream);
}

REPL.prototype.resetContext = function() {
  this.context = this.createContext();
};

* Always use `this.context` or `self.context`.
* Move `resetContext` to `REPLServer.createContext`.
* Add `REPLServer.resetContext`, memoize `context` here.
* Memoize `exports.repl` in `start`.
@ry
Copy link

ry commented Apr 11, 2011

Thanks Ben. Looks good. landed in master

@ry
Copy link

ry commented Apr 12, 2011

landed in d63a551

@ry ry closed this Apr 12, 2011
coolaj86 pushed a commit that referenced this pull request Apr 15, 2011
* Always use `this.context` or `self.context`.
* Move `resetContext` to `REPLServer.createContext`.
* Add `REPLServer.resetContext`, memoize `context` here.
* Memoize `exports.repl` in `start`.

Closes GH-851.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants