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 834dc16
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 39 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);
}

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;
}
else
{
native_bool source_exists{};
if(is_archive)
{
visit_jar_entry(*this, [&](auto const &zip_entry) { source_exists = zip_entry.isFile(); });
}

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

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 });
}

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 };
}
}
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()))
{
auto const o_file_path{ native_transient_string{ entry->second.o.unwrap().path } };

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

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;
}
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;
}
else
{
return err(
fmt::format("Found a binary ({}), without a source", entry->second.o.unwrap().path));
}

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 };
}
else
{
return with_module_type(module_type);
return find_result{ entry->second, module_type };
}
}
else if(entry->second.cpp.is_some())
{
return with_module_type(module_type::cpp);
return find_result{ entry->second, module_type::cpp };
}
else 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 };
}
}

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());
});
}
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()); });
}
else
{
Expand Down

0 comments on commit 834dc16

Please sign in to comment.