Program.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Lua.Runtime;
  2. using Lua;
  3. using Lua.Standard;
  4. using System;
  5. var state = LuaState.Create();
  6. state.OpenStandardLibraries();
  7. {
  8. var closure = state.Load("return function (a,b,...) print('a : '..a..' b :'..'args : ',...) end", "@simple");
  9. using var threadLease = state.MainThread.RentUserThread();
  10. var access = threadLease.Thread.RootAccess;
  11. {
  12. var count = await access.RunAsync(closure, 0);
  13. var results = access.ReadTopValues(count);
  14. for (int i = 0; i < results.Length; i++)
  15. {
  16. Console.WriteLine(results[i]);
  17. }
  18. var f = results[0].Read<LuaClosure>();
  19. results.Dispose();
  20. access.Push("hello", "world", 1, 2, 3);
  21. count = await access.RunAsync(f, 5);
  22. results = access.ReadTopValues(count);
  23. for (int i = 0; i < results.Length; i++)
  24. {
  25. Console.WriteLine(results[i]);
  26. }
  27. results.Dispose();
  28. }
  29. }
  30. {
  31. var results = await state.DoStringAsync(
  32. """
  33. return function (...)
  34. local args = {...}
  35. for i = 1, #args do
  36. local v = args[i]
  37. print('In Lua:', coroutine.yield('from lua', i,v))
  38. end
  39. end
  40. """, "coroutine");
  41. var f = results[0].Read<LuaClosure>();
  42. using var coroutineLease = state.MainThread.RentCoroutine(f);
  43. var coroutine = coroutineLease.Thread;
  44. {
  45. var stack = new LuaStack();
  46. stack.PushRange("a", "b", "c", "d", "e");
  47. for (int i = 0; coroutine.CanResume; i++)
  48. {
  49. if (i != 0)
  50. {
  51. stack.Push("from C# ");
  52. stack.Push(i);
  53. }
  54. await coroutine.ResumeAsync(stack);
  55. Console.Write("In C#:\t");
  56. for (int j = 1; j < stack.Count; j++)
  57. {
  58. Console.Write(stack[j]);
  59. Console.Write('\t');
  60. }
  61. Console.WriteLine();
  62. stack.Clear();
  63. }
  64. }
  65. }