40#ifndef GEOGRAM_LUA_LUA_WRAP
43#include <geogram/lua/lua.h>
51#ifdef GEO_COMPILER_MSVC
52#pragma warning( push )
53#pragma warning( disable: 4702 )
82 return lua_isboolean(L,idx);
97 return lua_islightuserdata(L,idx);
110 if(!lua_isinteger(L,idx)) {
113 lua_Integer x = lua_tointeger(L,idx);
114 return (x > 0) ? 1 : 0;
126 lua_pushstring(L, error);
127 lua_setfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
148 lua_setfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
157 lua_getfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
158 bool result = !lua_isnil(L,-1);
177 std::string error = std::string(
"Argument ") +
178 String::to_string(idx) +
": wrong argument type (" +
179 "got " + lua_typename(L,lua_type(L,idx)) +
")";
199 if(lua_gettop(L) != expected_nb_args) {
201 "Expected " + String::to_string(expected_nb_args)
202 +
" arg(s), got " + String::to_string(lua_gettop(L));
224 lua_getfield(L,LUA_REGISTRYINDEX,
"last_geogram_error");
225 if(lua_isstring(L,-1)) {
226 error += lua_tostring(L,-1);
230 return luaL_error(L,error.c_str());
294 lua_to(lua_State* L,
int idx) {
295 x_ = int(lua_tointeger(L,idx));
300 operator int()
const {
310 template<>
class lua_to<Numeric::uint32> {
312 lua_to(lua_State* L,
int idx) {
328 template<>
class lua_to<Numeric::uint64> {
330 lua_to(lua_State* L,
int idx) {
347 template<>
class lua_to<Numeric::int64> {
349 lua_to(lua_State* L,
int idx) {
367 lua_to(lua_State* L,
int idx) {
368 x_ = float(lua_tonumber(L,idx));
373 operator float()
const {
385 lua_to(lua_State* L,
int idx) {
386 x_ = double(lua_tonumber(L,idx));
391 operator double()
const {
403 lua_to(lua_State* L,
int idx) {
404 x_ = (lua_toboolean(L,idx) != 0);
409 operator bool()
const {
421 lua_to(lua_State* L,
int idx) {
422 x_ = lua_tostring(L,idx);
424 static bool can_convert(lua_State* L,
int idx) {
427 operator const char*()
const {
437 template<>
class lua_to<const std::string&> {
439 lua_to(lua_State* L,
int idx) {
440 x_ = lua_tostring(L,idx);
445 operator const std::string&()
const {
457 lua_to(lua_State* L,
int idx) {
458 x_ = lua_tostring(L,idx);
463 operator std::string()
const {
474 template<
class T,
unsigned int N>
class lua_to< const
vecng<N,T>& > {
476 lua_to(lua_State* L,
int idx) {
479 static bool can_convert(lua_State* L,
int idx) {
495 lua_to(lua_State* L,
int idx) {
498 static bool can_convert(lua_State* L,
int idx) {
521 template<
class T>
inline void lua_push(lua_State* L, T x) {
530 template<>
inline void lua_push(lua_State* L,
int x) {
531 lua_pushinteger(L,lua_Integer(x));
538 lua_pushinteger(L,lua_Integer(x));
545 lua_pushinteger(L,lua_Integer(x));
552 lua_pushinteger(L,lua_Integer(x));
558 template<>
inline void lua_push(lua_State* L,
float x) {
559 lua_pushnumber(L,lua_Number(x));
565 template<>
inline void lua_push(lua_State* L,
double x) {
566 lua_pushnumber(L,lua_Number(x));
572 template<>
inline void lua_push(lua_State* L,
bool x) {
573 lua_pushboolean(L,x?1:0);
579 template<>
inline void lua_push(lua_State* L,
const char* x) {
586 template<>
inline void lua_push(lua_State* L,
const std::string& x) {
587 lua_pushstring(L,x.c_str());
593 template<>
inline void lua_push(lua_State* L, std::string x) {
594 lua_pushstring(L,x.c_str());
603 lua_State* L,
const std::vector<T>& x
606 for(
size_t i=0; i<x.size(); ++i) {
608 lua_seti(L,-2,lua_Integer(i+1));
622#define LUA_DECLAREENUMTYPE(T) \
623 template<> inline void lua_push(lua_State* L, T x) { \
624 lua_push(L, int(x)); \
627 template<> class lua_to<T> : public GEO::lua_to<int> { \
629 lua_to(lua_State* L, int idx) : lua_to<int>(L,idx) { \
631 operator T() const { \
632 return T(lua_to<int>::operator int()); \
645 template <
class R>
inline int lua_wrap(lua_State* L, R (*fptr)(
void)) {
662 lua_State* L, R (*fptr)(T1)
684 template <
class R,
class T1,
class T2>
inline int lua_wrap(
685 lua_State* L, R (*fptr)(T1,T2)
709 template <
class R,
class T1,
class T2,
class T3>
inline int lua_wrap(
710 lua_State* L, R (*fptr)(T1,T2,T3)
736 template <
class R,
class T1,
class T2,
class T3,
class T4>
737 inline int lua_wrap(lua_State* L, R (*fptr)(T1,T2,T3,T4)) {
765 template <>
inline int lua_wrap(lua_State* L,
void (*fptr)(
void)) {
779 template <
class T1>
inline int lua_wrap(lua_State* L,
void (*fptr)(T1)) {
799 lua_State* L,
void (*fptr)(T1,T2)
821 template <
class T1,
class T2,
class T3>
822 inline int lua_wrap(lua_State* L,
void (*fptr)(T1,T2,T3)) {
845 template <
class T1,
class T2,
class T3,
class T4>
846 inline int lua_wrap(lua_State* L,
void (*fptr)(T1,T2,T3,T4)) {
872 class T1,
class T2,
class T3,
class T4,
class T5
875 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5)
904 class T1,
class T2,
class T3,
class T4,
class T5,
class T6
907 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6)
938 class T1,
class T2,
class T3,
class T4,
class T5,
942 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7)
976 class T1,
class T2,
class T3,
class T4,
class T5,
977 class T6,
class T7,
class T8
980 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8)
1016 class T1,
class T2,
class T3,
class T4,
class T5,
1017 class T6,
class T7,
class T8,
class T9
1020 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9)
1058 class T1,
class T2,
class T3,
class T4,
class T5,
1059 class T6,
class T7,
class T8,
class T9,
class T10
1062 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)
1102 class T1,
class T2,
class T3,
class T4,
class T5,
1103 class T6,
class T7,
class T8,
class T9,
class T10,
1107 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11)
1148 class T1,
class T2,
class T3,
class T4,
class T5,
1149 class T6,
class T7,
class T8,
class T9,
class T10,
1150 class T11,
class T12
1153 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12)
1197 class T1,
class T2,
class T3,
class T4,
class T5,
1198 class T6,
class T7,
class T8,
class T9,
class T10,
1199 class T11,
class T12,
class T13
1202 lua_State* L,
void (*fptr)(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13)
1246 template<>
inline int lua_wrap(lua_State* L, lua_CFunction fptr) {
1272 FPTR f = Memory::generic_pointer_to_function_pointer<FPTR>(
1273 lua_touserdata(L, lua_upvalueindex(1))
1284 static void push(lua_State* L, FPTR f) {
1285 lua_pushlightuserdata(
1286 L, Memory::function_pointer_to_generic_pointer<FPTR>(f)
1309 lua_pushcfunction(L,f);
1323 lua_State* L, FPTR f,
const std::string& name
1327 lua_pushstring(L,name.c_str());
1340 lua_State* L, FPTR f,
const std::string& name
1343 lua_setglobal(L,name.c_str());
1357 std::string result(functionname);
1358 size_t pos = result.find_last_of(
":");
1359 if(pos != std::string::npos) {
1360 result = result.substr(pos+1, result.length()-pos);
1382#define lua_bindwrapper(L, f) lua_bindwrapperwithname( \
1383 (L),(f),GEO::lua_wrappername(L,#f) \
1400#define lua_bindwrapperglobal(L, f) lua_bindwrapperwithnameglobal( \
1401 (L),(f),GEO::lua_wrappername(L,#f) \
1409#ifdef GEO_COMPILER_MSVC
1410#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...
Functions to exchange vec2,vec3,vec4 and mat4 objects between Lua and Geogram.
Types and functions for memory manipulation.
Global Vorpaline namespace.
std::string lua_wrappername(lua_State *L, const char *functionname)
Converts a C++ function name into a LUA function name.
bool lua_tomat(lua_State *L, int index, ::GEO::Matrix< N, T > &result)
Converts a Lua object into a Geogram mat2, mat3 or mat4.
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.
void lua_push(lua_State *L, const ::GEO::vecng< N, T > &V)
Pushes a vector onto the Lua stack.
int my_lua_islightuserdata(lua_State *L, int idx)
Tests whether a LUA variable is a light user data.
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.
bool lua_tovec(lua_State *L, int index, ::GEO::vecng< N, T > &result)
Converts a lua object into a Geogram vec2,vec3 or vec4.
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.