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
return
0
;
_translator
.
ThrowError
(
_luaState
,
caughtExcept
);
//_luaState.PushNil();
return
1
;
}
...
...
src/Metatables.cs
View file @
a32b3b5d
...
...
@@ -151,7 +151,12 @@ namespace NLua
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
)
...
...
@@ -253,7 +283,12 @@ namespace NLua
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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>
...
...
@@ -324,7 +369,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
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
)
...
...
@@ -624,7 +674,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
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
)
...
...
@@ -634,7 +689,7 @@ namespace NLua
if
(
obj
==
null
)
{
_translator
.
ThrowError
(
luaState
,
"Trying to index an invalid object reference"
);
return
2
;
return
1
;
}
string
methodName
=
luaState
.
ToString
(
2
,
false
);
...
...
@@ -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
_translator
.
ThrowError
(
luaState
,
"Can't pass instance to static method "
+
methodName
);
return
1
;
}
}
else
...
...
@@ -921,7 +977,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
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
)
...
...
@@ -931,7 +992,7 @@ namespace NLua
if
(
target
==
null
)
{
_translator
.
ThrowError
(
luaState
,
"trying to index and invalid object reference"
);
return
0
;
return
1
;
}
var
type
=
target
.
GetType
();
...
...
@@ -975,12 +1036,16 @@ namespace NLua
setter
.
Invoke
(
target
,
methodArgs
);
}
else
{
_translator
.
ThrowError
(
luaState
,
detailMessage
);
// Pass the original message from trySetMember because it is probably best
return
1
;
}
}
}
catch
(
Exception
e
)
{
ThrowError
(
luaState
,
e
);
return
1
;
}
return
0
;
...
...
@@ -1043,7 +1108,8 @@ namespace NLua
}
catch
(
Exception
e
)
{
ThrowError
(
luaState
,
e
);
detailMessage
=
"Error setting field: "
+
e
.
Message
;
return
false
;
}
return
true
;
...
...
@@ -1059,7 +1125,8 @@ namespace NLua
}
catch
(
Exception
e
)
{
ThrowError
(
luaState
,
e
);
detailMessage
=
"Error setting property: "
+
e
.
Message
;
return
false
;
}
return
true
;
...
...
@@ -1079,7 +1146,10 @@ namespace NLua
bool
success
=
TrySetMember
(
luaState
,
targetType
,
target
,
bindingType
,
out
detail
);
if
(!
success
)
{
_translator
.
ThrowError
(
luaState
,
detail
);
return
1
;
}
return
0
;
}
...
...
@@ -1112,7 +1182,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
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
)
...
...
@@ -1153,7 +1228,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
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
)
...
...
@@ -1196,7 +1276,6 @@ namespace NLua
if
(
del
==
null
)
{
_translator
.
ThrowError
(
luaState
,
"Trying to invoke a not delegate or callable value"
);
luaState
.
PushNil
();
return
1
;
}
...
...
@@ -1237,7 +1316,12 @@ namespace NLua
var
luaState
=
LuaState
.
FromIntPtr
(
state
);
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
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
)
...
...
@@ -1260,7 +1344,6 @@ namespace NLua
if
(
klass
==
null
)
{
_translator
.
ThrowError
(
luaState
,
"Trying to call constructor on an invalid type reference"
);
luaState
.
PushNil
();
return
1
;
}
...
...
@@ -1283,7 +1366,7 @@ namespace NLua
catch
(
TargetInvocationException
e
)
{
ThrowError
(
luaState
,
e
);
luaState
.
PushNil
()
;
return
1
;
}
catch
{
...
...
@@ -1305,7 +1388,6 @@ namespace NLua
string
constructorName
=
constructors
.
Length
==
0
?
"unknown"
:
constructors
[
0
].
Name
;
_translator
.
ThrowError
(
luaState
,
string
.
Format
(
"{0} does not contain constructor({1}) argument match"
,
klass
.
UnderlyingSystemType
,
constructorName
));
luaState
.
PushNil
();
return
1
;
}
...
...
@@ -1343,7 +1425,6 @@ namespace NLua
if
(
target
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot call "
+
operation
+
" on a nil object"
);
// luaState.PushNil();
return
1
;
}
...
...
@@ -1367,7 +1448,6 @@ namespace NLua
}
translator
.
ThrowError
(
luaState
,
"Cannot call ("
+
operation
+
") on object type "
+
type
.
Name
);
luaState
.
PushNil
();
return
1
;
}
...
...
src/ObjectTranslator.cs
View file @
a32b3b5d
...
...
@@ -239,7 +239,6 @@ namespace NLua
}
Push
(
luaState
,
e
);
//luaState.Error();
}
/*
...
...
@@ -253,7 +252,12 @@ namespace NLua
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
)
...
...
@@ -302,7 +306,10 @@ namespace NLua
exception
=
null
;
}
if
(
exception
!=
null
)
{
ThrowError
(
luaState
,
exception
);
return
1
;
}
}
if
(
assembly
!=
null
&&
!
assemblies
.
Contains
(
assembly
))
assemblies
.
Add
(
assembly
);
...
...
@@ -310,6 +317,7 @@ namespace NLua
catch
(
Exception
e
)
{
ThrowError
(
luaState
,
e
);
return
1
;
}
return
0
;
}
...
...
@@ -376,7 +384,12 @@ namespace NLua
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
)
...
...
@@ -384,7 +397,7 @@ namespace NLua
if
(
luaState
.
Type
(
1
)
!=
LuaType
.
Table
)
{
ThrowError
(
luaState
,
"register_table: first arg is not a table"
);
return
0
;
return
1
;
}
LuaTable
luaTable
=
GetTable
(
luaState
,
1
);
...
...
@@ -393,7 +406,7 @@ namespace NLua
if
(
string
.
IsNullOrEmpty
(
superclassName
))
{
ThrowError
(
luaState
,
"register_table: superclass name can not be null"
);
return
0
;
return
1
;
}
var
klass
=
FindType
(
superclassName
);
...
...
@@ -401,7 +414,7 @@ namespace NLua
if
(
klass
==
null
)
{
ThrowError
(
luaState
,
"register_table: can not find superclass '"
+
superclassName
+
"'"
);
return
0
;
return
1
;
}
// Creates and pushes the object in the stack, setting
...
...
@@ -438,7 +451,12 @@ namespace NLua
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
)
...
...
@@ -447,7 +465,7 @@ namespace NLua
if
(!
luaState
.
GetMetaTable
(
1
))
{
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
return
0
;
return
1
;
}
luaState
.
PushString
(
"__index"
);
...
...
@@ -455,12 +473,18 @@ namespace NLua
object
obj
=
GetRawNetObject
(
luaState
,
-
1
);
if
(
obj
==
null
)
{
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
return
1
;
}
var
luaTableField
=
obj
.
GetType
().
GetField
(
"__luaInterface_luaTable"
);
if
(
luaTableField
==
null
)
{
ThrowError
(
luaState
,
"unregister_table: arg is not valid table"
);
return
1
;
}
// ReSharper disable once PossibleNullReferenceException
luaTableField
.
SetValue
(
obj
,
null
);
...
...
@@ -484,7 +508,12 @@ namespace NLua
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
)
...
...
@@ -505,7 +534,6 @@ namespace NLua
if
(
target
==
null
)
{
ThrowError
(
luaState
,
"get_method_bysig: first arg is not type or object reference"
);
luaState
.
PushNil
();
return
1
;
}
...
...
@@ -529,7 +557,6 @@ namespace NLua
catch
(
Exception
e
)
{
ThrowError
(
luaState
,
e
);
luaState
.
PushNil
();
}
return
1
;
...
...
@@ -546,7 +573,12 @@ namespace NLua
{
var
state
=
LuaState
.
FromIntPtr
(
luaState
);
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
)
...
...
@@ -558,7 +590,10 @@ namespace NLua
klass
=
(
ProxyType
)
_objects
[
udata
];
if
(
klass
==
null
)
{
ThrowError
(
luaState
,
"get_constructor_bysig: first arg is invalid type reference"
);
return
1
;
}
var
signature
=
new
Type
[
luaState
.
GetTop
()
-
1
];
...
...
@@ -575,7 +610,6 @@ namespace NLua
catch
(
Exception
e
)
{
ThrowError
(
luaState
,
e
);
luaState
.
PushNil
();
}
return
1
;
}
...
...
tests/src/LuaTests.cs
View file @
a32b3b5d
...
...
@@ -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
*/
[
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