Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix lua_tostring gc #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 149 additions & 129 deletions Assets/Plugins/Slua_Managed/LuaDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public enum LuaThreadStatus : int

public sealed class LuaIndexes
{
#if LUA_5_3
// for lua5.3
public static int LUA_REGISTRYINDEX = -1000000 - 1000;
#if LUA_5_3
// for lua5.3
public static int LUA_REGISTRYINDEX = -1000000 - 1000;
#else
// for lua5.1 or luajit
public static int LUA_REGISTRYINDEX = -10000;
Expand All @@ -93,18 +93,19 @@ public struct ReaderInfo
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int LuaCSFunction(IntPtr luaState);
#else
public delegate int LuaCSFunction(IntPtr luaState);
#else
public delegate int LuaCSFunction(IntPtr luaState);
#endif

public delegate string LuaChunkReader(IntPtr luaState, ref ReaderInfo data, ref uint size);

public delegate int LuaFunctionCallback(IntPtr luaState);
public class LuaDLL
{
public const int SHORT_STRING_MAX_LEN = 250;
public static int LUA_MULTRET = -1;
#if UNITY_IPHONE && !UNITY_EDITOR
const string LUADLL = "__Internal";
#if UNITY_IPHONE && !UNITY_EDITOR
const string LUADLL = "__Internal";
#else
const string LUADLL = "slua";
#endif
Expand Down Expand Up @@ -224,126 +225,126 @@ public static void lua_newtable(IntPtr luaState)
LuaDLL.lua_createtable(luaState, 0, 0);
}

#if LUA_5_3
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_getglobal(IntPtr luaState, string name);

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_setglobal(IntPtr luaState, string name);

public static void lua_insert(IntPtr luaState, int newTop)
{
lua_rotate(luaState, newTop, 1);
}

public static void lua_pushglobaltable(IntPtr l)
{
lua_rawgeti(l, LuaIndexes.LUA_REGISTRYINDEX, 2);
}

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_rotate(IntPtr luaState, int index, int n);

public static int lua_rawlen(IntPtr luaState, int stackPos)
{
return LuaDLLWrapper.luaS_rawlen(luaState, stackPos);
}

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int luaL_loadbufferx(IntPtr luaState, byte[] buff, int size, string name, IntPtr x);

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_callk(IntPtr luaState, int nArgs, int nResults,int ctx,IntPtr k);

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_pcallk(IntPtr luaState, int nArgs, int nResults, int errfunc,int ctx,IntPtr k);

// [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
// public static extern int luaS_pcall(IntPtr luaState, int nArgs, int nResults, int errfunc);

public static int lua_call(IntPtr luaState, int nArgs, int nResults)
{
return lua_callk(luaState, nArgs, nResults, 0, IntPtr.Zero);
}

public static int lua_pcall(IntPtr luaState, int nArgs, int nResults, int errfunc)
{
return lua_pcallk(luaState, nArgs, nResults, errfunc, 0, IntPtr.Zero);
}

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern double lua_tonumberx(IntPtr luaState, int index, IntPtr x);
public static double lua_tonumber(IntPtr luaState, int index)
{
return lua_tonumberx(luaState, index, IntPtr.Zero);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern Int64 lua_tointegerx(IntPtr luaState, int index,IntPtr x);
public static int lua_tointeger(IntPtr luaState, int index)
{
return (int)lua_tointegerx(luaState, index, IntPtr.Zero);
}


public static int luaL_loadbuffer(IntPtr luaState, byte[] buff, int size, string name)
{
return luaL_loadbufferx(luaState, buff, size, name, IntPtr.Zero);
}

public static void lua_remove(IntPtr l, int idx)
{
lua_rotate(l, (idx), -1);
lua_pop(l, 1);
}

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_rawgeti(IntPtr luaState, int tableIndex, Int64 index);

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_rawseti(IntPtr luaState, int tableIndex, Int64 index);

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_pushinteger(IntPtr luaState, Int64 i);

public static Int64 luaL_checkinteger(IntPtr luaState, int stackPos) {
luaL_checktype(luaState, stackPos, LuaTypes.LUA_TNUMBER);
return lua_tointegerx(luaState, stackPos, IntPtr.Zero);
}

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int luaS_yield(IntPtr luaState,int nrets);

public static int lua_yield(IntPtr luaState,int nrets) {
return luaS_yield(luaState,nrets);
}


[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_resume(IntPtr L, IntPtr from, int narg);
#if LUA_5_3
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_getglobal(IntPtr luaState, string name);
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_setglobal(IntPtr luaState, string name);
public static void lua_insert(IntPtr luaState, int newTop)
{
lua_rotate(luaState, newTop, 1);
}
public static void lua_pushglobaltable(IntPtr l)
{
lua_rawgeti(l, LuaIndexes.LUA_REGISTRYINDEX, 2);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_rotate(IntPtr luaState, int index, int n);
public static int lua_rawlen(IntPtr luaState, int stackPos)
{
return LuaDLLWrapper.luaS_rawlen(luaState, stackPos);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int luaL_loadbufferx(IntPtr luaState, byte[] buff, int size, string name, IntPtr x);
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_callk(IntPtr luaState, int nArgs, int nResults,int ctx,IntPtr k);
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_pcallk(IntPtr luaState, int nArgs, int nResults, int errfunc,int ctx,IntPtr k);
// [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
// public static extern int luaS_pcall(IntPtr luaState, int nArgs, int nResults, int errfunc);
public static int lua_call(IntPtr luaState, int nArgs, int nResults)
{
return lua_callk(luaState, nArgs, nResults, 0, IntPtr.Zero);
}
public static int lua_pcall(IntPtr luaState, int nArgs, int nResults, int errfunc)
{
return lua_pcallk(luaState, nArgs, nResults, errfunc, 0, IntPtr.Zero);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern double lua_tonumberx(IntPtr luaState, int index, IntPtr x);
public static double lua_tonumber(IntPtr luaState, int index)
{
return lua_tonumberx(luaState, index, IntPtr.Zero);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern Int64 lua_tointegerx(IntPtr luaState, int index,IntPtr x);
public static int lua_tointeger(IntPtr luaState, int index)
{
return (int)lua_tointegerx(luaState, index, IntPtr.Zero);
}
public static int luaL_loadbuffer(IntPtr luaState, byte[] buff, int size, string name)
{
return luaL_loadbufferx(luaState, buff, size, name, IntPtr.Zero);
}
public static void lua_remove(IntPtr l, int idx)
{
lua_rotate(l, (idx), -1);
lua_pop(l, 1);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_rawgeti(IntPtr luaState, int tableIndex, Int64 index);
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_rawseti(IntPtr luaState, int tableIndex, Int64 index);
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_pushinteger(IntPtr luaState, Int64 i);
public static Int64 luaL_checkinteger(IntPtr luaState, int stackPos) {
luaL_checktype(luaState, stackPos, LuaTypes.LUA_TNUMBER);
return lua_tointegerx(luaState, stackPos, IntPtr.Zero);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int luaS_yield(IntPtr luaState,int nrets);
public static int lua_yield(IntPtr luaState,int nrets) {
return luaS_yield(luaState,nrets);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_resume(IntPtr L, IntPtr from, int narg);
public static int lua_resume(IntPtr L, int narg)
{
return lua_resume(L, IntPtr.Zero, narg);
}

public static void lua_replace(IntPtr luaState, int index) {
lua_copy(luaState, -1, (index));
lua_pop(luaState, 1);
}

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_copy(IntPtr luaState,int from,int toidx);

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_isinteger(IntPtr luaState, int p);

[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_compare(IntPtr luaState, int index1, int index2, int op);

public static int lua_equal(IntPtr luaState, int index1, int index2)
{
return lua_compare(luaState, index1, index2, 0);
}

public static void lua_replace(IntPtr luaState, int index) {
lua_copy(luaState, -1, (index));
lua_pop(luaState, 1);
}
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_copy(IntPtr luaState,int from,int toidx);
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_isinteger(IntPtr luaState, int p);
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_compare(IntPtr luaState, int index1, int index2, int op);
public static int lua_equal(IntPtr luaState, int index1, int index2)
{
return lua_compare(luaState, index1, index2, 0);
}
#else
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_resume(IntPtr L, int narg);
Expand Down Expand Up @@ -458,9 +459,9 @@ public static void lua_pop(IntPtr luaState, int amount)
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_rawset(IntPtr luaState, int index);

#if LUA_5_3
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_setmetatable(IntPtr luaState, int objIndex);
#if LUA_5_3
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void lua_setmetatable(IntPtr luaState, int objIndex);
#else
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int lua_setmetatable(IntPtr luaState, int objIndex);
Expand Down Expand Up @@ -554,18 +555,37 @@ public static string lua_tostring(IntPtr luaState, int index)
{
int strlen;

/*
* LUA jit ˼· �Ȱ�lua���ַ��� ref����������lua��GC���޷� �ͷ�����ַ���
* Ȼ��c#���дһ��LuaString���Լ�һ��lua�ַ���ָ���ַΪkey��weak�ֵ�
* ��Ϊc#��߸��ַ���ֻ��weak�ֵ��������ù�ϵ������c#���Զ� ������ЩLuaString
* �������LuaString����������������lua��ref,�������ַ�����luaGC�Ϳ�����Ч��
* ��֮ ֻҪ��֤��c#�ַ��� �����ֵ��е�����lua�DZ�û��GC������ַ����ͺð�
* TODO lua5.3 ����ֱ�� �Ѷ��ַ���ȫ�� ������������Ϊ5.3 ���ַ���������GC,����ָ���ַ�����ᷢ���ı䣩
*/

IntPtr str = luaS_tolstring32(luaState, index, out strlen); // fix il2cpp 64 bit
LuaState state = LuaState.get(luaState);
string s = null;
if (strlen > 0 && str != IntPtr.Zero)
{
//Lua 5.3 �汾���ַ���������40���� ���浽intern���棬��Ӧ����һ��
if (strlen <= SHORT_STRING_MAX_LEN && state.TryGetLuaString(str, out s))
{
return s;
}
s = Marshal.PtrToStringAnsi(str);
// fallback method
if(s == null)
if (s == null)
{
byte[] b = new byte[strlen];
Marshal.Copy(str, b, 0, strlen);
s = System.Text.Encoding.Default.GetString(b);
}
if (strlen <= SHORT_STRING_MAX_LEN)
{
state.refString(str, index, s);
}
}
return (s == null) ? string.Empty : s;
}
Expand Down Expand Up @@ -668,8 +688,8 @@ public static int lua_absindex(IntPtr luaState, int index)

public static int lua_upvalueindex(int i)
{
#if LUA_5_3
return LuaIndexes.LUA_REGISTRYINDEX - i;
#if LUA_5_3
return LuaIndexes.LUA_REGISTRYINDEX - i;
#else
return LuaIndexes.LUA_GLOBALSINDEX - i;
#endif
Expand Down
Loading