From bdaa64092d035fb068b60b88ecd4d1b291cc2398 Mon Sep 17 00:00:00 2001 From: wixoaGit Date: Sun, 23 Feb 2025 21:36:44 -0500 Subject: [PATCH] Fix most of Rider's complaints in DMProc.cs --- OpenDreamRuntime/Procs/DMProc.cs | 72 ++++++++++--------- .../Procs/DebugAdapter/DreamDebugManager.cs | 2 +- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/OpenDreamRuntime/Procs/DMProc.cs b/OpenDreamRuntime/Procs/DMProc.cs index b0b7ca1344..89e64a1b16 100644 --- a/OpenDreamRuntime/Procs/DMProc.cs +++ b/OpenDreamRuntime/Procs/DMProc.cs @@ -127,7 +127,7 @@ private bool CheckIfNullProc() { return false; } - private static List? GetArgumentNames(ProcDefinitionJson json) { + private static List GetArgumentNames(ProcDefinitionJson json) { if (json.Arguments == null) { return new(); } else { @@ -179,12 +179,12 @@ public sealed class DMProcState : ProcState { public static readonly Stack Pool = new(); - private static readonly ArrayPool _dreamValuePool = ArrayPool.Create(); + private static readonly ArrayPool DreamValuePool = ArrayPool.Create(); #region Opcode Handlers - //Human readable friendly version, which will be converted to a more efficient lookup at runtime. - private static readonly Dictionary _opcodeHandlers = new() { + //Human-readable friendly version, which will be converted to a more efficient lookup at runtime. + private static readonly Dictionary OpcodeHandlers = new() { {DreamProcOpcode.BitShiftLeft, DMOpcodeHandlers.BitShiftLeft}, {DreamProcOpcode.PushType, DMOpcodeHandlers.PushType}, {DreamProcOpcode.PushString, DMOpcodeHandlers.PushString}, @@ -331,7 +331,7 @@ public sealed class DMProcState : ProcState { {DreamProcOpcode.ReturnFloat, DMOpcodeHandlers.ReturnFloat} }; - public static readonly unsafe delegate*[] OpcodeHandlers; + public static readonly unsafe delegate*[] OpcodeHandlersTable; #endregion @@ -341,39 +341,40 @@ public sealed class DMProcState : ProcState { /// This stores our 'src' value. May be null! public DreamObject? Instance; + public DreamObject? Usr; public int ArgumentCount; private readonly Stack _catchPosition = new(); private readonly Stack _catchVarIndex = new(); - public const int NoTryCatchVar = -1; - public IDreamValueEnumerator?[] Enumerators = new IDreamValueEnumerator?[16]; + private const int NoTryCatchVar = -1; + public readonly IDreamValueEnumerator?[] Enumerators = new IDreamValueEnumerator?[16]; - private int _pc = 0; public int ProgramCounter => _pc; + private int _pc; private bool _firstResume = true; // Contains both arguments (at index 0) and local vars (at index ArgumentCount) - private DreamValue[] _localVariables; + private DreamValue[] _localVariables = default!; - private DMProc _proc; + private DMProc _proc = default!; public override DMProc Proc => _proc; /// Static initializer for maintainer friendly OpcodeHandlers to performance friendly _opcodeHandlers static unsafe DMProcState() { - int maxOpcode = (int)_opcodeHandlers.Keys.Max(); + int maxOpcode = (int)OpcodeHandlers.Keys.Max(); - OpcodeHandlers = new delegate*[256]; - foreach (var (dpo, handler) in _opcodeHandlers) { - OpcodeHandlers[(int) dpo] = (delegate*) handler.Method.MethodHandle.GetFunctionPointer(); + OpcodeHandlersTable = new delegate*[256]; + foreach (var (dpo, handler) in OpcodeHandlers) { + OpcodeHandlersTable[(int) dpo] = (delegate*) handler.Method.MethodHandle.GetFunctionPointer(); } var invalid = DMOpcodeHandlers.Invalid; var invalidPtr = (delegate*)invalid.Method.MethodHandle.GetFunctionPointer(); - OpcodeHandlers[0] = invalidPtr; + OpcodeHandlersTable[0] = invalidPtr; for (int i = maxOpcode + 1; i < 256; i++) { - OpcodeHandlers[i] = invalidPtr; + OpcodeHandlersTable[i] = invalidPtr; } } @@ -389,8 +390,8 @@ private DMProcState(DMProcState other, DreamThread thread) { _firstResume = false; Result = other.Result; - _stack = _dreamValuePool.Rent(other._stack.Length); - _localVariables = _dreamValuePool.Rent(other._localVariables.Length); + _stack = DreamValuePool.Rent(other._stack.Length); + _localVariables = DreamValuePool.Rent(other._localVariables.Length); Array.Copy(other._localVariables, _localVariables, other._localVariables.Length); } @@ -400,8 +401,8 @@ public void Initialize(DMProc proc, DreamThread thread, int maxStackSize, DreamO Instance = instance; Usr = usr; ArgumentCount = Math.Max(arguments.Count, _proc.ArgumentNames?.Count ?? 0); - _localVariables = _dreamValuePool.Rent(256); - _stack = _dreamValuePool.Rent(maxStackSize); + _localVariables = DreamValuePool.Rent(256); + _stack = DreamValuePool.Rent(maxStackSize); _firstResume = true; for (int i = 0; i < ArgumentCount; i++) { @@ -426,7 +427,7 @@ public override unsafe ProcStatus Resume() { if (procBytecode.Length == 0) return ProcStatus.Returned; - fixed (delegate** handlers = &OpcodeHandlers[0]) { + fixed (delegate** handlers = &OpcodeHandlersTable[0]) { fixed (byte* bytecode = &procBytecode[0]) { var l = procBytecode.Length; // The length never changes so we stick it in a register. @@ -547,14 +548,14 @@ public override void Dispose() { ArgumentCount = 0; Array.Clear(Enumerators); _pc = 0; - _proc = null; + _proc = null!; - _dreamValuePool.Return(_stack); + DreamValuePool.Return(_stack); _stackIndex = 0; - _stack = null; + _stack = null!; - _dreamValuePool.Return(_localVariables, true); - _localVariables = null; + DreamValuePool.Return(_localVariables, true); + _localVariables = null!; _catchPosition.Clear(); _catchVarIndex.Clear(); @@ -574,8 +575,9 @@ public void SetArgument(int id, DreamValue value) { } #region Stack - private DreamValue[] _stack; - private int _stackIndex = 0; + + private DreamValue[] _stack = default!; + private int _stackIndex; public ReadOnlyMemory DebugStack() => _stack.AsMemory(0, _stackIndex); public void Push(DreamValue value) { @@ -622,9 +624,11 @@ public DreamProcArguments PopProcArguments(DreamProc? proc, DMCallArgumentsType return CreateProcArguments(values, proc, argumentsType, argumentStackSize); } + #endregion #region Operands + [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadByte() { var r = _proc.Bytecode[_pc]; @@ -706,6 +710,9 @@ private static void ThrowInvalidReferenceType(DMReference.Type type) { /// as well as what it's being indexed with. /// /// A ListIndex DMReference + /// What index is being accessed + /// What is being indexed + /// Peek the stack instead of popping public void GetIndexReferenceValues(DreamReference reference, out DreamValue index, out DreamValue indexing, bool peek = false) { if (reference.Type != DMReference.Type.ListIndex) ThrowReferenceNotListIndex(); @@ -873,7 +880,7 @@ private static void ThrowPopInvalidType(DMReference.Type type) { } public DreamValue DereferenceField(DreamValue owner, string field) { - if (owner.TryGetValueAsDreamObject(out var ownerObj) && ownerObj != null) { + if (owner.TryGetValueAsDreamObject(out var ownerObj)) { if (!ownerObj.TryGetVariable(field, out var fieldValue)) ThrowTypeHasNoField(field, ownerObj); @@ -904,7 +911,7 @@ private static void ThrowInvalidAppearanceVar(string field) { } [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowTypeHasNoField(string field, DreamObject? ownerObj) { + private static void ThrowTypeHasNoField(string field, DreamObject ownerObj) { throw new Exception($"Type {ownerObj.ObjectDefinition.Type} has no field called \"{field}\""); } @@ -951,6 +958,7 @@ private static void ThrowAttemptedToIndexString(DreamValue index) { ++i; } } + // If the caller supplied excess positional arguments, they have no // name, but the debugger should report them anyways. while (i < ArgumentCount) { @@ -960,10 +968,6 @@ private static void ThrowAttemptedToIndexString(DreamValue index) { } public IEnumerable<(string, DreamValue)> DebugLocals() { - if (_proc.LocalNames is null) { - yield break; - } - string[] names = new string[_localVariables.Length - ArgumentCount]; int count = 0; foreach (var info in _proc.LocalNames) { diff --git a/OpenDreamRuntime/Procs/DebugAdapter/DreamDebugManager.cs b/OpenDreamRuntime/Procs/DebugAdapter/DreamDebugManager.cs index 56339a2316..8f747f90bf 100644 --- a/OpenDreamRuntime/Procs/DebugAdapter/DreamDebugManager.cs +++ b/OpenDreamRuntime/Procs/DebugAdapter/DreamDebugManager.cs @@ -238,7 +238,7 @@ public ProcStatus HandleBreakpoint(DMProcState state) { // Execute the original opcode unsafe { - return DMProcState.OpcodeHandlers[breakpoint.OriginalOpcode](state); + return DMProcState.OpcodeHandlersTable[breakpoint.OriginalOpcode](state); } }