refactor lua libraries

This commit is contained in:
MihailRis
2024-07-22 18:14:54 +03:00
parent cfe1f1ad28
commit b5e7b63a9d
3 changed files with 42 additions and 53 deletions
+11 -29
View File
@@ -6,10 +6,7 @@
template<int n, template<class> class Op>
static int l_binop(lua::State* L) {
uint argc = lua::gettop(L);
if (argc != 2 && argc != 3) {
throw std::runtime_error("invalid arguments number (2 or 3 expected)");
}
uint argc = lua::check_argc(L, 2, 3);
auto a = lua::tovec<n>(L, 1);
if (lua::isnumber(L, 2)) { // scalar second operand overload
@@ -43,7 +40,7 @@ static int l_binop(lua::State* L) {
template<int n, glm::vec<n, float>(*func)(const glm::vec<n, float>&)>
static int l_unaryop(lua::State* L) {
uint argc = lua::gettop(L);
uint argc = lua::check_argc(L, 1, 2);
auto vec = func(lua::tovec<n>(L, 1));
switch (argc) {
case 1:
@@ -55,27 +52,20 @@ static int l_unaryop(lua::State* L) {
return 1;
case 2:
return lua::setvec(L, 2, vec);
default: {
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
}
}
return 0;
}
template<int n, float(*func)(const glm::vec<n, float>&)>
static int l_scalar_op(lua::State* L) {
lua::check_argc(L, 1);
auto vec = lua::tovec<n>(L, 1);
if (lua::gettop(L) != 1) {
throw std::runtime_error("invalid arguments number (1 expected)");
}
return lua::pushnumber(L, func(vec));
}
template<int n>
static int l_pow(lua::State* L) {
uint argc = lua::gettop(L);
if (argc != 2 && argc != 3) {
throw std::runtime_error("invalid arguments number (2 or 3 expected)");
}
uint argc = lua::check_argc(L, 2, 3);
auto a = lua::tovec<n>(L, 1);
if (lua::isnumber(L, 2)) {
@@ -107,10 +97,7 @@ static int l_pow(lua::State* L) {
template<int n>
static int l_dot(lua::State* L) {
uint argc = lua::gettop(L);
if (argc != 1) {
throw std::runtime_error("invalid arguments number (1 expected)");
}
lua::check_argc(L, 2);
const auto& a = lua::tovec<n>(L, 1);
const auto& b = lua::tovec<n>(L, 2);
return lua::pushnumber(L, glm::dot(a, b));
@@ -118,7 +105,7 @@ static int l_dot(lua::State* L) {
template<int n>
static int l_inverse(lua::State* L) {
uint argc = lua::gettop(L);
uint argc = lua::check_argc(L, 1, 2);
auto vec = lua::tovec<n>(L, 1);
switch (argc) {
case 1:
@@ -130,31 +117,26 @@ static int l_inverse(lua::State* L) {
return 1;
case 2:
return lua::setvec(L, 2, -vec);
default: {
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
}
}
return 0;
}
static int l_spherical_rand(lua::State* L) {
int argc = lua::gettop(L);
uint argc = lua::check_argc(L, 1, 2);
switch (argc) {
case 1:
return lua::pushvec3(L, glm::sphericalRand(lua::tonumber(L, 1)));
case 2:
return lua::setvec(L, 2,
glm::sphericalRand(static_cast<float>(lua::tonumber(L, 1))));
default:
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
}
return 0;
}
template<int n>
static int l_tostring(lua::State* L) {
lua::check_argc(L, 1);
auto vec = lua::tovec<n>(L, 1);
if (lua::gettop(L) != 1) {
throw std::runtime_error("invalid arguments number (1 expected)");
}
std::stringstream ss;
ss << "vec" << std::to_string(n) << "{";
for (int i = 0; i < n; i++) {