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
a32b3b5d
Commit
a32b3b5d
authored
May 02, 2020
by
Vinicius Jarina
Browse files
Review error handling in Metatables/ObjectTranslator.
* Fixes #375 Possible bug not detecting constructor(.ctor) argument match
parent
07b3dab8
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/Lua.cs
View file @
a32b3b5d
...
@@ -372,7 +372,6 @@ namespace NLua
...
@@ -372,7 +372,6 @@ namespace NLua
return
0
;
return
0
;
_translator
.
ThrowError
(
_luaState
,
caughtExcept
);
_translator
.
ThrowError
(
_luaState
,
caughtExcept
);
//_luaState.PushNil();
return
1
;
return
1
;
}
}
...
...
src/Metatables.cs
View file @
a32b3b5d
...
@@ -151,7 +151,12 @@ namespace NLua
...
@@ -151,7 +151,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_Addition"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_Addition"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/*
/*
...
@@ -164,7 +169,12 @@ namespace NLua
...
@@ -164,7 +169,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_Subtraction"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_Subtraction"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/*
/*
...
@@ -177,7 +187,12 @@ namespace NLua
...
@@ -177,7 +187,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_Multiply"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_Multiply"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/*
/*
...
@@ -190,7 +205,12 @@ namespace NLua
...
@@ -190,7 +205,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_Division"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_Division"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/*
/*
...
@@ -203,7 +223,12 @@ namespace NLua
...
@@ -203,7 +223,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_Modulus"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_Modulus"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/*
/*
...
@@ -216,7 +241,12 @@ namespace NLua
...
@@ -216,7 +241,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
UnaryNegationLua
(
state
,
translator
);
int
result
=
UnaryNegationLua
(
state
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
static
int
UnaryNegationLua
(
LuaState
luaState
,
ObjectTranslator
translator
)
static
int
UnaryNegationLua
(
LuaState
luaState
,
ObjectTranslator
translator
)
...
@@ -253,7 +283,12 @@ namespace NLua
...
@@ -253,7 +283,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_Equality"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_Equality"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/*
/*
...
@@ -266,7 +301,12 @@ namespace NLua
...
@@ -266,7 +301,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_LessThan"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_LessThan"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/*
/*
...
@@ -279,7 +319,12 @@ namespace NLua
...
@@ -279,7 +319,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
MatchOperator
(
state
,
"op_LessThanOrEqual"
,
translator
);
int
result
=
MatchOperator
(
state
,
"op_LessThanOrEqual"
,
translator
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
/// <summary>
/// <summary>
...
@@ -324,7 +369,12 @@ namespace NLua
...
@@ -324,7 +369,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
instance
=
translator
.
MetaFunctionsInstance
;
var
instance
=
translator
.
MetaFunctionsInstance
;
return
instance
.
GetMethodInternal
(
luaState
);
int
result
=
instance
.
GetMethodInternal
(
luaState
);
var
exception
=
translator
.
GetObject
(
luaState
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
luaState
.
Error
();
return
result
;
}
}
private
int
GetMethodInternal
(
LuaState
luaState
)
private
int
GetMethodInternal
(
LuaState
luaState
)
...
@@ -624,7 +674,12 @@ namespace NLua
...
@@ -624,7 +674,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
instance
=
translator
.
MetaFunctionsInstance
;
var
instance
=
translator
.
MetaFunctionsInstance
;
return
instance
.
GetBaseMethodInternal
(
luaState
);
int
result
=
instance
.
GetBaseMethodInternal
(
luaState
);
var
exception
=
translator
.
GetObject
(
luaState
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
luaState
.
Error
();
return
result
;
}
}
private
int
GetBaseMethodInternal
(
LuaState
luaState
)
private
int
GetBaseMethodInternal
(
LuaState
luaState
)
...
@@ -634,7 +689,7 @@ namespace NLua
...
@@ -634,7 +689,7 @@ namespace NLua
if
(
obj
==
null
)
if
(
obj
==
null
)
{
{
_translator
.
ThrowError
(
luaState
,
"Trying to index an invalid object reference"
);
_translator
.
ThrowError
(
luaState
,
"Trying to index an invalid object reference"
);
return
2
;
return
1
;
}
}
string
methodName
=
luaState
.
ToString
(
2
,
false
);
string
methodName
=
luaState
.
ToString
(
2
,
false
);
...
@@ -840,6 +895,7 @@ namespace NLua
...
@@ -840,6 +895,7 @@ namespace NLua
{
{
// If we reach this point we found a static method, but can't use it in this context because the user passed in an instance
// If we reach this point we found a static method, but can't use it in this context because the user passed in an instance
_translator
.
ThrowError
(
luaState
,
"Can't pass instance to static method "
+
methodName
);
_translator
.
ThrowError
(
luaState
,
"Can't pass instance to static method "
+
methodName
);
return
1
;
}
}
}
}
else
else
...
@@ -921,7 +977,12 @@ namespace NLua
...
@@ -921,7 +977,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
instance
=
translator
.
MetaFunctionsInstance
;
var
instance
=
translator
.
MetaFunctionsInstance
;
return
instance
.
SetFieldOrPropertyInternal
(
luaState
);
int
result
=
instance
.
SetFieldOrPropertyInternal
(
luaState
);
var
exception
=
translator
.
GetObject
(
luaState
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
luaState
.
Error
();
return
result
;
}
}
private
int
SetFieldOrPropertyInternal
(
LuaState
luaState
)
private
int
SetFieldOrPropertyInternal
(
LuaState
luaState
)
...
@@ -931,7 +992,7 @@ namespace NLua
...
@@ -931,7 +992,7 @@ namespace NLua
if
(
target
==
null
)
if
(
target
==
null
)
{
{
_translator
.
ThrowError
(
luaState
,
"trying to index and invalid object reference"
);
_translator
.
ThrowError
(
luaState
,
"trying to index and invalid object reference"
);
return
0
;
return
1
;
}
}
var
type
=
target
.
GetType
();
var
type
=
target
.
GetType
();
...
@@ -975,12 +1036,16 @@ namespace NLua
...
@@ -975,12 +1036,16 @@ namespace NLua
setter
.
Invoke
(
target
,
methodArgs
);
setter
.
Invoke
(
target
,
methodArgs
);
}
}
else
else
{
_translator
.
ThrowError
(
luaState
,
detailMessage
);
// Pass the original message from trySetMember because it is probably best
_translator
.
ThrowError
(
luaState
,
detailMessage
);
// Pass the original message from trySetMember because it is probably best
return
1
;
}
}
}
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
ThrowError
(
luaState
,
e
);
ThrowError
(
luaState
,
e
);
return
1
;
}
}
return
0
;
return
0
;
...
@@ -1043,7 +1108,8 @@ namespace NLua
...
@@ -1043,7 +1108,8 @@ namespace NLua
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
ThrowError
(
luaState
,
e
);
detailMessage
=
"Error setting field: "
+
e
.
Message
;
return
false
;
}
}
return
true
;
return
true
;
...
@@ -1059,7 +1125,8 @@ namespace NLua
...
@@ -1059,7 +1125,8 @@ namespace NLua
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
ThrowError
(
luaState
,
e
);
detailMessage
=
"Error setting property: "
+
e
.
Message
;
return
false
;
}
}
return
true
;
return
true
;
...
@@ -1079,7 +1146,10 @@ namespace NLua
...
@@ -1079,7 +1146,10 @@ namespace NLua
bool
success
=
TrySetMember
(
luaState
,
targetType
,
target
,
bindingType
,
out
detail
);
bool
success
=
TrySetMember
(
luaState
,
targetType
,
target
,
bindingType
,
out
detail
);
if
(!
success
)
if
(!
success
)
{
_translator
.
ThrowError
(
luaState
,
detail
);
_translator
.
ThrowError
(
luaState
,
detail
);
return
1
;
}
return
0
;
return
0
;
}
}
...
@@ -1112,7 +1182,12 @@ namespace NLua
...
@@ -1112,7 +1182,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
instance
=
translator
.
MetaFunctionsInstance
;
var
instance
=
translator
.
MetaFunctionsInstance
;
return
instance
.
GetClassMethodInternal
(
luaState
);
int
result
=
instance
.
GetClassMethodInternal
(
luaState
);
var
exception
=
translator
.
GetObject
(
luaState
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
luaState
.
Error
();
return
result
;
}
}
private
int
GetClassMethodInternal
(
LuaState
luaState
)
private
int
GetClassMethodInternal
(
LuaState
luaState
)
...
@@ -1153,7 +1228,12 @@ namespace NLua
...
@@ -1153,7 +1228,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
instance
=
translator
.
MetaFunctionsInstance
;
var
instance
=
translator
.
MetaFunctionsInstance
;
return
instance
.
SetClassFieldOrPropertyInternal
(
luaState
);
int
result
=
instance
.
SetClassFieldOrPropertyInternal
(
luaState
);
var
exception
=
translator
.
GetObject
(
luaState
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
luaState
.
Error
();
return
result
;
}
}
private
int
SetClassFieldOrPropertyInternal
(
LuaState
luaState
)
private
int
SetClassFieldOrPropertyInternal
(
LuaState
luaState
)
...
@@ -1196,7 +1276,6 @@ namespace NLua
...
@@ -1196,7 +1276,6 @@ namespace NLua
if
(
del
==
null
)
if
(
del
==
null
)
{
{
_translator
.
ThrowError
(
luaState
,
"Trying to invoke a not delegate or callable value"
);
_translator
.
ThrowError
(
luaState
,
"Trying to invoke a not delegate or callable value"
);
luaState
.
PushNil
();
return
1
;
return
1
;
}
}
...
@@ -1237,7 +1316,12 @@ namespace NLua
...
@@ -1237,7 +1316,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
var
instance
=
translator
.
MetaFunctionsInstance
;
var
instance
=
translator
.
MetaFunctionsInstance
;
return
instance
.
CallConstructorInternal
(
luaState
);
int
result
=
instance
.
CallConstructorInternal
(
luaState
);
var
exception
=
translator
.
GetObject
(
luaState
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
luaState
.
Error
();
return
result
;
}
}
private
static
ConstructorInfo
[]
ReorderConstructors
(
ConstructorInfo
[]
constructors
)
private
static
ConstructorInfo
[]
ReorderConstructors
(
ConstructorInfo
[]
constructors
)
...
@@ -1260,7 +1344,6 @@ namespace NLua
...
@@ -1260,7 +1344,6 @@ namespace NLua
if
(
klass
==
null
)
if
(
klass
==
null
)
{
{
_translator
.
ThrowError
(
luaState
,
"Trying to call constructor on an invalid type reference"
);
_translator
.
ThrowError
(
luaState
,
"Trying to call constructor on an invalid type reference"
);
luaState
.
PushNil
();
return
1
;
return
1
;
}
}
...
@@ -1283,7 +1366,7 @@ namespace NLua
...
@@ -1283,7 +1366,7 @@ namespace NLua
catch
(
TargetInvocationException
e
)
catch
(
TargetInvocationException
e
)
{
{
ThrowError
(
luaState
,
e
);
ThrowError
(
luaState
,
e
);
luaState
.
PushNil
()
;
return
1
;
}
}
catch
catch
{
{
...
@@ -1305,7 +1388,6 @@ namespace NLua
...
@@ -1305,7 +1388,6 @@ namespace NLua
string
constructorName
=
constructors
.
Length
==
0
?
"unknown"
:
constructors
[
0
].
Name
;
string
constructorName
=
constructors
.
Length
==
0
?
"unknown"
:
constructors
[
0
].
Name
;
_translator
.
ThrowError
(
luaState
,
string
.
Format
(
"{0} does not contain constructor({1}) argument match"
,
_translator
.
ThrowError
(
luaState
,
string
.
Format
(
"{0} does not contain constructor({1}) argument match"
,
klass
.
UnderlyingSystemType
,
constructorName
));
klass
.
UnderlyingSystemType
,
constructorName
));
luaState
.
PushNil
();
return
1
;
return
1
;
}
}
...
@@ -1343,7 +1425,6 @@ namespace NLua
...
@@ -1343,7 +1425,6 @@ namespace NLua
if
(
target
==
null
)
if
(
target
==
null
)
{
{
translator
.
ThrowError
(
luaState
,
"Cannot call "
+
operation
+
" on a nil object"
);
translator
.
ThrowError
(
luaState
,
"Cannot call "
+
operation
+
" on a nil object"
);
// luaState.PushNil();
return
1
;
return
1
;
}
}
...
@@ -1367,7 +1448,6 @@ namespace NLua
...
@@ -1367,7 +1448,6 @@ namespace NLua
}
}
translator
.
ThrowError
(
luaState
,
"Cannot call ("
+
operation
+
") on object type "
+
type
.
Name
);
translator
.
ThrowError
(
luaState
,
"Cannot call ("
+
operation
+
") on object type "
+
type
.
Name
);
luaState
.
PushNil
();
return
1
;
return
1
;
}
}
...
...
src/ObjectTranslator.cs
View file @
a32b3b5d
...
@@ -239,7 +239,6 @@ namespace NLua
...
@@ -239,7 +239,6 @@ namespace NLua
}
}
Push
(
luaState
,
e
);
Push
(
luaState
,
e
);
//luaState.Error();
}
}
/*
/*
...
@@ -253,7 +252,12 @@ namespace NLua
...
@@ -253,7 +252,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
translator
.
LoadAssemblyInternal
(
state
);
int
result
=
translator
.
LoadAssemblyInternal
(
state
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
private
int
LoadAssemblyInternal
(
LuaState
luaState
)
private
int
LoadAssemblyInternal
(
LuaState
luaState
)
...
@@ -302,7 +306,10 @@ namespace NLua
...
@@ -302,7 +306,10 @@ namespace NLua
exception
=
null
;
exception
=
null
;
}
}
if
(
exception
!=
null
)
if
(
exception
!=
null
)
{
ThrowError
(
luaState
,
exception
);
ThrowError
(
luaState
,
exception
);
return
1
;
}
}
}
if
(
assembly
!=
null
&&
!
assemblies
.
Contains
(
assembly
))
if
(
assembly
!=
null
&&
!
assemblies
.
Contains
(
assembly
))
assemblies
.
Add
(
assembly
);
assemblies
.
Add
(
assembly
);
...
@@ -310,6 +317,7 @@ namespace NLua
...
@@ -310,6 +317,7 @@ namespace NLua
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
ThrowError
(
luaState
,
e
);
ThrowError
(
luaState
,
e
);
return
1
;
}
}
return
0
;
return
0
;
}
}
...
@@ -376,7 +384,12 @@ namespace NLua
...
@@ -376,7 +384,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
translator
.
RegisterTableInternal
(
state
);
int
result
=
translator
.
RegisterTableInternal
(
state
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
private
int
RegisterTableInternal
(
LuaState
luaState
)
private
int
RegisterTableInternal
(
LuaState
luaState
)
...
@@ -384,7 +397,7 @@ namespace NLua
...
@@ -384,7 +397,7 @@ namespace NLua
if
(
luaState
.
Type
(
1
)
!=
LuaType
.
Table
)
if
(
luaState
.
Type
(
1
)
!=
LuaType
.
Table
)
{
{
ThrowError
(
luaState
,
"register_table: first arg is not a table"
);
ThrowError
(
luaState
,
"register_table: first arg is not a table"
);
return
0
;
return
1
;
}
}
LuaTable
luaTable
=
GetTable
(
luaState
,
1
);
LuaTable
luaTable
=
GetTable
(
luaState
,
1
);
...
@@ -393,7 +406,7 @@ namespace NLua
...
@@ -393,7 +406,7 @@ namespace NLua
if
(
string
.
IsNullOrEmpty
(
superclassName
))
if
(
string
.
IsNullOrEmpty
(
superclassName
))
{
{
ThrowError
(
luaState
,
"register_table: superclass name can not be null"
);
ThrowError
(
luaState
,
"register_table: superclass name can not be null"
);
return
0
;
return
1
;
}
}
var
klass
=
FindType
(
superclassName
);
var
klass
=
FindType
(
superclassName
);
...
@@ -401,7 +414,7 @@ namespace NLua
...
@@ -401,7 +414,7 @@ namespace NLua
if
(
klass
==
null
)
if
(
klass
==
null
)
{
{
ThrowError
(
luaState
,
"register_table: can not find superclass '"
+
superclassName
+
"'"
);
ThrowError
(
luaState
,
"register_table: can not find superclass '"
+
superclassName
+
"'"
);
return
0
;
return
1
;
}
}
// Creates and pushes the object in the stack, setting
// Creates and pushes the object in the stack, setting
...
@@ -438,7 +451,12 @@ namespace NLua
...
@@ -438,7 +451,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
translator
.
UnregisterTableInternal
(
state
);
int
result
=
translator
.
UnregisterTableInternal
(
state
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
private
int
UnregisterTableInternal
(
LuaState
luaState
)
private
int
UnregisterTableInternal
(
LuaState
luaState
)
...
@@ -447,7 +465,7 @@ namespace NLua
...
@@ -447,7 +465,7 @@ namespace NLua
if
(!
luaState
.
GetMetaTable
(
1
))
if
(!
luaState
.
GetMetaTable
(
1
))
{
{
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
return
0
;
return
1
;
}
}
luaState
.
PushString
(
"__index"
);
luaState
.
PushString
(
"__index"
);
...
@@ -455,12 +473,18 @@ namespace NLua
...
@@ -455,12 +473,18 @@ namespace NLua
object
obj
=
GetRawNetObject
(
luaState
,
-
1
);
object
obj
=
GetRawNetObject
(
luaState
,
-
1
);
if
(
obj
==
null
)
if
(
obj
==
null
)
{
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
return
1
;
}
var
luaTableField
=
obj
.
GetType
().
GetField
(
"__luaInterface_luaTable"
);
var
luaTableField
=
obj
.
GetType
().
GetField
(
"__luaInterface_luaTable"
);
if
(
luaTableField
==
null
)
if
(
luaTableField
==
null
)
{
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
return
1
;
}
// ReSharper disable once PossibleNullReferenceException
// ReSharper disable once PossibleNullReferenceException
luaTableField
.
SetValue
(
obj
,
null
);
luaTableField
.
SetValue
(
obj
,
null
);
...
@@ -484,7 +508,12 @@ namespace NLua
...
@@ -484,7 +508,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
translator
.
GetMethodSignatureInternal
(
state
);
int
result
=
translator
.
GetMethodSignatureInternal
(
state
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
private
int
GetMethodSignatureInternal
(
LuaState
luaState
)
private
int
GetMethodSignatureInternal
(
LuaState
luaState
)
...
@@ -505,7 +534,6 @@ namespace NLua
...
@@ -505,7 +534,6 @@ namespace NLua
if
(
target
==
null
)
if
(
target
==
null
)
{
{
ThrowError
(
luaState
,
"get_method_bysig: first arg is not type or object reference"
);
ThrowError
(
luaState
,
"get_method_bysig: first arg is not type or object reference"
);
luaState
.
PushNil
();
return
1
;
return
1
;
}
}
...
@@ -529,7 +557,6 @@ namespace NLua
...
@@ -529,7 +557,6 @@ namespace NLua
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
ThrowError
(
luaState
,
e
);
ThrowError
(
luaState
,
e
);
luaState
.
PushNil
();
}
}
return
1
;
return
1
;
...
@@ -546,7 +573,12 @@ namespace NLua
...
@@ -546,7 +573,12 @@ namespace NLua
{
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
state
);
return
translator
.
GetConstructorSignatureInternal
(
state
);
int
result
=
translator
.
GetConstructorSignatureInternal
(
state
);
var
exception
=
translator
.
GetObject
(
state
,
-
1
)
as
LuaScriptException
;
if
(
exception
!=
null
)
return
state
.
Error
();
return
result
;
}
}
private
int
GetConstructorSignatureInternal
(
LuaState
luaState
)
private
int
GetConstructorSignatureInternal
(
LuaState
luaState
)
...
@@ -558,7 +590,10 @@ namespace NLua
...
@@ -558,7 +590,10 @@ namespace NLua
klass
=
(
ProxyType
)
_objects
[
udata
];
klass
=
(
ProxyType
)
_objects
[
udata
];
if
(
klass
==
null
)
if
(
klass
==
null
)
{
ThrowError
(
luaState
,
"get_constructor_bysig: first arg is invalid type reference"
);
ThrowError
(
luaState
,
"get_constructor_bysig: first arg is invalid type reference"
);
return
1
;
}
var
signature
=
new
Type
[
luaState
.
GetTop
()
-
1
];
var
signature
=
new
Type
[
luaState
.
GetTop
()
-
1
];
...
@@ -575,7 +610,6 @@ namespace NLua
...
@@ -575,7 +610,6 @@ namespace NLua
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
ThrowError
(
luaState
,
e
);
ThrowError
(
luaState
,
e
);
luaState
.
PushNil
();
}
}
return
1
;
return
1
;
}
}
...
...
tests/src/LuaTests.cs
View file @
a32b3b5d
...
@@ -1214,6 +1214,27 @@ namespace NLuaTest
...
@@ -1214,6 +1214,27 @@ namespace NLuaTest
}
}
}
}
/*
/*
* Tests instantiating an object with one-argument constructor
*/
[
Test
]
public
void
CreateNetObjectWrongArgCons
()
{
using
(
Lua
lua
=
new
Lua
())
{
try
{
lua
.
DoString
(
"luanet.load_assembly(\"NLuaTest\")"
);
lua
.
DoString
(
"TestClass=luanet.import_type(\"NLuaTest.TestTypes.TestClass\")"
);
lua
.
DoString
(
"test=TestClass(3, 3)"
);
Assert
.
Fail
(
"Should throw an Exception"
);
}
catch
(
Exception
e
)
{
Assert
.
True
(
e
is
LuaScriptException
,
"#1"
);
}
}
}
/*
* Tests instantiating an object with overloaded constructor
* Tests instantiating an object with overloaded constructor
*/
*/
[
Test
]
[
Test
]
...
...
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