Picon

Re: LTN9: Creating Strings Piece by Piece

>Any thoughts on making a function that's the opposite as well? Something
>that would take a string and a seperator and return a table whose elements
>contained the original string split apart based on the separator. Or can
>this be done with gsub without creating a bunch of temp strings? I'm not

How about this?

function split(s,t)
 local a={}
 gsub(s,"(.-)"..t,function (x) tinsert(%a,x) end)
end

(This assumes that the separator t occurs at the end of the string s too.)

gsub only creates the the strings for the captures and the string that is
returned (in this cases, the returned string is ignored).
--lhf

Picon

Re: Plua Code Available ? (Palm OS)

On Sat, 2001-09-29 at 10:59, jon fernquest wrote:

> 
> I'd love to volunteer.
> Right now I have a lot of free time.
> I've been poking around a lot with Plua,
> PocketC, and Forth on the Palm, and some simple
> metrowerk's C applications as well as RsrcEdit on the Palm.
> 
> I'm new to Lua and the Palm (Perl, Python, C experience)
> so I'm ready and willing to take some subordinate position
> involving certain coding tasks, testing, documentation, etc...
> 
> Dragon Forth directly exposes the underlying all the low-level
> Palm API functions. This might be too low-level for Lua (?)
> but it would make it easier to build functionality on....like with Plua
> it's not exactly obvious how to extract all the to do list items
> for a given category.
> 
> API functions like PocketC's for memos
> (Palm OS's makeshift file system) , DOC files ,
> and To Do list entries would also be useful.
> 
> Could you point me to the source of the GCC tools you're
> using?

	Well I am using the PRC-tools and the pilrc. Then prc tools is the gcc
for palm and pilrc is a resource compiler. You will also need a palm os
SDK, the last one is the 4.0.

(Continue reading)

Roberto Ierusalimschy | 1 Oct 2001 17:22
Picon

Re: lua_getinfo

On Fri, 28 Sep 2001, Curt Carpenter wrote:

> Why is it that in a call hook calling lua_getinfo(l, "lnuS", ar) will
> sometimes generate an error complaining that "value for `lua_getinfo' is
> not a function"?

A bug?

> Shouldn't by definition the ar I get in a call hook be a function?

Yes.

> Or to rephrase the problem, how can I get as much
> information in a call hook as possible in a generic way without running
> the risk of generating an error?

You should be able to call lua_getinfo without errors. What version are you
using (Lua 4.0 or 4.1)?

-- Roberto

Curt Carpenter | 1 Oct 2001 18:56
Picon
Favicon

RE: lua_getinfo

4.0

-----Original Message-----
From: Roberto Ierusalimschy [mailto:rieru <at> delirius.cs.uiuc.edu] 
Sent: Monday, October 01, 2001 8:23 AM
To: Multiple recipients of list
Subject: Re: lua_getinfo

On Fri, 28 Sep 2001, Curt Carpenter wrote:

> Why is it that in a call hook calling lua_getinfo(l, "lnuS", ar) will 
> sometimes generate an error complaining that "value for `lua_getinfo' 
> is not a function"?

A bug?

> Shouldn't by definition the ar I get in a call hook be a function?

Yes.

> Or to rephrase the problem, how can I get as much
> information in a call hook as possible in a generic way without 
> running the risk of generating an error?

You should be able to call lua_getinfo without errors. What version are
you using (Lua 4.0 or 4.1)?

-- Roberto

(Continue reading)

Paul Hsieh | 2 Oct 2001 00:56
Picon
Favicon

User data garbage collection ...

I've discovered that userdata can be formed from a pointer to memory coming from 
either lua_newuserdata, or malloc/&static as usual.  Is the distinction merely that 
the first will be garbage collected (and luaM_free() will be called on it) and the 
second will not?

Even if that is so, how do I get a callback into my embedding program to call the 
"destructor" at the time of garbage collection for a userdata that I've created?  
The objects I intend to create in my program will be numerous, and use a lot of 
system resources (including a seperate thread), so I need for them to be recycled 
whenever possible.

Is there something I am missing, or is Lua simply not a state where it can solve my 
problem?

--
Paul Hsieh
qed <at> pobox.com

Christian Vogler | 2 Oct 2001 01:03

Re: User data garbage collection ...

On Mon, Oct 01, 2001 at 03:56:18PM -0700, Paul Hsieh wrote:
> Even if that is so, how do I get a callback into my embedding
> program to call the "destructor" at the time of garbage collection
> for a userdata that I've created?  The objects I intend to create in
> my program will be numerous, and use a lot of system resources
> (including a seperate thread), so I need for them to be recycled
> whenever possible.  Is there something I am missing, or is Lua
> simply not a state where it can solve my problem? 

You might want to take a look at the tolua library source code for
ideas, particularly tolua_tm.c. Basically it uses the "gc" tag method
to destroy objects and free up the memory associated with them.

- Christian

Chris Tavares | 2 Oct 2001 01:18

RE: User data garbage collection ...

You should not rely on garbage collectors to free up any resource other than
memory - this holds true for Java, Smalltalk, .NET, Lua, or any other
garbage collected system. The problem is that you generally have no idea
WHEN your object will get collected - it'll happen when the collector needs
more RAM. Typically, system resources that you need collected are a lot
scarcer than memory, so you'll run out of resources long before you run out
of memory.

Granted, you can force lua to do a gc, but that'll have a performance
impact.

If, on the other hand, all you care about is preventing resource leaks, and
don't necessarily care when the cleanup happens, you can use the "gc" tag
method.

-Chris

-----Original Message-----
From: owner-lua-l <at> tecgraf.puc-rio.br
[mailto:owner-lua-l <at> tecgraf.puc-rio.br]On Behalf Of Paul Hsieh
Sent: Monday, October 01, 2001 3:56 PM
To: Multiple recipients of list
Subject: User data garbage collection ...

I've discovered that userdata can be formed from a pointer to memory coming
from
either lua_newuserdata, or malloc/&static as usual.  Is the distinction
merely that
the first will be garbage collected (and luaM_free() will be called on it)
and the
(Continue reading)

Matt Cooper | 2 Oct 2001 04:18
Picon
Picon

How to create tables in C and reference in scripts?

I apologise if this has been covered before and I realise it's a complete newbie
question, but I code for a MUD that has begun utilising Lua to remove some of
the overhead the code is creating.

I have some general knowledge of C and some basic knowledge of Lua and its uses,
but I have been unable to define a table (within C) and then reference the table
from a script. As an example, I have a single entity (a room within the MUD)
which can contain a number of objects or people and I wish to create two tables
for these and attach them to the room entity.

To date, what I have is:
  lua_newtable(L);
  for (i = 1, ppl = room->people; ppl; i++, ppl = ppl->next_in_room) {
    lua_pushstring(L, "ppl"); lua_pushuserdata(L, ppl); lua_rawseti(L, 1, i);
  }
  lua_pushstring(L, "struct"); lua_pushuserdata(L, room); lua_settable(L, -3);

Firstly, is this correct? Can I then reference the people within the room from
the script? Along a more advanced line, how can I then define these "people" as
tables themselves, a table within a table so to speak?

Terence Martin | 2 Oct 2001 08:19

Re: How to create tables in C and reference in scripts?

> Firstly, is this correct? Can I then reference the people within the room
from
> the script? Along a more advanced line, how can I then define these
"people" as
> tables themselves, a table within a table so to speak?
>

A table within a table would be something like this: (untested code)

// Make new outer table. Put an identifier and an empty table
// on the stack
lua_newtable (L);

// Push the key for the sub-table, then the sub table itself,
// and then set the key into the outer table
lua_pushstring (L , "subtable");
lua_newtable (L);
lua_settable (L , -3);

// Now we can set a global for the outer table
lua_setglobal (L , "OuterTable");

This would set up a global variable named OuterTable, which has a key inside
it named "subtable" that is a second table. You can manipulate the
table/subtable from lua just as if you had created them in the script
itself. From code, you would use lua_getglobal to get at the outer table,
and lua_gettable to get at the keys inside it.

Paul Hsieh | 2 Oct 2001 18:35
Picon
Favicon

RE: User data garbage collection ...

Thanks for everyone's help.  I was trying to figure this all out by just looking through the 
lau.h file and I was looking for some kind of c function pointer declaration being passed 
around (D'oh!).
Well ok since "gettagmethod(tag(obj), "gc")" appears to be deprecated, how am I supposed to stimulate
explicit 
garbage collection?  I.e., basically I need a kind of "free".  Obviously there is a kind of chicken and egg
thing with trying 
to pass a object to be "freed" in a garbage collecting language like Lua, so the next best thing is to just lose
the reference 
then explicitly garbage collect a particular tag (I don't really want to garbage collect the whole
system).  However 
looking through lgc.c there does not seem to be a way to do that.
Without something like this my choices are 1) explicitely garbage collect the whole system or 2) write my
own explicit 
free which frees all the extraneous stuff in the userdata except for the containing base memory which would
be cleaned 
up at garbage collection time.  Is this correct?

--
Paul Hsieh
qed <at> pobox.com


Gmane