Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit d73b5c5

Browse files
committed
lc-run: Deprecate 'main' as default entry point; prefer 'Main'.
Since lowercase identifiers are reserved for syntax, lc-run should use a default handler called `Main()` not `main()`. For the time being: 1) look for a handler called `Main()`, or 2) look for a handler called `main()` and emit a deprecation warning, or 3) print an error message.
1 parent 666f2e4 commit d73b5c5

4 files changed

Lines changed: 61 additions & 6 deletions

File tree

tests/lc-run/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ check:
2424
$(LC_COMPILE) --modulepath $(TEST_DIR) --modulepath $(MODULE_DIR) \
2525
lc-run-test.mlc --output $(TEST_DIR)/lc-run-test.lcm
2626
$(LC_RUN) -l $(TEST_DIR)/lc-run-lib.lcm $(TEST_DIR)/lc-run-test.lcm
27-
test "`$(LC_RUN) -l $(TEST_DIR)/lc-run-lib.lcm --list-handlers $(TEST_DIR)/lc-run-test.lcm`" = "main"
27+
test "`$(LC_RUN) -l $(TEST_DIR)/lc-run-lib.lcm --list-handlers $(TEST_DIR)/lc-run-test.lcm`" = "Main"
28+
$(LC_RUN) $(TEST_DIR)/lc-run-lib.lcm
29+
test "`$(LC_RUN) --list-handlers $(TEST_DIR)/lc-run-lib.lcm`" = "main"

tests/lc-run/lc-run-lib.mlc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module com.livecode.lc_run_lib
22

3-
public handler lib_main()
3+
-- Intentional lower-case handler name
4+
public handler main()
45
-- Do nothing
56
end handler
67

tests/lc-run/lc-run-test.mlc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module com.livecode.lc_run_test
22

33
use com.livecode.lc_run_lib
44

5-
public handler main()
6-
lib_main()
5+
public handler Main()
6+
main()
77
end handler
88

99
end module

toolchain/lc-compile/src/lc-run.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ enum {
3636
kMCRunExitStatusUncaughtError = 126,
3737
};
3838

39+
/* Possible entry point handler names. The first is the default. */
40+
const char *kMCRunHandlerNames[] = { "Main", "main", nil };
41+
3942
struct MCRunConfiguration
4043
{
4144
MCStringRef m_filename;
@@ -421,6 +424,51 @@ MCRunListHandlers (MCScriptModuleRef p_module)
421424
return true;
422425
}
423426

427+
static bool
428+
MCRunGetHandlerName (MCScriptModuleRef p_module,
429+
MCNameRef p_config_name,
430+
MCNameRef & r_name)
431+
{
432+
MCNameRef t_main_name = nil;
433+
if (!MCNameIsEmpty (p_config_name))
434+
{
435+
t_main_name = p_config_name;
436+
}
437+
else
438+
{
439+
MCAutoProperListRef t_handler_list; /* List of MCNameRef */
440+
441+
if (!MCScriptCopyHandlersOfModule (p_module, &t_handler_list))
442+
return false;
443+
444+
for (size_t i = 0; nil != kMCRunHandlerNames[i]; ++i)
445+
{
446+
MCNameRef t_name = MCNAME(kMCRunHandlerNames[i]);
447+
448+
uindex_t t_ignored;
449+
if (!MCProperListFirstIndexOfElement (*t_handler_list, t_name,
450+
0, t_ignored))
451+
continue;
452+
453+
if (i > 0)
454+
{
455+
MCAutoStringRef t_message;
456+
if (!MCStringFormat (&t_message, "WARNING: Deprecated default handler '%s' (use '-H %s')\n", kMCRunHandlerNames[i], kMCRunHandlerNames[i]))
457+
return false;
458+
MCRunPrintMessage (stderr, *t_message);
459+
}
460+
461+
t_main_name = t_name;
462+
break;
463+
}
464+
465+
if (nil == t_main_name)
466+
t_main_name = MCNAME(kMCRunHandlerNames[0]);
467+
}
468+
469+
r_name = MCValueRetain (t_main_name);
470+
return true;
471+
}
424472

425473
/* ----------------------------------------------------------------
426474
* Main program
@@ -439,7 +487,7 @@ main (int argc,
439487
/* Defaults */
440488
MCRunConfiguration t_config;
441489
t_config.m_filename = MCValueRetain (kMCEmptyString);
442-
t_config.m_handler = MCValueRetain (MCNAME("main"));
490+
t_config.m_handler = MCValueRetain (kMCEmptyName);
443491
t_config.m_list_handlers = false;
444492
if (!MCProperListCreateMutable (t_config.m_load_filenames))
445493
MCRunStartupError(MCSTR("Initialization"));
@@ -465,11 +513,15 @@ main (int argc,
465513
}
466514
else
467515
{
516+
MCNewAutoNameRef t_handler;
517+
if (!MCRunGetHandlerName (*t_module, t_config.m_handler, &t_handler))
518+
MCRunStartupError (MCSTR("Handler"));
519+
468520
if (!MCScriptCreateInstanceOfModule (*t_module, &t_instance))
469521
MCRunStartupError(MCSTR("Create Instance"));
470522

471523
if (!MCScriptCallHandlerOfInstance(*t_instance,
472-
t_config.m_handler,
524+
*t_handler,
473525
NULL, 0,
474526
&t_ignored_retval))
475527
MCRunHandlerError();

0 commit comments

Comments
 (0)