Browse Source

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.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
5f30377bbc
  1. 58
      lib/module.js
  2. 2
      src/node.cc
  3. 4
      test/common.js
  4. 2
      test/pummel/test-timers.js

58
lib/module.js

@ -2,6 +2,13 @@
// Module // 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 internalModuleCache = {};
var extensionCache = {}; var extensionCache = {};
@ -44,7 +51,9 @@ function loadNative (id) {
m.loaded = true; m.loaded = true;
return m; return m;
} }
exports.requireNative = requireNative; exports.requireNative = requireNative;
function requireNative (id) { function requireNative (id) {
if (internalModuleCache[id]) return internalModuleCache[id].exports; if (internalModuleCache[id]) return internalModuleCache[id].exports;
if (!natives[id]) throw new Error('No such native module ' + id); 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.main = process.mainModule;
require.registerExtension = registerExtension; 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) { } else {
// create wrapper function debug('load root module');
var wrapper = "(function (exports, require, module, __filename, __dirname) { " // root module
+ content global.require = require;
+ "\n});"; 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 { } 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;
}
} }
}; };

2
src/node.cc

@ -1721,6 +1721,8 @@ static void PrintHelp() {
" prefixed to the module search path,\n" " prefixed to the module search path,\n"
" require.paths.\n" " require.paths.\n"
"NODE_DEBUG Print additional debugging output.\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" "\n"
"Documentation can be found at http://nodejs.org/api.html" "Documentation can be found at http://nodejs.org/api.html"
" or with 'man node'\n"); " or with 'man node'\n");

4
test/common.js

@ -7,6 +7,8 @@ exports.fixturesDir = path.join(exports.testDir, "fixtures");
exports.libDir = path.join(exports.testDir, "../lib"); exports.libDir = path.join(exports.testDir, "../lib");
exports.PORT = 12346; exports.PORT = 12346;
exports.assert = require('assert');
var sys = require("sys"); var sys = require("sys");
for (var i in sys) exports[i] = sys[i]; for (var i in sys) exports[i] = sys[i];
exports.assert = require('assert'); for (var i in exports) global[i] = exports[i];

2
test/pummel/test-timers.js

@ -1,5 +1,7 @@
require("../common"); require("../common");
assert = require('assert');
var WINDOW = 200; // why is does this need to be so big? var WINDOW = 200; // why is does this need to be so big?
var interval_count = 0; var interval_count = 0;

Loading…
Cancel
Save