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
be0bfcc8
Commit
be0bfcc8
authored
Oct 07, 2014
by
Mervill
Browse files
Forced consistent line endings.
parent
e5330179
Changes
14
Hide whitespace changes
Inline
Side-by-side
Core/NLua/CheckType.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
using
System.Reflection
;
using
System.Collections.Generic
;
...
...
@@ -47,7 +47,7 @@ namespace NLua
*/
sealed
class
CheckType
{
#if SILVERLIGHT
#if SILVERLIGHT
private
Dictionary
<
Type
,
ExtractValue
>
extractValues
=
new
Dictionary
<
Type
,
ExtractValue
>();
#else
private
Dictionary
<
long
,
ExtractValue
>
extractValues
=
new
Dictionary
<
long
,
ExtractValue
>
();
...
...
@@ -57,25 +57,25 @@ namespace NLua
public
CheckType
(
ObjectTranslator
translator
)
{
this
.
translator
=
translator
;
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
object
)),
new
ExtractValue
(
GetAsObject
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
sbyte
)),
new
ExtractValue
(
GetAsSbyte
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
byte
)),
new
ExtractValue
(
GetAsByte
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
short
)),
new
ExtractValue
(
GetAsShort
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
ushort
)),
new
ExtractValue
(
GetAsUshort
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
int
)),
new
ExtractValue
(
GetAsInt
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
uint
)),
new
ExtractValue
(
GetAsUint
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
long
)),
new
ExtractValue
(
GetAsLong
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
ulong
)),
new
ExtractValue
(
GetAsUlong
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
double
)),
new
ExtractValue
(
GetAsDouble
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
char
)),
new
ExtractValue
(
GetAsChar
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
float
)),
new
ExtractValue
(
GetAsFloat
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
decimal
)),
new
ExtractValue
(
GetAsDecimal
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
bool
)),
new
ExtractValue
(
GetAsBoolean
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
string
)),
new
ExtractValue
(
GetAsString
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
char
[])),
new
ExtractValue
(
GetAsCharArray
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
LuaFunction
)),
new
ExtractValue
(
GetAsFunction
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
LuaTable
)),
new
ExtractValue
(
GetAsTable
));
this
.
translator
=
translator
;
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
object
)),
new
ExtractValue
(
GetAsObject
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
sbyte
)),
new
ExtractValue
(
GetAsSbyte
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
byte
)),
new
ExtractValue
(
GetAsByte
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
short
)),
new
ExtractValue
(
GetAsShort
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
ushort
)),
new
ExtractValue
(
GetAsUshort
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
int
)),
new
ExtractValue
(
GetAsInt
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
uint
)),
new
ExtractValue
(
GetAsUint
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
long
)),
new
ExtractValue
(
GetAsLong
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
ulong
)),
new
ExtractValue
(
GetAsUlong
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
double
)),
new
ExtractValue
(
GetAsDouble
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
char
)),
new
ExtractValue
(
GetAsChar
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
float
)),
new
ExtractValue
(
GetAsFloat
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
decimal
)),
new
ExtractValue
(
GetAsDecimal
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
bool
)),
new
ExtractValue
(
GetAsBoolean
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
string
)),
new
ExtractValue
(
GetAsString
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
char
[])),
new
ExtractValue
(
GetAsCharArray
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
LuaFunction
)),
new
ExtractValue
(
GetAsFunction
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
LuaTable
)),
new
ExtractValue
(
GetAsTable
));
extractValues
.
Add
(
GetExtractDictionaryKey
(
typeof
(
LuaUserData
)),
new
ExtractValue
(
GetAsUserdata
));
extractNetObject
=
new
ExtractValue
(
GetAsNetObject
);
}
...
...
@@ -93,8 +93,8 @@ namespace NLua
{
if
(
paramType
.
IsByRef
)
paramType
=
paramType
.
GetElementType
();
var
extractKey
=
GetExtractDictionaryKey
(
paramType
);
var
extractKey
=
GetExtractDictionaryKey
(
paramType
);
return
extractValues
.
ContainsKey
(
extractKey
)
?
extractValues
[
extractKey
]
:
extractNetObject
;
}
...
...
@@ -109,7 +109,7 @@ namespace NLua
if
(
underlyingType
!=
null
)
paramType
=
underlyingType
;
// Silently convert nullable types to their non null requics
var
extractKey
=
GetExtractDictionaryKey
(
paramType
);
if
(
paramType
.
Equals
(
typeof
(
object
)))
...
...
@@ -119,7 +119,7 @@ namespace NLua
if
(
paramType
.
IsGenericParameter
)
{
if
(
luatype
==
LuaTypes
.
Boolean
)
return
extractValues
[
GetExtractDictionaryKey
(
typeof
(
bool
))];
else
if
(
luatype
==
LuaTypes
.
String
)
else
if
(
luatype
==
LuaTypes
.
String
)
return
extractValues
[
GetExtractDictionaryKey
(
typeof
(
string
))];
else
if
(
luatype
==
LuaTypes
.
Table
)
return
extractValues
[
GetExtractDictionaryKey
(
typeof
(
LuaTable
))];
...
...
@@ -130,23 +130,23 @@ namespace NLua
else
if
(
luatype
==
LuaTypes
.
Number
)
return
extractValues
[
GetExtractDictionaryKey
(
typeof
(
double
))];
}
bool
netParamIsString
=
paramType
==
typeof
(
string
)
||
paramType
==
typeof
(
char
[]);
bool
netParamIsNumeric
=
paramType
==
typeof
(
int
)
||
paramType
==
typeof
(
uint
)
||
paramType
==
typeof
(
long
)
||
paramType
==
typeof
(
ulong
)
||
paramType
==
typeof
(
short
)
||
paramType
==
typeof
(
float
)
||
paramType
==
typeof
(
double
)
||
paramType
==
typeof
(
decimal
)
||
paramType
==
typeof
(
byte
);
if
(
netParamIsNumeric
)
{
if
(
LuaLib
.
LuaIsNumber
(
luaState
,
stackPos
)
&&
!
netParamIsString
)
return
extractValues
[
extractKey
];
bool
netParamIsString
=
paramType
==
typeof
(
string
)
||
paramType
==
typeof
(
char
[]);
bool
netParamIsNumeric
=
paramType
==
typeof
(
int
)
||
paramType
==
typeof
(
uint
)
||
paramType
==
typeof
(
long
)
||
paramType
==
typeof
(
ulong
)
||
paramType
==
typeof
(
short
)
||
paramType
==
typeof
(
float
)
||
paramType
==
typeof
(
double
)
||
paramType
==
typeof
(
decimal
)
||
paramType
==
typeof
(
byte
);
if
(
netParamIsNumeric
)
{
if
(
LuaLib
.
LuaIsNumber
(
luaState
,
stackPos
)
&&
!
netParamIsString
)
return
extractValues
[
extractKey
];
}
else
if
(
paramType
==
typeof
(
bool
))
{
if
(
LuaLib
.
LuaIsBoolean
(
luaState
,
stackPos
))
return
extractValues
[
extractKey
];
return
extractValues
[
extractKey
];
}
else
if
(
netParamIsString
)
{
if
(
LuaLib
.
LuaNetIsStringStrict
(
luaState
,
stackPos
))
return
extractValues
[
extractKey
];
...
...
@@ -183,17 +183,17 @@ namespace NLua
}
return
null
;
}
#if SILVERLIGHT
private
Type
GetExtractDictionaryKey
(
Type
targetType
)
{
return
targetType
;
}
#else
private
long
GetExtractDictionaryKey
(
Type
targetType
)
{
return
targetType
.
TypeHandle
.
Value
.
ToInt64
();
}
#if SILVERLIGHT
private
Type
GetExtractDictionaryKey
(
Type
targetType
)
{
return
targetType
;
}
#else
private
long
GetExtractDictionaryKey
(
Type
targetType
)
{
return
targetType
.
TypeHandle
.
Value
.
ToInt64
();
}
#endif
...
...
@@ -313,10 +313,10 @@ namespace NLua
private
object
GetAsBoolean
(
LuaState
luaState
,
int
stackPos
)
{
return
LuaLib
.
LuaToBoolean
(
luaState
,
stackPos
);
}
}
private
object
GetAsCharArray
(
LuaState
luaState
,
int
stackPos
)
{
if
(!
LuaLib
.
LuaNetIsStringStrict
(
luaState
,
stackPos
))
...
...
@@ -326,7 +326,7 @@ namespace NLua
}
private
object
GetAsString
(
LuaState
luaState
,
int
stackPos
)
{
{
if
(!
LuaLib
.
LuaNetIsStringStrict
(
luaState
,
stackPos
))
return
null
;
string
retVal
=
LuaLib
.
LuaToString
(
luaState
,
stackPos
).
ToString
();
...
...
Core/NLua/Event/DebugHookEventArgs.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
namespace
NLua.Event
...
...
Core/NLua/Extensions/GeneralExtensions.cs
View file @
be0bfcc8
...
...
@@ -21,11 +21,11 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using
System
;
using
System.Linq
;
using
System.Collections.Generic
;
using
System.Reflection
;
*/
using
System
;
using
System.Linq
;
using
System.Collections.Generic
;
using
System.Reflection
;
using
System.Runtime.CompilerServices
;
namespace
NLua.Extensions
...
...
@@ -42,7 +42,7 @@ namespace NLua.Extensions
/// <returns>
/// <c>true</c> if the specified obj is null; otherwise, <c>false</c>.
/// </returns>
///
///
#if USE_KOPILUA
public
static
bool
IsNull
(
object
obj
)
...
...
@@ -58,152 +58,152 @@ namespace NLua.Extensions
#endif
}
static
class
TypeExtensions
{
public
static
bool
HasMethod
(
this
Type
t
,
string
name
)
{
var
op
=
t
.
GetMethods
(
BindingFlags
.
Public
|
BindingFlags
.
Instance
|
BindingFlags
.
Static
);
return
op
.
Count
(
m
=>
m
.
Name
==
name
)
>
0
;
}
public
static
bool
HasAdditionOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Addition"
);
}
public
static
bool
HasSubtractionOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Subtraction"
);
}
public
static
bool
HasMultiplyOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Multiply"
);
}
public
static
bool
HasDivisionOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Division"
);
}
public
static
bool
HasModulusOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Modulus"
);
}
public
static
bool
HasUnaryNegationOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
// Unary - will always have only one version.
var
op
=
t
.
GetMethod
(
"op_UnaryNegation"
,
BindingFlags
.
Public
|
BindingFlags
.
Instance
|
BindingFlags
.
Static
);
return
op
!=
null
;
}
public
static
bool
HasEqualityOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Equality"
);
}
public
static
bool
HasLessThanOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_LessThan"
);
}
public
static
bool
HasLessThanOrEqualOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_LessThanOrEqual"
);
}
public
static
IEnumerable
<
MethodInfo
>
GetMethods
(
this
Type
t
,
string
name
,
BindingFlags
flags
)
{
return
t
.
GetMethods
(
flags
).
Where
(
m
=>
m
.
Name
==
name
);
}
public
static
MethodInfo
[]
GetExtensionMethods
(
this
Type
type
,
IEnumerable
<
Assembly
>
assemblies
=
null
)
{
List
<
Type
>
types
=
new
List
<
Type
>
();
types
.
AddRange
(
type
.
Assembly
.
GetTypes
().
Where
(
t
=>
t
.
IsPublic
));
if
(
assemblies
!=
null
)
{
foreach
(
Assembly
item
in
assemblies
)
{
if
(
item
==
type
.
Assembly
)
continue
;
types
.
AddRange
(
item
.
GetTypes
().
Where
(
t
=>
t
.
IsPublic
));
}
}
var
query
=
from
extensionType
in
types
where
extensionType
.
IsSealed
&&
!
extensionType
.
IsGenericType
&&
!
extensionType
.
IsNested
from
method
in
extensionType
.
GetMethods
(
BindingFlags
.
Static
|
BindingFlags
.
Public
)
where
method
.
IsDefined
(
typeof
(
ExtensionAttribute
),
false
)
where
method
.
GetParameters
()
[
0
].
ParameterType
==
type
select
method
;
return
query
.
ToArray
<
MethodInfo
>
();
}
/// <summary>
/// Extends the System.Type-type to search for a given extended MethodeName.
/// </summary>
/// <param name="MethodeName">Name of the Methode</param>
/// <returns>the found Methode or null</returns>
public
static
MethodInfo
GetExtensionMethod
(
this
Type
t
,
string
name
,
IEnumerable
<
Assembly
>
assemblies
=
null
)
{
var
mi
=
from
methode
in
t
.
GetExtensionMethods
(
assemblies
)
where
methode
.
Name
==
name
select
methode
;
if
(!
mi
.
Any
<
MethodInfo
>
())
return
null
;
else
return
mi
.
First
<
MethodInfo
>
();
}
static
class
TypeExtensions
{
public
static
bool
HasMethod
(
this
Type
t
,
string
name
)
{
var
op
=
t
.
GetMethods
(
BindingFlags
.
Public
|
BindingFlags
.
Instance
|
BindingFlags
.
Static
);
return
op
.
Count
(
m
=>
m
.
Name
==
name
)
>
0
;
}
public
static
bool
HasAdditionOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Addition"
);
}
public
static
bool
HasSubtractionOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Subtraction"
);
}
public
static
bool
HasMultiplyOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Multiply"
);
}
public
static
bool
HasDivisionOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Division"
);
}
public
static
bool
HasModulusOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Modulus"
);
}
public
static
bool
HasUnaryNegationOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
// Unary - will always have only one version.
var
op
=
t
.
GetMethod
(
"op_UnaryNegation"
,
BindingFlags
.
Public
|
BindingFlags
.
Instance
|
BindingFlags
.
Static
);
return
op
!=
null
;
}
public
static
bool
HasEqualityOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_Equality"
);
}
public
static
bool
HasLessThanOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_LessThan"
);
}
public
static
bool
HasLessThanOrEqualOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
return
t
.
HasMethod
(
"op_LessThanOrEqual"
);
}
public
static
IEnumerable
<
MethodInfo
>
GetMethods
(
this
Type
t
,
string
name
,
BindingFlags
flags
)
{
return
t
.
GetMethods
(
flags
).
Where
(
m
=>
m
.
Name
==
name
);
}
public
static
MethodInfo
[]
GetExtensionMethods
(
this
Type
type
,
IEnumerable
<
Assembly
>
assemblies
=
null
)
{
List
<
Type
>
types
=
new
List
<
Type
>
();
types
.
AddRange
(
type
.
Assembly
.
GetTypes
().
Where
(
t
=>
t
.
IsPublic
));
if
(
assemblies
!=
null
)
{
foreach
(
Assembly
item
in
assemblies
)
{
if
(
item
==
type
.
Assembly
)
continue
;
types
.
AddRange
(
item
.
GetTypes
().
Where
(
t
=>
t
.
IsPublic
));
}
}
var
query
=
from
extensionType
in
types
where
extensionType
.
IsSealed
&&
!
extensionType
.
IsGenericType
&&
!
extensionType
.
IsNested
from
method
in
extensionType
.
GetMethods
(
BindingFlags
.
Static
|
BindingFlags
.
Public
)
where
method
.
IsDefined
(
typeof
(
ExtensionAttribute
),
false
)
where
method
.
GetParameters
()
[
0
].
ParameterType
==
type
select
method
;
return
query
.
ToArray
<
MethodInfo
>
();
}
/// <summary>
/// Extends the System.Type-type to search for a given extended MethodeName.
/// </summary>
/// <param name="MethodeName">Name of the Methode</param>
/// <returns>the found Methode or null</returns>
public
static
MethodInfo
GetExtensionMethod
(
this
Type
t
,
string
name
,
IEnumerable
<
Assembly
>
assemblies
=
null
)
{
var
mi
=
from
methode
in
t
.
GetExtensionMethods
(
assemblies
)
where
methode
.
Name
==
name
select
methode
;
if
(!
mi
.
Any
<
MethodInfo
>
())
return
null
;
else
return
mi
.
First
<
MethodInfo
>
();
}
}
static
class
StringExtensions
{
public
static
IEnumerable
<
string
>
SplitWithEscape
(
this
string
input
,
char
separator
,
char
escapeCharacter
)
{
int
start
=
0
;
int
index
=
0
;
while
(
index
<
input
.
Length
)
{
index
=
input
.
IndexOf
(
separator
,
index
);
if
(
index
==
-
1
)
break
;
if
(
input
[
index
-
1
]
==
escapeCharacter
)
{
input
=
input
.
Remove
(
index
-
1
,
1
);
continue
;
}
yield
return
input
.
Substring
(
start
,
index
-
start
);
index
++;
start
=
index
;
}
yield
return
input
.
Substring
(
start
);
}
static
class
StringExtensions
{
public
static
IEnumerable
<
string
>
SplitWithEscape
(
this
string
input
,
char
separator
,
char
escapeCharacter
)
{
int
start
=
0
;
int
index
=
0
;
while
(
index
<
input
.
Length
)
{
index
=
input
.
IndexOf
(
separator
,
index
);
if
(
index
==
-
1
)
break
;
if
(
input
[
index
-
1
]
==
escapeCharacter
)
{
input
=
input
.
Remove
(
index
-
1
,
1
);
continue
;
}
yield
return
input
.
Substring
(
start
,
index
-
start
);
index
++;
start
=
index
;
}
yield
return
input
.
Substring
(
start
);
}
}
}
\ No newline at end of file
Core/NLua/GenerateEventAssembly/ClassGenerator.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
namespace
NLua
...
...
Core/NLua/GenerateEventAssembly/DelegateGenerator.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
namespace
NLua
...
...
Core/NLua/Lua.cs
View file @
be0bfcc8
...
...
@@ -22,9 +22,9 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
using
System.IO
;
using
System.IO
;
using
System.Linq
;
using
System.Threading
;
using
System.Reflection
;
...
...
@@ -101,45 +101,45 @@ namespace NLua
//private object luaLock = new object();
private
bool
_StatePassed
;
private
bool
executing
;
static
string
initLuanet
=
@"local metatable = {}
local rawget = rawget
local import_type = luanet.import_type
local load_assembly = luanet.load_assembly
luanet.error, luanet.type = error, type
-- Lookup a .NET identifier component.
function metatable:__index(key) -- key is e.g. 'Form'
-- Get the fully-qualified name, e.g. 'System.Windows.Forms.Form'
local fqn = rawget(self,'.fqn')
fqn = ((fqn and fqn .. '.') or '') .. key
-- Try to find either a luanet function or a CLR type
local obj = rawget(luanet,key) or import_type(fqn)
-- If key is neither a luanet function or a CLR type, then it is simply
-- an identifier component.
if obj == nil then
-- It might be an assembly, so we load it too.
pcall(load_assembly,fqn)
obj = { ['.fqn'] = fqn }
setmetatable(obj, metatable)
end
-- Cache this lookup
rawset(self, key, obj)
return obj
end
-- A non-type has been called; e.g. foo = System.Foo()
function metatable:__call(...)
error('No such type: ' .. rawget(self,'.fqn'), 2)
end
-- This is the root of the .NET namespace
luanet['.fqn'] = false
setmetatable(luanet, metatable)
-- Preload the mscorlib assembly
static
string
initLuanet
=
@"local metatable = {}
local rawget = rawget
local import_type = luanet.import_type
local load_assembly = luanet.load_assembly
luanet.error, luanet.type = error, type
-- Lookup a .NET identifier component.
function metatable:__index(key) -- key is e.g. 'Form'
-- Get the fully-qualified name, e.g. 'System.Windows.Forms.Form'
local fqn = rawget(self,'.fqn')
fqn = ((fqn and fqn .. '.') or '') .. key
-- Try to find either a luanet function or a CLR type
local obj = rawget(luanet,key) or import_type(fqn)
-- If key is neither a luanet function or a CLR type, then it is simply
-- an identifier component.
if obj == nil then
-- It might be an assembly, so we load it too.
pcall(load_assembly,fqn)
obj = { ['.fqn'] = fqn }
setmetatable(obj, metatable)
end
-- Cache this lookup
rawset(self, key, obj)
return obj
end
-- A non-type has been called; e.g. foo = System.Foo()
function metatable:__call(...)
error('No such type: ' .. rawget(self,'.fqn'), 2)
end
-- This is the root of the .NET namespace
luanet['.fqn'] = false
setmetatable(luanet, metatable)
-- Preload the mscorlib assembly
luanet.load_assembly('mscorlib')"
;
static
string
clr_package
=
@"---
...
...
@@ -267,56 +267,56 @@ end
return
globals
;
}
}
#
endregion
public
Lua
()
{
luaState
=
LuaLib
.
LuaLNewState
();
LuaLib
.
LuaLOpenLibs
(
luaState
);
Init
();
// We need to keep this in a managed reference so the delegate doesn't get garbage collected
panicCallback
=
new
LuaNativeFunction
(
PanicCallback
);
LuaLib
.
LuaAtPanic
(
luaState
,
panicCallback
);
}
/*
* CAUTION: NLua.Lua instances can't share the same lua state!
*/
public
Lua
(
LuaState
lState
)
{
LuaLib
.
LuaPushString
(
lState
,
"LUAINTERFACE LOADED"
);
LuaLib
.
LuaGetTable
(
lState
,
(
int
)
LuaIndexes
.
Registry
);
if
(
LuaLib
.
LuaToBoolean
(
lState
,
-
1
))
{
LuaLib
.
LuaSetTop
(
lState
,
-
2
);
throw
new
LuaException
(
"There is already a NLua.Lua instance associated with this Lua state"
);
}
else
{
luaState
=
lState
;
_StatePassed
=
true
;
LuaLib
.
LuaSetTop
(
luaState
,
-
2
);
Init
();
}
}
void
Init
()
{
LuaLib
.
LuaPushString
(
luaState
,
"LUAINTERFACE LOADED"
);
LuaLib
.
LuaPushBoolean
(
luaState
,
true
);
LuaLib
.
LuaSetTable
(
luaState
,
(
int
)
LuaIndexes
.
Registry
);
if
(
_StatePassed
==
false
)
{
LuaLib
.
LuaNewTable
(
luaState
);
LuaLib
.
LuaSetGlobal
(
luaState
,
"luanet"
);
}
LuaLib
.
LuaNetPushGlobalTable
(
luaState
);
LuaLib
.
LuaGetGlobal
(
luaState
,
"luanet"
);
LuaLib
.
LuaPushString
(
luaState
,
"getmetatable"
);
LuaLib
.
LuaGetGlobal
(
luaState
,
"getmetatable"
);
LuaLib
.
LuaSetTable
(
luaState
,
-
3
);
LuaLib
.
LuaNetPopGlobalTable
(
luaState
);
translator
=
new
ObjectTranslator
(
this
,
luaState
);
ObjectTranslatorPool
.
Instance
.
Add
(
luaState
,
translator
);
LuaLib
.
LuaNetPopGlobalTable
(
luaState
);
LuaLib
.
LuaLDoString
(
luaState
,
Lua
.
initLuanet
);
#
endregion
public
Lua
()
{
luaState
=
LuaLib
.
LuaLNewState
();
LuaLib
.
LuaLOpenLibs
(
luaState
);
Init
();
// We need to keep this in a managed reference so the delegate doesn't get garbage collected
panicCallback
=
new
LuaNativeFunction
(
PanicCallback
);
LuaLib
.
LuaAtPanic
(
luaState
,
panicCallback
);
}
/*
* CAUTION: NLua.Lua instances can't share the same lua state!
*/
public
Lua
(
LuaState
lState
)
{
LuaLib
.
LuaPushString
(
lState
,
"LUAINTERFACE LOADED"
);
LuaLib
.
LuaGetTable
(
lState
,
(
int
)
LuaIndexes
.
Registry
);
if
(
LuaLib
.
LuaToBoolean
(
lState
,
-
1
))
{
LuaLib
.
LuaSetTop
(
lState
,
-
2
);
throw
new
LuaException
(
"There is already a NLua.Lua instance associated with this Lua state"
);
}
else
{
luaState
=
lState
;
_StatePassed
=
true
;
LuaLib
.
LuaSetTop
(
luaState
,
-
2
);
Init
();
}
}
void
Init
()
{
LuaLib
.
LuaPushString
(
luaState
,
"LUAINTERFACE LOADED"
);
LuaLib
.
LuaPushBoolean
(
luaState
,
true
);
LuaLib
.
LuaSetTable
(
luaState
,
(
int
)
LuaIndexes
.
Registry
);
if
(
_StatePassed
==
false
)
{
LuaLib
.
LuaNewTable
(
luaState
);
LuaLib
.
LuaSetGlobal
(
luaState
,
"luanet"
);
}
LuaLib
.
LuaNetPushGlobalTable
(
luaState
);
LuaLib
.
LuaGetGlobal
(
luaState
,
"luanet"
);
LuaLib
.
LuaPushString
(
luaState
,
"getmetatable"
);
LuaLib
.
LuaGetGlobal
(
luaState
,
"getmetatable"
);
LuaLib
.
LuaSetTable
(
luaState
,
-
3
);
LuaLib
.
LuaNetPopGlobalTable
(
luaState
);
translator
=
new
ObjectTranslator
(
this
,
luaState
);
ObjectTranslatorPool
.
Instance
.
Add
(
luaState
,
translator
);
LuaLib
.
LuaNetPopGlobalTable
(
luaState
);
LuaLib
.
LuaLDoString
(
luaState
,
Lua
.
initLuanet
);
}
public
void
Close
()
...
...
@@ -440,68 +440,68 @@ end
var
result
=
translator
.
GetFunction
(
luaState
,
-
1
);
translator
.
PopValues
(
luaState
,
oldTop
);
return
result
;
}
/// <summary>
/// Executes a Lua chunk and returns all the chunk's return values in an array.
/// </summary>
/// <param name = "chunk">Chunk to execute</param>
/// <param name = "chunkName">Name to associate with the chunk. Defaults to "chunk".</param>
/// <returns></returns>
public
object
[]
DoString
(
byte
[]
chunk
,
string
chunkName
=
"chunk"
)
{
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
executing
=
true
;
if
(
LuaLib
.
LuaLLoadBuffer
(
luaState
,
chunk
,
chunkName
)
==
0
)
{
try
{
if
(
LuaLib
.
LuaPCall
(
luaState
,
0
,
-
1
,
0
)
==
0
)
return
translator
.
PopValues
(
luaState
,
oldTop
);
else
ThrowExceptionFromError
(
oldTop
);
}
finally
{
executing
=
false
;
}
}
else
ThrowExceptionFromError
(
oldTop
);
return
null
;
// Never reached - keeps compiler happy
}
/// <summary>
/// Executes a Lua chunk and returns all the chunk's return values in an array.
/// </summary>
/// <param name = "chunk">Chunk to execute</param>
/// <param name = "chunkName">Name to associate with the chunk. Defaults to "chunk".</param>
/// <returns></returns>
public
object
[]
DoString
(
string
chunk
,
string
chunkName
=
"chunk"
)
{
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
executing
=
true
;
if
(
LuaLib
.
LuaLLoadBuffer
(
luaState
,
chunk
,
chunkName
)
==
0
)
{
try
{
if
(
LuaLib
.
LuaPCall
(
luaState
,
0
,
-
1
,
0
)
==
0
)
return
translator
.
PopValues
(
luaState
,
oldTop
);
else
ThrowExceptionFromError
(
oldTop
);
}
finally
{
executing
=
false
;
}
}
else
ThrowExceptionFromError
(
oldTop
);
return
null
;
// Never reached - keeps compiler happy
}
/// <summary>
/// Executes a Lua chunk and returns all the chunk's return values in an array.
/// </summary>
/// <param name = "chunk">Chunk to execute</param>
/// <param name = "chunkName">Name to associate with the chunk. Defaults to "chunk".</param>
/// <returns></returns>
public
object
[]
DoString
(
byte
[]
chunk
,
string
chunkName
=
"chunk"
)
{
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
executing
=
true
;
if
(
LuaLib
.
LuaLLoadBuffer
(
luaState
,
chunk
,
chunkName
)
==
0
)
{
try
{
if
(
LuaLib
.
LuaPCall
(
luaState
,
0
,
-
1
,
0
)
==
0
)
return
translator
.
PopValues
(
luaState
,
oldTop
);
else
ThrowExceptionFromError
(
oldTop
);
}
finally
{
executing
=
false
;
}
}
else
ThrowExceptionFromError
(
oldTop
);
return
null
;
// Never reached - keeps compiler happy
}
/// <summary>
/// Executes a Lua chunk and returns all the chunk's return values in an array.
/// </summary>
/// <param name = "chunk">Chunk to execute</param>
/// <param name = "chunkName">Name to associate with the chunk. Defaults to "chunk".</param>
/// <returns></returns>
public
object
[]
DoString
(
string
chunk
,
string
chunkName
=
"chunk"
)
{
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
executing
=
true
;
if
(
LuaLib
.
LuaLLoadBuffer
(
luaState
,
chunk
,
chunkName
)
==
0
)
{
try
{
if
(
LuaLib
.
LuaPCall
(
luaState
,
0
,
-
1
,
0
)
==
0
)
return
translator
.
PopValues
(
luaState
,
oldTop
);
else
ThrowExceptionFromError
(
oldTop
);
}
finally
{
executing
=
false
;
}
}
else
ThrowExceptionFromError
(
oldTop
);
return
null
;
// Never reached - keeps compiler happy
}
/*
...
...
@@ -537,18 +537,18 @@ end
public
object
this
[
string
fullPath
]
{
get
{
object
returnValue
=
null
;
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
string
[]
path
=
FullPathToArray
(
fullPath
);
LuaLib
.
LuaGetGlobal
(
luaState
,
path
[
0
]);
returnValue
=
translator
.
GetObject
(
luaState
,
-
1
);
returnValue
=
translator
.
GetObject
(
luaState
,
-
1
);
LuaBase
dispose
=
null
;
if
(
path
.
Length
>
1
)
{
if
(
path
.
Length
>
1
)
{
dispose
=
returnValue
as
LuaBase
;
string
[]
remainingPath
=
new
string
[
path
.
Length
-
1
];
Array
.
Copy
(
path
,
1
,
remainingPath
,
0
,
path
.
Length
-
1
);
returnValue
=
GetObject
(
remainingPath
);
if
(
dispose
!=
null
)
returnValue
=
GetObject
(
remainingPath
);
if
(
dispose
!=
null
)
dispose
.
Dispose
();
}
...
...
@@ -556,7 +556,7 @@ end
return
returnValue
;
}
set
{
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
string
[]
path
=
FullPathToArray
(
fullPath
);
if
(
path
.
Length
==
1
)
{
translator
.
Push
(
luaState
,
value
);
...
...
@@ -804,16 +804,16 @@ end
LuaLib
.
LuaSetTable
(
luaState
,
-
3
);
}
string
[]
FullPathToArray
(
string
fullPath
)
{
return
fullPath
.
SplitWithEscape
(
'.'
,
'\\'
).
ToArray
();
string
[]
FullPathToArray
(
string
fullPath
)
{
return
fullPath
.
SplitWithEscape
(
'.'
,
'\\'
).
ToArray
();
}
/*
* Creates a new table as a global variable or as a field
* inside an existing table
*/
public
void
NewTable
(
string
fullPath
)
{
{
string
[]
path
=
FullPathToArray
(
fullPath
);
int
oldTop
=
LuaLib
.
LuaGetTop
(
luaState
);
...
...
@@ -924,14 +924,14 @@ end
return
LuaCore
.
LuaSetLocal
(
luaState
,
luaDebug
,
n
).
ToString
();
}
public
int
GetStack
(
int
level
,
ref
LuaDebug
ar
)
{
return
LuaCore
.
LuaGetStack
(
luaState
,
level
,
ref
ar
);
}
public
int
GetInfo
(
string
what
,
ref
LuaDebug
ar
)
{
return
LuaCore
.
LuaGetInfo
(
luaState
,
what
,
ref
ar
);
public
int
GetStack
(
int
level
,
ref
LuaDebug
ar
)
{
return
LuaCore
.
LuaGetStack
(
luaState
,
level
,
ref
ar
);
}
public
int
GetInfo
(
string
what
,
ref
LuaDebug
ar
)
{
return
LuaCore
.
LuaGetInfo
(
luaState
,
what
,
ref
ar
);
}
/// <summary>
...
...
@@ -966,16 +966,16 @@ end
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaHook
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
#if USE_KOPILUA
#if USE_KOPILUA
static
void
DebugHookCallback
(
LuaState
luaState
,
LuaDebug
debug
)
{
#else
static
void
DebugHookCallback
(
LuaState
luaState
,
IntPtr
luaDebug
)
{
LuaDebug
debug
=
(
LuaDebug
)
System
.
Runtime
.
InteropServices
.
Marshal
.
PtrToStructure
(
luaDebug
,
typeof
(
LuaDebug
));
#endif
ObjectTranslator
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
Lua
lua
=
translator
.
Interpreter
;
{
LuaDebug
debug
=
(
LuaDebug
)
System
.
Runtime
.
InteropServices
.
Marshal
.
PtrToStructure
(
luaDebug
,
typeof
(
LuaDebug
));
#endif
ObjectTranslator
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
Lua
lua
=
translator
.
Interpreter
;
lua
.
DebugHookCallbackInternal
(
luaState
,
debug
);
}
...
...
Core/NLua/LuaFunction.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
using
System.Text
;
using
System.Collections.Generic
;
...
...
Core/NLua/LuaLib/LuaLib.cs
View file @
be0bfcc8
...
...
@@ -23,7 +23,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
using
System.IO
;
using
NLua.Extensions
;
...
...
@@ -130,12 +130,12 @@ namespace NLua
}
public
static
void
LuaGetGlobal
(
LuaState
luaState
,
string
name
)
{
{
LuaCore
.
LuaNetGetGlobal
(
luaState
,
name
);
}
public
static
void
LuaSetGlobal
(
LuaState
luaState
,
string
name
)
{
{
LuaCore
.
LuaNetSetGlobal
(
luaState
,
name
);
}
...
...
@@ -272,11 +272,11 @@ namespace NLua
public
static
bool
LuaIsString
(
LuaState
luaState
,
int
index
)
{
return
LuaCore
.
LuaIsString
(
luaState
,
index
)
!=
0
;
}
public
static
bool
LuaNetIsStringStrict
(
LuaState
luaState
,
int
index
)
{
return
LuaCore
.
LuaNetIsStringStrict
(
luaState
,
index
)
!=
0
;
}
public
static
bool
LuaNetIsStringStrict
(
LuaState
luaState
,
int
index
)
{
return
LuaCore
.
LuaNetIsStringStrict
(
luaState
,
index
)
!=
0
;
}
public
static
bool
LuaIsCFunction
(
LuaState
luaState
,
int
index
)
...
...
@@ -383,7 +383,7 @@ namespace NLua
}
public
static
int
LuaLLoadBuffer
(
LuaState
luaState
,
string
buff
,
string
name
)
{
{
return
LuaCore
.
LuaNetLoadBuffer
(
luaState
,
buff
,
(
uint
)
0
,
name
);
}
...
...
@@ -400,12 +400,12 @@ namespace NLua
public
static
bool
LuaLCheckMetatable
(
LuaState
luaState
,
int
index
)
{
return
LuaCore
.
LuaLCheckMetatable
(
luaState
,
index
);
}
public
static
int
LuaNetRegistryIndex
()
{
return
LuaCore
.
LuaNetRegistryIndex
();
}
}
public
static
int
LuaNetRegistryIndex
()
{
return
LuaCore
.
LuaNetRegistryIndex
();
}
public
static
int
LuaNetToNetObject
(
LuaState
luaState
,
int
index
)
{
...
...
@@ -452,14 +452,14 @@ namespace NLua
return
LuaCore
.
LuaNetGetTag
();
}
public
static
void
LuaNetPushGlobalTable
(
LuaState
luaState
)
{
LuaCore
.
LuaNetPushGlobalTable
(
luaState
);
}
public
static
void
LuaNetPopGlobalTable
(
LuaState
luaState
)
{
LuaCore
.
LuaNetPopGlobalTable
(
luaState
);
public
static
void
LuaNetPushGlobalTable
(
LuaState
luaState
)
{
LuaCore
.
LuaNetPushGlobalTable
(
luaState
);
}
public
static
void
LuaNetPopGlobalTable
(
LuaState
luaState
)
{
LuaCore
.
LuaNetPopGlobalTable
(
luaState
);
}
}
}
\ No newline at end of file
Core/NLua/LuaTable.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
using
System.Text
;
using
System.Collections
;
...
...
Core/NLua/LuaUserData.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
using
System.Text
;
using
System.Collections.Generic
;
...
...
Core/NLua/Metatables.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using
System
;
using
System
;
using
System.Linq
;
using
System.IO
;
using
System.Collections
;
...
...
@@ -51,11 +51,11 @@ namespace NLua
*
*/
public
class
MetaFunctions
{
public
LuaNativeFunction
GcFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
IndexFunction
{
get
;
private
set
;
}
{
public
LuaNativeFunction
GcFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
IndexFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
NewIndexFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
BaseIndexFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
BaseIndexFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
ClassIndexFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
ClassNewindexFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
ExecuteDelegateFunction
{
get
;
private
set
;
}
...
...
@@ -63,13 +63,13 @@ namespace NLua
public
LuaNativeFunction
ToStringFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
AddFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
SubtractFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
MultiplyFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
DivisionFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
ModulosFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
UnaryNegationFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
EqualFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
LessThanFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
SubtractFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
MultiplyFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
DivisionFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
ModulosFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
UnaryNegationFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
EqualFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
LessThanFunction
{
get
;
private
set
;
}
public
LuaNativeFunction
LessThanOrEqualFunction
{
get
;
private
set
;
}
Dictionary
<
object
,
object
>
memberCache
=
new
Dictionary
<
object
,
object
>
();
...
...
@@ -77,25 +77,25 @@ namespace NLua
/*
* __index metafunction for CLR objects. Implemented in Lua.
*/
static
string
luaIndexFunction
=
@"local function index(obj,name)
local meta = getmetatable(obj)
local cached = meta.cache[name]
if cached ~= nil then
return cached
else
local value,isFunc = get_object_member(obj,name)
if isFunc then
meta.cache[name]=value
end
return value
end
end
*/
static
string
luaIndexFunction
=
@"local function index(obj,name)
local meta = getmetatable(obj)
local cached = meta.cache[name]
if cached ~= nil then
return cached
else
local value,isFunc = get_object_member(obj,name)
if isFunc then
meta.cache[name]=value
end
return value
end
end
return index"
;
public
static
string
LuaIndexFunction
{
get
{
return
luaIndexFunction
;
}
get
{
return
luaIndexFunction
;
}
}
public
MetaFunctions
(
ObjectTranslator
translator
)
{
...
...
@@ -108,15 +108,15 @@ namespace NLua
CallConstructorFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
CallConstructor
);
ClassIndexFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
GetClassMethod
);
ClassNewindexFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
SetClassFieldOrProperty
);
ExecuteDelegateFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
RunFunctionDelegate
);
AddFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
AddLua
);
SubtractFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
SubtractLua
);
MultiplyFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
MultiplyLua
);
DivisionFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
DivideLua
);
ModulosFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
ModLua
);
UnaryNegationFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
UnaryNegationLua
);
EqualFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
EqualLua
);
LessThanFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
LessThanLua
);
ExecuteDelegateFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
RunFunctionDelegate
);
AddFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
AddLua
);
SubtractFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
SubtractLua
);
MultiplyFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
MultiplyLua
);
DivisionFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
DivideLua
);
ModulosFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
ModLua
);
UnaryNegationFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
UnaryNegationLua
);
EqualFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
EqualLua
);
LessThanFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
LessThanLua
);
LessThanOrEqualFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
LessThanOrEqualLua
);
}
...
...
@@ -186,147 +186,147 @@ namespace NLua
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
}
/*
* __add metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
AddLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Addition"
,
translator
);
static
int
AddLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Addition"
,
translator
);
}
/*
* __sub metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
SubtractLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Subtraction"
,
translator
);
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
SubtractLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Subtraction"
,
translator
);
}
/*
* __mul metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
MultiplyLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Multiply"
,
translator
);
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
MultiplyLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Multiply"
,
translator
);
}
/*
* __div metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
DivideLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Division"
,
translator
);
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
DivideLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Division"
,
translator
);
}
/*
* __mod metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
ModLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Modulus"
,
translator
);
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
ModLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Modulus"
,
translator
);
}
/*
* __unm metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
UnaryNegationLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
UnaryNegationLua
(
luaState
,
translator
);
}
static
int
UnaryNegationLua
(
LuaState
luaState
,
ObjectTranslator
translator
)
{
object
obj1
=
translator
.
GetRawNetObject
(
luaState
,
1
);
if
(
obj1
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot negate a nil object"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
Type
type
=
obj1
.
GetType
();
MethodInfo
opUnaryNegation
=
type
.
GetMethod
(
"op_UnaryNegation"
);
if
(
opUnaryNegation
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot negate object ("
+
type
.
Name
+
" does not overload the operator -)"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
obj1
=
opUnaryNegation
.
Invoke
(
obj1
,
new
object
[]
{
obj1
});
translator
.
Push
(
luaState
,
obj1
);
return
1
;
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
UnaryNegationLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
UnaryNegationLua
(
luaState
,
translator
);
}
static
int
UnaryNegationLua
(
LuaState
luaState
,
ObjectTranslator
translator
)
{
object
obj1
=
translator
.
GetRawNetObject
(
luaState
,
1
);
if
(
obj1
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot negate a nil object"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
Type
type
=
obj1
.
GetType
();
MethodInfo
opUnaryNegation
=
type
.
GetMethod
(
"op_UnaryNegation"
);
if
(
opUnaryNegation
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot negate object ("
+
type
.
Name
+
" does not overload the operator -)"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
obj1
=
opUnaryNegation
.
Invoke
(
obj1
,
new
object
[]
{
obj1
});
translator
.
Push
(
luaState
,
obj1
);
return
1
;
}
/*
* __eq metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
EqualLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Equality"
,
translator
);
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
EqualLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_Equality"
,
translator
);
}
/*
* __lt metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
LessThanLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_LessThan"
,
translator
);
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
LessThanLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_LessThan"
,
translator
);
}
/*
* __le metafunction of CLR objects.
*/
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
LessThanOrEqualLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_LessThanOrEqual"
,
translator
);
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
LessThanOrEqualLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
MatchOperator
(
luaState
,
"op_LessThanOrEqual"
,
translator
);
}
/// <summary>
...
...
@@ -339,6 +339,8 @@ namespace NLua
#if WINDOWS_PHONE
Debug
.
WriteLine
(
"lua stack depth: {0}"
,
depth
);
#elif UNITY_3D
UnityEngine
.
Debug
.
Log
(
string
.
Format
(
"lua stack depth: {0}"
,
depth
));
#elif !SILVERLIGHT
Debug
.
Print
(
"lua stack depth: {0}"
,
depth
);
#endif
...
...
@@ -356,6 +358,8 @@ namespace NLua
#if WINDOWS_PHONE
Debug
.
WriteLine
(
"{0}: ({1}) {2}"
,
i
,
typestr
,
strrep
);
#elif UNITY_3D
UnityEngine
.
Debug
.
Log
(
string
.
Format
(
"{0}: ({1}) {2}"
,
i
,
typestr
,
strrep
));
#elif !SILVERLIGHT
Debug
.
Print
(
"{0}: ({1}) {2}"
,
i
,
typestr
,
strrep
);
#endif
...
...
@@ -422,10 +426,10 @@ namespace NLua
object
[]
arr
=
(
object
[])
obj
;
translator
.
Push
(
luaState
,
arr
[
intIndex
]);
}
}
else
{
if
(!
string
.
IsNullOrEmpty
(
methodName
)
&&
IsExtensionMethodPresent
(
objType
,
methodName
))
{
return
GetExtensionMethod
(
luaState
,
objType
,
obj
,
methodName
);
}
else
{
if
(!
string
.
IsNullOrEmpty
(
methodName
)
&&
IsExtensionMethodPresent
(
objType
,
methodName
))
{
return
GetExtensionMethod
(
luaState
,
objType
,
obj
,
methodName
);
}
// Try to use get_Item to index into this .net object
var
methods
=
objType
.
GetMethods
();
...
...
@@ -531,36 +535,36 @@ namespace NLua
var
members
=
objType
.
GetMember
(
methodName
,
BindingFlags
.
Static
|
BindingFlags
.
Instance
|
BindingFlags
.
Public
);
return
(
members
.
Length
>
0
);
}
bool
IsExtensionMethodPresent
(
Type
type
,
string
name
)
{
object
cachedMember
=
CheckMemberCache
(
memberCache
,
type
,
name
);
if
(
cachedMember
!=
null
)
return
true
;
return
translator
.
IsExtensionMethodPresent
(
type
,
name
);
}
bool
IsExtensionMethodPresent
(
Type
type
,
string
name
)
{
object
cachedMember
=
CheckMemberCache
(
memberCache
,
type
,
name
);
if
(
cachedMember
!=
null
)
return
true
;
return
translator
.
IsExtensionMethodPresent
(
type
,
name
);
}
int
GetExtensionMethod
(
LuaState
luaState
,
Type
type
,
object
obj
,
string
name
)
{
object
cachedMember
=
CheckMemberCache
(
memberCache
,
type
,
name
);
if
(
cachedMember
!=
null
&&
cachedMember
is
LuaNativeFunction
)
{
translator
.
PushFunction
(
luaState
,
(
LuaNativeFunction
)
cachedMember
);
translator
.
Push
(
luaState
,
true
);
return
2
;
}
MethodInfo
methodInfo
=
translator
.
GetExtensionMethod
(
type
,
name
);
var
wrapper
=
new
LuaNativeFunction
((
new
LuaMethodWrapper
(
translator
,
obj
,
type
,
methodInfo
)).
invokeFunction
);
SetMemberCache
(
memberCache
,
type
,
name
,
wrapper
);
translator
.
PushFunction
(
luaState
,
wrapper
);
translator
.
Push
(
luaState
,
true
);
return
2
;
{
object
cachedMember
=
CheckMemberCache
(
memberCache
,
type
,
name
);
if
(
cachedMember
!=
null
&&
cachedMember
is
LuaNativeFunction
)
{
translator
.
PushFunction
(
luaState
,
(
LuaNativeFunction
)
cachedMember
);
translator
.
Push
(
luaState
,
true
);
return
2
;
}
MethodInfo
methodInfo
=
translator
.
GetExtensionMethod
(
type
,
name
);
var
wrapper
=
new
LuaNativeFunction
((
new
LuaMethodWrapper
(
translator
,
obj
,
type
,
methodInfo
)).
invokeFunction
);
SetMemberCache
(
memberCache
,
type
,
name
,
wrapper
);
translator
.
PushFunction
(
luaState
,
wrapper
);
translator
.
Push
(
luaState
,
true
);
return
2
;
}
/*
...
...
@@ -1023,110 +1027,110 @@ namespace NLua
}
static
bool
IsInteger
(
double
x
)
{
return
Math
.
Ceiling
(
x
)
==
x
;
}
static
object
GetTargetObject
(
LuaState
luaState
,
string
operation
,
ObjectTranslator
translator
)
{
Type
t
;
object
target
=
translator
.
GetRawNetObject
(
luaState
,
1
);
if
(
target
!=
null
)
{
t
=
target
.
GetType
();
if
(
t
.
HasMethod
(
operation
))
return
target
;
}
target
=
translator
.
GetRawNetObject
(
luaState
,
2
);
if
(
target
!=
null
)
{
t
=
target
.
GetType
();
if
(
t
.
HasMethod
(
operation
))
return
target
;
}
return
null
;
}
static
int
MatchOperator
(
LuaState
luaState
,
string
operation
,
ObjectTranslator
translator
)
{
var
validOperator
=
new
MethodCache
();
object
target
=
GetTargetObject
(
luaState
,
operation
,
translator
);
if
(
target
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot call "
+
operation
+
" on a nil object"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
Type
type
=
target
.
GetType
();
var
operators
=
type
.
GetMethods
(
operation
,
BindingFlags
.
Public
|
BindingFlags
.
Instance
|
BindingFlags
.
Static
);
foreach
(
var
op
in
operators
)
{
bool
isOk
=
translator
.
MatchParameters
(
luaState
,
op
,
ref
validOperator
);
if
(!
isOk
)
continue
;
object
result
;
if
(
op
.
IsStatic
)
result
=
op
.
Invoke
(
null
,
validOperator
.
args
);
else
result
=
op
.
Invoke
(
target
,
validOperator
.
args
);
translator
.
Push
(
luaState
,
result
);
return
1
;
}
translator
.
ThrowError
(
luaState
,
"Cannot call ("
+
operation
+
") on object type "
+
type
.
Name
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
internal
Array
TableToArray
(
Func
<
int
,
object
>
luaParamValueExtractor
,
Type
paramArrayType
,
int
startIndex
,
int
count
)
{
}
static
object
GetTargetObject
(
LuaState
luaState
,
string
operation
,
ObjectTranslator
translator
)
{
Type
t
;
object
target
=
translator
.
GetRawNetObject
(
luaState
,
1
);
if
(
target
!=
null
)
{
t
=
target
.
GetType
();
if
(
t
.
HasMethod
(
operation
))
return
target
;
}
target
=
translator
.
GetRawNetObject
(
luaState
,
2
);
if
(
target
!=
null
)
{
t
=
target
.
GetType
();
if
(
t
.
HasMethod
(
operation
))
return
target
;
}
return
null
;
}
static
int
MatchOperator
(
LuaState
luaState
,
string
operation
,
ObjectTranslator
translator
)
{
var
validOperator
=
new
MethodCache
();
object
target
=
GetTargetObject
(
luaState
,
operation
,
translator
);
if
(
target
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot call "
+
operation
+
" on a nil object"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
Type
type
=
target
.
GetType
();
var
operators
=
type
.
GetMethods
(
operation
,
BindingFlags
.
Public
|
BindingFlags
.
Instance
|
BindingFlags
.
Static
);
foreach
(
var
op
in
operators
)
{
bool
isOk
=
translator
.
MatchParameters
(
luaState
,
op
,
ref
validOperator
);
if
(!
isOk
)
continue
;
object
result
;
if
(
op
.
IsStatic
)
result
=
op
.
Invoke
(
null
,
validOperator
.
args
);
else
result
=
op
.
Invoke
(
target
,
validOperator
.
args
);
translator
.
Push
(
luaState
,
result
);
return
1
;
}
translator
.
ThrowError
(
luaState
,
"Cannot call ("
+
operation
+
") on object type "
+
type
.
Name
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
internal
Array
TableToArray
(
Func
<
int
,
object
>
luaParamValueExtractor
,
Type
paramArrayType
,
int
startIndex
,
int
count
)
{
Array
paramArray
;
if
(
count
==
0
)
return
Array
.
CreateInstance
(
paramArrayType
,
0
);
return
Array
.
CreateInstance
(
paramArrayType
,
0
);
var
luaParamValue
=
luaParamValueExtractor
(
startIndex
);
if
(
luaParamValue
is
LuaTable
)
{
LuaTable
table
=
(
LuaTable
)
luaParamValue
;
IDictionaryEnumerator
tableEnumerator
=
table
.
GetEnumerator
();
tableEnumerator
.
Reset
();
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
table
.
Values
.
Count
);
int
paramArrayIndex
=
0
;
while
(
tableEnumerator
.
MoveNext
())
{
object
value
=
tableEnumerator
.
Value
;
if
(
paramArrayType
==
typeof
(
object
))
{
if
(
value
!=
null
&&
value
.
GetType
()
==
typeof
(
double
)
&&
IsInteger
((
double
)
value
))
value
=
Convert
.
ToInt32
((
double
)
value
);
}
if
(
luaParamValue
is
LuaTable
)
{
LuaTable
table
=
(
LuaTable
)
luaParamValue
;
IDictionaryEnumerator
tableEnumerator
=
table
.
GetEnumerator
();
tableEnumerator
.
Reset
();
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
table
.
Values
.
Count
);
int
paramArrayIndex
=
0
;
while
(
tableEnumerator
.
MoveNext
())
{
object
value
=
tableEnumerator
.
Value
;
if
(
paramArrayType
==
typeof
(
object
))
{
if
(
value
!=
null
&&
value
.
GetType
()
==
typeof
(
double
)
&&
IsInteger
((
double
)
value
))
value
=
Convert
.
ToInt32
((
double
)
value
);
}
#if SILVERLIGHT
paramArray
.
SetValue
(
Convert
.
ChangeType
(
value
,
paramArrayType
,
System
.
Globalization
.
CultureInfo
.
InvariantCulture
),
paramArrayIndex
);
#else
paramArray
.
SetValue
(
Convert
.
ChangeType
(
value
,
paramArrayType
),
paramArrayIndex
);
#endif
paramArrayIndex
++;
}
#else
paramArray
.
SetValue
(
Convert
.
ChangeType
(
value
,
paramArrayType
),
paramArrayIndex
);
#endif
paramArrayIndex
++;
}
}
else
{
paramArray
=
Array
.
CreateInstance
(
paramArrayType
,
count
);
paramArray
.
SetValue
(
luaParamValue
,
0
);
for
(
int
i
=
1
;
i
<
count
;
i
++)
{
startIndex
++;
var
value
=
luaParamValueExtractor
(
startIndex
);
var
value
=
luaParamValueExtractor
(
startIndex
);
paramArray
.
SetValue
(
value
,
i
);
}
}
return
paramArray
;
}
}
return
paramArray
;
}
/*
...
...
@@ -1176,7 +1180,7 @@ namespace NLua
currentLuaParam
++;
return
extractValue
(
luaState
,
currentParam
);
};
int
count
=
(
nLuaParams
-
currentLuaParam
)
+
1
;
int
count
=
(
nLuaParams
-
currentLuaParam
)
+
1
;
Array
paramArray
=
TableToArray
(
extractDelegate
,
paramArrayType
,
currentLuaParam
,
count
);
paramList
.
Add
(
paramArray
);
...
...
Core/NLua/Method/LuaMethodWrapper.cs
View file @
be0bfcc8
...
...
@@ -136,31 +136,31 @@ namespace NLua.Method
if
(
_LastCalledMethod
.
cachedMethod
!=
null
)
{
// Cached?
int
numStackToSkip
=
isStatic
?
0
:
1
;
// If this is an instance invoe we will have an extra arg on the stack for the targetObject
int
numArgsPassed
=
LuaLib
.
LuaGetTop
(
luaState
)
-
numStackToSkip
;
int
numArgsPassed
=
LuaLib
.
LuaGetTop
(
luaState
)
-
numStackToSkip
;
MethodBase
method
=
_LastCalledMethod
.
cachedMethod
;
if
(
numArgsPassed
==
_LastCalledMethod
.
argTypes
.
Length
)
{
// No. of args match?
if
(!
LuaLib
.
LuaCheckStack
(
luaState
,
_LastCalledMethod
.
outList
.
Length
+
6
))
throw
new
LuaException
(
"Lua stack overflow"
);
throw
new
LuaException
(
"Lua stack overflow"
);
object
[]
args
=
_LastCalledMethod
.
args
;
try
{
for
(
int
i
=
0
;
i
<
_LastCalledMethod
.
argTypes
.
Length
;
i
++)
{
for
(
int
i
=
0
;
i
<
_LastCalledMethod
.
argTypes
.
Length
;
i
++)
{
MethodArgs
type
=
_LastCalledMethod
.
argTypes
[
i
];
int
index
=
i
+
1
+
numStackToSkip
;
Func
<
int
,
object
>
valueExtractor
=
(
currentParam
)
=>
{
Func
<
int
,
object
>
valueExtractor
=
(
currentParam
)
=>
{
return
type
.
extractValue
(
luaState
,
currentParam
);
};
if
(
_LastCalledMethod
.
argTypes
[
i
].
isParamsArray
)
{
int
count
=
index
-
_LastCalledMethod
.
argTypes
.
Length
;
Array
paramArray
=
_Translator
.
TableToArray
(
valueExtractor
,
type
.
paramsArrayType
,
index
,
count
);
int
count
=
index
-
_LastCalledMethod
.
argTypes
.
Length
;
Array
paramArray
=
_Translator
.
TableToArray
(
valueExtractor
,
type
.
paramsArrayType
,
index
,
count
);
args
[
_LastCalledMethod
.
argTypes
[
i
].
index
]
=
paramArray
;
}
else
{
}
else
{
args
[
type
.
index
]
=
valueExtractor
(
index
);
}
...
...
Core/NLua/ObjectTranslator.cs
View file @
be0bfcc8
...
...
@@ -22,7 +22,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
using
System
;
using
System.IO
;
using
System.Reflection
;
...
...
@@ -94,8 +94,8 @@ namespace NLua
registerTableFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
RegisterTable
);
unregisterTableFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
UnregisterTable
);
getMethodSigFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
GetMethodSignature
);
getConstructorSigFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
GetConstructorSignature
);
ctypeFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
CType
);
getConstructorSigFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
GetConstructorSignature
);
ctypeFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
CType
);
enumFromIntFunction
=
new
LuaNativeFunction
(
ObjectTranslator
.
EnumFromInt
);
CreateLuaObjectList
(
luaState
);
...
...
@@ -127,7 +127,7 @@ namespace NLua
*/
private
void
CreateIndexingMetaFunction
(
LuaState
luaState
)
{
LuaLib
.
LuaPushString
(
luaState
,
"luaNet_indexfunction"
);
LuaLib
.
LuaPushString
(
luaState
,
"luaNet_indexfunction"
);
LuaLib
.
LuaLDoString
(
luaState
,
MetaFunctions
.
LuaIndexFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
(
int
)
LuaIndexes
.
Registry
);
}
...
...
@@ -197,9 +197,9 @@ namespace NLua
LuaLib
.
LuaSetGlobal
(
luaState
,
"get_method_bysig"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
getConstructorSigFunction
);
LuaLib
.
LuaSetGlobal
(
luaState
,
"get_constructor_bysig"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
ctypeFunction
);
LuaLib
.
LuaSetGlobal
(
luaState
,
"ctype"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
enumFromIntFunction
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
ctypeFunction
);
LuaLib
.
LuaSetGlobal
(
luaState
,
"ctype"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
enumFromIntFunction
);
LuaLib
.
LuaSetGlobal
(
luaState
,
"enum"
);
}
...
...
@@ -273,40 +273,40 @@ namespace NLua
{
try
{
string
assemblyName
=
LuaLib
.
LuaToString
(
luaState
,
1
).
ToString
();
Assembly
assembly
=
null
;
Assembly
assembly
=
null
;
Exception
exception
=
null
;
try
{
assembly
=
Assembly
.
Load
(
assemblyName
);
}
catch
(
BadImageFormatException
)
{
// The assemblyName was invalid. It is most likely a path.
}
catch
(
FileNotFoundException
e
)
{
exception
=
e
;
}
#if !SILVERLIGHT
if
(
assembly
==
null
)
{
try
{
assembly
=
Assembly
.
Load
(
AssemblyName
.
GetAssemblyName
(
assemblyName
));
}
catch
(
FileNotFoundException
e
)
{
exception
=
e
;
}
if
(
assembly
==
null
)
{
AssemblyName
mscor
=
assemblies
[
0
].
GetName
();
AssemblyName
name
=
new
AssemblyName
();
name
.
Name
=
assemblyName
;
name
.
CultureInfo
=
mscor
.
CultureInfo
;
name
.
Version
=
mscor
.
Version
;
name
.
SetPublicKeyToken
(
mscor
.
GetPublicKeyToken
());
name
.
SetPublicKey
(
mscor
.
GetPublicKey
());
assembly
=
Assembly
.
Load
(
name
);
if
(
assembly
!=
null
)
exception
=
null
;
}
if
(
exception
!=
null
)
ThrowError
(
luaState
,
exception
);
assembly
=
Assembly
.
Load
(
assemblyName
);
}
catch
(
BadImageFormatException
)
{
// The assemblyName was invalid. It is most likely a path.
}
catch
(
FileNotFoundException
e
)
{
exception
=
e
;
}
#if !SILVERLIGHT
if
(
assembly
==
null
)
{
try
{
assembly
=
Assembly
.
Load
(
AssemblyName
.
GetAssemblyName
(
assemblyName
));
}
catch
(
FileNotFoundException
e
)
{
exception
=
e
;
}
if
(
assembly
==
null
)
{
AssemblyName
mscor
=
assemblies
[
0
].
GetName
();
AssemblyName
name
=
new
AssemblyName
();
name
.
Name
=
assemblyName
;
name
.
CultureInfo
=
mscor
.
CultureInfo
;
name
.
Version
=
mscor
.
Version
;
name
.
SetPublicKeyToken
(
mscor
.
GetPublicKeyToken
());
name
.
SetPublicKey
(
mscor
.
GetPublicKey
());
assembly
=
Assembly
.
Load
(
name
);
if
(
assembly
!=
null
)
exception
=
null
;
}
if
(
exception
!=
null
)
ThrowError
(
luaState
,
exception
);
}
#endif
if
(
assembly
!=
null
&&
!
assemblies
.
Contains
(
assembly
))
...
...
@@ -327,16 +327,16 @@ namespace NLua
return
klass
;
}
return
null
;
}
public
bool
IsExtensionMethodPresent
(
Type
type
,
string
name
)
{
return
GetExtensionMethod
(
type
,
name
)
!=
null
;
}
public
MethodInfo
GetExtensionMethod
(
Type
type
,
string
name
)
{
return
type
.
GetExtensionMethod
(
name
,
assemblies
);
public
bool
IsExtensionMethodPresent
(
Type
type
,
string
name
)
{
return
GetExtensionMethod
(
type
,
name
)
!=
null
;
}
public
MethodInfo
GetExtensionMethod
(
Type
type
,
string
name
)
{
return
type
.
GetExtensionMethod
(
name
,
assemblies
);
}
/*
...
...
@@ -588,7 +588,7 @@ namespace NLua
return
;
}
// Object already in the list of Lua objects? Push the stored reference.
// Object already in the list of Lua objects? Push the stored reference.
bool
found
=
(!
o
.
GetType
().
IsValueType
)
&&
objectsBackMap
.
TryGetValue
(
o
,
out
index
);
if
(
found
)
{
...
...
@@ -647,8 +647,8 @@ namespace NLua
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
LuaLib
.
LuaPushString
(
luaState
,
"__newindex"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
NewIndexFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
// Bind C# operator with Lua metamethods (__add, __sub, __mul)
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
// Bind C# operator with Lua metamethods (__add, __sub, __mul)
RegisterOperatorsFunctions
(
luaState
,
o
.
GetType
());
}
}
else
...
...
@@ -667,52 +667,52 @@ namespace NLua
}
void
RegisterOperatorsFunctions
(
LuaState
luaState
,
Type
type
)
{
if
(
type
.
HasAdditionOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__add"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
AddFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasSubtractionOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__sub"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
SubtractFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasMultiplyOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__mul"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
MultiplyFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasDivisionOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__div"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
DivisionFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasModulusOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__mod"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
ModulosFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasUnaryNegationOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__unm"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
UnaryNegationFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasEqualityOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__eq"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
EqualFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasLessThanOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__lt"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
LessThanFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasLessThanOrEqualOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__le"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
LessThanOrEqualFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
{
if
(
type
.
HasAdditionOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__add"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
AddFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasSubtractionOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__sub"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
SubtractFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasMultiplyOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__mul"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
MultiplyFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasDivisionOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__div"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
DivisionFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasModulusOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__mod"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
ModulosFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasUnaryNegationOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__unm"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
UnaryNegationFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasEqualityOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__eq"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
EqualFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasLessThanOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__lt"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
LessThanFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
if
(
type
.
HasLessThanOrEqualOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__le"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
LessThanOrEqualFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
}
/*
...
...
@@ -735,7 +735,7 @@ namespace NLua
bool
found
=
objects
.
TryGetValue
(
udata
,
out
o
);
// The other variant of collectObject might have gotten here first, in that case we will silently ignore the missing entry
if
(
found
)
if
(
found
)
CollectObject
(
o
,
udata
);
}
...
...
@@ -954,80 +954,80 @@ namespace NLua
return
metaFunctions
.
TableToArray
(
luaParamValue
,
paramArrayType
,
startIndex
,
count
);
}
private
Type
TypeOf
(
LuaState
luaState
,
int
idx
)
{
int
udata
=
LuaLib
.
LuaNetCheckUData
(
luaState
,
1
,
"luaNet_class"
);
if
(
udata
==
-
1
)
return
null
;
ProxyType
pt
=
(
ProxyType
)
objects
[
udata
];
return
pt
.
UnderlyingSystemType
;
}
static
int
PushError
(
LuaState
luaState
,
string
msg
)
{
LuaLib
.
LuaPushNil
(
luaState
);
LuaLib
.
LuaPushString
(
luaState
,
msg
);
return
2
;
}
private
Type
TypeOf
(
LuaState
luaState
,
int
idx
)
{
int
udata
=
LuaLib
.
LuaNetCheckUData
(
luaState
,
1
,
"luaNet_class"
);
if
(
udata
==
-
1
)
return
null
;
ProxyType
pt
=
(
ProxyType
)
objects
[
udata
];
return
pt
.
UnderlyingSystemType
;
}
static
int
PushError
(
LuaState
luaState
,
string
msg
)
{
LuaLib
.
LuaPushNil
(
luaState
);
LuaLib
.
LuaPushString
(
luaState
,
msg
);
return
2
;
}
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
private
static
int
CType
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
translator
.
CTypeInternal
(
luaState
);
}
int
CTypeInternal
(
LuaState
luaState
)
{
Type
t
=
TypeOf
(
luaState
,
1
);
if
(
t
==
null
)
return
PushError
(
luaState
,
"Not a CLR Class"
);
PushObject
(
luaState
,
t
,
"luaNet_metatable"
);
return
1
;
}
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
private
static
int
CType
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
translator
.
CTypeInternal
(
luaState
);
}
int
CTypeInternal
(
LuaState
luaState
)
{
Type
t
=
TypeOf
(
luaState
,
1
);
if
(
t
==
null
)
return
PushError
(
luaState
,
"Not a CLR Class"
);
PushObject
(
luaState
,
t
,
"luaNet_metatable"
);
return
1
;
}
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
private
static
int
EnumFromInt
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
translator
.
EnumFromIntInternal
(
luaState
);
}
int
EnumFromIntInternal
(
LuaState
luaState
)
{
Type
t
=
TypeOf
(
luaState
,
1
);
if
(
t
==
null
||
!
t
.
IsEnum
)
return
PushError
(
luaState
,
"Not an Enum."
);
object
res
=
null
;
LuaTypes
lt
=
LuaLib
.
LuaType
(
luaState
,
2
);
if
(
lt
==
LuaTypes
.
Number
)
{
int
ival
=
(
int
)
LuaLib
.
LuaToNumber
(
luaState
,
2
);
res
=
Enum
.
ToObject
(
t
,
ival
);
}
else
if
(
lt
==
LuaTypes
.
String
)
{
string
sflags
=
LuaLib
.
LuaToString
(
luaState
,
2
);
string
err
=
null
;
try
{
res
=
Enum
.
Parse
(
t
,
sflags
,
true
);
}
catch
(
ArgumentException
e
)
{
err
=
e
.
Message
;
}
if
(
err
!=
null
)
return
PushError
(
luaState
,
err
);
}
else
{
return
PushError
(
luaState
,
"Second argument must be a integer or a string."
);
}
PushObject
(
luaState
,
res
,
"luaNet_metatable"
);
return
1
;
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
private
static
int
EnumFromInt
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
translator
.
EnumFromIntInternal
(
luaState
);
}
int
EnumFromIntInternal
(
LuaState
luaState
)
{
Type
t
=
TypeOf
(
luaState
,
1
);
if
(
t
==
null
||
!
t
.
IsEnum
)
return
PushError
(
luaState
,
"Not an Enum."
);
object
res
=
null
;
LuaTypes
lt
=
LuaLib
.
LuaType
(
luaState
,
2
);
if
(
lt
==
LuaTypes
.
Number
)
{
int
ival
=
(
int
)
LuaLib
.
LuaToNumber
(
luaState
,
2
);
res
=
Enum
.
ToObject
(
t
,
ival
);
}
else
if
(
lt
==
LuaTypes
.
String
)
{
string
sflags
=
LuaLib
.
LuaToString
(
luaState
,
2
);
string
err
=
null
;
try
{
res
=
Enum
.
Parse
(
t
,
sflags
,
true
);
}
catch
(
ArgumentException
e
)
{
err
=
e
.
Message
;
}
if
(
err
!=
null
)
return
PushError
(
luaState
,
err
);
}
else
{
return
PushError
(
luaState
,
"Second argument must be a integer or a string."
);
}
PushObject
(
luaState
,
res
,
"luaNet_metatable"
);
return
1
;
}
}
}
\ No newline at end of file
Core/NLua/ObjectTranslatorPool.cs
View file @
be0bfcc8
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
/*
* This file is part of NLua.
*
...
...
@@ -58,13 +58,13 @@ namespace NLua
}
public
ObjectTranslator
Find
(
LuaState
luaState
)
{
if
(
translators
.
ContainsKey
(
luaState
))
return
translators
[
luaState
];
LuaState
main
=
LuaCore
.
LuaNetGetMainState
(
luaState
);
if
(
translators
.
ContainsKey
(
main
))
{
if
(
translators
.
ContainsKey
(
luaState
))
return
translators
[
luaState
];
LuaState
main
=
LuaCore
.
LuaNetGetMainState
(
luaState
);
if
(
translators
.
ContainsKey
(
main
))
return
translators
[
main
];
return
null
;
...
...
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