lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, 18 Apr 2010 22:06:02 -0600
HyperHacker <hyperhacker@gmail.com> wrote:

> The solution to that is to copy it:
> lua_pushvalue(L, someindex);
> n = lua_tonumber(L, -1);
> lua_pop(L, 1);
> You can define C preprocessor macros for this, but they get a little
> hairy for the fact that macros can't return a value. Better to use a
> function.

GCC (and some of the LLVM front-ends) support return values for macros:

#define COPY_NUMBER(_L, _idx)	\
  ({				\
	int _n;			\
	lua_pushval(_L,_idx);	\
	_n = lua_tonumber(_L, -1);\
	lua_pop(_L, 1);		\
        n;			\
   })

Else you can always return via a paramater:
#define COPY_NUMBER(_L, _idx, _n) \
	do {			\
		...
	} while (0)

Useful in some cases, but like all macros a recipe for disaster
if you aren't careful.

	Regards,
	nash