<commit_before>#include "LuaScript.h"

LuaScript::LuaScript() {
	state = luaL_newstate();
	luaL_openlibs(state);
}

LuaScript::~LuaScript() {
	if (state) {
		lua_close(state);
	}
}

void LuaScript::ExecuteFile(const std::string file) {
	if (luaL_dofile(state, file.c_str())) {
		throw LuaException(lua_tostring(state, -1));
	}
}

template<typename R, typename ...T>
R LuaScript::callFunction(const std::string name, const T... args) {
	lua_getglobal(state, name.c_str());
	applyAllArgs(state, args...);

}

template<typename I, typename ...T>
static void applyAllArgs(lua_State* state, const I item, const T... tail) {
	applyArg(state, item);
	if (sizeof...(tail)) {
		applyAllArgs(state, tail...);
	}
}

static void applyArg(lua_State* state, const int item) { lua_pushnumber(state, item); }
static void applyArg(lua_State *state, const float item) { lua_pushnumber(state, item); }
static void applyArg(lua_State *state, const double item) { lua_pushnumber(state, item); }
static void applyArg(lua_State *state, const std::string item) { lua_pushstring(state, item.c_str()); }
static void applyArg(lua_State *state, const char* item) { lua_pushstring(state, item); }
static void applyArg(lua_State *state, const bool item) { lua_pushboolean(state, item ? 1 : 0); }<commit_msg>Should take care of the return values<commit_after>#include "LuaScript.h"

template<typename T>
static T getResult(lua_State* state);

LuaScript::LuaScript() {
	state = luaL_newstate();
	luaL_openlibs(state);
}

LuaScript::~LuaScript() {
	if (state) {
		lua_close(state);
	}
}

void LuaScript::ExecuteFile(const std::string file) {
	if (luaL_dofile(state, file.c_str())) {
		throw LuaException(lua_tostring(state, -1));
	}
}

template<typename R, typename ...T>
R LuaScript::callFunction(const std::string name, const T... args) {
	lua_getglobal(state, name.c_str());
	int argCount = sizeof...(args);
	applyAllArgs(state, args...);
	lua_pcall(state, argCount, 1, 0);

	R result = getResult<R>(state);
	lua_pop(state, 1);
	return result;
}

template<typename I, typename ...T>
static void applyAllArgs(lua_State* state, const I item, const T... tail) {
	applyArg(state, item);
	if (sizeof...(tail)) {
		applyAllArgs(state, tail...);
	}
}

static void applyArg(lua_State* state, const int item) { lua_pushnumber(state, item); }
static void applyArg(lua_State* state, const float item) { lua_pushnumber(state, item); }
static void applyArg(lua_State* state, const double item) { lua_pushnumber(state, item); }
static void applyArg(lua_State* state, const std::string item) { lua_pushstring(state, item.c_str()); }
static void applyArg(lua_State* state, const char* item) { lua_pushstring(state, item); }
static void applyArg(lua_State* state, const bool item) { lua_pushboolean(state, item ? 1 : 0); }

template<>
int getResult(lua_State* state) {
    return lua_tointeger(state, -1);
}

template<>
float getResult(lua_State* state) {
    return lua_tonumber(state, -1);
}

template<>
double getResult(lua_State* state) {
    return lua_tonumber(state, -1);
}

template<>
const char* getResult(lua_State* state) {
    return lua_tostring(state, -1);
}

template<>
std::string getResult(lua_State* state) {
    return std::string(lua_tostring(state, -1));
}
{'fraction_non_alphanumeric': 0.15077399380804954, 'fraction_numerical': 0.0043343653250774, 'mean_word_length': 5.514112903225806, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7012356', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1135, 'n_words': 284}