lua-users home
lua-l archive

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


This topic thread is getting deep into Lua Threads, and I'm not going to
touch that aspect, however, even if that approach solves all of your
function naming issues, there is a point I would like to bring up...

The question of a "main" or "Initialize" function for scripts comes up in
this list frequently and the simple answer is not to have a named "main" or
"initialize" in the script files.  When you load a script it will be loaded
as its own chunk (i.e. function like scope).  So all you have to do to "kick
the script off" is run the script as a whole, the script as a whole is
effectively a function (with the exception of issues relating to argument
passing).  If you wish to be able to rerun the "main" function of the script
multiple times, simply load the script and save off the return value from
loading which is the chunk (function) represented by the script (note this
refers to lua_load and its friends such as luaL_loadfile, and NOT to
lua_dofile).

A nice interface for a script to register functions would be for it simply
to return a table containing the functions (since its a chunk it can return
values when run).  Of course if you wish to protect the globals table,
you'll want to set the environment of the script chunk before running it (of
course in this topic thread you are already looking at that in the context
of setting the environment for the thread so you're moving in that direction
with the additional complexity needed to cope with multi-tasking).

Of course, simply setting the environment is not sufficient for real script
security.  To really nail down security between scripts is a complicated
issue, and Kevin Baca started a wiki page devoted to these issues (though
its a bit barren still).  At any rate, hopefully people will contribute to
this wiki topic which can be found at...
http://lua-users.org/wiki/ScriptSecurity

BTW: If you are reading this long winded post and have helpful information
on Script Security please add to the wiki topic (if the wiki will worki).


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of S. Brekelmans
Sent: Saturday, October 18, 2003 1:07 PM
To: lua@bazar2.conectiva.com.br
Subject: Re: Running a script in each Lua thread.



Hello,

I've searched through the archives and it has been very useful in getting
me started with lua. I'm at a bit of an impass right now and am hoping
someone on the list can help.

Currently I am opening a lua_State, registering all of my C functions with
that, and then creating a new thread for each entity I need using
lua_newthread(). Subsequent to creatring a new thread, I load a script
to be run by that thread, and then use lua_getglobal(thread, "funcname")
to find the function I want to call in the script. then I call
lua_resume().
This works perfectly as long as the function name is unique. The problem
is
I would like to have each script have an Initialize() function.
Unfortunately, the Initialize() lua function of whichever script was last
loaded gets called no matter which lua thread I am resuming.

What is the proper way of doing this? My application, like so many others,
is in a game system where I would like to be able to create new game
entities, attach a lua script to then, and let the script run, using
lua_yield()
from within C functions to yield the scripts. I also would like to keep my
function
names identical across all scripts to simplify the calling interface from
C code.
I've tried using multiple lua_open() calls to create seperate lua_States
for each entity,
but then I have to register my C Functions for each new entity. This seems
somewhat
expensive since we are targetting the console market where memory and
speed are a key requirement.

I've seen some details in the archives but they all use lua_cobegin() a
function that
is no longer available in lua-5.0.

Any tips or even pointers to previous message threads about this would be
great. If worse
comes the worse I can create a new lua_State using lua_open() for each
object, but that
seems like too much overhead, what with having to register the C functions
each time.

Many thanks for your time.

Steven Brekelmans