|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using Laylua.Marshaling; |
| 4 | +using Laylua.Moon; |
| 5 | + |
| 6 | +namespace Laylua; |
| 7 | + |
| 8 | +internal static unsafe class LuaCoroutineRuntime |
| 9 | +{ |
| 10 | + public static int PushFunctionAndArguments(LuaThread coroutineThread, LuaFunction function, object?[] arguments) |
| 11 | + { |
| 12 | + PrepareFunctionCall(coroutineThread, function, arguments.Length); |
| 13 | + |
| 14 | + foreach (var argument in arguments) |
| 15 | + { |
| 16 | + coroutineThread.Stack.Push(argument); |
| 17 | + } |
| 18 | + |
| 19 | + return arguments.Length; |
| 20 | + } |
| 21 | + |
| 22 | + public static int PushFunctionAndStackArguments(LuaThread coroutineThread, LuaFunction function, ReadOnlySpan<LuaStackValue> arguments) |
| 23 | + { |
| 24 | + PrepareFunctionCall(coroutineThread, function, arguments.Length); |
| 25 | + |
| 26 | + foreach (var argument in arguments) |
| 27 | + { |
| 28 | + PushStackArgument(coroutineThread, argument); |
| 29 | + } |
| 30 | + |
| 31 | + return arguments.Length; |
| 32 | + } |
| 33 | + |
| 34 | + public static int PushFunctionAndStackArguments(LuaThread coroutineThread, LuaFunction function, LuaStackValueRange arguments) |
| 35 | + { |
| 36 | + PrepareFunctionCall(coroutineThread, function, arguments.Count); |
| 37 | + |
| 38 | + foreach (var argument in arguments) |
| 39 | + { |
| 40 | + PushStackArgument(coroutineThread, argument); |
| 41 | + } |
| 42 | + |
| 43 | + return arguments.Count; |
| 44 | + } |
| 45 | + |
| 46 | + private static void PrepareFunctionCall(LuaThread coroutineThread, LuaFunction function, int argumentCount) |
| 47 | + { |
| 48 | + coroutineThread.Stack.EnsureFreeCapacity(1 + argumentCount); |
| 49 | + coroutineThread.Stack.Push(function); |
| 50 | + } |
| 51 | + |
| 52 | + private static void PushStackArgument(LuaThread coroutineThread, LuaStackValue argument) |
| 53 | + { |
| 54 | + var sourceThread = argument.Thread; |
| 55 | + if (argument.Type == LuaType.None) |
| 56 | + { |
| 57 | + throw new InvalidOperationException("The given stack value is not valid."); |
| 58 | + } |
| 59 | + |
| 60 | + if (sourceThread.MainThread.State.L != coroutineThread.MainThread.State.L) |
| 61 | + { |
| 62 | + throw new InvalidOperationException("The given stack value is owned by a different Lua state."); |
| 63 | + } |
| 64 | + |
| 65 | + sourceThread.Stack.EnsureFreeCapacity(1); |
| 66 | + coroutineThread.Stack.EnsureFreeCapacity(1); |
| 67 | + |
| 68 | + lua_pushvalue(sourceThread.State.L, argument.Index); |
| 69 | + lua_xmove(sourceThread.State.L, coroutineThread.State.L, 1); |
| 70 | + } |
| 71 | + |
| 72 | + public static ResumeStepResult ResumeStep(LuaThread coroutineThread, LuaThread fromThread, int argumentCount) |
| 73 | + { |
| 74 | + var coroutineL = coroutineThread.State.L; |
| 75 | + var fromL = fromThread.State.L; |
| 76 | + var laylua = LayluaState.FromExtraSpace(coroutineL); |
| 77 | + var initialPanic = laylua.Panic; |
| 78 | + |
| 79 | + var status = lua_resume(coroutineL, fromL, argumentCount, out var resultCount); |
| 80 | + |
| 81 | + if (status == LuaStatus.Yield) |
| 82 | + { |
| 83 | + // Inside the yielding C function, lua_yield long-jumps directly back into |
| 84 | + // lua_resume, bypassing the assembly trampoline that wraps every native Lua |
| 85 | + // export and would normally pop the panic frame it pushed around lua_yieldk. |
| 86 | + // Drain that orphaned frame here so the panic stack stays balanced. |
| 87 | + while (!ReferenceEquals(laylua.Panic, initialPanic)) |
| 88 | + { |
| 89 | + var leakedPanic = laylua.PopPanic(); |
| 90 | + if (leakedPanic == null) |
| 91 | + { |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + laylua.ResetPanic(leakedPanic); |
| 96 | + } |
| 97 | + |
| 98 | + Debug.Assert(ReferenceEquals(laylua.Panic, initialPanic)); |
| 99 | + |
| 100 | + if (resultCount != 1 || !AwaitableUserData.IsAwaitable(coroutineL, -1)) |
| 101 | + { |
| 102 | + lua_settop(coroutineL, 0); |
| 103 | + throw new LuaException("The Lua coroutine yielded a value that is not an awaitable."); |
| 104 | + } |
| 105 | + |
| 106 | + if (!AwaitableUserData.TryGetBox(coroutineL, -1, out var box) || box is null) |
| 107 | + { |
| 108 | + lua_settop(coroutineL, 0); |
| 109 | + throw new LuaException("Failed to extract the awaitable from the yielded user data."); |
| 110 | + } |
| 111 | + |
| 112 | + lua_pop(coroutineL); |
| 113 | + return new ResumeStepResult(LuaStatus.Yield, box, resultCount: 0); |
| 114 | + } |
| 115 | + |
| 116 | + if (status == LuaStatus.Ok) |
| 117 | + { |
| 118 | + Debug.Assert(ReferenceEquals(laylua.Panic, initialPanic)); |
| 119 | + return new ResumeStepResult(LuaStatus.Ok, awaitableBox: null, resultCount); |
| 120 | + } |
| 121 | + |
| 122 | + Debug.Assert(ReferenceEquals(laylua.Panic, initialPanic)); |
| 123 | + LuaThread.ThrowLuaException(coroutineThread, status); |
| 124 | + return default; |
| 125 | + } |
| 126 | + |
| 127 | + public static void ClearStack(LuaThread coroutineThread) |
| 128 | + { |
| 129 | + lua_settop(coroutineThread.State.L, 0); |
| 130 | + } |
| 131 | + |
| 132 | + public static T? ReadFirstResult<T>(LuaThread coroutineThread, int resultCount) |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + if (resultCount == 0) |
| 137 | + { |
| 138 | + throw new InvalidOperationException("The code evaluation succeeded, but returned no results."); |
| 139 | + } |
| 140 | + |
| 141 | + return coroutineThread.MainThread.Marshaler.GetValue<T>(coroutineThread, 1); |
| 142 | + } |
| 143 | + finally |
| 144 | + { |
| 145 | + ClearStack(coroutineThread); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public static LuaFunctionResults CollectResults(LuaThread coroutineThread, LuaThread destinationThread, int resultCount) |
| 150 | + { |
| 151 | + if (resultCount < 0) |
| 152 | + { |
| 153 | + throw new ArgumentOutOfRangeException(nameof(resultCount), "The result count cannot be negative."); |
| 154 | + } |
| 155 | + |
| 156 | + var oldTop = destinationThread.Stack.Count; |
| 157 | + coroutineThread.Stack.XMove(destinationThread, resultCount); |
| 158 | + |
| 159 | + var range = LuaStackValueRange.FromTop(destinationThread.Stack, oldTop, destinationThread.Stack.Count); |
| 160 | + return new LuaFunctionResults(range); |
| 161 | + } |
| 162 | +} |
0 commit comments