Skip to content

Commit

Permalink
Make module/1 test for existence, add module/2 to force module creati…
Browse files Browse the repository at this point in the history
…on, re issue #582
  • Loading branch information
infradig committed Aug 31, 2024
1 parent a97bf4f commit 2c0d9b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ Non-standard predicates
module_info/2 # module_info(+atom, -list)

module/1 # module(?atom)
module/2 # module(+atom,force)
modules/1 # modules(-list)

listing/0
Expand Down
26 changes: 23 additions & 3 deletions src/bif_predicates.c
Original file line number Diff line number Diff line change
Expand Up @@ -5718,11 +5718,30 @@ static bool bif_module_1(query *q)
const char *name = C_STR(q, p1);
module *m = find_module(q->pl, name);

if (!m)
return 0;

q->st.m = m;
return true;
}

static bool bif_module_2(query *q)
{
GET_FIRST_ARG(p1,atom);
GET_NEXT_ARG(p2,atom);
const char *name = C_STR(q, p1);
module *m = find_module(q->pl, name);
const char *s = C_STR(q, p2);
bool force = !strcmp(s, "force");

if (!m) {
if (q->p->command)
fprintf(stdout, "Info: created module '%s'\n", name);
if (force) {
if (q->p->command)
fprintf(stdout, "Info: created module '%s'\n", name);

m = module_create(q->pl, name);
m = module_create(q->pl, name);
} else
return 0;
}

q->st.m = m;
Expand Down Expand Up @@ -6546,6 +6565,7 @@ builtins g_other_bifs[] =
{"prolog_load_context", 2, bif_prolog_load_context_2, "+atom,?term", false, false, BLAH},
{"strip_module", 3, bif_strip_module_3, "+callable,?atom,?callable", false, false, BLAH},
{"module", 1, bif_module_1, "?atom", false, false, BLAH},
{"module", 2, bif_module_2, "+atom,+atom", false, false, BLAH},
{"modules", 1, bif_modules_1, "-list", false, false, BLAH},
{"using", 0, bif_using_0, NULL, false, false, BLAH},
{"use_module", 1, bif_use_module_1, "+term", false, false, BLAH},
Expand Down

0 comments on commit 2c0d9b0

Please sign in to comment.