#include "lua_processor.h" #include #include "vfs.h" LuaProcessor::LuaProcessor(void) { _lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::io); /* Expose vfs_node struct members to Lua. */ _lua.new_usertype("vfs_node", "name", &vfs_node::name, "type", &vfs_node::type, "children", &vfs_node::children, "read_only", &vfs_node::read_only); } LuaProcessor::~LuaProcessor(void) {} sol::object LuaProcessor::execute(const std::string& script, vfs_node* current_dir, const std::vector& args) { try { /* Pass C++ objects/points into the Lua env. */ _lua["current_dir"] = current_dir; /* Create and populate the 'arg' table for the script. */ sol::table arg_table = _lua.create_table(); for(size_t i = 0; i < args.size(); ++i) { arg_table[i+1] = args[i]; /* Lua arrays 1-indexed. */ } _lua["arg"] = arg_table; return _lua.script(script); } catch(const sol::error& e) { /* Return the error message as a string in a sol::object. */ return sol::make_object(_lua, e.what()); } }