Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/hello_world_argv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2023 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <stdio.h>

int main(int argc, char* argv[]) {
printf("hello, world! (%d)\n", argc);
return 0;
}
7 changes: 7 additions & 0 deletions test/optimizer/JSDCE-defaultArg-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var usedAsDefaultArg = 42;

function g(a, b = usedAsDefaultArg) {
return a + b + 1;
}

Module["g"] = g;
9 changes: 9 additions & 0 deletions test/optimizer/JSDCE-defaultArg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var usedAsDefaultArg = 42;
var notUsed = 43;

// exported
function g(a, b = usedAsDefaultArg) {
return a+b+1;
}

Module['g'] = g;
7 changes: 7 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,7 @@ def test_extern_prepost(self):
'minifyLocals': ('optimizer/test-js-optimizer-minifyLocals.js', ['minifyLocals']),
'JSDCE': ('optimizer/JSDCE.js', ['JSDCE']),
'JSDCE-hasOwnProperty': ('optimizer/JSDCE-hasOwnProperty.js', ['JSDCE']),
'JSDCE-defaultArg': ('optimizer/JSDCE-defaultArg.js', ['JSDCE']),
'JSDCE-fors': ('optimizer/JSDCE-fors.js', ['JSDCE']),
'JSDCE-objectPattern': ('optimizer/JSDCE-objectPattern.js', ['JSDCE']),
'AJSDCE': ('optimizer/AJSDCE.js', ['AJSDCE']),
Expand Down Expand Up @@ -14008,3 +14009,9 @@ def test_noExitRuntime_deps(self):
''')
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library=lib.js', '-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=foo'])
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.


def test_arguments_global(self):
self.emcc(test_file('hello_world_argv.c'), ['-sENVIRONMENT=web', '-sSTRICT', '--closure=1', '-O2'])
10 changes: 9 additions & 1 deletion tools/acorn-optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,20 @@ function runJSDCE(ast, aggressive) {
ensureData(scopes[scopes.length - 1], node.id.name).def = 1;
}
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.

param = param.argument;
}
if (param.type === 'AssignmentPattern') {
c(param.right);
param = param.left;
}
assert(param.type === 'Identifier', param.type);
const name = param.name;
ensureData(scope, name).def = 1;
scope[name].param = 1;
});
scopes.push(scope);
c(node.body);
// we can ignore self-references, i.e., references to ourselves inside
// ourselves, for named defined (defun) functions
Expand Down