title: How to return C++ object to lua 5.2?
body: <p>How to return C++ object to lua?</p>
  
  <p>My C++ code is following:</p>
  
  <pre><code>class MyClass
  {
  public:
    void say()
    {
      print("Hello\r\n");
    }
  };
  
  int test(lua_State* l)
  {
    MyClass* obj = new MyClass();
    lua_pushlightuserdata(l, obj);
    return 1;
  }
  </code></pre>
  
  <p>Lua Test is following:</p>
  
  <pre><code>local a = MyClass:new()
  a:say()  &lt;--- OK, beacause I set metatable!!
  local b = test()
  b:say()  &lt;--- ERROR: attempt to index local 'b' (a userdata value)
  </code></pre>
  
  <p>How to modify test() function to work fine? <br>
  obj will auto destory by lua ?</p>
  
  <p>PS: I has set MyClass metatable is following</p>
  
  <pre><code>void l_registerClass()
  {
    lua_newtable(l);
    int methods = lua_gettop(l);
    luaL_newmetatable(l, "MyClass");
    int metatable = lua_gettop(l);
    lua_pushvalue(l, methods);
    lua_setglobal(l, "MyClass");
  
    lua_pushvalue(l, methods);  
    l_set(l, metatable, "__metatable");
  
    //set metatable __index
    lua_pushvalue(l, methods);
    l_set(l, metatable, "__index");
  
    //set metatable __gc
    lua_pushcfunction(l, l_destructor);  
    l_set(l, metatable, "__gc");
  
    //set method table
    lua_newtable(l);                // mt for method table  
    lua_pushcfunction(l, l_constructor);  
    lua_pushvalue(l, -1);           // dup new_T function  
    l_set(l, methods, "new");         // add new_T to method table  
    l_set(l, -3, "__call");           // mt.__call = new_T  
    lua_setmetatable(l, methods);  
  
    // set methods metatable   
    lua_pushstring(l, "say");
    lua_pushcclosure(l, l_proxy, 1);  
    lua_settable(l, methods);
  
    lua_pop(l, 2);
  }
  
  int l_proxy(lua_State* l)
  {
    int i = (int)lua_tonumber(l, lua_upvalueindex(1));
    lua_remove(l, 1);  // remove self so member function args start at index 1
    //call real function
    MyClass* obj = getInstance();
    obj-&gt;say();
    return 1;
  }
  </code></pre>
  
  <p>I should don't lost step ? <br>
  I don't use any Lua Binding Framework, I am using pure Lua Library.</p>
  
  <p>==== update 1 ====</p>
  
  <p>Thanks for user1520427's answer, but....</p>
  
  <pre><code>int test(lua_State* l)
  {
    MyClass** c = (MyClass**)lua_newuserdata(l, sizeof(MyClass*));
    *c = new MyClass();       // we manage this
    lua_getglobal(l, "MyClass");
    lua_setmetatable(l, -2);
    return 1;
  }
  </code></pre>
  
  <p>and I test it in Lua</p>
  
  <pre><code>local b = test()
  print( type(b) )
  local meta = getmetatable(b)
  for k,v in pairs(meta) do
    print("    ", k, v)
  end
  </code></pre>
  
  <p>Lua show metatable is correct.</p>
  
  <pre><code>userdata
    say     function: 00602860
    new     function: 00493665
  </code></pre>
  
  <p>But lua still shows the same error in </p>
  
  <pre><code>b:say()   &lt;-- attempt to index local 'b' (a userdata value)
  </code></pre>
  
  <p>=== update 2 ===</p>
  
  <pre><code>int test(lua_State* l)
  {
    MyClass** c = (MyClass**)lua_newuserdata(l, sizeof(MyClass*));
    *c = new MyClass();       // we manage this
    luaL_getmetatable(l, "MyClass");  //
    lua_getglobal(l, "MyClass");
    lua_setmetatable(l, -2);
    return 1;
  }
  </code></pre>
  
  <p>the lua test result:</p>
  
  <pre><code>b:say()   &lt;-- attempt to call method 'say' (a nil value)
  </code></pre>
  
  <p>=== update 3 ===</p>
  
  <pre><code>int test(lua_State* l)
  {
    MyClass** c = (MyClass**)lua_newuserdata(l, sizeof(MyClass*));
    *c = new MyClass();       // we manage this
    luaL_getmetatable(l, "MyClass");  
    luaL_setmetatable(l, "MyClass");  //modify
    return 1;
  }
  </code></pre>
  
  <p>Lua test result:</p>
  
  <pre><code>b:say()   &lt;-- calling 'say' on bad self
  </code></pre>