从Lua 5.1移植到5.2

时间:2015-02-04 22:09:59

标签: c# c++ lua porting lua-5.2

我有一些问题将一些旧的Lua 5.1代码移植到Lua 5.2。我希望能够使用Lua 5.2 dll / lib库存,因此需要使用现有的Lua 5.2 API完成任何移植。为了使它更复杂一点,我使用DllImport来调用/调用一些Lua API调用。这意味着提供的任何#define快捷方式都不起作用。例如,使用lua_pushglobaltable是不可能的。由于LUA_REGISTRYINDEX不再可访问,因此需要进行大多数更新。

到目前为止我的内容如下:


1a)替换

lua_pushstring(luaState, "tablename");
lua_settable(luaState, LUA_REGISTRYINDEX); // LUA_REGISTRYINDEX no longer accessible

1b)

lua_setglobal(luaState, "tablename");

2a)替换

lua_pushstring(luaState, "tablename");
lua_gettable(luaState, LUA_REGISTRYINDEX); // LUA_REGISTRYINDEX no longer accessible

2b)用

lua_getglobal(luaState, "tablename");

3a)替换

lua_pushvalue(luaState, LUA_GLOBALSINDEX);

3b)

// not sure, something equivalent to lua_pushglobaltable(L)

4a)替换

lua_replace(luaState, LUA_GLOBALSINDEX);

4b)随着

// I dont even have a guess here

5a)替换

luaL_ref(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); // also luaL_unref

5b)随着

luaL_ref(luaState, <some arbitrary constant>); // this is probably wrong

6a)替换

lua_rawgeti(luaState, LUA_REGISTRYINDEX, reference);

6b)用

lua_rawgeti(luaState, <same arbitrary constant>, reference); // this is probably wrong

7a)替换

lua_pcall(IntPtr luaState, int nArgs, int nResults, int errfunc);

7b)随着

lua_pcallk(IntPtr luaState, int nArgs, int nResults, int errfunc, int ctx, [MarshalAs(UnmanagedType.FunctionPtr)]LuaCSFunction function);
lua_pcallk(luaState, nArgs, nResults, errfunc, 0, null);

8a)替换

lua_getfield(luaState, LUA_REGISTRYINDEX, meta);

8b)用

luaL_setmetatable(IntPtr luaState, string meta);

9a)替换

lua_rawset(luaState, LUA_REGISTRYINDEX);

9b)用

lua_settable(luaState, -3);

现在一切都编译,但我得到内存访问冲突异常,这意味着我可能会错误地替换一些东西。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我相信我已经设法升级了这个,所以我将添加下面的内容和转换的详细信息。我在LUA低级API中创建了一个C包装器,以导出我需要的功能:

1a)替换

lua_settable(luaState, LUA_REGISTRYINDEX); // LUA_REGISTRYINDEX no longer accessible

1b)

lua_settablereg(luaState);

2a)替换

lua_gettable(luaState, LUA_REGISTRYINDEX); // LUA_REGISTRYINDEX no longer accessible

2b)用

lua_gettablereg(luaState);

3a)替换

lua_pushvalue(luaState, LUA_GLOBALSINDEX);

3b)

lua_pushglobaltablefunction(luaState)

4a)替换

lua_replace(luaState, LUA_GLOBALSINDEX);

4b)随着

lua_popglobaltablefunction(luaState)

5a)替换

luaL_ref(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); // also luaL_unref

5b)随着

luaL_refreg(luaState); // also luaL_unrefreg

6a)替换

lua_rawgeti(luaState, LUA_REGISTRYINDEX, reference);

6b)用

lua_rawgetireg(luaState, reference);

7a)替换

lua_pcall(luaState, nArgs, nResults, errfunc);

7b)随着

lua_pcalla(luaState, nArgs, nResults, errfunc);

8a)替换

lua_getfield(luaState, LUA_REGISTRYINDEX, meta);

8b)用

lua_getfieldreg(luaState, string meta);

9a)替换

lua_rawset(luaState, LUA_REGISTRYINDEX);

9b)用

lua_rawsetreg(luaState);

定义如下:

__declspec(dllexport) void lua_pushglobaltablefunction(lua_State *L) 
{
   lua_pushglobaltable(L);
}

__declspec(dllexport) void lua_popglobaltablefunction(lua_State *L) 
{
   lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
}

__declspec(dllexport) int luaL_regref(lua_State *L) 
{
   return luaL_ref(L, LUA_REGISTRYINDEX);
}

__declspec(dllexport) void luaL_unregref(lua_State *L, int reference)
{
   luaL_unref(L, LUA_REGISTRYINDEX, reference);
}

__declspec(dllexport) void lua_settablereg(lua_State *L)
{
   lua_settable(L, LUA_REGISTRYINDEX);
}

__declspec(dllexport) void lua_gettablereg(lua_State *L)
{
   lua_gettable(L, LUA_REGISTRYINDEX);
}

__declspec(dllexport) void lua_rawsetreg(lua_State *L)
{
   lua_rawset(L, LUA_REGISTRYINDEX);
}

__declspec(dllexport) void lua_rawgetreg(lua_State *L)
{
   lua_rawget(L, LUA_REGISTRYINDEX);
}

__declspec(dllexport) void lua_rawgetireg(lua_State *L, int reference)
{
   lua_rawgeti(L, LUA_REGISTRYINDEX, reference);
}

__declspec(dllexport) void lua_getfieldreg(lua_State *L,const char *fieldname)
{
   lua_getfield(L, LUA_REGISTRYINDEX, fieldname);
}

__declspec(dllexport) int luaL_loadbuffername(lua_State *L,const char *buff, int size,const char * name)
{
   return luaL_loadbuffer(L,buff,size,name);
}

__declspec(dllexport) double lua_tonum(lua_State *L, int index)
{
   return lua_tonumber(L, index);
}

__declspec(dllexport) int lua_pcalla(lua_State *L,int nArgs,int nResults,int errfunc)
{
   return lua_pcall(L,nArgs,nResults,errfunc);
}