lua-users home
lua-l archive

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

The C Api allows you to set a hook which is triggered every n instructions -
which seems to be a fair way too. 
I have no idea how it slows down the execution speed. You could benchmark this
by turning this checking on and off and consider if the loss of speed is
It could also be imaginable to modify the either the loaded string or the
compiled bytecode by inserting count functions, like changing this here:

function foo () 
  return foo()

while true do 


function foo() callhookcount()
  return foo()

while true do callhookcount()

This would not change the line numbering and could be done before loading the
lua-code string. The callhookcount function would raise an error if it was
called for a certain amount of calls. This workaround would be quite cheap.

I would however also appreciate the possibility to specify a hook on lua side
that is called after a specified number of instructions without activating
unnecessary debugging codes in the VM. Such an extension would allow to create
something similiar like preemptive single threaded multitasking by yielding
routines automatically - with the benefit of being able to monitor the exact
amount of used resources by each coroutine.


> Hello all,
> I'm taken with the idea of embedding Lua for use in a multi-user game
> environment, where users will be able to write and load Lua objects and
> functions in real time that will interact in the game.
> However, I am looking for a way to put limits on the code that users can
> run.  Namely, I don't want to see somebody kick something off with an
> infinite loop and lock up the entire game.
> The solution seems to be to use Lua's debugging API to let the system
> check the running process occasionally, and limit either the amount of
> execution time a particular function gets, or the number of times that it
> can loop through a particular point.  If I'm not mistaken, the line hook
> should be sufficient for this sort of thing.
> My question is, primarily, is this a bad way to go about it?  The hooks
> are classified as being there for debugging purposes, and I'm essentially
> wanting to use them to create a sandbox.  Are there any problems or
> pitfalls to this approach that I should be aware of?  Would I be taking a
> significant performance hit with this sort of monitoring?
> I'd appreciate any thoughts or admonitions that the community might have
> on this matter before I write a lot of ill advised code.  If you need more
> of an idea of what I'm wanting to do, let me know, I'll be glad to share
> my schemes.