Skip to content

Commit c56eb77

Browse files
authored
Don't emit code to call main if there is no main (#5850)
also emit it if we are a dynamically-linkable module, since then `main` may be in other code we will link in.
1 parent 4a76853 commit c56eb77

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/jsifier.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ var proxiedFunctionTable = ["null" /* Reserve index 0 for an undefined function*
2525
// map: pair(sig, syncOrAsync) -> function body
2626
var proxiedFunctionInvokers = {};
2727

28+
// used internally. set when there is a main() function.
29+
// also set when in a linkable module, as the main() function might
30+
// arrive from a dynamically-linked library, and not necessarily
31+
// the current compilation unit.
32+
var HAS_MAIN = ('_main' in IMPLEMENTED_FUNCTIONS) || MAIN_MODULE || SIDE_MODULE;
33+
2834
// JSifier
2935
function JSify(data, functionsOnly) {
30-
//B.start('jsifier');
3136
var mainPass = !functionsOnly;
3237

3338
var itemsDict = { type: [], GlobalVariableStub: [], functionStub: [], function: [], GlobalVariable: [], GlobalVariablePostSet: [] };
@@ -569,6 +574,7 @@ function JSify(data, functionsOnly) {
569574
if (DETERMINISTIC) {
570575
print(read('deterministic.js'));
571576
}
577+
572578
var postFile = BUILD_AS_SHARED_LIB || SIDE_MODULE ? 'postamble_sharedlib.js' : 'postamble.js';
573579
var postParts = processMacros(preprocess(read(postFile), postFile)).split('{{GLOBAL_VARS}}');
574580
print(postParts[0]);

src/postamble.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ dependenciesFulfilled = function runCaller() {
163163
if (!Module['calledRun']) dependenciesFulfilled = runCaller; // try this again later, after new deps are fulfilled
164164
}
165165

166+
#if HAS_MAIN
166167
Module['callMain'] = Module.callMain = function callMain(args) {
167168
#if ASSERTIONS
168169
assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on __ATMAIN__)');
@@ -237,6 +238,7 @@ Module['callMain'] = Module.callMain = function callMain(args) {
237238
calledMain = true;
238239
}
239240
}
241+
#endif // HAS_MAIN
240242

241243
{{GLOBAL_VARS}}
242244

@@ -280,7 +282,9 @@ function run(args) {
280282

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

285+
#if HAS_MAIN
283286
if (Module['_main'] && shouldRunNow) Module['callMain'](args);
287+
#endif // HAS_MAIN
284288

285289
postRun();
286290
}
@@ -381,6 +385,7 @@ if (Module['preInit']) {
381385
}
382386
}
383387

388+
#if HAS_MAIN
384389
// shouldRunNow refers to calling main(), not run().
385390
#if INVOKE_RUN
386391
var shouldRunNow = true;
@@ -390,6 +395,7 @@ var shouldRunNow = false;
390395
if (Module['noInitialRun']) {
391396
shouldRunNow = false;
392397
}
398+
#endif // HAS_MAIN
393399

394400
#if NO_EXIT_RUNTIME
395401
Module["noExitRuntime"] = true;

0 commit comments

Comments
 (0)