Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function scoping issue #913

Closed
gfwilliams opened this issue Sep 7, 2016 · 3 comments
Closed

Function scoping issue #913

gfwilliams opened this issue Sep 7, 2016 · 3 comments

Comments

@gfwilliams
Copy link
Member

http://forum.espruino.com/conversations/292464

// constructor function
function PengMod(a) {
      // "a" will be resolved with the method a not with the locale variable a.
      console.log( a );
      if (a !== "hello" ) throw Error("Bug!");
}
// This function masks the local variable a in the constructor!!!
PengMod.a = function () {
};
new PengMod( "hello" );

Fails even without the new - this appears to be some problem with the function's own fields getting priority over the parameters

@gfwilliams
Copy link
Member Author

gfwilliams commented Sep 7, 2016

This is more painful than expected. When defined, PengMod contains a as a 'native name string' representing the parameter. When PengMod.a gets set, it overwrites that parameter.

Current solutions:

  • Add a new variable containing a list of parameters - but this will use one extra variable for almost every function, which'll really hurt.
  • Add a variable containing parameters as a string. This'll use one more variable in some cases, but will be much more efficient for multiple arguments. It means that bound variables still need some kind of hack though.
  • Add a list of parameter names to the front of the code var. This will save variables in most cases, but might need some work. Bound variables needed too.
  • Add \xff to the beginning of each function parameter... Huge hack though

At the moment prepending to the code variable seems best. It's a relatively large change though.

@gfwilliams
Copy link
Member Author

... but we can't prepend to the code variable because now we're directly linking to the native string if it exists (to allow functions to be executed straight out of flash).

So either we ditch that and make everything more efficient, we do the hack, or we add the list of parameters.

@gfwilliams
Copy link
Member Author

Oh well... Added \xff. Turns out to be the tidiest way of fixing this and the least likely to break anything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant