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

Commit 72f6a38

Browse files
committed
lc-run: Add new -l,--load option.
Add a new option to lc-run that permits additional builder modules to be loaded in addition to the main program. Files are loaded in the reverse order specified on the command line, so: lc-run -l foo.lcm -l bar.lcm -- baz.lcm will result in `baz.lcm` being loaded, followed by `bar.lcm` and finally `foo.lcm`.
1 parent 81add2e commit 72f6a38

5 files changed

Lines changed: 73 additions & 5 deletions

File tree

tests/lc-run/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ TEST_DIR = $(SOLUTION_DIR)/_tests/lc-run
1919
# 2) Does lc-run list the handlers of a trivial program?
2020
check:
2121
mkdir -p $(TEST_DIR)
22+
$(LC_COMPILE) --modulepath $(TEST_DIR) --modulepath $(MODULE_DIR) \
23+
lc-run-lib.mlc --output $(TEST_DIR)/lc-run-lib.lcm
2224
$(LC_COMPILE) --modulepath $(TEST_DIR) --modulepath $(MODULE_DIR) \
2325
lc-run-test.mlc --output $(TEST_DIR)/lc-run-test.lcm
24-
$(LC_RUN) $(TEST_DIR)/lc-run-test.lcm
25-
test "`$(LC_RUN) --list-handlers $(TEST_DIR)/lc-run-test.lcm`" = "main"
26+
$(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"

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module com.livecode.lc_run_lib
2+
3+
public handler lib_main()
4+
-- Do nothing
5+
end handler
6+
7+
end module

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module com.livecode.lc_run_test
22

3+
use com.livecode.lc_run_lib
4+
35
public handler main()
4-
-- nothing
6+
lib_main()
57
end handler
68

79
end module

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct MCRunConfiguration
4040
{
4141
MCStringRef m_filename;
4242
MCNameRef m_handler;
43+
MCProperListRef m_load_filenames;
4344
bool m_list_handlers;
4445
};
4546

@@ -65,6 +66,7 @@ MCRunUsage (int p_exit_status)
6566
"Run a compiled Livecode Builder bytecode file.\n"
6667
"\n"
6768
"Options:\n"
69+
" -l, --load LCMLIB Load an additional bytecode file.\n"
6870
" -H, --handler NAME Specify name of handler to run.\n"
6971
" --list-handlers List possible entry points in LCMFILE and exit.\n"
7072
" -h, --help Print this message.\n"
@@ -248,6 +250,21 @@ MCRunParseCommandLine (int argc,
248250
continue;
249251
}
250252

253+
if (MC_RUN_STRING_EQUAL (t_arg, "--load") ||
254+
MC_RUN_STRING_EQUAL (t_arg, "-l"))
255+
{
256+
if (NULL == t_argopt)
257+
MCRunBadOptionArgError (t_arg, t_argopt);
258+
259+
++t_arg_idx; /* Consume option argument */
260+
261+
/* Add to load list */
262+
if (!MCProperListPushElementOntoFront (x_config.m_load_filenames,
263+
t_argopt))
264+
return false;
265+
continue;
266+
}
267+
251268
if (MC_RUN_STRING_EQUAL (t_arg, "--list-handlers"))
252269
{
253270
x_config.m_list_handlers = true;
@@ -327,6 +344,36 @@ MCRunLoadModule (MCStringRef p_filename,
327344
if (!MCScriptCreateModuleFromStream (*t_stream, &t_module))
328345
return false;
329346

347+
r_module = MCScriptRetainModule (*t_module);
348+
return true;
349+
}
350+
351+
static bool
352+
MCRunLoadModules (MCStringRef p_filename,
353+
MCProperListRef p_load_filenames,
354+
MCScriptModuleRef & r_module)
355+
{
356+
/* Load main module */
357+
MCAutoScriptModuleRef t_module;
358+
if (!MCRunLoadModule (p_filename, &t_module))
359+
return false;
360+
361+
/* Load other modules */
362+
MCAutoScriptModuleRefArray t_load_modules;
363+
uindex_t t_num_load = MCProperListGetLength (p_load_filenames);
364+
365+
if (!t_load_modules.New(t_num_load))
366+
return false;
367+
368+
for (uindex_t i = 0; i < t_num_load; ++i)
369+
{
370+
MCValueRef t_element;
371+
t_element = MCProperListFetchElementAtIndex (p_load_filenames, i);
372+
if (!MCRunLoadModule ((MCStringRef) t_element, t_load_modules[i]))
373+
return false;
374+
}
375+
376+
/* Check main module is usable */
330377
if (!MCScriptEnsureModuleIsUsable (*t_module))
331378
return false;
332379

@@ -394,6 +441,8 @@ main (int argc,
394441
t_config.m_filename = MCValueRetain (kMCEmptyString);
395442
t_config.m_handler = MCValueRetain (MCNAME("main"));
396443
t_config.m_list_handlers = false;
444+
if (!MCProperListCreateMutable (t_config.m_load_filenames))
445+
MCRunStartupError(MCSTR("Initialization"));
397446

398447
/* ---------- Process command-line arguments */
399448
if (!MCRunParseCommandLine (argc, argv, t_config))
@@ -404,8 +453,10 @@ main (int argc,
404453
MCAutoScriptInstanceRef t_instance;
405454
MCAutoValueRef t_ignored_retval;
406455

407-
if (!MCRunLoadModule (t_config.m_filename, &t_module))
408-
MCRunStartupError(MCSTR("Load Module"));
456+
if (!MCRunLoadModules (t_config.m_filename,
457+
t_config.m_load_filenames,
458+
&t_module))
459+
MCRunStartupError (MCSTR("Load Modules"));
409460

410461
if (t_config.m_list_handlers)
411462
{

toolchain/lc-run.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ the value of `the command arguments`.
2525

2626
## OPTIONS
2727

28+
* -l, --load _LCMLIB_:
29+
In addition to loading the _LCMFILE_, load a module from the
30+
bytecode file _LCMLIB_. Any number of `--preload` options may be
31+
provided. _LCMFILE_ is loaded first, followed by the _LCMLIB_s in
32+
reverse order.
33+
2834
* -H, --handler _NAME_:
2935
Call the handler with the specified _NAME_ as the entry point of the program.
3036
_NAME_ must have public visibility and accept no arguments. The default value

0 commit comments

Comments
 (0)