This repository has been archived on 2023-09-10. You can view files and clone it, but cannot push or open issues/pull-requests.
joko/lib/Loader.cpp

62 lines
1.7 KiB
C++
Raw Permalink Normal View History

2023-09-09 22:03:28 +00:00
#include "Loader.hpp"
#include "lib/Function.hpp"
2023-09-10 14:16:20 +00:00
#include "lib/StaticFunction.hpp"
2023-09-09 22:03:28 +00:00
#include "lib/config.in.hpp"
#include <dlfcn.h>
namespace jk
{
/*explicit*/ Loader::Loader(std::shared_ptr<VM> vm,
2023-09-10 14:16:20 +00:00
std::shared_ptr<StaticPass> static_pass,
std::shared_ptr<Compiler> compiler,
2023-09-09 22:03:28 +00:00
std::shared_ptr<SymTable> sym)
: m_vm { vm }
2023-09-10 14:16:20 +00:00
, m_static_pass { static_pass }
, m_compiler { compiler }
2023-09-09 22:03:28 +00:00
, 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<Function>(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,
2023-09-10 14:16:20 +00:00
fun_val->type().lock(),
Loc {"global", 1, 1});
2023-09-09 22:03:28 +00:00
m_vm->set_global(global_addr, ref);
}
2023-09-10 14:16:20 +00:00
void Loader::declare_static(std::string const& name, static_fun_t body)
{
m_static_pass->add_static(name);
auto fun = std::make_shared<StaticFunction>(body);
m_compiler->declare_static(name, fun);
}
2023-09-09 22:03:28 +00:00
}