-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
Conversation
Please merge #4281 before this PR, because it includes code gen for most of the binding instructions this PR changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what to think of the change from let
to var
. I always read to not use var
. After some research, it also seems to be the recommendation I find most. Personally, var
make it just harder to read code compared to let
. Is there some guidance somewhere we could follow?
The deferred
stuff makes sense to me to use var
on, but on anything else I would prefer to stick with let
, const
taking precedence over both ofc.
The reason I didn't use The only difference between try {
throw new Error();
var foo = 42;
} catch {
console.log(foo);
} Accessing Btw the reason the above logs let foo = undefined;
try {
throw new Error();
foo = 42;
} catch {
console.log(foo);
} Now the behavior is a bit more clear, right? Why did I use I just don't see the point in adding support for |
I was already aware of all that. AFAIU to me it all boils down to scoping and hoisting vs TDZ. My personal preference is
I agree that we should prioritize WBG over the output. But the readability of the generated output is not worth nothing. E.g. removing the complexity around So unless there are other reasons to use |
Done. I used I also added reference tests for closures, because they had some code gen unique to them. |
I'm going insane. Code gen for closures isn't consistent between compilers. I'm not having another |
let val = js.r#let("v", "undefined".to_string()); | ||
js.prelude(&format!("if ({} !== 0) {{", ptr)); | ||
js.prelude(&format!("v{} = {}({}, {}).slice();", i, f, ptr, len)); | ||
js.prelude(&format!("{val} = {f}({ptr}, {len}).slice();")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we just be able to use var
here and skip a line, like in deferred
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the introduction of let
, the purpose of var
has now become to declare variables as accessible from within the finally
block.
The goal is to generate correct JS code, not code golfing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In lack of a proper response: please use var
here.
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.