Skip to content

Don't emit code to call main if there is no main #5850

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

Merged
merged 3 commits into from
Nov 28, 2017
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
8 changes: 7 additions & 1 deletion src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ var proxiedFunctionTable = ["null" /* Reserve index 0 for an undefined function*
// map: pair(sig, syncOrAsync) -> function body
var proxiedFunctionInvokers = {};

// used internally. set when there is a main() function.
// also set when in a linkable module, as the main() function might
// arrive from a dynamically-linked library, and not necessarily
// the current compilation unit.
var HAS_MAIN = ('_main' in IMPLEMENTED_FUNCTIONS) || MAIN_MODULE || SIDE_MODULE;

// JSifier
function JSify(data, functionsOnly) {
//B.start('jsifier');
var mainPass = !functionsOnly;

var itemsDict = { type: [], GlobalVariableStub: [], functionStub: [], function: [], GlobalVariable: [], GlobalVariablePostSet: [] };
Expand Down Expand Up @@ -569,6 +574,7 @@ function JSify(data, functionsOnly) {
if (DETERMINISTIC) {
print(read('deterministic.js'));
}

var postFile = BUILD_AS_SHARED_LIB || SIDE_MODULE ? 'postamble_sharedlib.js' : 'postamble.js';
var postParts = processMacros(preprocess(read(postFile), postFile)).split('{{GLOBAL_VARS}}');
print(postParts[0]);
Expand Down
6 changes: 6 additions & 0 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ dependenciesFulfilled = function runCaller() {
if (!Module['calledRun']) dependenciesFulfilled = runCaller; // try this again later, after new deps are fulfilled
}

#if HAS_MAIN
Module['callMain'] = Module.callMain = function callMain(args) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Module['callMain'] and Module.callMain are same in JS, can we remove the latter part?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I'm actually not sure why we have this pattern. I suspect it is very old, and either worked around a closure compiler bug or a bug in our understanding of closure compiler ;) We should remove it across the codebase, leaving only Module['X'].

#if ASSERTIONS
assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on __ATMAIN__)');
Expand Down Expand Up @@ -237,6 +238,7 @@ Module['callMain'] = Module.callMain = function callMain(args) {
calledMain = true;
}
}
#endif // HAS_MAIN

{{GLOBAL_VARS}}

Expand Down Expand Up @@ -280,7 +282,9 @@ function run(args) {

if (Module['onRuntimeInitialized']) Module['onRuntimeInitialized']();

#if HAS_MAIN
if (Module['_main'] && shouldRunNow) Module['callMain'](args);
#endif // HAS_MAIN

postRun();
}
Expand Down Expand Up @@ -381,6 +385,7 @@ if (Module['preInit']) {
}
}

#if HAS_MAIN
// shouldRunNow refers to calling main(), not run().
#if INVOKE_RUN
var shouldRunNow = true;
Expand All @@ -390,6 +395,7 @@ var shouldRunNow = false;
if (Module['noInitialRun']) {
shouldRunNow = false;
}
#endif // HAS_MAIN

#if NO_EXIT_RUNTIME
Module["noExitRuntime"] = true;
Expand Down