title: Protecting application from erroneous Lua script
body: <p>I am using Lua in a microcontroller project.
  My software uses an RTOS, and appart from its other tasks, it also calls a Lua script that the user provides.</p>
  
  <p>The script must have two functions: an <code>init</code> function and a <code>run</code> function. My application calls the <code>init</code> function during initalization, and then calls the <code>run</code> function periodically.</p>
  
  <p>The way I load the script is something very similar to <code>dofile()</code>.</p>
  
  <p>The <code>run</code> function is needed to return. This way the underlying thread yields the processor for the other lower priority threads.</p>
  
  <p>So the question is, how can I protect the system from a user providing a "dangerous" script, i.e. a script that never finishes its initial execution, before I have the chance to call the functions I need. 
  See the following:</p>
  
  <pre><code>function init
      --Do some stuff.
  end
  
  function run
      --Do some other stuff.
  end
  
  while (true) do
      --Do nasty stuff, without ever returning.
  end
  </code></pre>
  
  <p>On the above example the code blocks during the initial loading of the script, and never returns. I never get the chance to call the <code>init</code> and <code>run</code> functions. How can I detect such cases, and how to get protected?</p>
  
  <p><strong>Edit</strong>
  Forgot to mention. I am using Lua 5.2.2.</p>