refactor lua libraries
This commit is contained in:
parent
cfe1f1ad28
commit
b5e7b63a9d
@ -3,6 +3,7 @@
|
||||
|
||||
#include "lua_util.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <exception>
|
||||
|
||||
/// Definitions can be found in local .cpp files
|
||||
@ -45,6 +46,29 @@ extern const luaL_Reg transformlib [];
|
||||
// Lua Overrides
|
||||
extern int l_print(lua::State* L);
|
||||
|
||||
namespace lua {
|
||||
inline uint check_argc(lua::State* L, int a) {
|
||||
int argc = lua::gettop(L);
|
||||
if (argc == a) {
|
||||
return static_cast<uint>(argc);
|
||||
} else {
|
||||
throw std::runtime_error(
|
||||
"invalid number of arguments (" + std::to_string(a) +
|
||||
" expected)");
|
||||
}
|
||||
}
|
||||
inline uint check_argc(lua::State* L, int a, int b) {
|
||||
int argc = lua::gettop(L);
|
||||
if (argc == a || argc == b) {
|
||||
return static_cast<uint>(argc);
|
||||
} else {
|
||||
throw std::runtime_error(
|
||||
"invalid number of arguments (" + std::to_string(a) + " or " +
|
||||
std::to_string(b) + " expected)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initialize_libs_extends(lua::State* L);
|
||||
|
||||
#endif // LOGIC_SCRIPTING_API_LUA_HPP_
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
/// mat4.idt() -> float[16] - creates identity matrix
|
||||
/// mat4.idt(dst: float[16]) -> float[16] - sets dst to identity matrix
|
||||
static int l_idt(lua::State* L) {
|
||||
uint argc = lua::gettop(L);
|
||||
uint argc = lua::check_argc(L, 0, 1);
|
||||
switch (argc) {
|
||||
case 0: {
|
||||
return lua::pushmat4(L, glm::mat4(1.0f));
|
||||
@ -19,9 +19,6 @@ static int l_idt(lua::State* L) {
|
||||
case 1: {
|
||||
return lua::setmat4(L, 1, glm::mat4(1.0f));
|
||||
}
|
||||
default: {
|
||||
throw std::runtime_error("invalid arguments number (0 or 1 expected)");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -40,10 +37,7 @@ static int l_determinant(lua::State* L) {
|
||||
/// mat4.mul(m1: float[16], v: float[3 or 4]) -> float[3 or 4] - creates vector of m1 and v multiplication result
|
||||
/// mat4.mul(m1: float[16], v: float[3 or 4], dst: float[3 or 4]) -> float[3 or 4] - updates dst vector with m1 and v multiplication result
|
||||
static int l_mul(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 matrix1 = lua::tomat4(L, 1);
|
||||
uint len2 = lua::objlen(L, 2);
|
||||
if (len2 < 3) {
|
||||
@ -134,7 +128,7 @@ inline int l_rotate(lua::State* L) {
|
||||
/// mat4.inverse(matrix: float[16]) -> float[16] - creates inversed version of the matrix
|
||||
/// mat4.inverse(matrix: float[16], dst: float[16]) -> float[16] - updates dst matrix with inversed version of the matrix
|
||||
static int l_inverse(lua::State* L) {
|
||||
uint argc = lua::gettop(L);
|
||||
uint argc = lua::check_argc(L, 1, 2);
|
||||
auto matrix = lua::tomat4(L, 1);
|
||||
switch (argc) {
|
||||
case 1: {
|
||||
@ -143,17 +137,15 @@ static int l_inverse(lua::State* L) {
|
||||
case 2: {
|
||||
return lua::setmat4(L, 2, glm::inverse(matrix));
|
||||
}
|
||||
default: {
|
||||
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// Overloads:
|
||||
/// mat4.transpose(matrix: float[16]) -> float[16] - creates transposed version of the matrix
|
||||
/// mat4.transpose(matrix: float[16], dst: float[16]) -> float[16] - updates dst matrix with transposed version of the matrix
|
||||
static int l_transpose(lua::State* L) {
|
||||
uint argc = lua::gettop(L);
|
||||
uint argc = lua::check_argc(L, 1, 2);
|
||||
auto matrix = lua::tomat4(L, 1);
|
||||
switch (argc) {
|
||||
case 1: {
|
||||
@ -162,9 +154,6 @@ static int l_transpose(lua::State* L) {
|
||||
case 2: {
|
||||
return lua::setmat4(L, 2, glm::transpose(matrix));
|
||||
}
|
||||
default: {
|
||||
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -216,10 +205,7 @@ static int l_decompose(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_look_at(lua::State* L) {
|
||||
int argc = lua::gettop(L);
|
||||
if (argc != 3 && argc != 4) {
|
||||
throw std::runtime_error("invalid arguments number (3 or 4 expected)");
|
||||
}
|
||||
uint argc = lua::check_argc(L, 3, 4);
|
||||
auto eye = lua::tovec<3>(L, 1);
|
||||
auto center = lua::tovec<3>(L, 2);
|
||||
auto up = lua::tovec<3>(L, 3);
|
||||
@ -232,10 +218,7 @@ static int l_look_at(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_from_quat(lua::State* L) {
|
||||
uint argc = lua::gettop(L);
|
||||
if (argc != 1 && argc != 2) {
|
||||
throw std::runtime_error("invalid arguments number (1 or 2 expected)");
|
||||
}
|
||||
uint argc = lua::check_argc(L, 1, 2);
|
||||
auto quat = lua::toquat(L, 1);
|
||||
switch (argc) {
|
||||
case 1:
|
||||
|
||||
@ -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++) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user