lua_register() while running script

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

lua_register() while running script

Markus Ewald
Yesterday I tried to write a C function ImportSystem() which uses
lua_register() to make some functions available for my scripts. I
planned to export ImportSystem() itself, so each script that needs to
use the System-library could call it. Because LuaC also compiles scripts
without knowing which functions exist and which not, I thought maybe Lua
checks for a symbol just before it is called. Well, it doesn't.

I'm just asking if anyone could imagine a good solution for the problem
(besides letting each function check if it was registered, of couse),
Else I'll have to always register all functions before calling
lua_do...(). I hope this doesn't decrease performance ?

-Markus-



Reply | Threaded
Open this post in threaded view
|

Re: lua_register() while running script

Luiz Henrique de Figueiredo
>Yesterday I tried to write a C function ImportSystem() which uses
>lua_register() to make some functions available for my scripts. I
>planned to export ImportSystem() itself, so each script that needs to
>use the System-library could call it. Because LuaC also compiles scripts
>without knowing which functions exist and which not, I thought maybe Lua
>checks for a symbol just before it is called. Well, it doesn't.

luac uses exactly the same parser and code generator as lua does.
In any case, everything in Lua is dynamic, so nothing is checked at compile
time.

I didn't understand your problem very well, but if you want to register
functions by demand, then you could do the following:
Set a "getglobal" tag method for tag(nil) to just return the name of the
variable.
Set a "function" tag method for tag"" to check whether the name is know,
and in this case call ImportSystem(). Perhaps it is simpler to write
this "function" tag method in C; the code would then look for the name in
a static table in C. If it is found, then register the function with the
given name and return this Cfunction.
There is no performance hit after the first time, so it's trully by demand.

I hope this helps. This could be considered a Lua idiom...
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: lua_register() while running script

Markus Ewald
Thanks, that already helped alot, but let me try to explain it by example.
I've got this function, which is callable from Lua and registers some other
callable functions:

 static int Lua_ImportSystem(lua_State *p_pCLuaState) {
   int         l_nArguments;

   l_nArguments = lua_gettop(p_pCLuaState);           // number of arguments
   if(l_nArguments == 0) {
     lua_register(p_pCLuaState, "SetGraphicsDriver",    Lua_SetGraphicsDriver);

     lua_register(p_pCLuaState, "SetGraphicsMode",      Lua_SetGraphicsMode);
     lua_register(p_pCLuaState, "Bind",                 Lua_Bind);
     lua_register(p_pCLuaState, "RegisterResourcePath",
Lua_RegisterResourcePath);
     lua_register(p_pCLuaState, "LoadWorld",            Lua_LoadWorld);
   } else {
     LogError("Lua::LoadWorld() - Error: Expected 0 arguments, got %i",
l_nArguments);
   }
   lua_pop(p_pCLuaState, l_nArguments);

   return 0;
 }

so I could execute this code

   pCLuaState = lua_open(LUA_MINSTACK);
   lua_register(m_pCLuaState, "ImportSystem" Lua_ImportSystem);

   lua_dostring("ImportSystem()\n"
                "RegisterResourcePath(\"data\\\\graphics\")\n"
                "RegisterResourcePath(\"data\\\\worlds\")\n"
                "LoadWorld(\"demo.cdt\")\n"

I thought the first call to ImportSystem() while the script runs could register
the functions the are used below.

-Markus-



Reply | Threaded
Open this post in threaded view
|

Re: lua_register() while running script

Luiz Henrique de Figueiredo
In reply to this post by Markus Ewald
>   lua_pop(p_pCLuaState, l_nArguments);

There is no need for this: when a C function returns, Lua runtime handles this.
Normally, you only need to call lua_pop in more complicated situations,
such as loops in C functions that call Lua and leave garbage behind (ie, where
in 3.2 you'd use begin/endblock).

>   pCLuaState = lua_open(LUA_MINSTACK);

Try lua_open(0) instead of lua_open(LUA_MINSTACK). LUA_MINSTACK is NOT the
default stack size, it's the minimum stack space guaranteed to be available
for a C function. Perhaps you're overflowing the stack.

>I thought the first call to ImportSystem() while the script runs could register
>the functions the are used below.

It should. See below for a simple example that works.
--lhf

/*
* min.c
* a minimal Lua interpreter. loads stdin only.
* no standard library, only a "print" function.
*/

#include <stdio.h>
#include "lua.h"

/* a simple "print". based on the code in lbaselib.c */
static int print(lua_State *L)
{
 int n=lua_gettop(L);
 int i;
 for (i=1; i<=n; i++)
 {
  if (i>1) printf("\t");
  if (lua_isstring(L,i))
   printf("%s",lua_tostring(L,i));
  else
   printf("%s:%p",lua_typename(L,lua_type(L,i)),lua_topointer(L,i));
 }
 printf("\n");
 return 0;
}

static int load(lua_State *L)
{
 lua_register(L,"print",print);
 return 0;
}

int main(void)
{
 lua_State *L=lua_open(0);
 lua_register(L,"load",load);
 lua_dostring(L,"load();print(load,print,'lua',{})");
 return 0;
}

Reply | Threaded
Open this post in threaded view
|

Re: lua_register() while running script

Markus Ewald
Thanks!
I still don't know what I had wrong last time, but it is working now.

-Markus-