Skip to content

Commit

Permalink
temp: tring to fix persist bug #20
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFola committed Aug 29, 2019
1 parent 58c8986 commit 231094a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 49 deletions.
1 change: 1 addition & 0 deletions include/Ark/VM/VM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ namespace Ark

void configure();
void safeRun(std::size_t untilFrameCount=0);
void init();

inline uint16_t readNumber()
{
Expand Down
106 changes: 57 additions & 49 deletions include/Ark/VM/VM.inl
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ void VM_t<debug>::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();
}
}
Expand Down Expand Up @@ -326,6 +330,57 @@ void VM_t<debug>::configure()
}
}

template<bool debug>
void VM_t<debug>::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<std::string, Value::ProcType>;
using map_fun_t = Mapping_t(*) ();
Mapping_t map = m_shared_lib_objects.back().template get<map_fun_t>("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<bool debug>
void VM_t<debug>::loadFunction(const std::string& name, internal::Value::ProcType function)
{
Expand Down Expand Up @@ -378,54 +433,7 @@ void VM_t<debug>::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<std::string, Value::ProcType>;
using map_fun_t = Mapping_t (*) ();
Mapping_t map = m_shared_lib_objects.back().template get<map_fun_t>("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);
Expand All @@ -441,7 +449,7 @@ void VM_t<debug>::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)
{
Expand Down
12 changes: 12 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ark::Value>& 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 };
Expand Down

0 comments on commit 231094a

Please sign in to comment.