Program.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Lua.Runtime;
  2. using Lua;
  3. using Lua.Standard;
  4. var state = LuaState.Create();
  5. state.OpenStandardLibraries();
  6. {
  7. var closure = state.Compile("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 results = await thread.RunAsync(closure, 0);
  12. for (int i = 0; i < results.Length; i++)
  13. {
  14. Console.WriteLine(results[i]);
  15. }
  16. var f = results[0].Read<LuaClosure>();
  17. results.Dispose();
  18. thread.Push("hello", "world", 1, 2, 3);
  19. var result2 = await thread.RunAsync(f, 5);
  20. for (int i = 0; i < result2.Length; i++)
  21. {
  22. Console.WriteLine(result2[i]);
  23. }
  24. result2.Dispose();
  25. }
  26. }
  27. {
  28. var results = await state.DoStringAsync(
  29. """
  30. return function (...)
  31. local args = {...}
  32. for i = 1, #args do
  33. local v = args[i]
  34. print('To Lua:\t' .. coroutine.yield('from C# ' .. i ..' '..v))
  35. end
  36. end
  37. """, "coroutine");
  38. var f = results[0].Read<LuaClosure>();
  39. using var coroutineLease = state.MainThread.RentCoroutine(f);
  40. var coroutine = coroutineLease.Thread;
  41. {
  42. coroutine.Push("a", "b", "c", "d", "e");
  43. for (int i = 0; coroutine.CanResume; i++)
  44. {
  45. if (i != 0) coroutine.Push($"from C# {i}");
  46. using var resumeResult = await coroutine.ResumeAsync();
  47. Console.Write("To C#:\t");
  48. for (int j = 0; j < resumeResult.Length; j++)
  49. {
  50. Console.Write(resumeResult[j]);
  51. Console.Write('\t');
  52. }
  53. Console.WriteLine();
  54. }
  55. }
  56. }