LuaBase.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Tencent is pleased to support the open source community by making xLua available.
  3. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. #if USE_UNI_LUA
  9. using LuaAPI = UniLua.Lua;
  10. using RealStatePtr = UniLua.ILuaState;
  11. using LuaCSFunction = UniLua.CSharpFunctionDelegate;
  12. #else
  13. using LuaAPI = XLua.LuaDLL.Lua;
  14. using RealStatePtr = System.IntPtr;
  15. using LuaCSFunction = XLua.LuaDLL.lua_CSFunction;
  16. #endif
  17. using System;
  18. namespace XLua
  19. {
  20. public abstract class LuaBase : IDisposable
  21. {
  22. protected bool disposed;
  23. protected readonly int luaReference;
  24. protected readonly LuaEnv luaEnv;
  25. #if UNITY_EDITOR || XLUA_GENERAL
  26. protected int _errorFuncRef { get { return luaEnv.errorFuncRef; } }
  27. protected RealStatePtr _L { get { return luaEnv.L; } }
  28. protected ObjectTranslator _translator { get { return luaEnv.translator; } }
  29. #endif
  30. public LuaBase(int reference, LuaEnv luaenv)
  31. {
  32. luaReference = reference;
  33. luaEnv = luaenv;
  34. }
  35. ~LuaBase()
  36. {
  37. Dispose(false);
  38. }
  39. public void Dispose()
  40. {
  41. Dispose(true);
  42. GC.SuppressFinalize(this);
  43. }
  44. public virtual void Dispose(bool disposeManagedResources)
  45. {
  46. if (!disposed)
  47. {
  48. if (luaReference != 0)
  49. {
  50. #if THREAD_SAFE || HOTFIX_ENABLE
  51. lock (luaEnv.luaEnvLock)
  52. {
  53. #endif
  54. bool is_delegate = this is DelegateBridgeBase;
  55. if (disposeManagedResources)
  56. {
  57. luaEnv.translator.ReleaseLuaBase(luaEnv.L, luaReference, is_delegate);
  58. }
  59. else //will dispse by LuaEnv.GC
  60. {
  61. luaEnv.equeueGCAction(new LuaEnv.GCAction { Reference = luaReference, IsDelegate = is_delegate });
  62. }
  63. #if THREAD_SAFE || HOTFIX_ENABLE
  64. }
  65. #endif
  66. }
  67. disposed = true;
  68. }
  69. }
  70. public override bool Equals(object o)
  71. {
  72. if (o != null && this.GetType() == o.GetType())
  73. {
  74. #if THREAD_SAFE || HOTFIX_ENABLE
  75. lock (luaEnv.luaEnvLock)
  76. {
  77. #endif
  78. LuaBase rhs = (LuaBase)o;
  79. var L = luaEnv.L;
  80. if (L != rhs.luaEnv.L)
  81. return false;
  82. int top = LuaAPI.lua_gettop(L);
  83. LuaAPI.lua_getref(L, rhs.luaReference);
  84. LuaAPI.lua_getref(L, luaReference);
  85. int equal = LuaAPI.lua_rawequal(L, -1, -2);
  86. LuaAPI.lua_settop(L, top);
  87. return (equal != 0);
  88. #if THREAD_SAFE || HOTFIX_ENABLE
  89. }
  90. #endif
  91. }
  92. else return false;
  93. }
  94. public override int GetHashCode()
  95. {
  96. LuaAPI.lua_getref(luaEnv.L, luaReference);
  97. var pointer = LuaAPI.lua_topointer(luaEnv.L, -1);
  98. LuaAPI.lua_pop(luaEnv.L, 1);
  99. return pointer.ToInt32();
  100. }
  101. internal virtual void push(RealStatePtr L)
  102. {
  103. LuaAPI.lua_getref(L, luaReference);
  104. }
  105. }
  106. }