kripken / box2d.js

Port of Box2D to JavaScript using Emscripten
1.32k stars 198 forks source link

Variable name shadowing causing random failures #46

Open AshleyScirra opened 10 years ago

AshleyScirra commented 10 years ago

The naming scheme used by the asm.js compilation uses functions and variables with the same name. This is an error in strict mode (see also issue #42). Evne if you're not using strict mode, surely this will cause bugs and crashes? How can the script possibly call the right function or reference the right variable if they both have the same names?

An example:

Line 8 defines 'xa' as:

function xa(a){var c=ka;ka=ka+a|0;ka=ka+7>>3<<3;return c}

Line 414 defines 'xa' (same case) as:

var xa=function(a){return x.stackAlloc(a)}

We seem to be hitting an issue that is associated with this: in strict mode it fails with "variable name 'xa' already defined", and when not in strict mode, we randomly hit "uncaught assertion: 3".

AshleyScirra commented 10 years ago

Looks like it's defined a third time too around line 210: function xa(a){a=a|0;return af(3,a|0)|0}

kripken commented 10 years ago

It's ok to reuse the same name in different scopes. When I wanted to verify that, I didn't see function xa in the box2d build in this repo though, where are you seeing it?

AshleyScirra commented 10 years ago

I was referring to box2d.js! It's here: https://github.com/kripken/box2d.js/blob/master/box2d.js

The line numbers given are in that file. In Chrome Canary it doesn't even start up the script any more! It just throws a javascript error about the redefinition of 'xa' right away!