Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Geisendörfer <felix@debuggable.com>2011-01-16 14:33:11 +0300
committerRyan Dahl <ry@tinyclouds.org>2011-01-17 01:52:55 +0300
commit0263f01475011691f6fbfbf32dceb28b56b69232 (patch)
tree2813e66f7c9b7db015593d5a1defbf7a57ae5cf9
parentf39fdf261050f37ef6c804ec012f2d03dc55e6eb (diff)
Fix test-require-cache-without-stat.js
This path adds an additional cache to the module system for caching the location of previously required modules. Since it is embedded in the loop that iterates over all require.paths, this patch also handles the case where require.paths is being modified. The patch also cleans up some code around it. See: https://groups.google.com/forum/#!topic/nodejs-dev/QGGlrvLDHVs
-rw-r--r--src/node.js40
-rw-r--r--test/simple/test-require-cache-without-stat.js7
2 files changed, 33 insertions, 14 deletions
diff --git a/src/node.js b/src/node.js
index 39cae19d60a..413c4de4641 100644
--- a/src/node.js
+++ b/src/node.js
@@ -166,6 +166,7 @@
// modules in thier own context.
Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0);
Module._cache = {};
+ Module._pathCache = {};
Module._extensions = {};
Module._paths = [];
@@ -216,22 +217,41 @@
// given a path check a the file exists with any of the set extensions
function tryExtensions(p, extension) {
for (var i = 0, EL = exts.length; i < EL; i++) {
- f = tryFile(p + exts[i]);
- if (f) { return f; }
+ var filename = tryFile(p + exts[i]);
+
+ if (filename) {
+ return filename;
+ }
}
return false;
};
+ var cacheKey = JSON.stringify({request: request, paths: paths});
+ if (Module._pathCache[cacheKey]) {
+ return Module._pathCache[cacheKey];
+ }
+
// For each path
for (var i = 0, PL = paths.length; i < PL; i++) {
- var p = paths[i],
- // try to join the request to the path
- f = tryFile(path.resolve(p, request)) ||
- // try it with each of the extensions
- tryExtensions(path.resolve(p, request)) ||
- // try it with each of the extensions at "index"
- tryExtensions(path.resolve(p, request, 'index'));
- if (f) { return f; }
+ var basePath = path.resolve(paths[i], request);
+
+ // try to join the request to the path
+ var filename = tryFile(basePath);
+
+ if (!filename) {
+ // try it with each of the extensions
+ filename = tryExtensions(basePath)
+ }
+
+ if (!filename) {
+ // try it with each of the extensions at "index"
+ filename = tryExtensions(path.resolve(basePath, 'index'))
+ }
+
+ if (filename) {
+ Module._pathCache[cacheKey] = filename;
+ return filename;
+ }
}
return false;
}
diff --git a/test/simple/test-require-cache-without-stat.js b/test/simple/test-require-cache-without-stat.js
index 3c76ea760c1..b9f000a9a5c 100644
--- a/test/simple/test-require-cache-without-stat.js
+++ b/test/simple/test-require-cache-without-stat.js
@@ -24,9 +24,8 @@ fs.stat = function() {
};
// Load the module 'a' and 'http' once. It should become cached.
-
-var m = common.fixturesDir + '/a';
-require(m);
+require.paths.push(common.fixturesDir);
+require('a');
require('http');
console.log("counterBefore = %d", counter);
@@ -35,7 +34,7 @@ var counterBefore = counter;
// Now load the module a bunch of times.
// stat should not be called.
for (var i = 0; i < 100; i++) {
- require(m);
+ require('a');
}
// Do the same with a built-in module