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

Improved variable names in generated JS #4280

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RunDevelopment
Copy link
Contributor

This PR implements a change I wanted to do for a while. The bindings defined JS variables inconsistently, using const, let, and var all over the place. As it turns out, most variables can be declared as const and everything else can be var. 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, and const 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:

  • New API for 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 JS const variable. The variable is immutable.
    • var(name, initial): This creates a JS var variable. The variable is mutable AND visible in the finally block.
    • guarnatee_alias: Same as alias, but panics if the name is taken. This is necessary, because one instruction assumes that a variable called retptr has been created, so we can't rename that variable.
  • Use the new API everywhere. All variables are now created with the new API.

@RunDevelopment RunDevelopment marked this pull request as draft November 19, 2024 16:40
@RunDevelopment
Copy link
Contributor Author

Please merge #4281 before this PR, because it includes code gen for most of the binding instructions this PR changes.

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

Successfully merging this pull request may close these issues.

1 participant