Load modules in individual contexts
Add NODE_MODULE_CONTEXTS env var Only one test was modified to check that this works. NEED to go through all tests and modify them so that NODE_MODULE_CONTEXTS=1 make test passes.
This commit is contained in:
parent
4e50197e53
commit
5f30377bbc
@ -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 ('string' === typeof content) {
|
||||
// create wrapper function
|
||||
var wrapper = "(function (exports, require, module, __filename, __dirname) { "
|
||||
+ content
|
||||
+ "\n});";
|
||||
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);
|
||||
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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");
|
||||
|
@ -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];
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user