Norman Ramsey | 1 Apr 04:53
Picon
Gravatar

Re: Masterminds of Programming (off topic)

 > [book] includes an interviews with the creators of a number of interesting
 > languages, including... Lua.

I shall look forward to many of the chapters, but I will have a hard
time convincing myself to shell out for anything that calls Adin
Falkoff a mastermind.

I now return to my regularly scheduled Lua hacking.

N

David Given | 1 Apr 06:21

Re: How do I add a "local" using the c api


Tom Miles wrote:
[...]
> When I create a state, I load an associated script file.  I want to add
> some variables into the state the script is running in that are only
> visible to that script.  I can add globals using lua_setglobal(L,
> "myvar"), or even lua_setfield(L, LUA_GLOBALSINDEX, "myvar"), but there
> doesn't seem to be an equivelant for locals.

Something that nobody's said yet which might clarify matters for you is
this...

You *cannot* add a local variable to an already compiled script. [*]

This is because locals do not actually exist in the compiled bytecode
--- they're just slots on the stack that the compiler has special
knowledge of, and, AFAIK, you can't change the bytecode after it's been
compiled. They're not looked up by name and I think that if you strip
the debugging information from your bytecode debug.getlocal() can't find
out what they're called.

The lua_Reader trick is a way of inserting boilerplate at compile time.
This will work, but be aware that it's not really modifying the state
after it's been compiled. Think of it as the equivalent of a #include at
the top of the script.

Using a shared environment table is usually a way of sharing values
between *C* functions (and you still need a table lookup anyway).

Setting upvalues is another way of sharing data between chunks, but to
(Continue reading)

Mark Hamburg | 1 Apr 06:35

Re: How do I add a "local" using the c api


On Mar 31, 2009, at 8:15 AM, Tom Miles wrote:

> Urm, how to put this another way...
>
> When I create a state, I load an associated script file.  I want to  
> add
> some variables into the state the script is running in that are only
> visible to that script.  I can add globals using lua_setglobal(L,
> "myvar"), or even lua_setfield(L, LUA_GLOBALSINDEX, "myvar"), but  
> there
> doesn't seem to be an equivelant for locals.

Let me see if I've got the sequence right. You've got C code that  
loads a script and executes it to set up the environment. The C code  
needs to define some values that only that script should see rather  
than having them go into the global environment where they might be  
seen by other scripts.

Correct?

It isn't locals that you want because then they would only be visible  
to your script and not to the C code.

What you want is a custom environment for the script. So, do something  
like the following:

	load the script  -- you now have a compiled chunk on the top of the  
stack
	get the environment from the chunk (lua_getfenv) -- call the  
(Continue reading)

Tim Mensch | 1 Apr 06:50

Re: Doxygen for Lua

dcharno wrote:
> Anyone using Doxygen to document their Lua code?  A search on this 
> topic turned up nothing.  I know there is Luadoc, but my C application 
> is already documented with Doxygen and it would be nice to have the 
> Lua extensions documented similarly.
Oddly enough...yes. There are Lua docs generated as part of my book:

http://www.lulu.com/content/paperback-book/navigating-the-playground-sdk%E2%84%A2/697902

Took advantage of the Doxygen LaTeX output, and poof, a book. Does 
LuaDoc do that too? :)

You can also download a PDF from http://developer.playfirst.com, if 
you're curious.

In any event, what I wrote (in Lua, no less, though wrapped in an EXE to 
allow Doxygen to call more easily) was a quick preprocessor that makes 
Lua code look like C++ code. Really it's a pretty simple hack: I only 
look for (and document) functions and global constants. When the 
preprocessor finds a "---" comment section, it removes the "---" and 
wraps it in C-style Doxygen comments, and then after the comment leaves 
just a function header, like so:

/**
  My docs.
*/
function MyFunction( foo );

Doxygen then deals with this more-or-less correctly. Of course it thinks 
it's a function returning type "function," taking a parameter type 
(Continue reading)

Tom Miles | 1 Apr 14:51
Picon

RE: How do I add a "local" using the c api

> -----Original Message-----
> From: lua-bounces <at> bazar2.conectiva.com.br [mailto:lua-
> bounces <at> bazar2.conectiva.com.br] On Behalf Of Mark Hamburg
> Sent: 01 April 2009 05:35
> To: Lua list
> Subject: Re: How do I add a "local" using the c api
> 
> 
> On Mar 31, 2009, at 8:15 AM, Tom Miles wrote:
> 
> > Urm, how to put this another way...
> >
> > When I create a state, I load an associated script file.  I want to
> > add
> > some variables into the state the script is running in that are only
> > visible to that script.  I can add globals using lua_setglobal(L,
> > "myvar"), or even lua_setfield(L, LUA_GLOBALSINDEX, "myvar"), but
> > there
> > doesn't seem to be an equivelant for locals.
> 
> Let me see if I've got the sequence right. You've got C code that
> loads a script and executes it to set up the environment. The C code
> needs to define some values that only that script should see rather
> than having them go into the global environment where they might be
> seen by other scripts.
> 
> Correct?

