sstephenson/execjs

Consecutive call to @context seem to be stateless

dcolley opened this issue · 2 comments

Given some javascript

function Calc() {
    var total = 0; // private variable
    this.clear = function(){ total = 0; }; //public function
    this.add = function(a){ total += a; };
    this.getTotal = function(){ return String(total); }
};
var mycal = new Calc();

And some ruby

      source = open( Dir.pwd+"/lib/calc.js" ).read
      @context = ExecJS.compile( source )
      @context.call 'mycal.clear'
      puts @context.call( 'mycal.getTotal' )
      @context.call 'mycal.add', 1
      @context.call 'mycal.add', 1
      puts @context.call( 'mycal.getTotal' )

... why does @context.call( 'mycal.getTotal' ) always return 0?

is there any explanation for closing this issue?

I've found that using a context compile with therubyracer as a backend maintains state, but using the nodejs backend doesn't maintain state. it might be worth adding to the documentation that you can't rely on state being maintained with context calls.