title: Compile Lua script to unsigned char buffer
body: <p>Im working on a server in C that dynamically generating Lua commands on the fly and send them by socket to the clients. Right now the server is using plain text, but I would like the server to pre-compile the script before sending it to the clients.</p>
  
  <p>I check luac.c but couldn't find how to be able to do something like this:</p>
  
  <pre><code>char lua_commands[ 1024 ] = { "a = 123; b = 456; c = a + b;" };
  
  int socket 
  unsigned int send_buffer_size
  unsigned char *send_buffer
  
  /* Compile lua_commands and store the binary script into send_buffer without
  having to write first the .out on disk then read it again in order store the content
  into send_buffer */
  
  send( socket, send_buffer, send_buffer_size, 0 );
  </code></pre>
  
  <p>Anybody can help me to achieve this?</p>
  
  <p>[ Update ]</p>
  
  <p>Ok, I think I figure it out:</p>
  
  <pre><code>#include "lua.h"
  #include "lauxlib.h"
  #include "ldo.h"
  #include "lfunc.h"
  #include "lmem.h"
  #include "lobject.h"
  #include "lopcodes.h"
  #include "lstring.h"
  #include "lundump.h"
  
  
  #define toproto(L,i) (clvalue(L-&gt;top+(i))-&gt;l.p)
  
  static int writer( lua_State *L, const void *p, size_t size, void *u ){
  
      unsigned int i = 0;
  
      unsigned char *d = ( unsigned char * )p;
  
      // Print all the bytes on the console.
      while( i != size ) {
          printf("%d ", d[ i ] );
          ++i;
      }
      return 0;
  }
  
  
  void compile( lua_State *L, char *command ){
  
      const Proto* f;
  
      if (luaL_loadstring( L, command ) !=0 ) {
          printf( "%s\n", lua_tostring( L, -1 ) );
      }
  
      f = toproto( L,-1 );
  
      lua_lock( L );
  
      luaU_dump( L, f, writer, NULL, 1 );
  
      lua_unlock( L );    
  }
  
  
  int main (int argc, const char * argv[]) {
  
      lua_State *L = lua_open(); 
  
      compile( L, "a = 123; b = 456; c = a + b; print( c );" );
  
      lua_close( L );
  
      return 0;
  }
  </code></pre>
  
  <p>However that leads me to another question, do I have to close and reopen (lua_open, lua_close) the Lua state every time I'm calling my compile() function with other Lua commands or the output will only be the result of the latest luaL_loadstring?</p>
  
  <p>Im not sure but look to me from the toproto macro definition that the top most stack will be returned am I correct?</p>