#include "Loader.hpp" #include "lib/Function.hpp" #include "lib/StaticFunction.hpp" #include "lib/config.in.hpp" #include namespace jk { /*explicit*/ Loader::Loader(std::shared_ptr vm, std::shared_ptr static_pass, std::shared_ptr compiler, std::shared_ptr sym) : m_vm { vm } , m_static_pass { static_pass } , m_compiler { compiler } , m_sym { sym } { } /*virtual*/ Loader::~Loader() { } void Loader::load() { for (auto entry: std::filesystem::directory_iterator(JOKO_LIBDIR)) { if (entry.path().extension() == ".so") { void* handle = dlopen(entry.path().string().c_str(), RTLD_LAZY); assert(handle); typedef void(*func)(Loader&); func f = (func) dlsym(handle, "lib"); assert(f); f(*this); } } } void Loader::declare(std::string const& name, foreign_t body) { auto fun = std::make_shared(body); auto fun_val = Value::make_function(fun); size_t addr = m_vm->push_heap(fun_val); auto ref = Value::make_ref(addr); size_t global_addr = m_sym->declare_global(name, fun_val->type().lock(), Loc {"global", 1, 1}); m_vm->set_global(global_addr, ref); } void Loader::declare_static(std::string const& name, static_fun_t body) { m_static_pass->add_static(name); auto fun = std::make_shared(body); m_compiler->declare_static(name, fun); } }