lua-users home
lua-l archive

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


2015-03-20 12:36 GMT+02:00 biswajit neog <biswajitneog@gmail.com>:

> I am new to Lua programming. I have gone through the notes on lua and
> searched on google. But I cant understand few of these functions.Please
> excuse me for being slow:

I excuse you this time. All newbies are entitled to one, just one, post of the
"I'm too lazy to read the manual" kind. Read it before posting again.

> 1. is LuaL_TNUMBER same as Lua_TONUMEBER?if not what is LUA_TNUMBER?

There is no LuaL_TNUMBER. The way you find that out is:

a. Open the manual in your browser.
b. Search on "LuaL_TNUMBER".

lua_TNUMBER is the predefined C name for an integer coding for the type
"number". The way you find that out is:

a. Open the manual in your browser.
b. Search on "Lua_TNUMBER".

> 2.I have to import a C function(with two arguments) to lua. I have seen in
> many examples, whenever C functions with arguments are called they do this
>
> luaL_checktype(L, 1, LUA_TNUMBER);
>
> my question is why do we do this to check the arguments?

If you don't, the program may crash with something like a segfault.
If you do, you get back to the interpreter level with a nice error
message telling you what type was actually passed.

> 3. why do we do a lua_pushinteger??
> ex:
> {
>   int b;
>   luaL_checktype(L, 1, LUA_TNUMBER);
>   b=SPI(lua_tointeger(L, 1));
>   lua_pushinteger(L, b);
>   return 1;
> }

It puts the integer on the stack because "return 1" says that one item
will be popped off the stack to be the return value at the Lua level.
"return 2" would say "pop off two items as return values".

> 4. Lua operates by pushing and popping into a lua stack.how does
> it help and why it is needed to do so??

This question cannot be answered in one or two sentences. Here the
manual is for people who already know the answer but need to check
the details. The best place to start is "Programming in Lua" by Roberto
Ierusalimschy, where it is only discussed quite late in the book. You can
find an old but not utterly obsolete version via Google, or you can buy
an electronic copy of the current version from <Google will also tell you
where>.