How know what functions are defined in a lua script ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How know what functions are defined in a lua script ?

Christophe Gimenez
Me again !!

Is there a way to know (in a C function) what functions had been defined in a lua script that have been procedeed by a lua_dofile() ?

That could be useful for me to know what "handler" are in the file and uses lua_callfunction() when the handler had been defined.

Reply | Threaded
Open this post in threaded view
|

RE: How know what functions are defined in a lua script ?

Vincent Penquerc'h-2
> Is there a way to know (in a C function) what functions had been 
> defined in a lua script that have been procedeed by a lua_dofile() ?
If you don't want to use a sort of "registration" system in your scripts,
you can list all globals before lua_dofile, do that after as well, and
see what globals were added. You then ask for their type to see if they
are functions. This won't give you any information on their argument
types, though, so it could be dodgy to make an autocall from C...

-- 
Lyrian 

Reply | Threaded
Open this post in threaded view
|

Re: How know what functions are defined in a lua script ?

Luiz Henrique de Figueiredo
In reply to this post by Christophe Gimenez
>From: Christophe Gimenez <[hidden email]>
>
>Is there a way to know (in a C function) what functions had been defined in a lua script that have been procedeed by a lua_dofile() ?

If your script only defines global functions (eg. function f()... end), then
you can set a "setglobal" tag method for tag(nil) and see if the new value is
a function; if it is, then save the name in a list to be processed after dofile.
--lhf

Reply | Threaded
Open this post in threaded view
|

RE: How know what functions are defined in a lua script ?

Christophe Gimenez
In reply to this post by Christophe Gimenez

-----Message d'origine-----
De:	Vincent Penquerc'h [SMTP:[hidden email]]
Date:	mardi 28 mars 2000 16:41
A:	Multiple recipients of list
Objet:	RE: How know what functions are defined in a lua script ?

> Is there a way to know (in a C function) what functions had been 
> defined in a lua script that have been procedeed by a lua_dofile() ?
If you don't want to use a sort of "registration" system in your scripts,

That's exactly what I'm trying to avoid...

you can list all globals before lua_dofile, do that after as well, and
see what globals were added. You then ask for their type to see if they
are functions. This won't give you any information on their argument
types, though, so it could be dodgy to make an autocall from C...

All the function I try to locate begins by "on_" (they are message handlers)

But how to list the globals ? Is there a way to do that with a lua api function ?

If I must parse myself the lua script I can't because I will use precompiled files.

Honnestly, trying to build a game is a really really hard task ;-)))

-- 
Lyrian 

Reply | Threaded
Open this post in threaded view
|

RE: How know what functions are defined in a lua script ?

Vincent Penquerc'h-2
> But how to list the globals ? Is there a way to do that with a 
> lua api function ?
There is: foreachvar, if I recall well. But actually, the tag method
way lhf proposed sounds better.

-- 
Lyrian 

Reply | Threaded
Open this post in threaded view
|

Re: How know what functions are defined in a lua script ?

Roberto Ierusalimschy
In reply to this post by Christophe Gimenez
> But how to list the globals ? Is there a way to do that with a lua api function ?


  const char *name = NULL;
  lua_beginblock();
  while ((name = lua_nextvar(name)) != NULL) {
    lua_Object value = lua_getresult(2);
    ...  /* uses `name' and `value' */
    lua_endblock();
    lua_beginblock();  /* reopens a block */
  }
  lua_endblock();


-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: How know what functions are defined in a lua script ?

Nicholas Hesketh
In reply to this post by Christophe Gimenez
-----Original Message-----
From: Christophe Gimenez <[hidden email]>
To: Multiple recipients of list <[hidden email]>
Date: 28 March 2000 17:24
Subject: RE: How know what functions are defined in a lua script ?


>
>you can list all globals before lua_dofile, do that after as well, and
>see what globals were added. You then ask for their type to see if they
>are functions. This won't give you any information on their argument
>types, though, so it could be dodgy to make an autocall from C...
>
>
>--
>Lyrian
>


It sounds like you'd be better off using a table to contain each function and all the argument
information you want.

Nick.


Reply | Threaded
Open this post in threaded view
|

RE: How know what functions are defined in a lua script ?

Vincent Penquerc'h-2
> It sounds like you'd be better off using a table to contain each 
> function and all the argument
> information you want.
I think the original poster did want to find out which functions
were added, without a registration system, and I think this table
would count as a registration system. But indeed, it would be
much simpler to do that...

To the original poster:
Or, without registration again, why not impose a naming convention,
a little like C++ name decoration, which adds characters to a
function name based on the parameters it expects...
Would this be too cumbersome ? And it looks like the beginnings
of registration too...

-- 
Lyrian 

Reply | Threaded
Open this post in threaded view
|

RE: How know what functions are defined in a lua script ?

Christophe Gimenez
In reply to this post by Christophe Gimenez
Don't know but your first idea is good for me ...

Here is what I will do :

function on_XXX1
end

function on_XXX2
end

function on_XXX3
end

function testfunction(funcname)
	if strsub(funcname,0,3) == 'on_' then
		registerHandler(funcname)
	end
end

foreachvar(testfunction)

Then, for an incoming event I'll just have to see if a qualified handler exist for that object...



-----Message d'origine-----
De:	Vincent Penquerc'h [SMTP:[hidden email]]
Date:	mercredi 29 mars 2000 11:38
A:	Multiple recipients of list
Objet:	RE: How know what functions are defined in a lua script ?

> It sounds like you'd be better off using a table to contain each 
> function and all the argument
> information you want.
I think the original poster did want to find out which functions
were added, without a registration system, and I think this table
would count as a registration system. But indeed, it would be
much simpler to do that...

To the original poster:
Or, without registration again, why not impose a naming convention,
a little like C++ name decoration, which adds characters to a
function name based on the parameters it expects...
Would this be too cumbersome ? And it looks like the beginnings
of registration too...

-- 
Lyrian