lua-users home
lua-l archive

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


Ok, it seems to work...I  modified body() in lparser.c to store the
string as it's being parsed.  I also made a few other trivial changes to
support this:

static void body (LexState *ls, expdesc *e, int needself, int line) {
  /* body ->  `(' parlist `)' chunk END */
  FuncState new_fs;
  const char *pszSource;

  open_func(ls, &new_fs);

  /* Function starts at current token */
  pszSource = ls->z->p; 
  while (*pszSource != '(') {
      pszSource--;
  }

  new_fs.f->linedefined = line;
  checknext(ls, '(');
  if (needself) {
    new_localvarliteral(ls, "self", 0);
    adjustlocalvars(ls, 1);
  }
  parlist(ls);
  checknext(ls, ')');
  chunk(ls);

  new_fs.f->actual_source = luaS_newlstr(ls->L, pszSource, ls->z->p -
pszSource);

  new_fs.f->lastlinedefined = ls->linenumber;
  check_match(ls, TK_END, TK_FUNCTION, line);
  close_func(ls);
  pushclosure(ls, &new_fs, e);
}

I don't know how "unsafe" it is, but it works so far for my purposes.

Brian

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Mark Meijer
Sent: Monday, September 15, 2008 6:13 PM
To: Lua list
Subject: Re: Function Source

One approach might be to somehow tie into the actual parsing process of
Lua, and keep your extra debug info (i.e. function definition in source
code format) as it reads the chunk. I wouldn't know how exactly, but
perhaps Luiz's tokenf is a starting point:
http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#tokenf (bottom of page)

Although I guess tokens are only part of the story. I don't know, I'm
just throwing random ideas around. MetaLua or LuaSub can probably help
more.

Parsing the code yourself might be a practical option, though. In case
you didn't know about this one yet: http://leg.luaforge.net/ :)



2008/9/15 Brian Weed <brianw@imaginengine.com>:
> I'm looking for a way to get the source string of a function from Lua,

> but I'd rather not have to parse it myself.
>
> Using the debug library, I see that I could get the source filename, 
> and line numbers of where the function was defined...But I was hoping 
> there was a way for me to ask the debug library to give me exactly the

> string that was used to define the function, and nothing more.  This 
> would only be for named functions.
>
> Example:
>
> -- bob.lua
> --
> return {
>     name = "Bob",
>     OnClick = function(self) print("You clicked on "..self.name) end, 
> }
> -- EOF
>
> I want to do this:
>
> local bob = dofile("bob.lua")
>
> print(debug.getinfo(bob.OnClick).string)
>
>>> function(self) print("You clicked on "..self.name) end
>
> Since this exact functionality does not seem to exist in the debug 
> library, I'm looking at adding it, but I wanted to ask here if someone

> already has something like this, or a parser that can get this
information.
>
> This is for an object editor, where the user can load, edit , and save

> the code associated with objects....Edit, and Save are trivial.  
> Loading is what I'm asking about.  Worst case is I could store the 
> function as a string, but I'd rather not.
>
> Brian