Skip to content

[acorn-optimizer] Don't ignore usages in default arguments - #20435

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix_missing_arguments
Oct 11, 2023
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix_missing_arguments

Conversation

@sbc100

@sbc100 sbc100 commented Oct 10, 2023

Copy link
Copy Markdown
Collaborator

This includes a unit test and real world test based on a user reported issue where function run(args = arguments_) was failing to keep the arguments_ global alive.

@sbc100
sbc100 requested a review from kripken October 10, 2023 19:43
@sbc100
sbc100 force-pushed the fix_missing_arguments branch 3 times, most recently from 709a77c to e6c105d Compare October 10, 2023 20:40
@RReverser

RReverser commented Oct 10, 2023 •

Copy link
Copy Markdown
Collaborator

Looking at number of issues and custom code related to scope tracking, may I suggest looking at escope or its more active fork eslint-scope?

While Acorn takes care of AST, this battle-tested package specifically focuses on taking an AST and augmenting it with scope information, so you can tell from each node whether it's a local binding, global binding, what it resolves to and so on. Here's demo from the original project: https://mazurov.github.io/escope-demo/.

I know Emscripten generally tries not to rely on many 3rd-party packages, but given the complexity of scope tracking, especially in JS with all the different kinds of bindings and scopes, I really believe it would be worth it in terms of saved time on individual bugs.

@sbc100

sbc100 commented Oct 10, 2023

Copy link
Copy Markdown
Collaborator Author

Looking at number of issues and custom code related to scope tracking, may I suggest looking at escope or its more active fork eslint-scope?

While Acorn takes care of AST, this battle-tested package specifically focuses on taking an AST and augmenting it with scope information, so you can tell from each node whether it's a local binding, global binding, what it resolves to and so on. Here's demo from the original project: https://mazurov.github.io/escope-demo/.

I know Emscripten generally tries not to rely on many 3rd-party packages, but given the complexity of scope tracking, especially in JS with all the different kinds of bindings and scopes, I really believe it would be worth it in terms of saved time on individual bugs.

Sounds reasonable. I don't think anyone would object if you or someone else wants to convert acorn-optimizer to this method.

I don't think we need to block this PR on that though.

@RReverser

Copy link
Copy Markdown
Collaborator

I don't think we need to block this PR on that though.

Certainly not, it's just that I couldn't help but notice series of custom fixes for scoping issues lately, and figured I'd bring it up. I'll create a separate issue for that.

Comment thread tools/acorn-optimizer.js Resolved
@sbc100
sbc100 force-pushed the fix_missing_arguments branch from e6c105d to b02d73a Compare October 10, 2023 21:39
This includes a unit test and real world test based on a user reported
issue where `function run(args = arguments_)` was failing to keep
the `arguments_` global alive.
@sbc100
sbc100 force-pushed the fix_missing_arguments branch from b02d73a to 6535541 Compare October 10, 2023 22:57
@sbc100
sbc100 enabled auto-merge (squash) October 11, 2023 05:37
Comment thread test/test_other.py
self.assertContained('error: noExitRuntime cannot be referenced via __deps mechansim', err)

def test_hello_world_argv(self):
self.do_runf(test_file('hello_world_argv.c'), 'hello, world! (1)')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did these tests really not pass before? We have lots of existing tests with argv,

$ grep argv test/*.c | wc -l
74

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one passes ok, but the one below will fail. Specifically you have to do -sENVIRONMENT=web -sSTRICT --closure=1.

In particular note the -sENVIRONMENT=web .. if you include node support the issue with arguments_ doesn't show up because node support assigs to this variable.

The reason I added two version of this test is that I've often wanted a simple test that just does hello world but includes argv.

Comment thread tools/acorn-optimizer.js
const scope = {};
scopes.push(scope);
node.params.forEach((param) => {
if (param.type === 'RestElement') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the test cover this? (what is a rest element?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RestElement is f(a, b, ...c) .. here c is RestElement. I didn't write a specific test for it, but we do have at least one library function with this pattern because I hit the assertion without it.

Do you want me to add a specific test for this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, if the assert was already hit then that sounds enough then.

@sbc100
sbc100 merged commit 5b63268 into emscripten-core:main Oct 11, 2023
@sbc100
sbc100 deleted the fix_missing_arguments branch October 11, 2023 19:30
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.

3 participants