Skip to content

Commit

Permalink
feat(module-loading): Consider case where .o exists without source
Browse files Browse the repository at this point in the history
  • Loading branch information
Samy-33 committed Dec 25, 2024
1 parent f7ad7ae commit 11c03c4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
19 changes: 5 additions & 14 deletions compiler+runtime/include/cpp/jank/runtime/module/loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace jank::runtime::module
struct file_entry
{
object_ptr to_runtime_data() const;
native_bool exists() const;
std::time_t last_modified_at() const;

/* If the file is within a JAR, this will be the path to the JAR. */
option<native_persistent_string> archive_path;
Expand Down Expand Up @@ -78,20 +80,10 @@ namespace jank::runtime::module

struct find_result
{
/*
* All the sources for a module
*/
/* All the sources for a module */
entry sources;
/*
* On the basis of origin, source that should be loaded.
*/
/* On the basis of origin, source that should be loaded. */
option<module_type> to_load;

find_result(entry const &sources, module_type const to_load)
: sources{ sources }
, to_load{ to_load }
{
}
};

/* These separators match what the JVM does on each system. */
Expand All @@ -106,8 +98,7 @@ namespace jank::runtime::module
native_bool is_loaded(native_persistent_string_view const &) const;
void set_loaded(native_persistent_string_view const &);

string_result<find_result>
find(native_persistent_string_view const &module, origin const ori);
string_result<find_result> find(native_persistent_string_view const &module, origin const ori);
string_result<void> load(native_persistent_string_view const &module, origin const ori);

string_result<void>
Expand Down
76 changes: 51 additions & 25 deletions compiler+runtime/src/cpp/jank/runtime/module/loader.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <boost/filesystem/operations.hpp>
#include <regex>
#include <iostream>

Expand Down Expand Up @@ -139,7 +140,7 @@ namespace jank::runtime::module
}

auto const &zip_entry(zf.getEntry(std::string{ entry.path }));
fn(zip_entry.readAsText());
fn(zip_entry);

Check warning on line 143 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L143

Added line #L143 was not covered by tests
}

static void register_entry(native_unordered_map<native_persistent_string, loader::entry> &entries,
Expand Down Expand Up @@ -317,6 +318,31 @@ namespace jank::runtime::module
make_box(path));
}

native_bool file_entry::exists() const
{
auto const is_archive{ archive_path.is_some() };
if(is_archive && !boost::filesystem::exists(native_transient_string{ archive_path.unwrap() }))
{
return false;

Check warning on line 326 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L322-L326

Added lines #L322 - L326 were not covered by tests
}
else
{
native_bool source_exists{};
if(is_archive)
{
visit_jar_entry(*this, [&](auto const &zip_entry) { source_exists = zip_entry.isFile(); });

Check warning on line 333 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L328-L333

Added lines #L328 - L333 were not covered by tests
}

return source_exists || boost::filesystem::exists(native_transient_string{ path });

Check warning on line 336 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L336

Added line #L336 was not covered by tests
}
}

std::time_t file_entry::last_modified_at() const
{
auto const source_path{ archive_path.unwrap_or(path) };
return boost::filesystem::last_write_time(native_transient_string{ source_path });

Check warning on line 343 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L341-L343

Added lines #L341 - L343 were not covered by tests
}

native_bool loader::is_loaded(native_persistent_string_view const &module) const
{
return loaded.contains(module);
Expand All @@ -339,66 +365,63 @@ namespace jank::runtime::module
return err(fmt::format("unable to find module: {}", module));
}

auto const with_module_type{ [&](module_type const type) {
loader::find_result res{ entry->second, type };
return res;
} };

//fmt::println("loading nested module {}", module);

