title: lua_dump unexpectedly (sometimes) pushes a userdata to the stack
body: <p>When trying to dump a function's bytecode to a buffer, I was surprised to see the Lua stack change size.  <a href="http://www.lua.org/manual/5.2/manual.html#lua_dump" rel="nofollow">Docs say that lua_dump should not push nor pop the stack</a>.  Some functions do and some do not.</p>
  
  <p>This does:</p>
  
  <pre><code>function f() print "x" end
  </code></pre>
  
  <p>This does not:</p>
  
  <pre><code>function f() return "x" end
  </code></pre>
  
  <p>lua_dump in both cases returns 0 (no error).</p>
  
  <p>I assume it has something to do with the use of the <code>print</code> function, but I can't find any docs describing what is going on or what might be contained in the userdata.  ..or whether that bytecode is going to be usable without it.</p>
  
  <p>sample code:</p>
  
  <pre><code>static int base64_write(lua_State* L, unsigned char* str, size_t len, 
          struct luaL_Buffer *buf)
  {
      unsigned int idx;
      char code[5];
      for (idx=0; idx&lt;len; idx++){
          sprintf(code, "\\%03d", (unsigned int) str[idx]);
          //printf(code);
          luaL_addlstring(buf, code, 4);
      }
      //printf("\n");
      return 0;
  }
  
  void dump_function(lua_State* L){
      int stack_sz;
      int res;
      luaL_Buffer buf;
  
      luaL_buffinit(L, &amp;buf);
  
      // ** test 1 - works as expected
      lua_settop(L,0);
      luaL_dostring(L, "function f() return 'x' end");
      lua_getglobal(L, "f");
      printf("stack sz: %i\n", lua_gettop(L));
      res = lua_dump(L, (lua_Writer)base64_write, &amp;buf);
      printf("stack sz: %i\n", lua_gettop(L));
  
      // ** test 2 - not as expected
      lua_settop(L,0);
      luaL_dostring(L, "function f() print 'x' end");
      lua_getglobal(L, "f");
      printf("stack sz: %i\n", lua_gettop(L));
      res = lua_dump(L, (lua_Writer)base64_write, &amp;buf);
      printf("stack sz: %i\n", lua_gettop(L));  // why is this 2?
      printf("type:%s\n", lua_typename(L, lua_type(L, -1))); // why userdata?
  }
  
  /* ----- Registration array ----- */
  static const luaL_Reg R[] = {
      {"test",    dump_function},
      {NULL,      NULL} /* sentinel */
  };
  
  /* ----- Registration function ----- */
  
  LUALIB_API int register_lua_tserialize(lua_State *L)
  {
      lua_newtable(L); 
      lua_setglobal(L, "test"); 
      lua_getglobal(L, "test"); 
      luaL_setfuncs(L, R, 0); 
      lua_settop(L, 0);
      return 1;
  }
  </code></pre>