LuaFunction.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace LuaInterface
  5. {
  6. public class LuaFunction : LuaBase
  7. {
  8. //private Lua interpreter;
  9. internal LuaCSFunction function;
  10. //internal int reference;
  11. public LuaFunction(int reference, LuaState interpreter)
  12. {
  13. _Reference = reference;
  14. this.function = null;
  15. _Interpreter = interpreter;
  16. }
  17. public LuaFunction(LuaCSFunction function, LuaState interpreter)
  18. {
  19. _Reference = 0;
  20. this.function = function;
  21. _Interpreter = interpreter;
  22. }
  23. //~LuaFunction()
  24. //{
  25. // if (reference != 0)
  26. // interpreter.dispose(reference);
  27. //}
  28. //bool disposed = false;
  29. //~LuaFunction()
  30. //{
  31. // Dispose(false);
  32. //}
  33. //public void Dispose()
  34. //{
  35. // Dispose(true);
  36. // GC.SuppressFinalize(this);
  37. //}
  38. //public virtual void Dispose(bool disposeManagedResources)
  39. //{
  40. // if (!this.disposed)
  41. // {
  42. // if (disposeManagedResources)
  43. // {
  44. // if (_Reference != 0)
  45. // _Interpreter.dispose(_Reference);
  46. // }
  47. // disposed = true;
  48. // }
  49. //}
  50. /*
  51. * Calls the function casting return values to the types
  52. * in returnTypes
  53. */
  54. internal object[] call(object[] args, Type[] returnTypes)
  55. {
  56. return _Interpreter.callFunction(this, args, returnTypes);
  57. }
  58. /*
  59. * Calls the function and returns its return values inside
  60. * an array
  61. */
  62. public object[] Call(params object[] args)
  63. {
  64. return _Interpreter.callFunction(this, args);
  65. }
  66. /*
  67. * Pushes the function into the Lua stack
  68. */
  69. internal void push(IntPtr luaState)
  70. {
  71. if (_Reference != 0)
  72. LuaDLL.lua_getref(luaState, _Reference);
  73. else
  74. _Interpreter.pushCSFunction(function);
  75. }
  76. public override string ToString()
  77. {
  78. return "function";
  79. }
  80. public override bool Equals(object o)
  81. {
  82. if (o is LuaFunction)
  83. {
  84. LuaFunction l = (LuaFunction)o;
  85. if (this._Reference != 0 && l._Reference != 0)
  86. return _Interpreter.compareRef(l._Reference, this._Reference);
  87. else
  88. return this.function == l.function;
  89. }
  90. else return false;
  91. }
  92. public override int GetHashCode()
  93. {
  94. if (_Reference != 0)
  95. return _Reference;
  96. else
  97. return function.GetHashCode();
  98. }
  99. }
  100. }