Permalink
Browse files

remove lame magical getters

  • Loading branch information...
tj committed Jan 24, 2013
1 parent d745209 commit 4882cd047cbc234df1d07cfe26f618ecb1a45e5e
Showing with 31 additions and 96 deletions.
  1. +14 −46 Readme.md
  2. +2 −2 examples/assert.js
  3. +6 −6 examples/trace.js
  4. +9 −1 index.js
  5. +0 −13 lib/__line.js
  6. +0 −19 lib/__stack.js
  7. +0 −9 lib/callsite.js
View
@@ -1,20 +1,15 @@
# callstack
- Access to v8's "raw" `CallSite`s. This module provides the following "magical" global getters:
-
- - `__stack` array `CallSite` objects starting from the callee
- - `__line` the current line number
+ Access to v8's "raw" `CallSite`s.
## Installation
$ npm install callsite
-## __stack
-
- For details on the v8 `CallSite` objects view the [stack trace api docs](http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi), for example `__stack[0].getFileName()`.
+## Example
```js
-require('callsite');
+var stack = require('callsite');
foo();
@@ -27,50 +22,23 @@ function bar() {
}
function baz() {
- __stack[0].fun == baz;
- __stack[1].fun == bar;
- __stack[2].fun == foo;
+ console.log();
+ stack().forEach(function(site){
+ console.log(' \033[36m%s\033[90m in %s:%d\033[0m'
+ , site.getFunctionName() || 'anonymous'
+ , site.getFileName()
+ , site.getLineNumber());
+ });
+ console.log();
}
```
-## __line
-
- Returns the current lineno:
-
-```js
-require('callsite');
-console.log('foo')
-console.log('bar')
-console.log(__line) // => 4
-```
-
## Why?
Because you can do weird, stupid, clever, wacky things such as:
-
- - [better-assert](https://github.com/visionmedia/better-assert)
-
-## License
-(The MIT License)
-
-Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-'Software'), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
+ - [better-assert](https://github.com/visionmedia/better-assert)
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
+## License
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ MIT
View
@@ -1,12 +1,12 @@
-require('../');
+var stack = require('..');
var fs = require('fs');
function assert(expr) {
if (expr) return;
- var call = __stack[1]
+ var call = stack()[1]
, file = call.getFileName()
, lineno = call.getLineNumber()
, src = fs.readFileSync(file, 'utf8')
View
@@ -1,5 +1,5 @@
-require('../');
+var stack = require('..');
foo();
@@ -13,11 +13,11 @@ function bar() {
function baz() {
console.log();
- __stack.forEach(function(call){
+ stack().forEach(function(site){
console.log(' \033[36m%s\033[90m in %s:%d\033[0m'
- , call.getFunctionName() || 'anonymous'
- , call.getFileName()
- , call.getLineNumber());
+ , site.getFunctionName() || 'anonymous'
+ , site.getFileName()
+ , site.getLineNumber());
});
console.log();
-}
+}
View
@@ -1,2 +1,10 @@
-module.exports = require('./lib/callsite');
+module.exports = function(){
+ var orig = Error.prepareStackTrace;
+ Error.prepareStackTrace = function(_, stack){ return stack; };
+ var err = new Error;
+ Error.captureStackTrace(err, arguments.callee);
+ var stack = err.stack;
+ Error.prepareStackTrace = orig;
+ return stack;
+};
View
@@ -1,13 +0,0 @@
-
-/**
- * Return the current line number
- *
- * @return {Number}
- * @api public
- */
-
-Object.defineProperty(global, '__line', {
- get: function(){
- return __stack[1].getLineNumber();
- }
-});
View
@@ -1,19 +0,0 @@
-
-/**
- * Return the stack trace.
- *
- * @return {Array}
- * @api public
- */
-
-Object.defineProperty(global, '__stack', {
- get: function(){
- var orig = Error.prepareStackTrace;
- Error.prepareStackTrace = function(_, stack){ return stack; };
- var err = new Error;
- Error.captureStackTrace(err, arguments.callee);
- var stack = err.stack;
- Error.prepareStackTrace = orig;
- return stack;
- }
-});
View
@@ -1,9 +0,0 @@
-
-/*!
- * callsite
- * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
- * MIT Licensed
- */
-
-require('./__stack');
-require('./__line');

0 comments on commit 4882cd0

Please sign in to comment.