Advertisement

How do you think LUA handles script functions?

Started by August 13, 2003 05:40 AM
9 comments, last by Craazer 21 years, 2 months ago
Hi I readed lua tutorial found here in gamedev and according to that lua can register function''s very easily like this: lua_register( luaVM, "addNPC", l_addNPC ); So can some one brighten a bit how does that register work? First the info about parameters what l_addNPC uses must be stored and then maybe some sort of asm code to call the function. Or?
Hey,

Sorry, I don''t have the source in front of me atm, but lua_register is just a macro. Find the definition in one of the headers and it''s all right there.

Kory
"The pioneers of a warless world are the youth who refuse military service" - Albert Einstein
Advertisement
quote: Original post by kspansel
Hey,

Sorry, I don''t have the source in front of me atm, but lua_register is just a macro. Find the definition in one of the headers and it''s all right there.

Kory


Oh right actualy I knew it''s a macro but ain''t that code unavaible becose it''s in libary? Well i''ll look it for more.
lua is free, and open source.

You can get the source code for free, and find out for yourself.
daerid@gmail.com
quote: Original post by Craazer
So can some one brighten a bit how does that register work? First the info about parameters what l_addNPC uses must be stored and then maybe some sort of asm code to call the function. Or?
All functions registered to Lua must take one argument, a pointer to a lua_state, and return an integer. RTFM.

How appropriate. You fight like a cow.
he''s not asking how to use the functions, he''s asking what
the lua interpreter code is actually doing when you register
a function, and when you call it...

As was mentioned before, you can look through the lua code,
as it''s open source, and discover this for yourself. I assume
it adds some new entry in the lua_state so the interpreter
will recognize the function, and when called, makes an external call by mangling the C function name you give it.
Advertisement
just because something is open source doesn''t mean it''s wasy for everyone to understand every little thing it does. Do all of you understand every tidbit of carmack''s work? Don''t lie.
Give the guy a break would you?
quote: Original post by Craazer
Hi I readed lua tutorial found here in gamedev and according to that lua can register function''s very easily like this:
lua_register( luaVM, "addNPC", l_addNPC );

So can some one brighten a bit how does that register work? First the info about parameters what l_addNPC uses must be stored and then maybe some sort of asm code to call the function. Or?


I don''t know how the first part work, maybe lua just don''t do any check (params ar put on the stack, the function is called and that''s all).
The second part, yes, is some little asm code.

This is taken for an article by Scott Bilas, and I used it in my scripting engine

DWORD Call_cdecl( const void* args, size_t sz, DWORD func ){    DWORD rc;               // here''s our return value...    __asm    {        mov   ecx, sz       // get size of buffer        mov   esi, args     // get buffer        sub   esp, ecx      // allocate stack space        mov   edi, esp      // start of destination stack frame        shr   ecx, 2        // make it dwords        rep   movsd         // copy params to real stack        call  [func]        // call the function        mov   rc,  eax      // save the return value        add   esp, sz       // restore the stack pointer    }    return ( rc );} DWORD Call_stdcall( const void* args, size_t sz, DWORD func ){    DWORD rc;               // here''s our return value...    __asm    {        mov   ecx, sz       // get size of buffer        mov   esi, args     // get buffer        sub   esp, ecx      // allocate stack space        mov   edi, esp      // start of destination stack frame        shr   ecx, 2        // make it dwords        rep   movsd         // copy it        call  [func]        // call the function        mov   rc,  eax      // save the return value    }    return ( rc );} 
Why do you need ASM? What''s wrong with C? And what does that last post have to do with any of this? Aren''t we talking about LUA?
quote: Original post by Craazer
Hi I readed lua tutorial found here in gamedev and according to that lua can register function''s very easily like this:
lua_register( luaVM, "addNPC", l_addNPC );

So can some one brighten a bit how does that register work? First the info about parameters what l_addNPC uses must be stored and then maybe some sort of asm code to call the function. Or?

As far as I know, all it does is associate the string ''addNPC'' with a function pointer to l_addNPC in a table inside luaVM. There''s no ASM, and no need to store details about parameters because all functions that you give to Lua have the same parameters. This is possible because everything in Lua is done via the stack that is provided to you. It just looks up the relevant function pointer whenever the specified name is found in the script you give it.



[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

This topic is closed to new replies.

Advertisement