Skip to content

Commit 6015b16

Browse files
committed
Inform users who try to use yeilding Lua methods from callbacks that this isn't allowed.
1 parent 73c2c4b commit 6015b16

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/BizHawk.Client.Common/lua/LuaLibraries.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public LuaLibraries(
148148

149149
private List<IDisposable> _disposables = new();
150150

151+
private int _resumeState = 0;
152+
private bool InCallback => _resumeState == 0;
153+
151154
public LuaDocumentation Docs { get; } = new LuaDocumentation();
152155

153156
private EmulationLuaLibrary EmulationLuaLibrary => (EmulationLuaLibrary)Libraries[typeof(EmulationLuaLibrary)];
@@ -305,6 +308,8 @@ private bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate
305308

306309
public void Sandbox(LuaFile luaFile, Action callback, Action<string> exceptionCallback = null)
307310
{
311+
_resumeState = Math.Max(0, _resumeState - 1);
312+
308313
bool setThread = SetCurrentThread(luaFile);
309314
_runningFiles.Push(luaFile);
310315
LuaSandbox.GetSandbox(luaFile.Thread).Sandbox(callback, exceptionCallback ?? _defaultExceptionCallback);
@@ -428,6 +433,7 @@ private bool SetCurrentThread(LuaFile luaFile)
428433
{
429434
var result = (WaitForFrame: false, Terminated: true);
430435
LuaStatus? execResult = null;
436+
_resumeState = 2;
431437
Sandbox(lf, () => execResult = lf.Thread.Resume(), exceptionCallback);
432438
if (execResult == null) return result;
433439

@@ -448,12 +454,22 @@ public static void Print(params object[] outputs)
448454

449455
private void FrameAdvance()
450456
{
457+
if (InCallback)
458+
{
459+
// Throw so that callback execution stops, avoiding potentail infinite loops. Unfortunately the message in the console will be ugly.
460+
throw new Exception("emu.frameadvance is not available in events or callbacks");
461+
}
451462
FrameAdvanceRequested = true;
452463
CurrentFile.Thread.Yield();
453464
}
454465

455466
private void EmuYield()
456467
{
468+
if (InCallback)
469+
{
470+
// Throw so that callback execution stops, avoiding potentail infinite loops. Unfortunately the message in the console will be ugly.
471+
throw new Exception("emu.yield is not available in events or callbacks");
472+
}
457473
CurrentFile.Thread.Yield();
458474
}
459475
}

0 commit comments

Comments
 (0)