musictheory / NilScript

Objective-C-style language superset of JavaScript with a tiny, simple runtime
Other
50 stars 5 forks source link

Bug: Function parameters get transformed too. #60

Closed IngwiePhoenix closed 8 years ago

IngwiePhoenix commented 9 years ago

function F(a, b) can be compiled into a bug IF the currently present scope devines a variable for that.

@implementation Bugged {
    var foo;
}
+(id) init {
    require(["foo"], function(foo){ // <---
        // ...
    });
}
@end

Becomes:

var Bugged = $oj_oj._registerClass({ $oj_c_Bugged:1 }, null, function($oj_s, $oj_m) { function Bugged() { this.$oj_i_Bugged$foo=null;this.constructor = Bugged;this.$oj_id = ++$oj_oj._id;}

$oj_s.init = function() {
    var self = this;require(["foo"], function(self.$oj_i_Bugged$foo){ // <---
        // ...
    });
}
return Bugged;});

I would believe that it should be valid to use self.foo = foo, but I just wanted to report this since it seems to be very unexpected behaviour.

iccir commented 9 years ago

That's a good bug.

Ideally, oj should do the same thing that Obj-C does and have foo refer to the parameter and not the ivar within the function scope (although this will cause a warning if -Wshadow is on).

Pragmatically, such behavior would add complexity to the AST transpiler, and I'm not sure how often this issue would come up when using standard Obj-C conventions (prefixed _ivars).

My gut instinct is: have this be a compiler error for now. Reinvestigate in the future if/when we keep track of additional scope information.