2

Luaコードで

Test = {}
function Test:new()
  local obj = {}
  setmetatable(obj, self)
  self.__index = self
  return obj
end
local a = Test:new()
a.ID = "abc123"
callCfunc(a)

Cコードで

int callCfunc(lua_State * l)
{
   void* obj = lua_topointer(l, 1);            //I hope get lua's a variable
   lua_pushlightuserdata(l, obj);   
   lua_getfield(l, 1, "ID");
   std::string id = lua_tostring(l, 1);        //I hoe get the value "abc123"
   ...
   return 0;
}

しかし、私のCの結果は

id = null

なんで?正常に動作するようにコードを変更するにはどうすればよいですか?
PS:luaへのCテストクラスマッピングを作成したくありません

==== update1 ====
さらに、正しい受信パラメータを確認するためのテストコードを追加しました。

int callCfunc(lua_State * l)
{
   std::string typeName = lua_typename(l, lua_type(l, 1));    // the typeName=="table"
   void* obj = lua_topointer(l, 1);            //I hope get lua's a variable
   lua_pushlightuserdata(l, obj);   
   lua_getfield(l, 1, "ID");
   std::string id = lua_tostring(l, 1);        //I hoe get the value "abc123"
   ...
   return 0;
}

結果

typeName == "table" 

したがって、着信パラメータタイプは正しいです

4

2 に答える 2

3


正しいcコードが必要な理由を見つけました...
In C Code

int callCfunc(lua_State * l)
{
   lua_getfield(l, 1, "ID");
   std::string id = lua_tostring(l, -1);        //-1
   ...
   return 0;
}
于 2013-01-08T08:21:45.430 に答える
0

たぶんこれ - 申し訳ありませんがテストしていません - 手元にコンパイラがありません

入力はスタックの一番上にある lua からのテーブルなので、getfield(l,1, "ID") はスタックの一番上のテーブル (この場合は入力テーブル) からフィールド ID を取得する必要があります。次に、結果をスタックの一番上にプッシュします

int callCfunc(lua_State * l)
{
   lua_getfield(l, 1, "ID");
   std::string id = lua_tostring(l, 1);        //I hoe get the value "abc123"
   ...
   return 0;
}
于 2013-01-08T07:07:24.843 に答える