Program.cs 1.9 KB

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