skeeto / skewer-mode

Live web development in Emacs
The Unlicense
1.1k stars 57 forks source link

repl with context #49

Open bunions1 opened 10 years ago

bunions1 commented 10 years ago

It is often very useful to be able to control the scope/context that repl statements will be evaluated in. For instance when developing an angular controller you probably want to have access to your dependency injected variables. There doesn't seem to be an official way to do that so I thought I'd share a hack that seems to work.

Place the line: skewer.globalEval = function(expression) {return eval(expression);} where you want your context, kind of like a debugger; statement. For instance:

angular.module('my-app').controller('MainCtrl', function ($scope, $http) { 
    skewer.globalEval = function(expression) {return eval(expression);}
    ....
  }

Now from the prompt you can easily access your $scope variables and such js> alert($scope.someVar);

skeeto commented 10 years ago

What's going on here is you've changed the indirect eval in globalEval into a direct eval. That's an interesting hack. It may be worth formally encoding this as a mode of operation in Skewer.

The consequence is that you won't be able to make any more global definitions (though that won't matter much in an Angular app) and you'll suffer some degraded performance (direct eval causes deoptimization).