2 #include <mof/script/LuaInterpreter.hpp>
3 #include <mof/script/lua_state.hpp>
5 #include "luabind/luabind.hpp"
6 #include <mof/ConsoleIO.hpp>
7 #include <mof/utilities.hpp>
8 #include <boost/bind.hpp>
16 struct LuaInterpreter::Impl
18 std::vector<lua_State*> threads_;
31 LuaInterpreter::LuaInterpreter(const mof::tstring& filename)
35 int error = luaL_dofile(lua_state::instance().raw_lua(), filename.c_str());
38 throw std::runtime_error(lua_tostring(lua_state::instance().raw_lua(), -1));
45 LuaInterpreter::~LuaInterpreter()
50 void LuaInterpreter::update()
52 foreach (auto& co, impl_->threads_) {
53 if (!co) continue;// スレッドは既に終了している
55 int error = lua_resume(co, 0);
56 if (!error) co = NULL;
57 else if (error != LUA_YIELD) throw std::logic_error(std::string("lua error:") + lua_tostring(co, -1));
62 void LuaInterpreter::start(const mof::tstring& entry_function)
64 lua_State* co = lua_newthread(lua_state::instance().raw_lua());
67 DEBUG_PRINT(_T("ERROR-Failed lua_newthread") );
68 throw std::runtime_error("Failed --- start lua script");
70 lua_getglobal(co, entry_function.c_str());
71 impl_->threads_.push_back(co);
76 void LuaInterpreter::bind(std::shared_ptr<CommandSet> commands)
78 lua_state::instance().bind(commands);