title: Lua Dump in C++
body: <p>I want to export a binary Chunk with lua_dump or luaU_dump..</p>
  
  <p>The error: the return is only &lt;-LuaR</p>
  
  <p>Don't have a compile error or static problems with the code, only the &lt;-LuaR return</p>
  
  <p>What can i do? to result the problem?</p>
  
  <pre><code>private:
  const char* buildLua(QString luaScript)
  {
      const Proto* f;
      char *byteCode = 0L;
      size_t byteCodeLen = 0;
      wdata wd = { &amp;byteCodeLen, &amp;byteCode };
      string ts = luaScript.toStdString();
      const char* cs;
      lua_State *L = luaL_newstate();
      f=combine(L,0);
      luaL_loadstring(L,ts.c_str());
      luaL_openlibs(L);
      lua_lock(L);
      luaU_dump(L,f,kpt_lua_Writer,&amp;wd,1);
      lua_unlock(L);
      lua_close(L);
      cs = byteCode;
      return cs;
  }
  
  static const char* kpt_lua_Reader(lua_State *L, void *ud, size_t *size)
  {
      UNUSED(L);
      if ((*(int*)ud)--)
      {
          *size=sizeof(FUNCTION)-1;
          return FUNCTION;
      }
      else
      {
          *size=0;
          return NULL;
      }
  }
  
  static int kpt_lua_Writer(lua_State * /*l*/, const void *p, size_t sz, void *ud)
  {
      wdata *wd = (wdata *)ud;
  
      char *newData;
  
      if((newData = (char *)realloc(*(wd-&gt;data), (*(wd-&gt;len)) + sz))) {
          memcpy(newData + (*(wd-&gt;len)), p, sz);
          *(wd-&gt;data) = newData;
          *(wd-&gt;len) += sz;
      } else {
          free(newData);
          return 1;
      }
  
      return 0;
  }
  
  static const Proto* combine(lua_State* L, int n)
  {
      if (n==1)
          return toproto(L,-1);
      else
      {
          Proto* f;
          int i=n;
          if (lua_load(L,kpt_lua_Reader,&amp;i,"=(keppedev)",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
          f=toproto(L,-1);
          for (i=0; i&lt;n; i++)
          {
              f-&gt;p[i]=toproto(L,i-n-1);
              if (f-&gt;p[i]-&gt;sizeupvalues&gt;0) f-&gt;p[i]-&gt;upvalues[0].instack=0;
          }
          f-&gt;sizelineinfo=0;
          return f;
      }
  }
  
  static void fatal(const char* message)
  {
      QWidget *widget = new QWidget();
      QMessageBox::warning(widget,"Keppe Develop",message);
  }
  </code></pre>
  
  <p><a href="http://www.keppe.org/img/LuaR.png" rel="nofollow noreferrer">http://www.keppe.org/img/LuaR.png</a></p>