BYVoid / continuation

JavaScript asynchronous Continuation-Passing Style transformation (deprecated).
Other
388 stars 42 forks source link

When using `cont()` in `if` block, the context in `if` is correct while the context after `if` is incorrect. #34

Closed curimit closed 9 years ago

curimit commented 9 years ago

Sample

this.name = 'outter';

function inner() {
  if (true) {
    setTimeout(cont(), 100);
    // Correct context here.
    console.log(this.name);
  }
  // Wrong context here.
  console.log(this.name);
};

var o = {
  name: 'inner',
  inner: inner
};

console.log(this.name);
o.inner();

Which compiles to

var o;
this.name = 'outter';
function inner() {
  (function (_$cont) {
    if (true) {
      setTimeout(function (arguments) {
        console.log(this.name);
        _$cont();
      }.bind(this, arguments), 100);
    } else {
      _$cont();
    }
  }.bind(this)(function (_$err) {
    if (_$err !== undefined)
      return _$cont(_$err);
    console.log(this.name);
  }));
}
o = {
  name: 'inner',
  inner: inner
};
console.log(this.name);
o.inner();
/* Generated by Continuation.js v0.1.5 */

This part of the compiled result got the wrong context:

function (_$err) {
    if (_$err !== undefined)
      return _$cont(_$err);
    console.log(this.name);
  }
curimit commented 9 years ago

Fixed: 48dd8dd