caspervonb / amokify

Other
25 stars 2 forks source link

Function callbacks can be broken #2

Closed caspervonb closed 9 years ago

caspervonb commented 9 years ago

Starting with a live function like this

setInterval(function() {
  console.log('tick');
}, 1000);

And adding an assignment expression into the module breaks the v8 function updates.

module.locals.foo = 1;

Call expressions are fine however, but only if they do not reference the module.exports or module.locals objects.

This only breaks on the first statement, if there is a statement referencing locals when starting for example, everything is fine.

As far as i know, this worked fine before #1 was added and things got moved around a bit, scopes have changed.

caspervonb commented 9 years ago

Having this as the first expression fixes everything.

(function noop() {}(module));

We end up with the generated module

function (require, module, exports) {
  module.exec(0, function () {
    (function noop() {
    }(module));
  });

  module.exec(1, function () {
    setInterval(function () {
      console.log('Are we live?')
    }, 1000);
  });
}

This happens the first time require, module or exports is accessed, so my hypotheses is that because they are not being captured the first time, the second time the module is called setScriptSource is failing because it is on another closure, or something to that effect.

caspervonb commented 9 years ago

Haven't quite figured out whats happening in setScriptSource here, but what we do know is that the closure is changing. We should be capturing these to be in the scope anyways, and it works that way.