Unverified Commit 805859a2 authored by Vinicius Jarina's avatar Vinicius Jarina Committed by GitHub
Browse files

weak ref (#280)

* weak ref

* Dont dispose LuaBase on Finalizer.
parent 190a8505
......@@ -726,8 +726,8 @@ namespace NLua
}
var luaDelegate = (LuaDelegate)Activator.CreateInstance(luaDelegateType);
luaDelegate.function = luaFunc;
luaDelegate.returnTypes = returnTypes.ToArray();
luaDelegate.Function = luaFunc;
luaDelegate.ReturnTypes = returnTypes.ToArray();
return Delegate.CreateDelegate(delegateType, luaDelegate, "CallFunction");
}
......
......@@ -1198,6 +1198,11 @@ namespace NLua
}
#region IDisposable Members
~Lua()
{
Dispose();
}
public virtual void Dispose()
{
if (_translator != null)
......@@ -1209,7 +1214,7 @@ namespace NLua
}
Close();
GC.WaitForPendingFinalizers();
GC.SuppressFinalize(this);
}
#endregion
}
......
......@@ -9,7 +9,7 @@ namespace NLua
{
private bool _disposed;
protected readonly int _Reference;
protected Lua _Interpreter;
protected WeakReference<Lua> _Interpreter;
protected LuaBase(int reference)
{
......@@ -27,13 +27,25 @@ namespace NLua
GC.SuppressFinalize(this);
}
void DisposeLuaReference()
{
if (_Interpreter == null)
return;
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return;
lua.DisposeInternal(_Reference);
}
public virtual void Dispose(bool disposeManagedResources)
{
if (_disposed)
return;
// TODO: Remove the disposeManagedResources check
if (_Reference != 0 && disposeManagedResources)
_Interpreter.DisposeInternal(_Reference);
{
DisposeLuaReference();
}
_Interpreter = null;
_disposed = true;
......@@ -45,7 +57,11 @@ namespace NLua
if (reference == null)
return false;
return _Interpreter.CompareRef(reference._Reference, _Reference);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return false;
return lua.CompareRef(reference._Reference, _Reference);
}
public override int GetHashCode()
......
......@@ -13,13 +13,13 @@ namespace NLua
public LuaFunction(int reference, Lua interpreter):base(reference)
{
function = null;
_Interpreter = interpreter;
_Interpreter = new WeakReference<Lua>(interpreter);
}
public LuaFunction(LuaNativeFunction nativeFunction, Lua interpreter):base (0)
{
function = nativeFunction;
_Interpreter = interpreter;
_Interpreter = new WeakReference<Lua>(interpreter);
}
/*
......@@ -28,7 +28,11 @@ namespace NLua
*/
internal object[] Call(object[] args, Type[] returnTypes)
{
return _Interpreter.CallFunction(this, args, returnTypes);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.CallFunction(this, args, returnTypes);
}
/*
......@@ -37,7 +41,10 @@ namespace NLua
*/
public object[] Call(params object[] args)
{
return _Interpreter.CallFunction(this, args);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.CallFunction(this, args);
}
/*
......@@ -45,10 +52,14 @@ namespace NLua
*/
internal void Push(LuaState luaState)
{
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return;
if (_Reference != 0)
luaState.RawGetInteger(LuaRegistry.Index, _Reference);
else
_Interpreter.PushCSFunction(function);
lua.PushCSFunction(function);
}
public override string ToString()
......@@ -63,8 +74,12 @@ namespace NLua
if (l == null)
return false;
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return false;
if (_Reference != 0 && l._Reference != 0)
return _Interpreter.CompareRef(l._Reference, _Reference);
return lua.CompareRef(l._Reference, _Reference);
return function == l.function;
}
......

