CoffeeScriptRedux icon indicating copy to clipboard operation
CoffeeScriptRedux copied to clipboard

compiled extends$ function filled with garbage declarations?

Open ciddan opened this issue 12 years ago • 11 comments

This is what I get after compiling one of my classes. It doesn't look right? Compiled on CoffeeScript 2.0.0-beta3

function extends$(child, parent) {
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  for (key in parent)
    if (isOwn$(parent, key))
      child[key] = parent[key];
  function ctor() {
    this.constructor = child;
  }
  ctor.prototype = parent.prototype;
  child.prototype = new ctor;
  child.__super__ = parent.prototype;
  return child;
}

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/825327-compiled-extends-function-filled-with-garbage-declarations?utm_campaign=plugin&utm_content=tracker%2F33145&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F33145&utm_medium=issues&utm_source=github).

ciddan avatar Feb 20 '13 12:02 ciddan

Wow, when did that happen? git bisecting now...

michaelficarra avatar Feb 20 '13 14:02 michaelficarra

I can't reproduce this. Please provide a test case.

michaelficarra avatar Feb 20 '13 14:02 michaelficarra

Hehehe. It's funny. It happens to me too in the on line demo. But only if i'm typing the code. If i type class Foo extends Bar it starts adding var key; lines on the compiled output for each new character i type after the "B" of Bar. If i refresh the page the code is shown without extra var key; lines.

Reproduced in both Firefox and Chrome in Ubuntu.

epidemian avatar Feb 20 '13 20:02 epidemian

Confirmed that this is currently happening in the online demo with the steps @epidemian noted.

joshuacc avatar Feb 20 '13 20:02 joshuacc

This happened when I was compiling my code using require('coffee-script-redux') and going the more manual route (in a grunt task). The CLI does not generate that extra noise. I couldn't really see what the CLI was doing that I wasn't, but...

ciddan avatar Feb 20 '13 20:02 ciddan

Wow, this is an insane bug. I must have accidental state somewhere in the declaration-emitting code. Re-opening.

michaelficarra avatar Feb 20 '13 22:02 michaelficarra

Yeah, it's quite a fun bug :smile_cat:. Good luck git bisecting it (BTW, thanks for mentioning that command; i didn't know about it!).

epidemian avatar Feb 20 '13 23:02 epidemian

@epidemian: It's very useful, especially when you can easily write a script that fails when it detects the bug.

michaelficarra avatar Feb 20 '13 23:02 michaelficarra

Huh, I still can't reproduce this on current master.

$ bin/coffee --js --cli 'a extends b'
// Generated by CoffeeScript 2.0.0-beta3
extends$(a, b);
function isOwn$(o, p) {
  return {}.hasOwnProperty.call(o, p);
}
function extends$(child, parent) {
  var key;
  for (key in parent)
    if (isOwn$(parent, key))
      child[key] = parent[key];
  function ctor() {
    this.constructor = child;
  }
  ctor.prototype = parent.prototype;
  child.prototype = new ctor;
  child.__super__ = parent.prototype;
  return child;
}

Can anyone give me a reproducible case?

michaelficarra avatar Feb 20 '13 23:02 michaelficarra

That's why i said good luck git bissecting it hehe. The bug looks to be easily triggered in the on line demo, but not so much in the command line compiler.

Here's a script that reproduces this (intended to run at the root of Redux project):

#! /usr/bin/env coffee

CS = require './lib/coffee-script/module'

compileFail = ->
  CS.cs2js 'class Foo extends Bar'

# Call a couple of times...
compileFail()
compileFail()

# And voila!
console.log compileFail()

Output:

// Generated by CoffeeScript 2.0.0-beta3
void function () {
  var Foo;
  Foo = function (super$) {
    extends$(Foo, super$);
    function Foo() {
    }
    void 0;
    return Foo;
  }(Bar);
  function isOwn$(o, p) {
    return {}.hasOwnProperty.call(o, p);
  }
  function extends$(child, parent) {
    var key;
    var key;
    var key;
    for (key in parent)
      if (isOwn$(parent, key))
        child[key] = parent[key];
    function ctor() {
      this.constructor = child;
    }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  }
}.call(this);

The amount of var key; declarations in the output is directly proportional to the times compileFail is called xD

epidemian avatar Feb 20 '13 23:02 epidemian

A simpler script to test it:

$ coffee --eval "console.log ((require './lib/coffee-script/module').cs2js 'a extends b' for i in [1..10])[9]"
// Generated by CoffeeScript 2.0.0-beta3
extends$(a, b);
function isOwn$(o, p) {
  return {}.hasOwnProperty.call(o, p);
}
function extends$(child, parent) {
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  var key;
  for (key in parent)
    if (isOwn$(parent, key))
      child[key] = parent[key];
  function ctor() {
    this.constructor = child;
  }
  ctor.prototype = parent.prototype;
  child.prototype = new ctor;
  child.__super__ = parent.prototype;
  return child;
}

One funny (or not so funny...) thing about this script is that the CS code needs to be compiled with the old CS compiler, as Redux will raise an error. I'm gonna open an issue for that now, but i wanted to leave this code here just as an evidence of this not being a very esoteric use-case.

epidemian avatar Feb 21 '13 00:02 epidemian