Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use getCaller() in both async and sync versions

  • Loading branch information...
commit 20f89456f7fc1d8e51b95ec1ab38b1ac154d9fc4 1 parent cd7169b
@substack authored
Showing with 12 additions and 15 deletions.
  1. +2 −12 lib/async.js
  2. +8 −0 lib/caller.js
  3. +2 −3 lib/sync.js
View
14 lib/async.js
@@ -1,15 +1,7 @@
var core = require('./core');
var fs = require('fs');
var path = require('path');
-
-function getCaller() {
- // see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
- var origPrepareStackTrace = Error.prepareStackTrace;
- Error.prepareStackTrace = function (_, stack) { return stack };
- var stack = (new Error()).stack;
- Error.prepareStackTrace = origPrepareStackTrace;
- return stack[2];
-}
+var caller = require('./caller.js');
module.exports = function resolve (x, opts, cb) {
if (core[x]) return cb(null, x);
@@ -30,9 +22,7 @@ module.exports = function resolve (x, opts, cb) {
var readFile = opts.readFile || fs.readFile;
var extensions = opts.extensions || [ '.js' ];
- var y = opts.basedir
- || path.dirname(getCaller().getFileName())
- ;
+ var y = opts.basedir || path.dirname(caller());
opts.paths = opts.paths || [];
View
8 lib/caller.js
@@ -0,0 +1,8 @@
+module.exports = function () {
+ // see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
+ var origPrepareStackTrace = Error.prepareStackTrace;
+ Error.prepareStackTrace = function (_, stack) { return stack };
+ var stack = (new Error()).stack;
+ Error.prepareStackTrace = origPrepareStackTrace;
+ return stack[2].getFileName();
+};
View
5 lib/sync.js
@@ -1,6 +1,7 @@
var core = require('./core');
var fs = require('fs');
var path = require('path');
+var caller = require('./caller.js');
module.exports = function (x, opts) {
if (core[x]) return x;
@@ -14,9 +15,7 @@ module.exports = function (x, opts) {
var readFileSync = opts.readFileSync || fs.readFileSync;
var extensions = opts.extensions || [ '.js' ];
- var y = opts.basedir
- || path.dirname(require.cache[__filename].parent.filename)
- ;
+ var y = opts.basedir || path.dirname(caller());
opts.paths = opts.paths || [];
Please sign in to comment.
Something went wrong with that request. Please try again.