GenericDelegateBridge.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. using System;
  9. using LuaAPI = XLua.LuaDLL.Lua;
  10. namespace XLua {
  11. public partial class DelegateBridge : DelegateBridgeBase {
  12. public void Action() {
  13. #if THREAD_SAFE || HOTFIX_ENABLE
  14. lock(luaEnv.luaEnvLock) {
  15. #endif
  16. var L = luaEnv.L;
  17. int oldTop = LuaAPI.lua_gettop(L);
  18. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  19. LuaAPI.lua_getref(L, luaReference);
  20. int error = LuaAPI.lua_pcall(L, 0, 0, errFunc);
  21. if (error != 0)
  22. luaEnv.ThrowExceptionFromError(oldTop);
  23. LuaAPI.lua_settop(L, oldTop);
  24. #if THREAD_SAFE || HOTFIX_ENABLE
  25. }
  26. #endif
  27. }
  28. public void Action<T1>(T1 p1) {
  29. #if THREAD_SAFE || HOTFIX_ENABLE
  30. lock(luaEnv.luaEnvLock) {
  31. #endif
  32. var L = luaEnv.L;
  33. var translator = luaEnv.translator;
  34. int oldTop = LuaAPI.lua_gettop(L);
  35. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  36. LuaAPI.lua_getref(L, luaReference);
  37. translator.PushByType(L, p1);
  38. int error = LuaAPI.lua_pcall(L, 1, 0, errFunc);
  39. if (error != 0)
  40. luaEnv.ThrowExceptionFromError(oldTop);
  41. LuaAPI.lua_settop(L, oldTop);
  42. #if THREAD_SAFE || HOTFIX_ENABLE
  43. }
  44. #endif
  45. }
  46. public void Action<T1, T2>(T1 p1, T2 p2) {
  47. #if THREAD_SAFE || HOTFIX_ENABLE
  48. lock(luaEnv.luaEnvLock) {
  49. #endif
  50. var L = luaEnv.L;
  51. var translator = luaEnv.translator;
  52. int oldTop = LuaAPI.lua_gettop(L);
  53. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  54. LuaAPI.lua_getref(L, luaReference);
  55. translator.PushByType(L, p1);
  56. translator.PushByType(L, p2);
  57. int error = LuaAPI.lua_pcall(L, 2, 0, errFunc);
  58. if (error != 0)
  59. luaEnv.ThrowExceptionFromError(oldTop);
  60. LuaAPI.lua_settop(L, oldTop);
  61. #if THREAD_SAFE || HOTFIX_ENABLE
  62. }
  63. #endif
  64. }
  65. public void Action<T1, T2, T3>(T1 p1, T2 p2, T3 p3) {
  66. #if THREAD_SAFE || HOTFIX_ENABLE
  67. lock(luaEnv.luaEnvLock) {
  68. #endif
  69. var L = luaEnv.L;
  70. var translator = luaEnv.translator;
  71. int oldTop = LuaAPI.lua_gettop(L);
  72. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  73. LuaAPI.lua_getref(L, luaReference);
  74. translator.PushByType(L, p1);
  75. translator.PushByType(L, p2);
  76. translator.PushByType(L, p3);
  77. int error = LuaAPI.lua_pcall(L, 3, 0, errFunc);
  78. if (error != 0)
  79. luaEnv.ThrowExceptionFromError(oldTop);
  80. LuaAPI.lua_settop(L, oldTop);
  81. #if THREAD_SAFE || HOTFIX_ENABLE
  82. }
  83. #endif
  84. }
  85. public void Action<T1, T2, T3, T4>(T1 p1, T2 p2, T3 p3, T4 p4) {
  86. #if THREAD_SAFE || HOTFIX_ENABLE
  87. lock(luaEnv.luaEnvLock) {
  88. #endif
  89. var L = luaEnv.L;
  90. var translator = luaEnv.translator;
  91. int oldTop = LuaAPI.lua_gettop(L);
  92. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  93. LuaAPI.lua_getref(L, luaReference);
  94. translator.PushByType(L, p1);
  95. translator.PushByType(L, p2);
  96. translator.PushByType(L, p3);
  97. translator.PushByType(L, p4);
  98. int error = LuaAPI.lua_pcall(L, 4, 0, errFunc);
  99. if (error != 0)
  100. luaEnv.ThrowExceptionFromError(oldTop);
  101. LuaAPI.lua_settop(L, oldTop);
  102. #if THREAD_SAFE || HOTFIX_ENABLE
  103. }
  104. #endif
  105. }
  106. public TResult Func<TResult>() {
  107. #if THREAD_SAFE || HOTFIX_ENABLE
  108. lock(luaEnv.luaEnvLock) {
  109. #endif
  110. var L = luaEnv.L;
  111. var translator = luaEnv.translator;
  112. int oldTop = LuaAPI.lua_gettop(L);
  113. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  114. LuaAPI.lua_getref(L, luaReference);
  115. int error = LuaAPI.lua_pcall(L, 0, 1, errFunc);
  116. if (error != 0)
  117. luaEnv.ThrowExceptionFromError(oldTop);
  118. TResult ret;
  119. try {
  120. translator.Get(L, -1, out ret);
  121. } catch (Exception e) {
  122. throw e;
  123. } finally {
  124. LuaAPI.lua_settop(L, oldTop);
  125. }
  126. return ret;
  127. #if THREAD_SAFE || HOTFIX_ENABLE
  128. }
  129. #endif
  130. }
  131. public TResult Func<T1, TResult>(T1 p1) {
  132. #if THREAD_SAFE || HOTFIX_ENABLE
  133. lock(luaEnv.luaEnvLock) {
  134. #endif
  135. var L = luaEnv.L;
  136. var translator = luaEnv.translator;
  137. int oldTop = LuaAPI.lua_gettop(L);
  138. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  139. LuaAPI.lua_getref(L, luaReference);
  140. translator.PushByType(L, p1);
  141. int error = LuaAPI.lua_pcall(L, 1, 1, errFunc);
  142. if (error != 0)
  143. luaEnv.ThrowExceptionFromError(oldTop);
  144. TResult ret;
  145. try {
  146. translator.Get(L, -1, out ret);
  147. } catch (Exception e) {
  148. throw e;
  149. } finally {
  150. LuaAPI.lua_settop(L, oldTop);
  151. }
  152. return ret;
  153. #if THREAD_SAFE || HOTFIX_ENABLE
  154. }
  155. #endif
  156. }
  157. public TResult Func<T1, T2, TResult>(T1 p1, T2 p2) {
  158. #if THREAD_SAFE || HOTFIX_ENABLE
  159. lock(luaEnv.luaEnvLock) {
  160. #endif
  161. var L = luaEnv.L;
  162. var translator = luaEnv.translator;
  163. int oldTop = LuaAPI.lua_gettop(L);
  164. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  165. LuaAPI.lua_getref(L, luaReference);
  166. translator.PushByType(L, p1);
  167. translator.PushByType(L, p2);
  168. int error = LuaAPI.lua_pcall(L, 2, 1, errFunc);
  169. if (error != 0)
  170. luaEnv.ThrowExceptionFromError(oldTop);
  171. TResult ret;
  172. try {
  173. translator.Get(L, -1, out ret);
  174. } catch (Exception e) {
  175. throw e;
  176. } finally {
  177. LuaAPI.lua_settop(L, oldTop);
  178. }
  179. return ret;
  180. #if THREAD_SAFE || HOTFIX_ENABLE
  181. }
  182. #endif
  183. }
  184. public TResult Func<T1, T2, T3, TResult>(T1 p1, T2 p2, T3 p3) {
  185. #if THREAD_SAFE || HOTFIX_ENABLE
  186. lock(luaEnv.luaEnvLock) {
  187. #endif
  188. var L = luaEnv.L;
  189. var translator = luaEnv.translator;
  190. int oldTop = LuaAPI.lua_gettop(L);
  191. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  192. LuaAPI.lua_getref(L, luaReference);
  193. translator.PushByType(L, p1);
  194. translator.PushByType(L, p2);
  195. translator.PushByType(L, p3);
  196. int error = LuaAPI.lua_pcall(L, 3, 1, errFunc);
  197. if (error != 0)
  198. luaEnv.ThrowExceptionFromError(oldTop);
  199. TResult ret;
  200. try {
  201. translator.Get(L, -1, out ret);
  202. } catch (Exception e) {
  203. throw e;
  204. } finally {
  205. LuaAPI.lua_settop(L, oldTop);
  206. }
  207. return ret;
  208. #if THREAD_SAFE || HOTFIX_ENABLE
  209. }
  210. #endif
  211. }
  212. public TResult Func<T1, T2, T3, T4, TResult>(T1 p1, T2 p2, T3 p3, T4 p4) {
  213. #if THREAD_SAFE || HOTFIX_ENABLE
  214. lock(luaEnv.luaEnvLock) {
  215. #endif
  216. var L = luaEnv.L;
  217. var translator = luaEnv.translator;
  218. int oldTop = LuaAPI.lua_gettop(L);
  219. int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
  220. LuaAPI.lua_getref(L, luaReference);
  221. translator.PushByType(L, p1);
  222. translator.PushByType(L, p2);
  223. translator.PushByType(L, p3);
  224. translator.PushByType(L, p4);
  225. int error = LuaAPI.lua_pcall(L, 4, 1, errFunc);
  226. if (error != 0)
  227. luaEnv.ThrowExceptionFromError(oldTop);
  228. TResult ret;
  229. try {
  230. translator.Get(L, -1, out ret);
  231. } catch (Exception e) {
  232. throw e;
  233. } finally {
  234. LuaAPI.lua_settop(L, oldTop);
  235. }
  236. return ret;
  237. #if THREAD_SAFE || HOTFIX_ENABLE
  238. }
  239. #endif
  240. }
  241. }
  242. }