/*
    PIP - Platform Independent Primitives
    PILuaProgram
    Andrey Bychkov work.a.b@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "piluaprogram.h"


PRIVATE_DEFINITION_START(PILuaProgram)
	lua_State * lua_state;
PRIVATE_DEFINITION_END(PILuaProgram)


PILuaProgram::PILuaProgram() {
	PRIVATE->lua_state = luaL_newstate();
	luaL_openlibs(PRIVATE->lua_state);
}


PILuaProgram::~PILuaProgram() {
	lua_close(PRIVATE->lua_state);
}


bool PILuaProgram::load(const PIString & script) {
	int ret = luaL_dostring(PRIVATE->lua_state, script.dataUTF8());
	if (ret != 0) return false;
	return true;
}


bool PILuaProgram::prepare() {
	return (lua_pcall(PRIVATE->lua_state, 0, 0, 0) == 0);
}


luabridge::LuaRef PILuaProgram::getGlobal(const PIString & name) {
	return luabridge::getGlobal(PRIVATE->lua_state, name.dataUTF8());
}


luabridge::Namespace PILuaProgram::getGlobalNamespace() {
	return luabridge::getGlobalNamespace(PRIVATE->lua_state);
}