if(ori == origin::source)
{
if(entry->second.jank.is_some())
{
return with_module_type(module_type::jank);
return find_result{ entry->second, module_type::jank };
}
else if(entry->second.cljc.is_some())
{
return with_module_type(module_type::cljc);
return find_result{ entry->second, module_type::cljc };

Check warning on line 376 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L376

Added line #L376 was not covered by tests
}
}
else
{
if(entry->second.o.is_some()
if(entry->second.o.is_some() && entry->second.o.unwrap().archive_path.is_none()
&& (entry->second.jank.is_some() || entry->second.cljc.is_some()))

Check warning on line 382 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L381-L382

Added lines #L381 - L382 were not covered by tests
{
auto const o_file_path{ native_transient_string{ entry->second.o.unwrap().path } };

Check warning on line 384 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L384

Added line #L384 was not covered by tests

native_transient_string source_path{};
std::time_t source_modified_time{};
module_type module_type{};

Check warning on line 387 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L386-L387

Added lines #L386 - L387 were not covered by tests

if(entry->second.jank.is_some())
if(entry->second.jank.is_some() && entry->second.jank.unwrap().exists())
{
source_path = entry->second.jank.unwrap().path;
source_modified_time = entry->second.jank.unwrap().last_modified_at();
module_type = module_type::jank;

Check warning on line 392 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L389-L392

Added lines #L389 - L392 were not covered by tests
}
else
else if(entry->second.cljc.is_some() && entry->second.cljc.unwrap().exists())
{
source_path = entry->second.cljc.unwrap().path;
source_modified_time = entry->second.cljc.unwrap().last_modified_at();
module_type = module_type::cljc;

Check warning on line 397 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L394-L397

Added lines #L394 - L397 were not covered by tests
}
else
{
return err(
fmt::format("Found a binary ({}), without a source", entry->second.o.unwrap().path));

Check warning on line 402 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L399-L402

Added lines #L399 - L402 were not covered by tests
}

if(boost::filesystem::last_write_time(o_file_path)
>= boost::filesystem::last_write_time(source_path))
if(boost::filesystem::last_write_time(o_file_path) >= source_modified_time)
{
return with_module_type(module_type::o);
return find_result{ entry->second, module_type::o };

Check warning on line 407 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L405-L407

Added lines #L405 - L407 were not covered by tests
}
else
{
return with_module_type(module_type);
return find_result{ entry->second, module_type };

Check warning on line 411 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L409-L411

Added lines #L409 - L411 were not covered by tests
}
}
else if(entry->second.cpp.is_some())
{
return with_module_type(module_type::cpp);
return find_result{ entry->second, module_type::cpp };

Check warning on line 416 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L416

Added line #L416 was not covered by tests
}
else if(entry->second.jank.is_some())
{
return with_module_type(module_type::jank);
return find_result{ entry->second, module_type::jank };

Check warning on line 420 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L420

Added line #L420 was not covered by tests
}
else if(entry->second.cljc.is_some())
{
return with_module_type(module_type::cljc);
return find_result{ entry->second, module_type::cljc };

Check warning on line 424 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L424

Added line #L424 was not covered by tests
}
}

Expand Down Expand Up @@ -473,7 +496,9 @@ namespace jank::runtime::module
{
if(entry.archive_path.is_some())
{
visit_jar_entry(entry, [&](auto const &str) { rt_ctx.jit_prc.eval_string(str); });
visit_jar_entry(entry, [&](auto const &zip_entry) {
rt_ctx.jit_prc.eval_string(zip_entry.readAsText());
});

Check warning on line 501 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L499-L501

Added lines #L499 - L501 were not covered by tests
}
else
{
Expand All @@ -493,7 +518,8 @@ namespace jank::runtime::module
{
if(entry.archive_path.is_some())
{
visit_jar_entry(entry, [&](auto const &str) { rt_ctx.eval_string(str); });
visit_jar_entry(entry,
[&](auto const &zip_entry) { rt_ctx.eval_string(zip_entry.readAsText()); });

Check warning on line 522 in compiler+runtime/src/cpp/jank/runtime/module/loader.cpp

View check run for this annotation

Codecov / codecov/patch

compiler+runtime/src/cpp/jank/runtime/module/loader.cpp#L521-L522

Added lines #L521 - L522 were not covered by tests
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion compiler+runtime/src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace jank
using namespace jank;
using namespace jank::runtime;

__rt_ctx->load_module("/clojure.core", module::origin::latest).expect_ok();
//__rt_ctx->load_module("/clojure.core", module::origin::latest).expect_ok();
__rt_ctx->compile_module(opts.target_ns).expect_ok();
}

Expand Down

0 comments on commit 11c03c4

Please sign in to comment.