Yes that is correct, only I don't need the "locals" to be visible to the
c code after I've added them to the state.
(Continue reading)

Tom Miles | 1 Apr 17:13
Picon

RE: How do I add a "local" using the c api

> Tom Miles wrote:
> [...]
> > When I create a state, I load an associated script file.  I want to
> add
> > some variables into the state the script is running in that are only
> > visible to that script.  I can add globals using lua_setglobal(L,
> > "myvar"), or even lua_setfield(L, LUA_GLOBALSINDEX, "myvar"), but
> there
> > doesn't seem to be an equivelant for locals.
> 
> Something that nobody's said yet which might clarify matters for you is
> this...
> 
> You *cannot* add a local variable to an already compiled script. [*]
> 

Thank you for that answer.  Finally everything is made clear :)
Mark Hamburg | 1 Apr 17:22

Re: How do I add a "local" using the c api

On Apr 1, 2009, at 5:51 AM, Tom Miles wrote:

> I'm getting a little confused here again as well, if another state  
> were
> to call getglobal() on our first state, would it be able to see it  
> using
> this methood?

This is where the definition of globals in C v Lua gets a little bit  
complicated and others can almost certainly explain it better than I  
can. On the Lua side, the function/chunk has an environment table and  
global lookups go to that table. There needn't be any other links to  
that table so beyond code that calls getfenv, that environment isn't  
going to escape. (You can protect yourself from Lua code doing this  
through appropriate metatable construction.)

On the C side, you have the table at LUA_GLOBASINDEX and the table at  
LUA_ENVIRONINDEX. My recollection is that the former is bound to the  
Lua state and is accessible to Lua code via getfenv. The latter is  
bound to the C code that is executing. Since you don't need to adjust  
either of these to set the environment for the script chunk, there  
shouldn't be an issue of the environment escaping.

Mark

Matthias Kluwe | 1 Apr 18:44
Picon

Re: Doxygen for Lua

Hi!

2009/3/31 dcharno <dcharno <at> comcast.net>:
> Anyone using Doxygen to document their Lua code?  A search on this topic
> turned up nothing.  I know there is Luadoc, but my C application is already
> documented with Doxygen and it would be nice to have the Lua extensions
> documented similarly.

The silver bullet would be getting the Lua grammar into doxygen
itself. Currently, the work of splitting the big scanner.l into
language specific ones seems to be in process. Look here
(http://doxygen.svn.sourceforge.net/viewvc/doxygen/trunk/src/pyscanner.l?revision=639&view=markup),
for example.

Alas, nothing to be seen for Lua here...

Regards,
Matthias

Jorge | 1 Apr 20:28
Picon

Profiling Memory usage.

Hi list.

I wonder how to profile memory usage. I have this application, a
content-based router that moves notifications and subscriptions trough a
p2p network. It's based around a socket server (LuaSocket based), and
keeps lots of tables for peer lists, loop detection, subscription
filters, and assorted stuff. The memory consumption fluctuates a lot
with usage patterns, and there are a few configuration parameters that
have a serious impact.
It's intended to be run on plain consumer-grade wireless routers
(WRT54GL's and such), so RAM consumption is an issue.
I've googled around and only found stuff related to pre-5.1 lua,and
wonder if there's an easy way to see what tables are consuming what.

Thanks in advance,

Jorge

Picon
Gravatar

Object Orientation issue

Hi,

I'm trying to use OO in Lua. I wrote the following code:

require('Product')

MockJSonParser = {}
function MockJSonParser:new()
    instance = {}

    setmetatable(instance, self)
    self.__index = MockJSonParser

    instance:setMockProductList()
    return instance
end

function MockJSonParser:setMockProductList()
    self.mockProducts = {}
    self.mockProducts[#self.mockProducts + 1] = Product:new(1,'Android', 10)
    self.mockProducts[#self.mockProducts + 1] = Product:new(2,'R2D2', 15)
    self.mockProducts[#self.mockProducts + 1] = Product:new(3,'C3PO', 500)
    self.mockProducts[#self.mockProducts + 1] = Product:new(4,'Azimo', 2500)
    self.mockProducts[#self.mockProducts + 1] = Product:new(5,'Transformer', 10000)
end

function MockJSonParser:parse()
    print('hi')
    return self.mockProducts
end
---------------------------------------------
parser = MockJSonParser:new()
print(parser)
table.foreach(parser,print)
list = parser:parse()
print(list)
table.foreach(list, print)

Where Product is a table that represents products, which has 3 fields: id, name and price

For some strange reason the "new" method is returning the last Product I've added to mockProducts list.

Here  is the output I got when I run this file:

/usr/bin/lua5.1: ...kspace/T-CommerceClient/media/lua/MockJSonParser.lua:31: attempt to call method 'parse' (a nil value)
stack traceback:
    ...kspace/T-CommerceClient/media/lua/MockJSonParser.lua:31: in main chunk
    [C]: ?
table: 0x8949fd0
id    5
name    Transformer
price    10000

Can anyone tell me what I'm doing wrong?

Thanks

--
João E. Hornburg


Gmane