Permalink
Browse files

.run() for more terse execution

  • Loading branch information...
1 parent 2b6512f commit 6d92598cd6a5a45f72196591aa80cb78d2aa804c @substack committed May 16, 2012
Showing with 25 additions and 21 deletions.
  1. +4 −19 example/giant.js
  2. +6 −0 example/sources/giant.js
  3. +13 −0 index.js
  4. +1 −1 package.json
  5. +1 −1 test/traverse.js
View
@@ -1,27 +1,12 @@
-var src = '(' + function () {
- (function fee () {
- [ 'foe', 'fum' ].forEach(fie);
- })();
-
- function fie (s) { smell(s) }
- function smell () { english('blood') }
-} + ')()';
+var fs = require('fs');
+var src = fs.readFileSync(__dirname + '/sources/giant.js', 'utf8');
var fritter = require('../');
var fry = fritter(src);
fry.on('error', function (err, c) {
console.log(String(err));
- console.log('--------------------');
c.stack.forEach(function (s) {
- console.log(
- s.callee.id && s.callee.id.name
- || src.slice(s.range[0], s.range[1] + 1)
- );
+ console.log(' in ' + fry.nameOf(s) + ', line ' + s.start.line);
});
- console.log('--------------------');
});
-
-var vm = require('vm');
-try {
- vm.runInNewContext(fry.source, fry.context);
-} catch (err) { /* ignore this */ }
+fry.run();
@@ -0,0 +1,6 @@
+(function fee () {
+ [ 'foe', 'fum' ].forEach(fie);
+})();
+
+function fie (s) { smell(s) }
+function smell () { english('blood') }
View
@@ -39,6 +39,19 @@ function Fritter (context, opts) {
Fritter.prototype = new EventEmitter;
+Fritter.prototype.run = function () {
+ var args = [], vars = [];
+ for (var key in this.context) {
+ vars.push(key);
+ args.push(this.context[key]);
+ }
+
+ var fn = Function(vars, this.source);
+ try {
+ fn.apply(null, args);
+ } catch (err) {}
+};
+
Fritter.prototype.defineContext = function () {
var self = this;
var nodes = this.nodes;
View
@@ -1,7 +1,7 @@
{
"name" : "fritter",
"description" : "stack trace instrumentation through AST manipulation",
- "version" : "0.0.2",
+ "version" : "0.1.0",
"repository" : {
"type" : "git",
"url" : "git://github.com/substack/node-fritter.git"
View
@@ -7,7 +7,7 @@ var src = fs.readFileSync(require.resolve('traverse'), 'utf8')
+ ';' + fs.readFileSync(__dirname + '/sources/traverse.js', 'utf8')
;
-test('fff', function (t) {
+test('traverse', function (t) {
t.plan(3);
var fry = fritter({

0 comments on commit 6d92598

Please sign in to comment.