text source of compiled functions

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

text source of compiled functions

Luiz Henrique de Figueiredo
While reviewing the list archive, I've come across some old messages about the
need to have access to the source of compiled functions.
It may have gone unnoticed that Lua 3.2 now supports this: the complete string
sent to lua_dostring is now kept inside Lua, and can be retrieved with the
debug interface.
In this way, code browsers and debuggers can show the complete text of each
function, or group of functions.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: text source of compiled functions

Steve Dekorte-4
Luiz Henrique de Figueiredo <[hidden email]> wrote:
> While reviewing the list archive, I've come across some old messages about the
> need to have access to the source of compiled functions.
> It may have gone unnoticed that Lua 3.2 now supports this: the complete string
> sent to lua_dostring is now kept inside Lua, and can be retrieved with the
> debug interface.
> In this way, code browsers and debuggers can show the complete text of each
> function, or group of functions.

Awesome! I had asked for that feature. Thanks!

Steve

Reply | Threaded
Open this post in threaded view
|

Re: text source of compiled functions

Pablo Saldo
In reply to this post by Luiz Henrique de Figueiredo
>It may have gone unnoticed that Lua 3.2 now supports this: the complete
string
>sent to lua_dostring is now kept inside Lua, and can be retrieved with the
>debug interface.

Is there a way to retrieve the code in a passive way, without executing any
code?

  (o,o)  Pablo Saldo  -  [hidden email]
  {`"'}  FUNDACAO UNIVERSIDADE FEDERAL DO RIO GRANDE
  -"-"-  ENGENHARIA DE COMPUTACAO

Reply | Threaded
Open this post in threaded view
|

Re: text source of compiled functions

Luiz Henrique de Figueiredo
In reply to this post by Luiz Henrique de Figueiredo
>From [hidden email] Tue Jul 27 01:54:42 1999
>From: Pablo Saldo <[hidden email]>

>>It may have gone unnoticed that Lua 3.2 now supports this: the complete
>string
>>sent to lua_dostring is now kept inside Lua, and can be retrieved with the
>>debug interface.
>
>Is there a way to retrieve the code in a passive way, without executing any
>code?

I'm not sure what you mean here.
lua_dostring always executes the string it gets, but this execution can be
simply the definition of some functions, eg:

 function f(x)
  return x+g(x)
 end

So, doing lua_dostring on this strings simply defines the function f.
(More precisely, it simply stores a function value into the global variable f.)
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: text source of compiled functions

Pablo Saldo
At 06:53 27/07/99 -0300, you wrote:
>I'm not sure what you mean here.
>lua_dostring always executes the string it gets, but this execution can be
>simply the definition of some functions, eg:
>
> function f(x)
>  return x+g(x)
> end
>
>So, doing lua_dostring on this strings simply defines the function f.
>(More precisely, it simply stores a function value into the global
variable f.)
>--lhf

what I mean is, once executed the function definition, as in the example,
how can I retrieve the code later, without calling de function definition
again.

  (o,o)  Pablo Saldo  -  [hidden email]
  {`"'}  FUNDACAO UNIVERSIDADE FEDERAL DO RIO GRANDE
  -"-"-  ENGENHARIA DE COMPUTACAO

Reply | Threaded
Open this post in threaded view
|

Re: text source of compiled functions

David Jeske-3
In reply to this post by Luiz Henrique de Figueiredo
On Tue, Jul 27, 1999 at 06:53:24AM -0300, Luiz Henrique de Figueiredo wrote:
> >From [hidden email] Tue Jul 27 01:54:42 1999
> >From: Pablo Saldo <[hidden email]>

> >Is there a way to retrieve the code in a passive way, without executing any
> >code?
> 
> I'm not sure what you mean here.
> lua_dostring always executes the string it gets, but this execution can be
> simply the definition of some functions, eg:

I'll restate the question as: If we define lua_dostring as:

function lua_dostring(a_string)
  a_block = lua_parsestring(a_string)
  lua_doblock(a_block)
end

How do we do "lua_parsestring" and "lua_doblock"?

I'm guessing that the closest you can get is to build the
tokenizer/compiler stuff in as a function, "compile" from source to
bytecodes first, and then "lua_dostring" the bytecodes as necessary.

Although I must admit that I've always disliked the whole
"lua_dostring()" interface. I'd prefer to prevent any side-effects as
a result of just bringing in a module. As a result, I use my own Perl
style 'require()' function which keeps track of module loads, and from
which I have always intended to enforce some concept of
namespaces/context. (i.e. make global variables "module-global" only)

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: text source of compiled functions

Luiz Henrique de Figueiredo
In reply to this post by Luiz Henrique de Figueiredo
>From [hidden email] Tue Jul 27 07:58:15 1999
>From: David Jeske <[hidden email]>

>I'll restate the question as: If we define lua_dostring as:
>
>function lua_dostring(a_string)
>  a_block = lua_parsestring(a_string)
>  lua_doblock(a_block)
>end
>
>How do we do "lua_parsestring" and "lua_doblock"?

There's no official API for this, but you can look inside ldo.c and luac.c
and see how it's done: "lua_parsestring" would be luaY_parser;
"lua_doblock" would be luaD_protectedrun.

>I'd prefer to prevent any side-effects as
>a result of just bringing in a module.

Well, the whole point of loading a chunk is to have some side-effects!
(At least, some function definitions, which are simply assignments.)
Now, as you say, "chunks" are probably different from what you call "modules".
But I think Lua has the mechanisms to allow you to define your own semantics
for "modules", e.g., the setglobal tag method.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: text source of compiled functions

Luiz Henrique de Figueiredo
In reply to this post by Luiz Henrique de Figueiredo
>From [hidden email] Thu Jul 29 01:34:37 1999
>From: Pablo Saldo <[hidden email]>

>>I'm not sure what you mean here.
>>lua_dostring always executes the string it gets, but this execution can be
>>simply the definition of some functions, eg:
>>
>> function f(x)
>>  return x+g(x)
>> end
>>
>>So, doing lua_dostring on this strings simply defines the function f.
>>(More precisely, it simply stores a function value into the global
>variable f.)
>>--lhf
>
>what I mean is, once executed the function definition, as in the example,
>how can I retrieve the code later, without calling de function definition
>again.

Just use lua_call("f").
--lhf

Reply | Threaded
Open this post in threaded view
|

documentation for next() in C

Russell Y. Webb
In reply to this post by Luiz Henrique de Figueiredo
Since there doesn't seem to be any docs for the C call next(), here is 
what I've figured out:

int lua_next(lua_Object tableObject, int tableIndex);	/* Out: ref, value */ 
	return: next index or 0 when end of table is reached
    tableIndex: 0 if you want the first item in the table
     lua stack: two lua_Objects are pushed onto the stack; first the key 
                and then the value of the entry

Russ