51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
|
#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);
|
||
|
}
|
||
|
|
||
|
}
|