Commit 5fc72461 authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Mono-style format.

parent a0e1671c
......@@ -23,7 +23,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface
......
......@@ -23,7 +23,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface
......
This diff is collapsed.
......@@ -23,7 +23,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.IO;
using System.Runtime.InteropServices;
......
......@@ -23,7 +23,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface
......
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
......@@ -38,24 +37,22 @@ namespace LuaInterface
/// </summary>
/// <param name="lua">The Lua VM to add the methods to</param>
/// <param name="o">The object to get the methods from</param>
public static void TaggedInstanceMethods(Lua lua, object o)
public static void TaggedInstanceMethods (Lua lua, object o)
{
#region Sanity checks
if(lua.IsNull())
throw new ArgumentNullException("lua");
if (lua.IsNull ())
throw new ArgumentNullException ("lua");
if(o.IsNull())
throw new ArgumentNullException("o");
if (o.IsNull ())
throw new ArgumentNullException ("o");
#endregion
foreach(var method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
{
foreach(LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true))
{
if(string.IsNullOrEmpty(attribute.Name))
lua.RegisterFunction(method.Name, o, method); // CLR name
foreach (var method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public)) {
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true)) {
if (string.IsNullOrEmpty (attribute.Name))
lua.RegisterFunction (method.Name, o, method); // CLR name
else
lua.RegisterFunction(attribute.Name, o, method); // Custom name
lua.RegisterFunction (attribute.Name, o, method); // Custom name
}
}
}
......@@ -67,27 +64,25 @@ namespace LuaInterface
/// </summary>
/// <param name="lua">The Lua VM to add the methods to</param>
/// <param name="type">The class type to get the methods from</param>
public static void TaggedStaticMethods(Lua lua, Type type)
public static void TaggedStaticMethods (Lua lua, Type type)
{
#region Sanity checks
if(lua.IsNull())
throw new ArgumentNullException("lua");
if (lua.IsNull ())
throw new ArgumentNullException ("lua");
if(type.IsNull())
throw new ArgumentNullException("type");
if (type.IsNull ())
throw new ArgumentNullException ("type");
if(!type.IsClass)
throw new ArgumentException("The type must be a class!", "type");
if (!type.IsClass)
throw new ArgumentException ("The type must be a class!", "type");
#endregion
foreach(var method in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
{
foreach(LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false))
{
if(string.IsNullOrEmpty(attribute.Name))
lua.RegisterFunction(method.Name, null, method); // CLR name
foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)) {
foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false)) {
if (string.IsNullOrEmpty (attribute.Name))
lua.RegisterFunction (method.Name, null, method); // CLR name
else
lua.RegisterFunction(attribute.Name, null, method); // Custom name
lua.RegisterFunction (attribute.Name, null, method); // Custom name
}
}
}
......@@ -100,26 +95,25 @@ namespace LuaInterface
/// <typeparam name="T">The enum type to register</typeparam>
/// <param name="lua">The Lua VM to add the enum to</param>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to select an enum type")]
public static void Enumeration<T>(Lua lua)
public static void Enumeration<T> (Lua lua)
{
#region Sanity checks
if(lua.IsNull())
throw new ArgumentNullException("lua");
if (lua.IsNull ())
throw new ArgumentNullException ("lua");
#endregion
var type = typeof(T);
if(!type.IsEnum)
throw new ArgumentException("The type must be an enumeration!");
if (!type.IsEnum)
throw new ArgumentException ("The type must be an enumeration!");
string[] names = Enum.GetNames(type);
var values = (T[])Enum.GetValues(type);
lua.NewTable(type.Name);
string[] names = Enum.GetNames (type);
var values = (T[])Enum.GetValues (type);
lua.NewTable (type.Name);
for(int i = 0; i < names.Length; i++)
{
string path = type.Name + "." + names[i];
lua[path] = values[i];
for (int i = 0; i < names.Length; i++) {
string path = type.Name + "." + names [i];
lua [path] = values [i];
}
}
#endregion
......
......@@ -22,7 +22,6 @@
* 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;
......@@ -40,7 +39,7 @@ namespace LuaInterface
*/
public class LuaTable : LuaBase
{
public LuaTable(int reference, Lua interpreter)
public LuaTable (int reference, Lua interpreter)
{
_Reference = reference;
_Interpreter = interpreter;
......@@ -49,63 +48,55 @@ namespace LuaInterface
/*
* Indexer for string fields of the table
*/
public object this[string field]
{
get
{
return _Interpreter.getObject(_Reference, field);
public object this [string field] {
get {
return _Interpreter.getObject (_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
set {
_Interpreter.setObject (_Reference, field, value);
}
}
/*
* Indexer for numeric fields of the table
*/
public object this[object field]
{
get
{
return _Interpreter.getObject(_Reference, field);
public object this [object field] {
get {
return _Interpreter.getObject (_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
set {
_Interpreter.setObject (_Reference, field, value);
}
}
public System.Collections.IDictionaryEnumerator GetEnumerator()
public System.Collections.IDictionaryEnumerator GetEnumerator ()
{
return _Interpreter.GetTableDict(this).GetEnumerator();
return _Interpreter.GetTableDict (this).GetEnumerator ();
}
public ICollection Keys
{
get { return _Interpreter.GetTableDict(this).Keys; }
public ICollection Keys {
get { return _Interpreter.GetTableDict (this).Keys; }
}
public ICollection Values
{
get { return _Interpreter.GetTableDict(this).Values; }
public ICollection Values {
get { return _Interpreter.GetTableDict (this).Values; }
}
/*
* Gets an string fields of a table ignoring its metatable,
* if it exists
*/
internal object rawget(string field)
internal object rawget (string field)
{
return _Interpreter.rawGetObject(_Reference, field);
return _Interpreter.rawGetObject (_Reference, field);
}
internal object rawgetFunction(string field)
internal object rawgetFunction (string field)
{
object obj = _Interpreter.rawGetObject(_Reference, field);
object obj = _Interpreter.rawGetObject (_Reference, field);
if(obj is LuaCore.lua_CFunction)
return new LuaFunction((LuaCore.lua_CFunction)obj, _Interpreter);
if (obj is LuaCore.lua_CFunction)
return new LuaFunction ((LuaCore.lua_CFunction)obj, _Interpreter);
else
return obj;
}
......@@ -113,12 +104,12 @@ namespace LuaInterface
/*
* Pushes this table into the Lua stack
*/
internal void push(LuaCore.lua_State luaState)
internal void push (LuaCore.lua_State luaState)
{
LuaLib.lua_getref(luaState, _Reference);
LuaLib.lua_getref (luaState, _Reference);
}
public override string ToString()
public override string ToString ()
{
return "table";
}
......
......@@ -22,7 +22,6 @@
* 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;
......@@ -33,7 +32,7 @@ namespace LuaInterface
public class LuaUserData : LuaBase
{
public LuaUserData(int reference, Lua interpreter)
public LuaUserData (int reference, Lua interpreter)
{
_Reference = reference;
_Interpreter = interpreter;
......@@ -42,30 +41,24 @@ namespace LuaInterface
/*
* Indexer for string fields of the userdata
*/
public object this[string field]
{
get
{
return _Interpreter.getObject(_Reference, field);
public object this [string field] {
get {
return _Interpreter.getObject (_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
set {
_Interpreter.setObject (_Reference, field, value);
}
}
/*
* Indexer for numeric fields of the userdata
*/
public object this[object field]
{
get
{
return _Interpreter.getObject(_Reference, field);
public object this [object field] {
get {
return _Interpreter.getObject (_Reference, field);
}
set
{
_Interpreter.setObject(_Reference, field, value);
set {
_Interpreter.setObject (_Reference, field, value);
}
}
......@@ -73,20 +66,20 @@ namespace LuaInterface
* Calls the userdata and returns its return values inside
* an array
*/
public object[] Call(params object[] args)
public object[] Call (params object[] args)
{
return _Interpreter.callFunction(this, args);
return _Interpreter.callFunction (this, args);
}
/*
* Pushes the userdata into the Lua stack
*/
internal void push(LuaCore.lua_State luaState)
internal void push (LuaCore.lua_State luaState)
{
LuaLib.lua_getref(luaState, _Reference);
LuaLib.lua_getref (luaState, _Reference);
}
public override string ToString()
public override string ToString ()
{
return "userdata";
}
......
This diff is collapsed.
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Diagnostics;
using System.Collections.Generic;
......@@ -34,28 +33,28 @@ namespace LuaInterface.Method
/// </summary>
class EventHandlerContainer : IDisposable
{
private Dictionary<Delegate, RegisterEventHandler> dict = new Dictionary<Delegate, RegisterEventHandler>();
private Dictionary<Delegate, RegisterEventHandler> dict = new Dictionary<Delegate, RegisterEventHandler> ();
public void Add(Delegate handler, RegisterEventHandler eventInfo)
public void Add (Delegate handler, RegisterEventHandler eventInfo)
{
dict.Add(handler, eventInfo);
dict.Add (handler, eventInfo);
}
public void Remove(Delegate handler)
public void Remove (Delegate handler)
{
bool found = dict.Remove(handler);
Debug.Assert(found);
bool found = dict.Remove (handler);
Debug.Assert (found);
}
/// <summary>
/// Remove any still registered handlers
/// </summary>
public void Dispose()
public void Dispose ()
{
foreach(KeyValuePair<Delegate, RegisterEventHandler> pair in dict)
pair.Value.RemovePending(pair.Key);
foreach (KeyValuePair<Delegate, RegisterEventHandler> pair in dict)
pair.Value.RemovePending (pair.Key);
dict.Clear();
dict.Clear ();
}
}
}
\ No newline at end of file
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface.Method
......@@ -39,11 +38,11 @@ namespace LuaInterface.Method
* Gets the function called name from the provided table,
* returning null if it does not exist
*/
public static LuaFunction getTableFunction(LuaTable luaTable, string name)
public static LuaFunction getTableFunction (LuaTable luaTable, string name)
{
object funcObj = luaTable.rawget(name);
object funcObj = luaTable.rawget (name);
if(funcObj is LuaFunction)
if (funcObj is LuaFunction)
return (LuaFunction)funcObj;
else
return null;
......@@ -52,29 +51,25 @@ namespace LuaInterface.Method
/*
* Calls the provided function with the provided parameters
*/
public static object callFunction(LuaFunction function, object[] args, Type[] returnTypes, object[] inArgs, int[] outArgs)
public static object callFunction (LuaFunction function, object[] args, Type[] returnTypes, object[] inArgs, int[] outArgs)
{
// args is the return array of arguments, inArgs is the actual array
// of arguments passed to the function (with in parameters only), outArgs
// has the positions of out parameters
object returnValue;
int iRefArgs;
object[] returnValues = function.call(inArgs, returnTypes);
object[] returnValues = function.call (inArgs, returnTypes);
if(returnTypes[0] == typeof(void))
{
if (returnTypes [0] == typeof(void)) {
returnValue = null;
iRefArgs = 0;
}
else
{
returnValue = returnValues[0];
} else {
returnValue = returnValues [0];
iRefArgs = 1;
}
for(int i = 0; i < outArgs.Length; i++)
{
args[outArgs[i]] = returnValues[iRefArgs];
for (int i = 0; i < outArgs.Length; i++) {
args [outArgs [i]] = returnValues [iRefArgs];
iRefArgs++;
}
......
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface.Method
......@@ -40,37 +39,33 @@ namespace LuaInterface.Method
public LuaFunction function;
public Type[] returnTypes;
public LuaDelegate()
public LuaDelegate ()
{
function = null;
returnTypes = null;
}
public object callFunction(object[] args, object[] inArgs, int[] outArgs)
public object callFunction (object[] args, object[] inArgs, int[] outArgs)
{
// args is the return array of arguments, inArgs is the actual array
// of arguments passed to the function (with in parameters only), outArgs
// has the positions of out parameters
object returnValue;
int iRefArgs;
object[] returnValues = function.call(inArgs, returnTypes);
object[] returnValues = function.call (inArgs, returnTypes);
if(returnTypes[0] == typeof(void))
{
if (returnTypes [0] == typeof(void)) {
returnValue = null;
iRefArgs = 0;
}
else
{
returnValue = returnValues[0];
} else {
returnValue = returnValues [0];
iRefArgs = 1;
}
// Sets the value of out and ref parameters (from
// the values returned by the Lua function).
for(int i = 0; i < outArgs.Length; i++)
{
args[outArgs[i]] = returnValues[iRefArgs];
for (int i = 0; i < outArgs.Length; i++) {
args [outArgs [i]] = returnValues [iRefArgs];
iRefArgs++;
}
......
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface.Method
......@@ -41,9 +40,9 @@ namespace LuaInterface.Method
// CP: Fix provided by Ben Bryant for delegates with one param
// link: http://luaforge.net/forum/message.php?msg_id=9318
public void handleEvent(object[] args)
public void handleEvent (object[] args)
{
handler.Call(args);
handler.Call (args);
}
//public void handleEvent(object sender,object data)
//{
......
......@@ -22,7 +22,6 @@
* 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;
......@@ -36,7 +35,7 @@ namespace LuaInterface.Method
/*
* Argument extraction with type-conversion function
*/
delegate object ExtractValue(LuaCore.lua_State luaState, int stackPos);
delegate object ExtractValue (LuaCore.lua_State luaState, int stackPos);
/*
* Wrapper class for methods/constructors accessed from Lua.
......@@ -48,10 +47,9 @@ namespace LuaInterface.Method
{
private ObjectTranslator _Translator;
private MethodBase _Method;
private MethodCache _LastCalledMethod = new MethodCache();
private MethodCache _LastCalledMethod = new MethodCache ();
private string _MethodName;
private MemberInfo[] _Members;
private IReflect _TargetType;
private ExtractValue _ExtractTarget;
private object _Target;
private BindingFlags _BindingType;
......@@ -59,19 +57,18 @@ namespace LuaInterface.Method
/*
* Constructs the wrapper for a known MethodBase instance
*/
public LuaMethodWrapper(ObjectTranslator translator, object target, IReflect targetType, MethodBase method)
public LuaMethodWrapper (ObjectTranslator translator, object target, IReflect targetType, MethodBase method)
{
_Translator = translator;
_Target = target;
_TargetType = targetType;
if(!targetType.IsNull())
_ExtractTarget = translator.typeChecker.getExtractor(targetType);
if (!targetType.IsNull ())
_ExtractTarget = translator.typeChecker.getExtractor (targetType);
_Method = method;
_MethodName = method.Name;
if(method.IsStatic)
if (method.IsStatic)
_BindingType = BindingFlags.Static;
else
_BindingType = BindingFlags.Instance;
......@@ -80,18 +77,17 @@ namespace LuaInterface.Method
/*
* Constructs the wrapper for a known method name
*/
public LuaMethodWrapper(ObjectTranslator translator, IReflect targetType, string methodName, BindingFlags bindingType)
public LuaMethodWrapper (ObjectTranslator translator, IReflect targetType, string methodName, BindingFlags bindingType)
{
_Translator = translator;
_MethodName = methodName;
_TargetType = targetType;
if(!targetType.IsNull())
_ExtractTarget = translator.typeChecker.getExtractor(targetType);
if (!targetType.IsNull ())
_ExtractTarget = translator.typeChecker.getExtractor (targetType);
_BindingType = bindingType;
//CP: Removed NonPublic binding search and added IgnoreCase
_Members = targetType.UnderlyingSystemType.GetMember(methodName, MemberTypes.Method, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase/*|BindingFlags.NonPublic*/);
_Members = targetType.UnderlyingSystemType.GetMember (methodName, MemberTypes.Method, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase/*|BindingFlags.NonPublic*/);
}
/// <summary>
......@@ -99,226 +95,187 @@ namespace LuaInterface.Method
/// </summary>
/// <returns>num of things on stack</returns>
/// <param name="e">null for no pending exception</param>
int SetPendingException(Exception e)
int SetPendingException (Exception e)
{
return _Translator.interpreter.SetPendingException(e);
return _Translator.interpreter.SetPendingException (e);
}
/*
* Calls the method. Receives the arguments from the Lua stack
* and returns values in it.
*/
public int call(LuaCore.lua_State luaState)
public int call (LuaCore.lua_State luaState)
{
var methodToCall = _Method;
object targetObject = _Target;
bool failedCall = true;
int nReturnValues = 0;
if(!LuaLib.lua_checkstack(luaState, 5))
throw new LuaException("Lua stack overflow");
if (!LuaLib.lua_checkstack (luaState, 5))
throw new LuaException ("Lua stack overflow");
bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static;
SetPendingException(null);
SetPendingException (null);
if(methodToCall.IsNull()) // Method from name
{
if(isStatic)
if (methodToCall.IsNull ()) { // Method from name
if (isStatic)
targetObject = null;
else
targetObject = _ExtractTarget(luaState, 1);
targetObject = _ExtractTarget (luaState, 1);
//LuaLib.lua_remove(luaState,1); // Pops the receiver
if(!_LastCalledMethod.cachedMethod.IsNull()) // Cached?
{
if (!_LastCalledMethod.cachedMethod.IsNull ()) { // 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.lua_gettop(luaState) - numStackToSkip;
if(numArgsPassed == _LastCalledMethod.argTypes.Length) // No. of args match?
{
if(!LuaLib.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
throw new LuaException("Lua stack overflow");
try
{
for(int i = 0; i < _LastCalledMethod.argTypes.Length; i++)
{
if(_LastCalledMethod.argTypes[i].isParamsArray)
{
object luaParamValue = _LastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip);
var paramArrayType = _LastCalledMethod.argTypes[i].paramsArrayType;
int numArgsPassed = LuaLib.lua_gettop (luaState) - numStackToSkip;
if (numArgsPassed == _LastCalledMethod.argTypes.Length) { // No. of args match?
if (!LuaLib.lua_checkstack (luaState, _LastCalledMethod.outList.Length + 6))
throw new LuaException ("Lua stack overflow");
try {
for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++) {
if (_LastCalledMethod.argTypes [i].isParamsArray) {
object luaParamValue = _LastCalledMethod.argTypes [i].extractValue (luaState, i + 1 + numStackToSkip);
var paramArrayType = _LastCalledMethod.argTypes [i].paramsArrayType;
Array paramArray;
if(luaParamValue is LuaTable)
{
if (luaParamValue is LuaTable) {
var table = (LuaTable)luaParamValue;
paramArray = Array.CreateInstance(paramArrayType, table.Values.Count);
paramArray = Array.CreateInstance (paramArrayType, table.Values.Count);
for(int x = 1; x <= table.Values.Count; x++)
paramArray.SetValue(Convert.ChangeType(table[x], paramArrayType), x - 1);
}
else
{
paramArray = Array.CreateInstance(paramArrayType, 1);
paramArray.SetValue(luaParamValue, 0);
for (int x = 1; x <= table.Values.Count; x++)
paramArray.SetValue (Convert.ChangeType (table [x], paramArrayType), x - 1);
} else {
paramArray = Array.CreateInstance (paramArrayType, 1);
paramArray.SetValue (luaParamValue, 0);
}
_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] = paramArray;
}
else
{
_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] =
_LastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip);
_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] = paramArray;
} else {
_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] =
_LastCalledMethod.argTypes [i].extractValue (luaState, i + 1 + numStackToSkip);
}
if(_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] == null &&
!LuaLib.lua_isnil(luaState, i + 1 + numStackToSkip))
throw new LuaException("argument number " + (i + 1) + " is invalid");
if (_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] == null &&
!LuaLib.lua_isnil (luaState, i + 1 + numStackToSkip))
throw new LuaException ("argument number " + (i + 1) + " is invalid");
}
if((_BindingType & BindingFlags.Static) == BindingFlags.Static)
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
else
{
if(_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
if ((_BindingType & BindingFlags.Static) == BindingFlags.Static)
_Translator.push (luaState, _LastCalledMethod.cachedMethod.Invoke (null, _LastCalledMethod.args));
else {
if (_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push (luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke (_LastCalledMethod.args));
else
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
_Translator.push (luaState, _LastCalledMethod.cachedMethod.Invoke (targetObject, _LastCalledMethod.args));
}
failedCall = false;
}
catch(TargetInvocationException e)
{
} catch (TargetInvocationException e) {
// Failure of method invocation
return SetPendingException(e.GetBaseException());
}
catch(Exception e)
{
if(_Members.Length == 1) // Is the method overloaded?
return SetPendingException (e.GetBaseException ());
} catch (Exception e) {
if (_Members.Length == 1) // Is the method overloaded?
// No, throw error
return SetPendingException(e);
return SetPendingException (e);
}
}
}
// Cache miss
if(failedCall)
{
if (failedCall) {
// System.Diagnostics.Debug.WriteLine("cache miss on " + methodName);
// If we are running an instance variable, we can now pop the targetObject from the stack
if(!isStatic)
{
if(targetObject.IsNull())
{
_Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName));
LuaLib.lua_pushnil(luaState);
if (!isStatic) {
if (targetObject.IsNull ()) {
_Translator.throwError (luaState, String.Format ("instance method '{0}' requires a non null target object", _MethodName));
LuaLib.lua_pushnil (luaState);
return 1;
}
LuaLib.lua_remove(luaState, 1); // Pops the receiver
LuaLib.lua_remove (luaState, 1); // Pops the receiver
}
bool hasMatch = false;
string candidateName = null;
foreach(var member in _Members)
{
foreach (var member in _Members) {
candidateName = member.ReflectedType.Name + "." + member.Name;
var m = (MethodInfo)member;
bool isMethod = _Translator.matchParameters(luaState, m, ref _LastCalledMethod);
bool isMethod = _Translator.matchParameters (luaState, m, ref _LastCalledMethod);
if(isMethod)
{
if (isMethod) {
hasMatch = true;
break;
}
}
if(!hasMatch)
{
if (!hasMatch) {
string msg = (candidateName == null) ? "invalid arguments to method call" : ("invalid arguments to method: " + candidateName);
_Translator.throwError(luaState, msg);
LuaLib.lua_pushnil(luaState);
_Translator.throwError (luaState, msg);
LuaLib.lua_pushnil (luaState);
return 1;
}
}
}
else // Method from MethodBase instance
{
if(methodToCall.ContainsGenericParameters)
{
/*bool isMethod = */_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod);
if(methodToCall.IsGenericMethodDefinition)
{
} else { // Method from MethodBase instance
if (methodToCall.ContainsGenericParameters) {
/*bool isMethod = */
_Translator.matchParameters (luaState, methodToCall, ref _LastCalledMethod);
if (methodToCall.IsGenericMethodDefinition) {
//need to make a concrete type of the generic method definition
var typeArgs = new List<Type>();
var typeArgs = new List<Type> ();
foreach(object arg in _LastCalledMethod.args)
typeArgs.Add(arg.GetType());
foreach (object arg in _LastCalledMethod.args)
typeArgs.Add (arg.GetType ());
var concreteMethod = (methodToCall as MethodInfo).MakeGenericMethod(typeArgs.ToArray());
_Translator.push(luaState, concreteMethod.Invoke(targetObject, _LastCalledMethod.args));
var concreteMethod = (methodToCall as MethodInfo).MakeGenericMethod (typeArgs.ToArray ());
_Translator.push (luaState, concreteMethod.Invoke (targetObject, _LastCalledMethod.args));
failedCall = false;
}
else if(methodToCall.ContainsGenericParameters)
{
_Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method");
LuaLib.lua_pushnil(luaState);
} else if (methodToCall.ContainsGenericParameters) {
_Translator.throwError (luaState, "unable to invoke method on generic class as the current method is an open generic method");
LuaLib.lua_pushnil (luaState);
return 1;
}
}
else
{
if(!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
{
targetObject = _ExtractTarget(luaState, 1);
LuaLib.lua_remove(luaState, 1); // Pops the receiver
} else {
if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null) {
targetObject = _ExtractTarget (luaState, 1);
LuaLib.lua_remove (luaState, 1); // Pops the receiver
}
if(!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod))
{
_Translator.throwError(luaState, "invalid arguments to method call");
LuaLib.lua_pushnil(luaState);
if (!_Translator.matchParameters (luaState, methodToCall, ref _LastCalledMethod)) {
_Translator.throwError (luaState, "invalid arguments to method call");
LuaLib.lua_pushnil (luaState);
return 1;
}
}
}
if(failedCall)
{
if(!LuaLib.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
throw new LuaException("Lua stack overflow");
try
{
if(isStatic)
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
else
{
if(_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
if (failedCall) {
if (!LuaLib.lua_checkstack (luaState, _LastCalledMethod.outList.Length + 6))
throw new LuaException ("Lua stack overflow");
try {
if (isStatic)
_Translator.push (luaState, _LastCalledMethod.cachedMethod.Invoke (null, _LastCalledMethod.args));
else {
if (_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push (luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke (_LastCalledMethod.args));
else
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
_Translator.push (luaState, _LastCalledMethod.cachedMethod.Invoke (targetObject, _LastCalledMethod.args));
}
}
catch(TargetInvocationException e)
{
return SetPendingException(e.GetBaseException());
}
catch(Exception e)
{
return SetPendingException(e);
} catch (TargetInvocationException e) {
return SetPendingException (e.GetBaseException ());
} catch (Exception e) {
return SetPendingException (e);
}
}
// Pushes out and ref return values
for(int index = 0; index < _LastCalledMethod.outList.Length; index++)
{
for (int index = 0; index < _LastCalledMethod.outList.Length; index++) {
nReturnValues++;
//for(int i=0;i<lastCalledMethod.outList.Length;i++)
_Translator.push(luaState, _LastCalledMethod.args[_LastCalledMethod.outList[index]]);
_Translator.push (luaState, _LastCalledMethod.args [_LastCalledMethod.outList [index]]);
}
//by isSingle 2010-09-10 11:26:31
......@@ -326,7 +283,7 @@ namespace LuaInterface.Method
// if not return void,we need add 1,
// or we will lost the function's return value
// when call dotnet function like "int foo(arg1,out arg2,out arg3)" in lua code
if(!_LastCalledMethod.IsReturnVoid && nReturnValues > 0)
if (!_LastCalledMethod.IsReturnVoid && nReturnValues > 0)
nReturnValues++;
return nReturnValues < 1 ? 1 : nReturnValues;
......
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface.Method
......
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Reflection;
using LuaInterface.Extensions;
......@@ -36,19 +35,16 @@ namespace LuaInterface.Method
{
private MethodBase _cachedMethod;
public MethodBase cachedMethod
{
get
{
public MethodBase cachedMethod {
get {
return _cachedMethod;
}
set
{
set {
_cachedMethod = value;
var mi = value as MethodInfo;
if(!mi.IsNull())
IsReturnVoid = string.Compare(mi.ReturnType.Name, "System.Void", true) == 0;
if (!mi.IsNull ())
IsReturnVoid = string.Compare (mi.ReturnType.Name, "System.Void", true) == 0;
}
}
......
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Reflection;
......@@ -41,7 +40,7 @@ namespace LuaInterface.Method
private EventInfo eventInfo;
private object target;
public RegisterEventHandler(EventHandlerContainer pendingEvents, object target, EventInfo eventInfo)
public RegisterEventHandler (EventHandlerContainer pendingEvents, object target, EventInfo eventInfo)
{
this.target = target;
this.eventInfo = eventInfo;
......@@ -51,13 +50,13 @@ namespace LuaInterface.Method
/*
* Adds a new event handler
*/
public Delegate Add(LuaFunction function)
public Delegate Add (LuaFunction function)
{
//CP: Fix by Ben Bryant for event handling with one parameter
//link: http://luaforge.net/forum/message.php?msg_id=9266
Delegate handlerDelegate = CodeGeneration.Instance.GetDelegate(eventInfo.EventHandlerType, function);
eventInfo.AddEventHandler(target, handlerDelegate);
pendingEvents.Add(handlerDelegate, this);
Delegate handlerDelegate = CodeGeneration.Instance.GetDelegate (eventInfo.EventHandlerType, function);
eventInfo.AddEventHandler (target, handlerDelegate);
pendingEvents.Add (handlerDelegate, this);
return handlerDelegate;
//MethodInfo mi = eventInfo.EventHandlerType.GetMethod("Invoke");
......@@ -72,18 +71,18 @@ namespace LuaInterface.Method
/*
* Removes an existing event handler
*/
public void Remove(Delegate handlerDelegate)
public void Remove (Delegate handlerDelegate)
{
RemovePending(handlerDelegate);
pendingEvents.Remove(handlerDelegate);
RemovePending (handlerDelegate);
pendingEvents.Remove (handlerDelegate);
}
/*
* Removes an existing event handler (without updating the pending handlers list)
*/
internal void RemovePending(Delegate handlerDelegate)
internal void RemovePending (Delegate handlerDelegate)
{
eventInfo.RemoveEventHandler(target, handlerDelegate);
eventInfo.RemoveEventHandler (target, handlerDelegate);
}
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -22,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment