joyent/node — pull request #5651 by kitcambridge (2013-06-09 19:56:41+00:00)
Title: module: Add `Module#initializeInContext`.

Occasionally—as in the case of [CoffeeScript](https://github.com/jashkenas/coffee-script/blob/master/src/coffee-script.coffee#L126-135) and [`plz`](https://github.com/robey/plz/blob/c6bd36850f740aebc710b215ea0d4616c05562de/src/plz/context.coffee#L71-84), for instance—it is helpful to programmatically create and initialize a module object. Both projects currently rely on a mixture of internal `Module` methods and manually copying over `require` properties, taking care to avoid the `require.paths` poison pill. Given that the module system already provides the necessary logic, I feel there's value in providing a "public" API for doing so (the `module` module is undocumented, so I don't know the extent to which it is meant to be used outside of Node core).

This pull request does not add or otherwise change existing module semantics; it simply extracts the context initialization logic from `Module#_compile` into a new method: `Module#initializeInContext`. It also adds a pseudo-private `Module#_createRequire` method, which is used by both `_compile` and `initializeInContext`. This new method provides a simple primitive for initializing—but not evaluating—a module, with all the requisite free variables and properties available to loaded modules.