gpt4 book ai didi

c# - 从 Lua 5.1 移植到 5.2

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:17:02 25 4
gpt4 key购买 nike

我在将一些较旧的 Lua 5.1 代码移植到 Lua 5.2 时遇到了一些问题。我希望能够使用现有的 Lua 5.2 dll/lib,因此任何移植都需要使用 Lua 5.2 的现有 API 来完成。为了让它更复杂一些,我使用 DllImport 来 P/Invoke 一些 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);

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

最佳答案

我相信我已经设法升级了它,所以我将在下面添加我所做的和转换的详细信息。我在 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);
}

关于c# - 从 Lua 5.1 移植到 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28332452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com