lua-users home
lua-l archive

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


Hi Roberto,

Glad to hear your reply. Let me explain the coroutine situation first.

I am using Lua as the scripting in my iPhone code, to handle the
logic, the game flow and so forth. I realized the key point to
cooperate between my host code and the script code is coroutine. what
i am doing is try to hide the explicit call of "yield" in the script
and make the coroutine more like a "thread". so I created a
master/main thread to run each coroutine and handle its returns and
other stuff. and for each call, where there might be a yield, or
specifically in the C/C++ code, I need to dispatch a new
thread/coroutine to run. This is because I need to save the thread
state in its own lua's coroutine state. since there will be many more
calls like this, there explode many more coroutines. I just made a
rough statistic on my current small demo, the numbers goes up to 20+.
I used the basic coroutine pool implementation like I said previously,
many of these coroutines are recycled and then the total creation down
to 4 or 5.

I roughly went through the coroutine codes in lua, it seemed just
allocate and deallocate the memory for each "state". I guess the
optimization on memory will be a good universal solution but for now I
only hope to relieve the coroutines. anyways, it works fine now, just
looking for any method that could make it better.

For another environment table, like I said before, since I dispatch a
new thread, I would like to add some flavors, such as "this" pointer.
what i am doing now is just add an entry to the global table, and
remove after it returns. I heard wow's gui scripting doing this way.
In my case when I create the function closure along with the global
variables, it fails (it should fail). so I am trying to figure out if
the new environment table will work this out internally (because it's
not global?). all in all, I will check out the work2 first and see.

Thanks a lot and look forward to more suggestions!

Hao

On Fri, Apr 2, 2010 at 4:51 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> I read several articles about lua 5.2, but confused about environment
>> tables. some says lua functions don't have environment tables any more, but
>> in the document of lua 5.2-work1/2, it says there is no global tables but
>> environment tables.
>
> The new proposal for environments (based on _ENV) came after the release
> of work2.
>
>
>> so, what kind of environment table is there? I also
>> found a new introducing function, loadin. my concern is where are the
>> entries provding to loadin? in the envrionment table or the global table?
>> what's the scope of those entries? can these variables be a closure?
>
> I did not undestand these questions.
>
>
>> Also, i have some questions about the coroutines. because i will create and
>> dispose lots of coroutines, i have to implement a coroutines pool to
>> optimize this payload. it works fine when a coroutine ends normally, not
>> yielded, no errors. so I can recycle it into my pool. but when the coroutine
>> yields or errors, lua seemed saves the status and the stack of the
>> coroutine, and I didn't find a way to reset the coroutine to the initial
>> state, where I can put a new function to run. in this case, i have to
>> dispose the coroutine rather reuse in my pool. it still works fine now since
>> so far i don't need to terminate a yielding thread, but I guess this would
>> happen in some time... so I was wondering if there is a way to reset a
>> coroutine to the initial state, in 5.1 or any new functions will be
>> introduced to 5.2?
>
> Why can not you simply allow those coroutines to be collected and create
> new ones?
>
> -- Roberto
>