TestLuaStack.cs

  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. //检测合理报错
  7. public class TestLuaStack : MonoBehaviour
  8. {
  9. public GameObject go = null;
  10. public GameObject go2 = null;
  11. public static LuaFunction show = null;
  12. public static LuaFunction testRay = null;
  13. public static LuaFunction showStack = null;
  14. public static LuaFunction test4 = null;
  15. private static GameObject testGo = null;
  16. private string tips = "";
  17. public static TestLuaStack Instance = null;
  18. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  19. static int Test1(IntPtr L)
  20. {
  21. try
  22. {
  23. show.BeginPCall();
  24. show.PCall();
  25. show.EndPCall();
  26. }
  27. catch (Exception e)
  28. {
  29. return LuaDLL.toluaL_exception(L, e);
  30. }
  31. return 0;
  32. }
  33. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  34. static int PushLuaError(IntPtr L)
  35. {
  36. try
  37. {
  38. testRay.BeginPCall();
  39. testRay.Push(Instance);
  40. testRay.PCall();
  41. testRay.EndPCall();
  42. }
  43. catch (Exception e)
  44. {
  45. return LuaDLL.toluaL_exception(L, e);
  46. }
  47. return 0;
  48. }
  49. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  50. static int Test3(IntPtr L)
  51. {
  52. try
  53. {
  54. testRay.BeginPCall();
  55. testRay.PCall();
  56. testRay.CheckRay();
  57. testRay.EndPCall();
  58. }
  59. catch (Exception e)
  60. {
  61. return LuaDLL.toluaL_exception(L, e);
  62. }
  63. return 0;
  64. }
  65. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  66. static int Test4(IntPtr L)
  67. {
  68. try
  69. {
  70. show.BeginPCall();
  71. show.PCall();
  72. show.EndPCall();
  73. }
  74. catch (Exception e)
  75. {
  76. return LuaDLL.toluaL_exception(L, e);
  77. }
  78. return 0;
  79. }
  80. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  81. static int Test5(IntPtr L)
  82. {
  83. try
  84. {
  85. test4.BeginPCall();
  86. test4.PCall();
  87. bool ret = test4.CheckBoolean();
  88. ret = !ret;
  89. test4.EndPCall();
  90. }
  91. catch (Exception e)
  92. {
  93. return LuaDLL.toluaL_exception(L, e);
  94. }
  95. return 0;
  96. }
  97. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  98. static int Test6(IntPtr L)
  99. {
  100. try
  101. {
  102. throw new LuaException("this a lua exception");
  103. }
  104. catch (Exception e)
  105. {
  106. return LuaDLL.toluaL_exception(L, e);
  107. }
  108. }
  109. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  110. static int TestOutOfBound(IntPtr L)
  111. {
  112. try
  113. {
  114. Transform transform = testGo.transform;
  115. Transform node = transform.GetChild(20);
  116. ToLua.Push(L, node);
  117. return 0;
  118. }
  119. catch (Exception e)
  120. {
  121. return LuaDLL.toluaL_exception(L, e);
  122. }
  123. }
  124. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  125. static int TestArgError(IntPtr L)
  126. {
  127. try
  128. {
  129. LuaDLL.luaL_typerror(L, 1, "number");
  130. }
  131. catch (Exception e)
  132. {
  133. return LuaDLL.toluaL_exception(L, e);
  134. }
  135. return 0;
  136. }
  137. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  138. static int TestTableInCo(IntPtr L)
  139. {
  140. try
  141. {
  142. LuaTable table = ToLua.CheckLuaTable(L, 1);
  143. string str = (string)table["name"];
  144. ToLua.Push(L, str);
  145. return 1;
  146. }
  147. catch (Exception e)
  148. {
  149. return LuaDLL.toluaL_exception(L, e);
  150. }
  151. }
  152. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  153. static int TestCycle(IntPtr L)
  154. {
  155. try
  156. {
  157. LuaState state = LuaState.Get(L);
  158. LuaFunction func = state.GetFunction("TestCycle");
  159. int c = (int)LuaDLL.luaL_checknumber(L, 1);
  160. if (c <= 2)
  161. {
  162. LuaDLL.lua_pushnumber(L, 1);
  163. }
  164. else
  165. {
  166. func.BeginPCall();
  167. func.Push(c - 1);
  168. func.PCall();
  169. int n1 = (int)func.CheckNumber();
  170. func.EndPCall();
  171. func.BeginPCall();
  172. func.Push(c - 2);
  173. func.PCall();
  174. int n2 = (int)func.CheckNumber();
  175. func.EndPCall();
  176. LuaDLL.lua_pushnumber(L, n1 + n2);
  177. }
  178. return 1;
  179. }
  180. catch (Exception e)
  181. {
  182. return LuaDLL.toluaL_exception(L, e);
  183. }
  184. }
  185. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  186. static int TestNull(IntPtr L)
  187. {
  188. try
  189. {
  190. GameObject go = (GameObject)ToLua.CheckUnityObject(L, 1, typeof(GameObject));
  191. ToLua.Push(L, go.name);
  192. return 1;
  193. }
  194. catch (Exception e)
  195. {
  196. return LuaDLL.toluaL_exception(L, e);
  197. }
  198. }
  199. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  200. static int TestAddComponent(IntPtr L)
  201. {
  202. try
  203. {
  204. GameObject go = (GameObject)ToLua.CheckUnityObject(L, 1, typeof(GameObject));
  205. go.AddComponent<TestInstantiate2>();
  206. return 0;
  207. }
  208. catch (Exception e)
  209. {
  210. return LuaDLL.toluaL_exception(L, e);
  211. }
  212. }
  213. static void TestMul1()
  214. {
  215. throw new Exception("multi stack error");
  216. }
  217. static void TestMul0()
  218. {
  219. TestMul1();
  220. }
  221. [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
  222. static int TestMulStack(IntPtr L)
  223. {
  224. try
  225. {
  226. TestMul0();
  227. return 0;
  228. }
  229. catch (Exception e)
  230. {
  231. //Debugger.Log("xxxx" + e.StackTrace);
  232. return LuaDLL.toluaL_exception(L, e);
  233. }
  234. }
  235. void OnSendMsg()
  236. {
  237. try
  238. {
  239. LuaFunction func = state.GetFunction("TestStack.Test6");
  240. func.BeginPCall();
  241. func.PCall();
  242. func.EndPCall();
  243. }
  244. catch(Exception e)
  245. {
  246. state.ThrowLuaException(e);
  247. }
  248. }
  249. LuaState state = null;
  250. public TextAsset text = null;
  251. static Action TestDelegate = delegate { };
  252. void Awake()
  253. {
  254. #if UNITY_5
  255. Application.logMessageReceived += ShowTips;
  256. #else
  257. Application.RegisterLogCallback(ShowTips);
  258. #endif
  259. Instance = this;
  260. new LuaResLoader();
  261. testGo = gameObject;
  262. state = new LuaState();
  263. state.Start();
  264. LuaBinder.Bind(state);
  265. state.BeginModule(null);
  266. state.RegFunction("TestArgError", TestArgError);
  267. state.RegFunction("TestTableInCo", TestTableInCo);
  268. state.RegFunction("TestCycle", TestCycle);
  269. state.RegFunction("TestNull", TestNull);
  270. state.RegFunction("TestAddComponent", TestAddComponent);
  271. state.RegFunction("TestOutOfBound", TestOutOfBound);
  272. state.RegFunction("TestMulStack", TestMulStack);
  273. state.BeginStaticLibs("TestStack");
  274. state.RegFunction("Test1", Test1);
  275. state.RegFunction("PushLuaError", PushLuaError);
  276. state.RegFunction("Test3", Test3);
  277. state.RegFunction("Test4", Test4);
  278. state.RegFunction("Test5", Test5);
  279. state.RegFunction("Test6", Test6);
  280. state.EndStaticLibs();
  281. state.EndModule();
  282. //state.DoFile("TestErrorStack.lua");
  283. state.Require("TestErrorStack");
  284. show = state.GetFunction("Show");
  285. testRay = state.GetFunction("TestRay");
  286. showStack = state.GetFunction("ShowStack");
  287. test4 = state.GetFunction("Test4");
  288. TestDelegate += TestD1;
  289. TestDelegate += TestD2;
  290. }
  291. void Update()
  292. {
  293. state.CheckTop();
  294. }
  295. void OnApplicationQuit()
  296. {
  297. #if UNITY_5
  298. Application.logMessageReceived -= ShowTips;
  299. #else
  300. Application.RegisterLogCallback(null);
  301. #endif
  302. state.Dispose();
  303. state = null;
  304. }
  305. void ShowTips(string msg, string stackTrace, LogType type)
  306. {
  307. tips += msg;
  308. tips += "\r\n";
  309. if (type == LogType.Error || type == LogType.Exception)
  310. {
  311. tips += stackTrace;
  312. }
  313. }
  314. void TestD1()
  315. {
  316. Debugger.Log("delegate 1");
  317. TestDelegate -= TestD2;
  318. }
  319. void TestD2()
  320. {
  321. Debugger.Log("delegate 2");
  322. }
  323. void OnGUI()
  324. {
  325. GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 150, 800, 400), tips);
  326. if (GUI.Button(new Rect(10, 10, 120, 40), "Error1"))
  327. {
  328. tips = "";
  329. showStack.BeginPCall();
  330. showStack.Push(go);
  331. showStack.PCall();
  332. showStack.EndPCall();
  333. showStack.Dispose();
  334. showStack = null;
  335. }
  336. else if (GUI.Button(new Rect(10, 60, 120, 40), "Instantiate Error"))
  337. {
  338. tips = "";
  339. LuaFunction func = state.GetFunction("Instantiate");
  340. func.BeginPCall();
  341. func.Push(go);
  342. func.PCall();
  343. func.EndPCall();
  344. func.Dispose();
  345. }
  346. else if (GUI.Button(new Rect(10, 110, 120, 40), "Check Error"))
  347. {
  348. tips = "";
  349. LuaFunction func = state.GetFunction("TestRay");
  350. func.BeginPCall();
  351. func.PCall();
  352. func.CheckRay(); //返回值出错
  353. func.EndPCall();
  354. func.Dispose();
  355. }
  356. else if (GUI.Button(new Rect(10, 160, 120, 40), "Push Error"))
  357. {
  358. tips = "";
  359. LuaFunction func = state.GetFunction("TestRay");
  360. func.BeginPCall();
  361. func.Push(Instance);
  362. func.PCall();
  363. func.EndPCall();
  364. func.Dispose();
  365. }
  366. else if (GUI.Button(new Rect(10, 210, 120, 40), "LuaPushError"))
  367. {
  368. //需要改lua文件让其出错
  369. tips = "";
  370. LuaFunction func = state.GetFunction("PushLuaError");
  371. func.BeginPCall();
  372. func.PCall();
  373. func.EndPCall();
  374. func.Dispose();
  375. }
  376. else if (GUI.Button(new Rect(10, 260, 120, 40), "Check Error"))
  377. {
  378. tips = "";
  379. LuaFunction func = state.GetFunction("Test5");
  380. func.BeginPCall();
  381. func.PCall();
  382. func.EndPCall();
  383. func.Dispose();
  384. }
  385. else if (GUI.Button(new Rect(10, 310, 120, 40), "Test Resume"))
  386. {
  387. tips = "";
  388. LuaFunction func = state.GetFunction("Test6");
  389. func.BeginPCall();
  390. func.PCall();
  391. func.EndPCall();
  392. func.Dispose();
  393. }
  394. else if (GUI.Button(new Rect(10, 360, 120, 40), "out of bound"))
  395. {
  396. tips = "";
  397. LuaFunction func = state.GetFunction("TestOutOfBound");
  398. func.BeginPCall();
  399. func.PCall();
  400. func.EndPCall();
  401. func.Dispose();
  402. }
  403. else if (GUI.Button(new Rect(10, 410, 120, 40), "TestArgError"))
  404. {
  405. tips = "";
  406. LuaFunction func = state.GetFunction("Test8");
  407. func.BeginPCall();
  408. func.PCall();
  409. func.EndPCall();
  410. func.Dispose();
  411. }
  412. else if (GUI.Button(new Rect(10, 460, 120, 40), "TestFuncDispose"))
  413. {
  414. tips = "";
  415. LuaFunction func = state.GetFunction("Test8");
  416. func.Dispose();
  417. func.BeginPCall();
  418. func.PCall();
  419. func.EndPCall();
  420. func.Dispose();
  421. }
  422. else if (GUI.Button(new Rect(10, 510, 120, 40), "SendMessage"))
  423. {
  424. tips = "";
  425. gameObject.SendMessage("OnSendMsg");
  426. }
  427. else if (GUI.Button(new Rect(10, 560, 120, 40), "SendMessageInLua"))
  428. {
  429. LuaFunction func = state.GetFunction("SendMsgError");
  430. func.BeginPCall();
  431. func.Push(gameObject);
  432. func.PCall();
  433. func.EndPCall();
  434. func.Dispose();
  435. }
  436. else if (GUI.Button(new Rect(10, 610, 120, 40), "AddComponent"))
  437. {
  438. tips = "";
  439. LuaFunction func = state.GetFunction("TestAddComponent");
  440. func.BeginPCall();
  441. func.Push(gameObject);
  442. func.PCall();
  443. func.EndPCall();
  444. func.Dispose();
  445. }
  446. else if (GUI.Button(new Rect(210, 10, 120, 40), "TableGetSet"))
  447. {
  448. tips = "";
  449. LuaTable table = state.GetTable("testev");
  450. int top = state.LuaGetTop();
  451. try
  452. {
  453. state.Push(table);
  454. state.LuaGetField(-1, "Add");
  455. LuaFunction func = state.CheckLuaFunction(-1);
  456. if (func != null)
  457. {
  458. func.Call();
  459. func.Dispose();
  460. }
  461. state.LuaPop(1);
  462. state.Push(123456);
  463. state.LuaSetField(-2, "value");
  464. state.LuaGetField(-1, "value");
  465. int n = (int)state.LuaCheckNumber(-1);
  466. Debugger.Log("value is: " + n);
  467. state.LuaPop(1);
  468. state.Push("Add");
  469. state.LuaGetTable(-2);
  470. func = state.CheckLuaFunction(-1);
  471. if (func != null)
  472. {
  473. func.Call();
  474. func.Dispose();
  475. }
  476. state.LuaPop(1);
  477. state.Push("look");
  478. state.Push(456789);
  479. state.LuaSetTable(-3);
  480. state.LuaGetField(-1, "look");
  481. n = (int)state.LuaCheckNumber(-1);
  482. Debugger.Log("look: " + n);
  483. }
  484. catch (Exception e)
  485. {
  486. state.LuaSetTop(top);
  487. throw e;
  488. }
  489. state.LuaSetTop(top);
  490. }
  491. else if (GUI.Button(new Rect(210, 60, 120, 40), "TestTableInCo"))
  492. {
  493. tips = "";
  494. LuaFunction func = state.GetFunction("TestCoTable");
  495. func.BeginPCall();
  496. func.PCall();
  497. func.EndPCall();
  498. func.Dispose();
  499. }
  500. else if (GUI.Button(new Rect(210, 110, 120, 40), "Instantiate2 Error"))
  501. {
  502. tips = "";
  503. LuaFunction func = state.GetFunction("Instantiate");
  504. func.BeginPCall();
  505. func.Push(go2);
  506. func.PCall();
  507. func.EndPCall();
  508. func.Dispose();
  509. }
  510. else if (GUI.Button(new Rect(210, 160, 120, 40), "Instantiate3 Error"))
  511. {
  512. tips = "";
  513. UnityEngine.Object.Instantiate(go2);
  514. }
  515. else if (GUI.Button(new Rect(210, 210, 120, 40), "TestCycle"))
  516. {
  517. tips = "";
  518. int n = 20;
  519. LuaFunction func = state.GetFunction("TestCycle");
  520. func.BeginPCall();
  521. func.Push(n);
  522. func.PCall();
  523. int c = (int)func.CheckNumber();
  524. func.EndPCall();
  525. Debugger.Log("Fib({0}) is {1}", n, c);
  526. }
  527. else if (GUI.Button(new Rect(210, 260, 120, 40), "TestNull"))
  528. {
  529. tips = "";
  530. Action action = ()=>
  531. {
  532. LuaFunction func = state.GetFunction("TestNull");
  533. func.BeginPCall();
  534. func.PushObject(null);
  535. func.PCall();
  536. func.EndPCall();
  537. };
  538. StartCoroutine(TestCo(action));
  539. }
  540. else if (GUI.Button(new Rect(210, 310, 120, 40), "TestMulti"))
  541. {
  542. tips = "";
  543. LuaFunction func = state.GetFunction("TestMulStack");
  544. func.BeginPCall();
  545. func.PushObject(null);
  546. func.PCall();
  547. func.EndPCall();
  548. }
  549. }
  550. IEnumerator TestCo(Action action)
  551. {
  552. yield return new WaitForSeconds(0.1f);
  553. action();
  554. }
  555. }

TestInstantiate.cs

  1. using UnityEngine;
  2. using System;
  3. using LuaInterface;
  4. public class TestInstantiate : MonoBehaviour
  5. {
  6. void Awake()
  7. {
  8. LuaState state = LuaState.Get(IntPtr.Zero);
  9. try
  10. {
  11. LuaFunction func = state.GetFunction("Show");
  12. func.BeginPCall();
  13. func.PCall();
  14. func.EndPCall();
  15. func.Dispose();
  16. func = null;
  17. }
  18. catch (Exception e)
  19. {
  20. state.ThrowLuaException(e);
  21. }
  22. }
  23. void Start()
  24. {
  25. Debugger.Log("start");
  26. }
  27. }

TestInstantiate2.cs

  1. using UnityEngine;
  2. using System;
  3. using LuaInterface;
  4. public class TestInstantiate2 : MonoBehaviour
  5. {
  6. void Awake()
  7. {
  8. try
  9. {
  10. throw new Exception("Instantiate exception 2");
  11. }
  12. catch (Exception e)
  13. {
  14. LuaState state = LuaState.Get(IntPtr.Zero);
  15. state.ThrowLuaException(e);
  16. }
  17. }
  18. }

?