Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
NLua
Commits
805859a2
Unverified
Commit
805859a2
authored
Feb 10, 2019
by
Vinicius Jarina
Committed by
GitHub
Feb 10, 2019
Browse files
weak ref (#280)
* weak ref * Dont dispose LuaBase on Finalizer.
parent
190a8505
Changes
11
Show whitespace changes
Inline
Side-by-side
src/GenerateEventAssembly/CodeGeneration.cs
View file @
805859a2
...
@@ -726,8 +726,8 @@ namespace NLua
...
@@ -726,8 +726,8 @@ namespace NLua
}
}
var
luaDelegate
=
(
LuaDelegate
)
Activator
.
CreateInstance
(
luaDelegateType
);
var
luaDelegate
=
(
LuaDelegate
)
Activator
.
CreateInstance
(
luaDelegateType
);
luaDelegate
.
f
unction
=
luaFunc
;
luaDelegate
.
F
unction
=
luaFunc
;
luaDelegate
.
r
eturnTypes
=
returnTypes
.
ToArray
();
luaDelegate
.
R
eturnTypes
=
returnTypes
.
ToArray
();
return
Delegate
.
CreateDelegate
(
delegateType
,
luaDelegate
,
"CallFunction"
);
return
Delegate
.
CreateDelegate
(
delegateType
,
luaDelegate
,
"CallFunction"
);
}
}
...
...
src/Lua.cs
View file @
805859a2
...
@@ -1198,6 +1198,11 @@ namespace NLua
...
@@ -1198,6 +1198,11 @@ namespace NLua
}
}
#
region
IDisposable
Members
#
region
IDisposable
Members
~
Lua
()
{
Dispose
();
}
public
virtual
void
Dispose
()
public
virtual
void
Dispose
()
{
{
if
(
_translator
!=
null
)
if
(
_translator
!=
null
)
...
@@ -1209,7 +1214,7 @@ namespace NLua
...
@@ -1209,7 +1214,7 @@ namespace NLua
}
}
Close
();
Close
();
GC
.
WaitForPending
Finalize
rs
(
);
GC
.
Suppress
Finalize
(
this
);
}
}
#
endregion
#
endregion
}
}
...
...
src/LuaBase.cs
View file @
805859a2
...
@@ -9,7 +9,7 @@ namespace NLua
...
@@ -9,7 +9,7 @@ namespace NLua
{
{
private
bool
_disposed
;
private
bool
_disposed
;
protected
readonly
int
_Reference
;
protected
readonly
int
_Reference
;
protected
Lua
_Interpreter
;
protected
WeakReference
<
Lua
>
_Interpreter
;
protected
LuaBase
(
int
reference
)
protected
LuaBase
(
int
reference
)
{
{
...
@@ -27,13 +27,25 @@ namespace NLua
...
@@ -27,13 +27,25 @@ namespace NLua
GC
.
SuppressFinalize
(
this
);
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
)
public
virtual
void
Dispose
(
bool
disposeManagedResources
)
{
{
if
(
_disposed
)
if
(
_disposed
)
return
;
return
;
// TODO: Remove the disposeManagedResources check
if
(
_Reference
!=
0
&&
disposeManagedResources
)
if
(
_Reference
!=
0
&&
disposeManagedResources
)
_Interpreter
.
DisposeInternal
(
_Reference
);
{
DisposeLuaReference
();
}
_Interpreter
=
null
;
_Interpreter
=
null
;
_disposed
=
true
;
_disposed
=
true
;
...
@@ -45,7 +57,11 @@ namespace NLua
...
@@ -45,7 +57,11 @@ namespace NLua
if
(
reference
==
null
)
if
(
reference
==
null
)
return
false
;
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
()
public
override
int
GetHashCode
()
...
...
src/LuaFunction.cs
View file @
805859a2
...
@@ -13,13 +13,13 @@ namespace NLua
...
@@ -13,13 +13,13 @@ namespace NLua
public
LuaFunction
(
int
reference
,
Lua
interpreter
):
base
(
reference
)
public
LuaFunction
(
int
reference
,
Lua
interpreter
):
base
(
reference
)
{
{
function
=
null
;
function
=
null
;
_Interpreter
=
interpreter
;
_Interpreter
=
new
WeakReference
<
Lua
>(
interpreter
)
;
}
}
public
LuaFunction
(
LuaNativeFunction
nativeFunction
,
Lua
interpreter
):
base
(
0
)
public
LuaFunction
(
LuaNativeFunction
nativeFunction
,
Lua
interpreter
):
base
(
0
)
{
{
function
=
nativeFunction
;
function
=
nativeFunction
;
_Interpreter
=
interpreter
;
_Interpreter
=
new
WeakReference
<
Lua
>(
interpreter
)
;
}
}
/*
/*
...
@@ -28,7 +28,11 @@ namespace NLua
...
@@ -28,7 +28,11 @@ namespace NLua
*/
*/
internal
object
[]
Call
(
object
[]
args
,
Type
[]
returnTypes
)
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
...
@@ -37,7 +41,10 @@ namespace NLua
*/
*/
public
object
[]
Call
(
params
object
[]
args
)
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
...
@@ -45,10 +52,14 @@ namespace NLua
*/
*/
internal
void
Push
(
LuaState
luaState
)
internal
void
Push
(
LuaState
luaState
)
{
{
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
;
if
(
_Reference
!=
0
)
if
(
_Reference
!=
0
)
luaState
.
RawGetInteger
(
LuaRegistry
.
Index
,
_Reference
);
luaState
.
RawGetInteger
(
LuaRegistry
.
Index
,
_Reference
);
else
else
_Interpreter
.
PushCSFunction
(
function
);
lua
.
PushCSFunction
(
function
);
}
}
public
override
string
ToString
()
public
override
string
ToString
()
...
@@ -63,8 +74,12 @@ namespace NLua
...
@@ -63,8 +74,12 @@ namespace NLua
if
(
l
==
null
)
if
(
l
==
null
)
return
false
;
return
false
;
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
false
;
if
(
_Reference
!=
0
&&
l
.
_Reference
!=
0
)
if
(
_Reference
!=
0
&&
l
.
_Reference
!=
0
)
return
_Interpreter
.
CompareRef
(
l
.
_Reference
,
_Reference
);
return
lua
.
CompareRef
(
l
.
_Reference
,
_Reference
);
return
function
==
l
.
function
;
return
function
==
l
.
function
;
}
}
...
...
src/LuaTable.cs
View file @
805859a2
using
System
;
using
System.Collections
;
using
System.Collections
;
using
NLua.Extensions
;
using
NLua.Extensions
;
...
@@ -11,7 +12,7 @@ namespace NLua
...
@@ -11,7 +12,7 @@ namespace NLua
{
{
public
LuaTable
(
int
reference
,
Lua
interpreter
):
base
(
reference
)
public
LuaTable
(
int
reference
,
Lua
interpreter
):
base
(
reference
)
{
{
_Interpreter
=
interpreter
;
_Interpreter
=
new
WeakReference
<
Lua
>(
interpreter
)
;
}
}
/*
/*
...
@@ -20,11 +21,17 @@ namespace NLua
...
@@ -20,11 +21,17 @@ namespace NLua
public
object
this
[
string
field
]
{
public
object
this
[
string
field
]
{
get
get
{
{
return
_Interpreter
.
GetObject
(
_Reference
,
field
);
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
null
;
return
lua
.
GetObject
(
_Reference
,
field
);
}
}
set
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
...
@@ -34,23 +41,55 @@ namespace NLua
public
object
this
[
object
field
]
{
public
object
this
[
object
field
]
{
get
get
{
{
return
_Interpreter
.
GetObject
(
_Reference
,
field
);
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
null
;
return
lua
.
GetObject
(
_Reference
,
field
);
}
}
set
set
{
{
_Interpreter
.
SetObject
(
_Reference
,
field
,
value
);
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
;
lua
.
SetObject
(
_Reference
,
field
,
value
);
}
}
}
}
public
IDictionaryEnumerator
GetEnumerator
()
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
...
@@ -59,7 +98,11 @@ namespace NLua
*/
*/
internal
object
RawGet
(
string
field
)
internal
object
RawGet
(
string
field
)
{
{
return
_Interpreter
.
RawGetObject
(
_Reference
,
field
);
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
null
;
return
lua
.
RawGetObject
(
_Reference
,
field
);
}
}
/*
/*
...
...
src/LuaUserData.cs
View file @
805859a2
using
System
;
namespace
NLua
namespace
NLua
{
{
public
class
LuaUserData
:
LuaBase
public
class
LuaUserData
:
LuaBase
{
{
public
LuaUserData
(
int
reference
,
Lua
interpreter
):
base
(
reference
)
public
LuaUserData
(
int
reference
,
Lua
interpreter
):
base
(
reference
)
{
{
_Interpreter
=
interpreter
;
_Interpreter
=
new
WeakReference
<
Lua
>(
interpreter
)
;
}
}
/*
/*
...
@@ -14,11 +16,19 @@ namespace NLua
...
@@ -14,11 +16,19 @@ namespace NLua
public
object
this
[
string
field
]
{
public
object
this
[
string
field
]
{
get
get
{
{
return
_Interpreter
.
GetObject
(
_Reference
,
field
);
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
null
;
return
lua
.
GetObject
(
_Reference
,
field
);
}
}
set
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
...
@@ -28,11 +38,19 @@ namespace NLua
public
object
this
[
object
field
]
{
public
object
this
[
object
field
]
{
get
get
{
{
return
_Interpreter
.
GetObject
(
_Reference
,
field
);
Lua
lua
;
if
(!
_Interpreter
.
TryGetTarget
(
out
lua
))
return
null
;
return
lua
.
GetObject
(
_Reference
,
field
);
}
}
set
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
...
@@ -42,7 +60,11 @@ namespace NLua
*/
*/
public
object
[]
Call
(
params
object
[]
args
)
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
()
public
override
string
ToString
()
...
...
src/Method/LuaDelegate.cs
View file @
805859a2
...
@@ -4,13 +4,13 @@ namespace NLua.Method
...
@@ -4,13 +4,13 @@ namespace NLua.Method
{
{
public
class
LuaDelegate
public
class
LuaDelegate
{
{
public
LuaFunction
f
unction
;
public
LuaFunction
F
unction
;
public
Type
[]
r
eturnTypes
;
public
Type
[]
R
eturnTypes
;
public
LuaDelegate
()
public
LuaDelegate
()
{
{
f
unction
=
null
;
F
unction
=
null
;
r
eturnTypes
=
null
;
R
eturnTypes
=
null
;
}
}
public
object
CallFunction
(
object
[]
args
,
object
[]
inArgs
,
int
[]
outArgs
)
public
object
CallFunction
(
object
[]
args
,
object
[]
inArgs
,
int
[]
outArgs
)
...
@@ -20,9 +20,9 @@ namespace NLua.Method
...
@@ -20,9 +20,9 @@ namespace NLua.Method
// has the positions of out parameters
// has the positions of out parameters
object
returnValue
;
object
returnValue
;
int
iRefArgs
;
int
iRefArgs
;
object
[]
returnValues
=
f
unction
.
Call
(
inArgs
,
r
eturnTypes
);
object
[]
returnValues
=
F
unction
.
Call
(
inArgs
,
R
eturnTypes
);
if
(
r
eturnTypes
[
0
]
==
typeof
(
void
))
if
(
R
eturnTypes
[
0
]
==
typeof
(
void
))
{
{
returnValue
=
null
;
returnValue
=
null
;
iRefArgs
=
0
;
iRefArgs
=
0
;
...
...
src/Method/LuaEventHandler.cs
View file @
805859a2
...
@@ -4,8 +4,6 @@ namespace NLua.Method
...
@@ -4,8 +4,6 @@ namespace NLua.Method
{
{
public
LuaFunction
Handler
=
null
;
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
)
public
void
HandleEvent
(
object
[]
args
)
{
{
Handler
.
Call
(
args
);
Handler
.
Call
(
args
);
...
...
src/Method/RegisterEventHandler.cs
View file @
805859a2
...
@@ -21,8 +21,6 @@ namespace NLua.Method
...
@@ -21,8 +21,6 @@ namespace NLua.Method
*/
*/
public
Delegate
Add
(
LuaFunction
function
)
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
);
Delegate
handlerDelegate
=
CodeGeneration
.
Instance
.
GetDelegate
(
_eventInfo
.
EventHandlerType
,
function
);
_eventInfo
.
AddEventHandler
(
_target
,
handlerDelegate
);
_eventInfo
.
AddEventHandler
(
_target
,
handlerDelegate
);
_pendingEvents
.
Add
(
handlerDelegate
,
this
);
_pendingEvents
.
Add
(
handlerDelegate
,
this
);
...
...
src/ObjectTranslator.cs
View file @
805859a2
...
@@ -57,7 +57,7 @@ namespace NLua
...
@@ -57,7 +57,7 @@ namespace NLua
/// <summary>
/// <summary>
/// We want to ensure that objects always have a unique ID
/// We want to ensure that objects always have a unique ID
/// </summary>
/// </summary>
int
nextObj
=
0
;
int
_
nextObj
;
public
MetaFunctions
MetaFunctionsInstance
=>
metaFunctions
;
public
MetaFunctions
MetaFunctionsInstance
=>
metaFunctions
;
public
Lua
Interpreter
=>
interpreter
;
public
Lua
Interpreter
=>
interpreter
;
...
@@ -415,7 +415,7 @@ namespace NLua
...
@@ -415,7 +415,7 @@ namespace NLua
PushObject
(
luaState
,
obj
,
"luaNet_metatable"
);
PushObject
(
luaState
,
obj
,
"luaNet_metatable"
);
luaState
.
NewTable
();
luaState
.
NewTable
();
luaState
.
PushString
(
"__index"
);
luaState
.
PushString
(
"__index"
);
luaState
.
PushCopy
(-
3
);
;
luaState
.
PushCopy
(-
3
);
luaState
.
SetTable
(-
3
);
luaState
.
SetTable
(-
3
);
luaState
.
PushString
(
"__newindex"
);
luaState
.
PushString
(
"__newindex"
);
luaState
.
PushCopy
(-
3
);
luaState
.
PushCopy
(-
3
);
...
@@ -808,7 +808,7 @@ namespace NLua
...
@@ -808,7 +808,7 @@ namespace NLua
private
int
AddObject
(
object
obj
)
private
int
AddObject
(
object
obj
)
{
{
// New object: inserts it in the list
// New object: inserts it in the list
int
index
=
nextObj
++;
int
index
=
_
nextObj
++;
_objects
[
index
]
=
obj
;
_objects
[
index
]
=
obj
;
if
(!
obj
.
GetType
().
IsValueType
||
obj
.
GetType
().
IsEnum
)
if
(!
obj
.
GetType
().
IsValueType
||
obj
.
GetType
().
IsEnum
)
...
@@ -1022,7 +1022,7 @@ namespace NLua
...
@@ -1022,7 +1022,7 @@ namespace NLua
private
Type
TypeOf
(
LuaState
luaState
,
int
idx
)
private
Type
TypeOf
(
LuaState
luaState
,
int
idx
)
{
{
int
udata
=
luaState
.
CheckUObject
(
1
,
"luaNet_class"
);
int
udata
=
luaState
.
CheckUObject
(
idx
,
"luaNet_class"
);
if
(
udata
==
-
1
)
if
(
udata
==
-
1
)
return
null
;
return
null
;
...
...
tests/src/TestTypes/DoWorkClass.cs
View file @
805859a2
...
@@ -11,9 +11,8 @@ namespace NLuaTest.TestTypes
...
@@ -11,9 +11,8 @@ namespace NLuaTest.TestTypes
public
void
DoWork
()
public
void
DoWork
()
{
{
//simulate work by sleeping
//simulate work by sleeping
Thread
.
Sleep
(
new
Random
().
Next
(
0
,
1000
)
);
Thread
.
Sleep
(
500
);
}
}
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment