breuleux / earl-grey

Programming language compiling to JavaScript
http://breuleux.github.io/earl-grey/
MIT License
464 stars 6 forks source link

Existential operator doesn't bind scope as expected when used with functions #29

Open davej opened 9 years ago

davej commented 9 years ago

Take the following code:

global.install-win??.focus??()

An over-simplified version of the assignments/executions in JS might be:

tmp$0 = global.installWin;
tmp$1 = tmp$0.focus;
tmp$1();

This doesn't retain the correct function scope and may cause bugs/errors. I would suggest:

tmp$0 = global.installWin;
tmp$1 = tmp$0.focus;
tmp$1.bind(tmp$0)(); // or perhaps `func.apply()` is better (more performant)?
davej commented 9 years ago

Might be cool to soak up 'x is not a function' errors too:

if (typeof(tmp$1) === 'function') {
   tmp$1.bind(tmp$0)();
}
davej commented 9 years ago

Thinking about it... might be easier to not bind at all and just call the function in the context of the parent scope:

tmp$0.focus();