using System;
using System.Collections;
using NLua.Extensions;
......@@ -11,7 +12,7 @@ namespace NLua
{
public LuaTable(int reference, Lua interpreter):base(reference)
{
_Interpreter = interpreter;
_Interpreter = new WeakReference<Lua>(interpreter);
}
/*
......@@ -20,11 +21,17 @@ namespace NLua
public object this[string field] {
get
{
return _Interpreter.GetObject(_Reference, field);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.GetObject(_Reference, field);
}
set
{
_Interpreter.SetObject(_Reference, field, value);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return;
lua.SetObject(_Reference, field, value);
}
}
......@@ -34,23 +41,55 @@ namespace NLua
public object this[object field] {
get
{
return _Interpreter.GetObject(_Reference, field);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.GetObject(_Reference, field);
}
set
{
_Interpreter.SetObject(_Reference, field, value);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return;
lua.SetObject(_Reference, field, value);
}
}
public IDictionaryEnumerator GetEnumerator()
{
return _Interpreter.GetTableDict(this).GetEnumerator();
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.GetTableDict(this).GetEnumerator();
}
public ICollection Keys
{
get
{
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.GetTableDict(this).Keys;
}
}
public ICollection Keys => _Interpreter.GetTableDict(this).Keys;
public ICollection Values
{
get
{
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
public ICollection Values => _Interpreter.GetTableDict(this).Values;
return lua.GetTableDict(this).Values;
}
}
/*
......@@ -59,7 +98,11 @@ namespace NLua
*/
internal object RawGet(string field)
{
return _Interpreter.RawGetObject(_Reference, field);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.RawGetObject(_Reference, field);
}
/*
......

using System;
namespace NLua
{
public class LuaUserData : LuaBase
{
public LuaUserData(int reference, Lua interpreter):base(reference)
{
_Interpreter = interpreter;
_Interpreter = new WeakReference<Lua>(interpreter);
}
/*
......@@ -14,11 +16,19 @@ namespace NLua
public object this[string field] {
get
{
return _Interpreter.GetObject(_Reference, field);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.GetObject(_Reference, field);
}
set
{
_Interpreter.SetObject(_Reference, field, value);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return;
lua.SetObject(_Reference, field, value);
}
}
......@@ -28,11 +38,19 @@ namespace NLua
public object this[object field] {
get
{
return _Interpreter.GetObject(_Reference, field);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.GetObject(_Reference, field);
}
set
{
_Interpreter.SetObject(_Reference, field, value);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return;
lua.SetObject(_Reference, field, value);
}
}
......@@ -42,7 +60,11 @@ namespace NLua
*/
public object[] Call(params object[] args)
{
return _Interpreter.CallFunction(this, args);
Lua lua;
if (!_Interpreter.TryGetTarget(out lua))
return null;
return lua.CallFunction(this, args);
}
public override string ToString()
......
......@@ -4,13 +4,13 @@ namespace NLua.Method
{
public class LuaDelegate
{
public LuaFunction function;
public Type[] returnTypes;
public LuaFunction Function;
public Type[] ReturnTypes;
public LuaDelegate()
{
function = null;
returnTypes = null;
Function = null;
ReturnTypes = null;
}
public object CallFunction(object[] args, object[] inArgs, int[] outArgs)
......@@ -20,9 +20,9 @@ namespace NLua.Method
// has the positions of out parameters
object returnValue;
int iRefArgs;
object[] returnValues = function.Call(inArgs, returnTypes);
object[] returnValues = Function.Call(inArgs, ReturnTypes);
if (returnTypes[0] == typeof(void))
if (ReturnTypes[0] == typeof(void))
{
returnValue = null;
iRefArgs = 0;
......
......@@ -4,8 +4,6 @@ namespace NLua.Method
{
public LuaFunction Handler = null;
// CP: Fix provided by Ben Bryant for delegates with one param
// link: http://luaforge.net/forum/message.php?msg_id=9318
public void HandleEvent(object[] args)
{
Handler.Call(args);
......
......@@ -21,8 +21,6 @@ namespace NLua.Method
*/
public Delegate Add(LuaFunction function)
{
//CP: Fix by Ben Bryant for event handling with one parameter
//link: http://luaforge.net/forum/message.php?msg_id=9266
Delegate handlerDelegate = CodeGeneration.Instance.GetDelegate(_eventInfo.EventHandlerType, function);
_eventInfo.AddEventHandler(_target, handlerDelegate);
_pendingEvents.Add(handlerDelegate, this);
......
......@@ -57,7 +57,7 @@ namespace NLua
/// <summary>
/// We want to ensure that objects always have a unique ID
/// </summary>
int nextObj = 0;
int _nextObj;
public MetaFunctions MetaFunctionsInstance => metaFunctions;
public Lua Interpreter => interpreter;
......@@ -415,7 +415,7 @@ namespace NLua
PushObject(luaState, obj, "luaNet_metatable");
luaState.NewTable();
luaState.PushString("__index");
luaState.PushCopy(-3);;
luaState.PushCopy(-3);
luaState.SetTable(-3);
luaState.PushString("__newindex");
luaState.PushCopy(-3);
......@@ -808,7 +808,7 @@ namespace NLua
private int AddObject(object obj)
{
// New object: inserts it in the list
int index = nextObj++;
int index = _nextObj++;
_objects[index] = obj;
if (!obj.GetType().IsValueType || obj.GetType().IsEnum)
......@@ -1022,7 +1022,7 @@ namespace NLua
private Type TypeOf(LuaState luaState, int idx)
{
int udata = luaState.CheckUObject(1, "luaNet_class");
int udata = luaState.CheckUObject(idx, "luaNet_class");
if (udata == -1)
return null;
......
......@@ -11,9 +11,8 @@ namespace NLuaTest.TestTypes
public void DoWork()
{
//simulate work by sleeping
Thread.Sleep(new Random().Next(0, 1000));
Thread.Sleep(500);
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment