diff --git a/include/Ark/VM/VM.hpp b/include/Ark/VM/VM.hpp index 33688769..4b14dba9 100644 --- a/include/Ark/VM/VM.hpp +++ b/include/Ark/VM/VM.hpp @@ -109,6 +109,7 @@ namespace Ark void configure(); void safeRun(std::size_t untilFrameCount=0); + void init(); inline uint16_t readNumber() { diff --git a/include/Ark/VM/VM.inl b/include/Ark/VM/VM.inl index 53766ef7..6bf95b97 100644 --- a/include/Ark/VM/VM.inl +++ b/include/Ark/VM/VM.inl @@ -118,12 +118,16 @@ void VM_t::doFile(const std::string& file) if (compiled_successfuly) { feed(path); + if (m_persist) + init(); run(); } } else // it's a bytecode file, run it { feed(file); + if (m_persist) + init(); run(); } } @@ -326,6 +330,57 @@ void VM_t::configure() } } +template +void VM_t::init() +{ + m_frames.clear(); + m_frames.emplace_back(); + + m_saved_scope.reset(); + + m_locals.clear(); + createNewScope(); + + // loading plugins + for (const auto& file : m_plugins) + { + namespace fs = std::filesystem; + + std::string path = "./" + file; + if (m_filename != "FILE") // bytecode loaded from file + path = "./" + (fs::path(m_filename).parent_path() / fs::path(file)).string(); + std::string lib_path = (fs::path(m_libdir) / fs::path(file)).string(); + + if constexpr (debug) + Ark::logger.info("Loading", file, "in", path, "or in", lib_path); + + if (Ark::Utils::fileExists(path)) // if it exists alongside the .arkc file + m_shared_lib_objects.emplace_back(path); + else if (Ark::Utils::fileExists(lib_path)) // check in LOAD_PATH otherwise + m_shared_lib_objects.emplace_back(lib_path); + else + throwVMError("could not load plugin " + file); + + // load data from it! + using Mapping_t = std::unordered_map; + using map_fun_t = Mapping_t(*) (); + Mapping_t map = m_shared_lib_objects.back().template get("getFunctionsMapping")(); + + for (auto&& kv : map) + { + // put it in the global frame, aka the first one + auto it = std::find(m_symbols.begin(), m_symbols.end(), kv.first); + if (it != m_symbols.end()) + { + if constexpr (debug) + Ark::logger.info("Loading", kv.first); + + registerVariable<0>(std::distance(m_symbols.begin(), it), Value(kv.second)); + } + } + } +} + template void VM_t::loadFunction(const std::string& name, internal::Value::ProcType function) { @@ -378,54 +433,7 @@ void VM_t::run() m_pp = 0; if (!m_persist) - { - m_frames.clear(); - m_frames.emplace_back(); - - m_saved_scope.reset(); - - m_locals.clear(); - createNewScope(); - - // loading plugins - for (const auto& file: m_plugins) - { - namespace fs = std::filesystem; - - std::string path = "./" + file; - if (m_filename != "FILE") // bytecode loaded from file - path = "./" + (fs::path(m_filename).parent_path() / fs::path(file)).string(); - std::string lib_path = (fs::path(m_libdir) / fs::path(file)).string(); - - if constexpr (debug) - Ark::logger.info("Loading", file, "in", path, "or in", lib_path); - - if (Ark::Utils::fileExists(path)) // if it exists alongside the .arkc file - m_shared_lib_objects.emplace_back(path); - else if (Ark::Utils::fileExists(lib_path)) // check in LOAD_PATH otherwise - m_shared_lib_objects.emplace_back(lib_path); - else - throwVMError("could not load plugin " + file); - - // load data from it! - using Mapping_t = std::unordered_map; - using map_fun_t = Mapping_t (*) (); - Mapping_t map = m_shared_lib_objects.back().template get("getFunctionsMapping")(); - - for (auto&& kv : map) - { - // put it in the global frame, aka the first one - auto it = std::find(m_symbols.begin(), m_symbols.end(), kv.first); - if (it != m_symbols.end()) - { - if constexpr (debug) - Ark::logger.info("Loading", kv.first); - - registerVariable<0>(std::distance(m_symbols.begin(), it), Value(kv.second)); - } - } - } - } + init(); if constexpr (debug) Ark::logger.info("Starting at PP:{0}, IP:{1}"s, m_pp, m_ip); @@ -441,7 +449,7 @@ void VM_t::safeRun(std::size_t untilFrameCount) try { m_running = true; - while (m_running) + while (m_running && m_frames.size() > m_until_frame_count) { if constexpr (debug) { diff --git a/src/main.cpp b/src/main.cpp index 8c318c1d..c9543b9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,18 @@ void bcr(const std::string& file) int main(int argc, char** argv) { + Ark::VM vm(true); + vm.loadFunction("myscene_coucou", [](const std::vector& s) { + for (auto&& u : s) + std::cout << "ark coucou: " << u << std::endl; + return Ark::Nil; + }); + vm.doFile("C:/Users/Folaefolc/Documents/Code/engine/assets/scripts/test.ark"); + vm.call("onStateChange", std::string("running")); + return 0; + + + using namespace clipp; enum class mode { help, dev_info, bytecode_reader, version, run };