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

51 lines
1.2 KiB
C++
Raw Normal View History

2023-09-09 22:03:28 +00:00
#include "Loader.hpp"
#include "lib/Function.hpp"
#include "lib/config.in.hpp"
#include <dlfcn.h>
namespace jk
{
/*explicit*/ Loader::Loader(std::shared_ptr<VM> vm,
std::shared_ptr<SymTable> sym)
: m_vm { vm }
, 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,
fun_val->type().lock(),
Loc {"global", 1, 1});
m_vm->set_global(global_addr, ref);
}
}