diff --git a/lib/module.js b/lib/module.js index 11ef3c50d7..6ff17e9af3 100644 --- a/lib/module.js +++ b/lib/module.js @@ -2,6 +2,13 @@ // Module + +// Set the environ variable NODE_MODULE_CONTEXT=1 to make node load all +// modules in thier own context. +var contextLoad = false; +if (parseInt(process.env["NODE_MODULE_CONTEXTS"]) > 0) contextLoad = true; +var Script; + var internalModuleCache = {}; var extensionCache = {}; @@ -44,7 +51,9 @@ function loadNative (id) { m.loaded = true; return m; } + exports.requireNative = requireNative; + function requireNative (id) { if (internalModuleCache[id]) return internalModuleCache[id].exports; if (!natives[id]) throw new Error('No such native module ' + id); @@ -374,18 +383,49 @@ Module.prototype._compile = function (content, filename) { require.main = process.mainModule; require.registerExtension = registerExtension; + var dirname = path.dirname(filename); + + if (contextLoad) { + if (!Script) Script = Script = process.binding('evals').Script; + + if (self.id !== ".") { + debug('load submodule'); + // not root module + var sandbox = {}; + for (var k in global) { + sandbox[k] = global[k]; + } + sandbox.require = require; + sandbox.exports = self.exports; + sandbox.__filename = filename; + sandbox.__dirname = dirname; + sandbox.module = self; + + Script.runInNewContext(content, sandbox, filename); - if ('string' === typeof content) { - // create wrapper function - var wrapper = "(function (exports, require, module, __filename, __dirname) { " - + content - + "\n});"; + } else { + debug('load root module'); + // root module + global.require = require; + global.exports = self.exports; + global.__filename = filename; + global.__dirname = dirname; + global.module = self; + Script.runInThisContext(content, filename); + } - var compiledWrapper = process.compile(wrapper, filename); - var dirName = path.dirname(filename); - compiledWrapper.apply(self.exports, [self.exports, require, self, filename, dirName]); } else { - self.exports = content; + if ('string' === typeof content) { + // create wrapper function + var wrapper = "(function (exports, require, module, __filename, __dirname) { " + + content + + "\n});"; + + var compiledWrapper = process.compile(wrapper, filename); + compiledWrapper.apply(self.exports, [self.exports, require, self, filename, dirname]); + } else { + self.exports = content; + } } }; diff --git a/src/node.cc b/src/node.cc index cd62b90224..6915c181a3 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1721,6 +1721,8 @@ static void PrintHelp() { " prefixed to the module search path,\n" " require.paths.\n" "NODE_DEBUG Print additional debugging output.\n" + "NODE_MODULE_CONTEXTS Set to 1 to load modules in their own\n" + " global contexts.\n" "\n" "Documentation can be found at http://nodejs.org/api.html" " or with 'man node'\n"); diff --git a/test/common.js b/test/common.js index 0f8c1be52e..9593afdce3 100644 --- a/test/common.js +++ b/test/common.js @@ -7,6 +7,8 @@ exports.fixturesDir = path.join(exports.testDir, "fixtures"); exports.libDir = path.join(exports.testDir, "../lib"); exports.PORT = 12346; +exports.assert = require('assert'); + var sys = require("sys"); for (var i in sys) exports[i] = sys[i]; -exports.assert = require('assert'); +for (var i in exports) global[i] = exports[i]; diff --git a/test/pummel/test-timers.js b/test/pummel/test-timers.js index 0aa470ca40..aa4e9892e4 100644 --- a/test/pummel/test-timers.js +++ b/test/pummel/test-timers.js @@ -1,5 +1,7 @@ require("../common"); +assert = require('assert'); + var WINDOW = 200; // why is does this need to be so big? var interval_count = 0;