40 #ifndef GEOGRAM_LUA_LUA_WRAP
49 #include <geogram/third_party/lua/lua.h>
50 #include <geogram/third_party/lua/lauxlib.h>
51 #include <geogram/third_party/lua/lualib.h>
54 #ifdef GEO_COMPILER_MSVC
55 #pragma warning( push )
56 #pragma warning( disable: 4702 )
85 return lua_isboolean(L,idx);
100 return lua_islightuserdata(L,idx);
113 if(!lua_isinteger(L,idx)) {
116 lua_Integer x = lua_tointeger(L,idx);
117 return (x > 0) ? 1 : 0;
129 lua_pushstring(L, error);
130 lua_setfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
151 lua_setfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
160 lua_getfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
161 bool result = !lua_isnil(L,-1);
180 std::string error = std::string(
"Argument ") +
181 String::to_string(idx) +
": wrong argument type (" +
182 "got " + lua_typename(L,lua_type(L,idx)) +
")";
202 if(lua_gettop(L) != expected_nb_args) {
204 "Expected " + String::to_string(expected_nb_args)
205 +
" arg(s), got " + String::to_string(lua_gettop(L));
227 lua_getfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
228 if(lua_isstring(L,-1)) {
229 error += lua_tostring(L,-1);
233 return luaL_error(L,error.c_str());
297 lua_to(lua_State* L,
int idx) {
298 x_ = int(lua_tointeger(L,idx));
303 operator int()
const {
315 lua_to(lua_State* L,
int idx) {
333 lua_to(lua_State* L,
int idx) {
352 lua_to(lua_State* L,
int idx) {
370 lua_to(lua_State* L,
int idx) {
371 x_ = float(lua_tonumber(L,idx));
376 operator float()
const {
388 lua_to(lua_State* L,
int idx) {
389 x_ = double(lua_tonumber(L,idx));
394 operator double()
const {
406 lua_to(lua_State* L,
int idx) {
407 x_ = (lua_toboolean(L,idx) != 0);
412 operator bool()
const {
424 lua_to(lua_State* L,
int idx) {
425 x_ = lua_tostring(L,idx);
427 static bool can_convert(lua_State* L,
int idx) {
430 operator const char*()
const {
440 template<>
class lua_to<const std::string&> {
442 lua_to(lua_State* L,
int idx) {
443 x_ = lua_tostring(L,idx);
448 operator const std::string&()
const {
460 lua_to(lua_State* L,
int idx) {
461 x_ = lua_tostring(L,idx);
466 operator std::string()
const {
484 template<
class T>
inline void lua_push(lua_State* L, T x) {
493 template<>
inline void lua_push(lua_State* L,
int x) {
494 lua_pushinteger(L,lua_Integer(x));
502 lua_pushinteger(L,lua_Integer(x));
509 lua_pushinteger(L,lua_Integer(x));
516 lua_pushinteger(L,lua_Integer(x));
522 template<>
inline void lua_push(lua_State* L,
float x) {
523 lua_pushnumber(L,lua_Number(x));
529 template<>
inline void lua_push(lua_State* L,
double x) {
530 lua_pushnumber(L,lua_Number(x));
536 template<>
inline void lua_push(lua_State* L,
bool x) {
537 lua_pushboolean(L,x?1:0);
543 template<>
inline void lua_push(lua_State* L,
const char* x) {
550 template<>
inline void lua_push(lua_State* L,
const std::string& x) {
551 lua_pushstring(L,x.c_str());
557 template<>
inline void lua_push(lua_State* L, std::string x) {
558 lua_pushstring(L,x.c_str());
567 lua_State* L,
const std::vector<T>& x
570 for(
size_t i=0; i<x.size(); ++i) {
572 lua_seti(L,-2,lua_Integer(i+1));
586 #define LUA_DECLAREENUMTYPE(T) \
587 template<> inline void lua_push(lua_State* L, T x) { \
588 lua_push(L, int(x)); \
591 template<> class lua_to<T> : public GEO::lua_to<int> { \
593 lua_to(lua_State* L, int idx) : lua_to<int>(L,idx) { \
595 operator T() const { \
596 return T(lua_to<int>::operator int()); \
609 template <
class R>
inline int lua_wrap(lua_State* L, R (*fptr)(
void)) {
626 lua_State* L, R (*fptr)(T1)
648 template <
class R,
class T1,
class T2>
inline int lua_wrap(
649 lua_State* L, R (*fptr)(T1,T2)
673 template <
class R,
class T1,
class T2,
class T3>
inline int lua_wrap(
674 lua_State* L, R (*fptr)(T1,T2,T3)
700 template <
class R,
class T1,
class T2,
class T3,
class T4>
701 inline int lua_wrap(lua_State* L, R (*fptr)(T1,T2,T3,T4)) {
729 template <>
inline int lua_wrap(lua_State* L,
void (*fptr)(
void)) {
743 template <
class T1>
inline int lua_wrap(lua_State* L,
void (*fptr)(T1)) {
763 lua_State* L,
void (*fptr)(T1,T2)
785 template <
class T1,
class T2,
class T3>
786 inline int lua_wrap(lua_State* L,
void (*fptr)(T1,T2,T3)) {
809 template <
class T1,
class T2,
class T3,
class T4>
810 inline int lua_wrap(lua_State* L,
void (*fptr)(T1,T2,T3,T4)) {
836 class T1,
class T2,
class T3,
class T4,
class T5
839 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5)
868 class T1,
class T2,
class T3,
class T4,
class T5,
class T6
871 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6)
902 class T1,
class T2,
class T3,
class T4,
class T5,
906 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7)
940 class T1,
class T2,
class T3,
class T4,
class T5,
941 class T6,
class T7,
class T8
944 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8)
980 class T1,
class T2,
class T3,
class T4,
class T5,
981 class T6,
class T7,
class T8,
class T9
984 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9)
1022 class T1,
class T2,
class T3,
class T4,
class T5,
1023 class T6,
class T7,
class T8,
class T9,
class T10
1026 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)
1066 class T1,
class T2,
class T3,
class T4,
class T5,
1067 class T6,
class T7,
class T8,
class T9,
class T10,
1071 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11)
1112 class T1,
class T2,
class T3,
class T4,
class T5,
1113 class T6,
class T7,
class T8,
class T9,
class T10,
1114 class T11,
class T12
1117 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12)
1161 class T1,
class T2,
class T3,
class T4,
class T5,
1162 class T6,
class T7,
class T8,
class T9,
class T10,
1163 class T11,
class T12,
class T13
1166 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13)
1210 template<>
inline int lua_wrap(lua_State* L, lua_CFunction fptr) {
1238 lua_touserdata(L, lua_upvalueindex(1))
1250 static void push(lua_State* L, FPTR f) {
1251 lua_pushlightuserdata(
1278 lua_pushcfunction(L,f);
1292 lua_State* L, FPTR f,
const std::string& name
1296 lua_pushstring(L,name.c_str());
1309 lua_State* L, FPTR f,
const std::string& name
1312 lua_setglobal(L,name.c_str());
1326 std::string result(functionname);
1327 size_t pos = result.find_last_of(
":");
1328 if(pos != std::string::npos) {
1329 result = result.substr(pos+1, result.length()-pos);
1351 #define lua_bindwrapper(L, f) lua_bindwrapperwithname( \
1352 (L),(f),GEO::lua_wrappername(L,#f) \
1369 #define lua_bindwrapperglobal(L, f) lua_bindwrapperwithnameglobal( \
1370 (L),(f),GEO::lua_wrappername(L,#f) \
1378 #ifdef GEO_COMPILER_MSVC
1379 #pragma warning( pop )
Assertion checking mechanism.
#define geo_assert_not_reached
Sets a non reachable point in the program.
#define geo_assert(x)
Verifies that a condition is met.
Converts LUA variables to C++ variables.
static bool can_convert(lua_State *L, int idx)
Tests whether a LUA variable can be converted to a C++ variable.
lua_to(lua_State *L, int idx)
lua_to constructor.
Manages wrappers around C++ functions to be called from LUA.
static int call(lua_State *L)
Implementation of the wrapper.
static void push(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
Common include file, providing basic definitions. Should be included before anything else by all head...
Types and functions for memory manipulation.
void(* function_pointer)()
Generic function pointer.
pointer function_pointer_to_generic_pointer(function_pointer fptr)
Converts a function pointer to a generic pointer.
function_pointer generic_pointer_to_function_pointer(pointer ptr)
Converts a generic pointer to a function pointer.
Global Vorpaline namespace.
std::string lua_wrappername(lua_State *L, const char *functionname)
Converts a C++ function name into a LUA function name.
int my_lua_isboolean(lua_State *L, int idx)
Tests whether a LUA variable is a boolean.
void lua_bindwrapperwithnameglobal(lua_State *L, FPTR f, const std::string &name)
Binds a wrapper to a name in the global scole.
bool lua_check_type(lua_State *L, int idx, lua_test_func test)
Tests whether a LUA variable has the correct type.
void lua_set_error(lua_State *L, const char *error)
Memorizes an error message in LUA registry.
void lua_bindwrapperwithname(lua_State *L, FPTR f, const std::string &name)
Binds a wrapper to a name in the table at the top of the LUA stack.
void lua_clear_error(lua_State *L)
Clears the last error message in LUA registry.
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
int my_lua_islightuserdata(lua_State *L, int idx)
Tests whether a LUA variable is a light user data.
void lua_push(lua_State *L, T x)
Converts and pushes a C++ variable onto the LUA stack.
bool lua_check_nb_args(lua_State *L, int expected_nb_args)
Tests whether the expected number of arguments was pushed onto the stack.
int my_lua_ispositiveinteger(lua_State *L, int idx)
Tests whether a LUA variable is a positive integer.
bool lua_has_error(lua_State *L)
Tests whether an error message was memorized in the registry.
int lua_notify_last_error(lua_State *L)
Takes the last error message memorized in the registry and sends it back to LUA.
int(* lua_test_func)(lua_State *L, int idx)
A pointer to a LUA function to test an argument in the LUA stack.
void lua_pushwrapper(lua_State *L, FPTR f)
Pushes a wrapper for a given C++ function onto the LUA stack.
int lua_wrap(lua_State *L, R(*fptr)(void))
Calls a C++ function from LUA.
Types and functions for numbers manipulation.
Functions for string manipulation.