Improved variable names in generated JS #4280
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a change I wanted to do for a while. The bindings defined JS variables inconsistently, using
const
,let
, andvar
all over the place. As it turns out, most variables can be declared asconst
and everything else can bevar
. So I create a simple API for creating variables. This API also takes care of resolving naming conflicts, so we'll never have to worry about temporary variables conflicting with argument names ever again. In the future, this system may also be used to resolve other naming conflicts, but I kept it simple for now.For those needing a refresher on
var
,let
, andconst
in JS:const name = value
: A block-scoped variable that cannot be reassigned.let name = value
: A block-scoped variable that can be reassigned.var name = value
: A function-scoped variable that can be reassigned.Changes:
JsBinding
that handles the creating of variables. The new function automatically resolves name conflicts and ensure that temporary variable names never conflict with parameter names. The main functions of the new API are:alias(name, expr)
: This creates a JSconst
variable. The variable is immutable.var(name, initial)
: This creates a JSvar
variable. The variable is mutable AND visible in thefinally
block.guarnatee_alias
: Same as alias, but panics if the name is taken. This is necessary, because one instruction assumes that a variable calledretptr
has been created, so we can't rename that variable.