Program.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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('To Lua:\t' .. coroutine.yield('from C# ' .. 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. coroutine.Push("a", "b", "c", "d", "e");
  45. for (int i = 0; coroutine.CanResume; i++)
  46. {
  47. if (i != 0) coroutine.Push($"from C# {i}");
  48. var count = await coroutine.ResumeAsync();
  49. using var resumeResult = coroutine.ReadReturnValues(count);
  50. Console.Write("To C#:\t");
  51. for (int j = 0; j < resumeResult.Length; j++)
  52. {
  53. Console.Write(resumeResult[j]);
  54. Console.Write('\t');
  55. }
  56. Console.WriteLine();
  57. }
  58. }
  59. }