LuaFunction.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. using System.Collections.Generic;
  19. namespace XLua
  20. {
  21. public partial class LuaFunction : LuaBase
  22. {
  23. public LuaFunction(int reference, LuaEnv luaenv) : base(reference, luaenv)
  24. {
  25. }
  26. //Action和Func是方便使用的无gc api,如果需要用到out,ref参数,建议使用delegate
  27. //如果需要其它个数的Action和Func, 这个类声明为partial,可以自己加
  28. public void Action<T>(T a)
  29. {
  30. #if THREAD_SAFE || HOTFIX_ENABLE
  31. lock (luaEnv.luaEnvLock)
  32. {
  33. #endif
  34. var L = luaEnv.L;
  35. var translator = luaEnv.translator;
  36. int oldTop = LuaAPI.lua_gettop(L);
  37. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  38. LuaAPI.lua_getref(L, luaReference);
  39. translator.PushByType(L, a);
  40. int error = LuaAPI.lua_pcall(L, 1, 0, errFunc);
  41. if (error != 0)
  42. luaEnv.ThrowExceptionFromError(oldTop);
  43. LuaAPI.lua_settop(L, oldTop);
  44. #if THREAD_SAFE || HOTFIX_ENABLE
  45. }
  46. #endif
  47. }
  48. public TResult Func<T, TResult>(T a)
  49. {
  50. #if THREAD_SAFE || HOTFIX_ENABLE
  51. lock (luaEnv.luaEnvLock)
  52. {
  53. #endif
  54. var L = luaEnv.L;
  55. var translator = luaEnv.translator;
  56. int oldTop = LuaAPI.lua_gettop(L);
  57. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  58. LuaAPI.lua_getref(L, luaReference);
  59. translator.PushByType(L, a);
  60. int error = LuaAPI.lua_pcall(L, 1, 1, errFunc);
  61. if (error != 0)
  62. luaEnv.ThrowExceptionFromError(oldTop);
  63. TResult ret;
  64. try
  65. {
  66. translator.Get(L, -1, out ret);
  67. }
  68. catch (Exception e)
  69. {
  70. throw e;
  71. }
  72. finally
  73. {
  74. LuaAPI.lua_settop(L, oldTop);
  75. }
  76. return ret;
  77. #if THREAD_SAFE || HOTFIX_ENABLE
  78. }
  79. #endif
  80. }
  81. public void Action<T1, T2>(T1 a1, T2 a2)
  82. {
  83. #if THREAD_SAFE || HOTFIX_ENABLE
  84. lock (luaEnv.luaEnvLock)
  85. {
  86. #endif
  87. var L = luaEnv.L;
  88. var translator = luaEnv.translator;
  89. int oldTop = LuaAPI.lua_gettop(L);
  90. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  91. LuaAPI.lua_getref(L, luaReference);
  92. translator.PushByType(L, a1);
  93. translator.PushByType(L, a2);
  94. int error = LuaAPI.lua_pcall(L, 2, 0, errFunc);
  95. if (error != 0)
  96. luaEnv.ThrowExceptionFromError(oldTop);
  97. LuaAPI.lua_settop(L, oldTop);
  98. #if THREAD_SAFE || HOTFIX_ENABLE
  99. }
  100. #endif
  101. }
  102. public TResult Func<T1, T2, TResult>(T1 a1, T2 a2)
  103. {
  104. #if THREAD_SAFE || HOTFIX_ENABLE
  105. lock (luaEnv.luaEnvLock)
  106. {
  107. #endif
  108. var L = luaEnv.L;
  109. var translator = luaEnv.translator;
  110. int oldTop = LuaAPI.lua_gettop(L);
  111. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  112. LuaAPI.lua_getref(L, luaReference);
  113. translator.PushByType(L, a1);
  114. translator.PushByType(L, a2);
  115. int error = LuaAPI.lua_pcall(L, 2, 1, errFunc);
  116. if (error != 0)
  117. luaEnv.ThrowExceptionFromError(oldTop);
  118. TResult ret;
  119. try
  120. {
  121. translator.Get(L, -1, out ret);
  122. }
  123. catch (Exception e)
  124. {
  125. throw e;
  126. }
  127. finally
  128. {
  129. LuaAPI.lua_settop(L, oldTop);
  130. }
  131. return ret;
  132. #if THREAD_SAFE || HOTFIX_ENABLE
  133. }
  134. #endif
  135. }
  136. //deprecated
  137. public object[] Call(object[] args, Type[] returnTypes)
  138. {
  139. #if THREAD_SAFE || HOTFIX_ENABLE
  140. lock (luaEnv.luaEnvLock)
  141. {
  142. #endif
  143. int nArgs = 0;
  144. var L = luaEnv.L;
  145. var translator = luaEnv.translator;
  146. int oldTop = LuaAPI.lua_gettop(L);
  147. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  148. LuaAPI.lua_getref(L, luaReference);
  149. if (args != null)
  150. {
  151. nArgs = args.Length;
  152. for (int i = 0; i < args.Length; i++)
  153. {
  154. translator.PushAny(L, args[i]);
  155. }
  156. }
  157. int error = LuaAPI.lua_pcall(L, nArgs, -1, errFunc);
  158. if (error != 0)
  159. luaEnv.ThrowExceptionFromError(oldTop);
  160. LuaAPI.lua_remove(L, errFunc);
  161. if (returnTypes != null)
  162. return translator.popValues(L, oldTop, returnTypes);
  163. else
  164. return translator.popValues(L, oldTop);
  165. #if THREAD_SAFE || HOTFIX_ENABLE
  166. }
  167. #endif
  168. }
  169. //deprecated
  170. public object[] Call(params object[] args)
  171. {
  172. return Call(args, null);
  173. }
  174. public T Cast<T>()
  175. {
  176. if (!typeof(T).IsSubclassOf(typeof(Delegate)))
  177. {
  178. throw new InvalidOperationException(typeof(T).Name + " is not a delegate type");
  179. }
  180. #if THREAD_SAFE || HOTFIX_ENABLE
  181. lock (luaEnv.luaEnvLock)
  182. {
  183. #endif
  184. var L = luaEnv.L;
  185. var translator = luaEnv.translator;
  186. push(L);
  187. T ret = (T)translator.GetObject(L, -1, typeof(T));
  188. LuaAPI.lua_pop(luaEnv.L, 1);
  189. return ret;
  190. #if THREAD_SAFE || HOTFIX_ENABLE
  191. }
  192. #endif
  193. }
  194. public void SetEnv(LuaTable env)
  195. {
  196. #if THREAD_SAFE || HOTFIX_ENABLE
  197. lock (luaEnv.luaEnvLock)
  198. {
  199. #endif
  200. var L = luaEnv.L;
  201. int oldTop = LuaAPI.lua_gettop(L);
  202. push(L);
  203. env.push(L);
  204. LuaAPI.lua_setfenv(L, -2);
  205. LuaAPI.lua_settop(L, oldTop);
  206. #if THREAD_SAFE || HOTFIX_ENABLE
  207. }
  208. #endif
  209. }
  210. internal override void push(RealStatePtr L)
  211. {
  212. LuaAPI.lua_getref(L, luaReference);
  213. }
  214. public override string ToString()
  215. {
  216. return "function :" + luaReference;
  217. }
  218. }
  219. }