Skip to content

Commit

Permalink
Merge remote-tracking branch 'saket/run-main-cmd' into binding
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaye committed Jan 17, 2024
2 parents 04f64f7 + abf4f44 commit e2836c8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/cpp/jank/runtime/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#include <folly/Synchronized.h>

#include <jank/result.hpp>
#include <jank/util/cli.hpp>
#include <jank/analyze/processor.hpp>
#include <jank/runtime/module/loader.hpp>
#include <jank/runtime/ns.hpp>
#include <jank/runtime/var.hpp>
#include <jank/runtime/obj/keyword.hpp>
#include <jank/jit/processor.hpp>
#include <jank/util/cli.hpp>

namespace jank::jit
{
Expand Down
11 changes: 10 additions & 1 deletion include/cpp/jank/util/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace jank::util::cli
{
run,
compile,
repl
repl,
run_main
};

struct options
Expand Down Expand Up @@ -37,6 +38,14 @@ namespace jank::util::cli
/* REPL command. */
native_bool repl_server{};

/* Run main command. */
native_transient_string target_module;

/* Extras.
* TODO: Use a native_persistent_vector instead.
* */
std::vector<native_transient_string> extra_opts;

command command{ command::repl };
};

Expand Down
16 changes: 16 additions & 0 deletions src/cpp/jank/util/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ namespace jank::util::cli
cli_repl.fallthrough();
cli_repl.add_flag("--server", opts.repl_server, "Start an nREPL server");

/* Run subcommand. */
auto &cli_run_main(*cli.add_subcommand("run-main", "Load and execute -main"));
cli_run_main.fallthrough();
cli_run_main.add_option("module", opts.target_module, "The entrypoint module")->required();

cli.require_subcommand(1);
cli.failure_message(CLI::FailureMessage::help);
cli.allow_extras();

try
{
cli.parse(argc, argv);
Expand All @@ -59,6 +66,11 @@ namespace jank::util::cli
return err(cli.exit(e));
}

if(cli.remaining_size() >= 0)
{
opts.extra_opts = cli.remaining();
}

if(cli.got_subcommand(&cli_run))
{
opts.command = command::run;
Expand All @@ -71,6 +83,10 @@ namespace jank::util::cli
{
opts.command = command::repl;
}
else if(cli.got_subcommand(&cli_run_main))
{
opts.command = command::run_main;
}

return ok(opts);
}
Expand Down
34 changes: 34 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ namespace jank
//);
}

void run_main(util::cli::options const &opts, runtime::context &rt_ctx)
{
{
profile::timer timer{ "require clojure.core" };
rt_ctx.load_module("/clojure.core").expect_ok();
}

{
profile::timer timer{ "eval user code" };
rt_ctx.load_module("/" + opts.target_module).expect_ok();

auto const main_var(rt_ctx.find_var(opts.target_module, "-main").unwrap_or(nullptr);
if(main_var)
{
/* TODO: Handle the case when `-main` accepts no arg. */
runtime::detail::native_transient_vector extra_args;
for(auto const &s : opts.extra_opts)
{
extra_args.push_back(make_box<runtime::obj::string>(s));
}
runtime::apply_to(main_var->deref(),
make_box<runtime::obj::vector>(extra_args.persistent()));
}
else
{
throw std::runtime_error{ fmt::format("Could not find #'{}/-main function!",
opts.target_module) };
}
}
}

void compile(util::cli::options const &opts, runtime::context &rt_ctx)
{
//rt_ctx.load_module("/clojure.core").expect_ok();
Expand Down Expand Up @@ -157,6 +188,9 @@ try
case util::cli::command::repl:
repl(opts, rt_ctx);
break;
case util::cli::command::run_main:
run_main(opts, rt_ctx);
break;
}
}
/* TODO: Unify error handling. JEEZE! */
Expand Down

0 comments on commit e2836c8

Please sign in to comment.