Skip to content

Commit

Permalink
Move duplicate opcode check to a unit test (OpenDreamProject#1563)
Browse files Browse the repository at this point in the history
* move opcode unique validity check to unit tests

* automatic code style changes :/
  • Loading branch information
distributivgesetz authored Dec 23, 2023
1 parent a3dcd59 commit c7e8d71
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
16 changes: 16 additions & 0 deletions Content.Tests/RuntimeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using DMCompiler.Bytecode;
using NUnit.Framework;

namespace Content.Tests;

[TestFixture]
public sealed class RuntimeTests {
/// <summary>
/// Validates that the opcodes in DreamProcOpcode are all unique, such that none resolve to the same byte.
/// </summary>
[Test]
public void EnsureOpcodesUnique() {
Assert.That(Enum.GetValues<DreamProcOpcode>(), Is.Unique);
}
}
21 changes: 0 additions & 21 deletions DMCompiler/Bytecode/DreamProcOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,27 +363,6 @@ public override string ToString() {

// Dummy class-as-namespace because C# just kinda be like this
public static class OpcodeVerifier {
/// <summary>
/// Validates that the opcodes in DreamProcOpcode are all unique, such that none resolve to the same byte.
/// </summary>
/// <returns>True if there are duplicate opcodes, false if not</returns>
// FIXME: Can this be made into something done during compiletime? Like, *this code's* compiletime? >:/
public static bool AreOpcodesInvalid() {
// I'm not *too* satisfied with this boolean schtick, as opposed to throwing,
// but I want each executable to be able to do what they want with this information.

HashSet<DreamProcOpcode> bytePool = new();
foreach (DreamProcOpcode usedInt in Enum.GetValues(typeof(DreamProcOpcode))) {
if(bytePool.Contains(usedInt)) {
return true;
}

bytePool.Add(usedInt);
}

return false;
}

/// <summary>
/// Calculates a hash of all the <c>DreamProcOpcode</c>s for warning on incompatibilities.
/// </summary>
Expand Down
10 changes: 3 additions & 7 deletions DMCompiler/DMCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using DMCompiler.Bytecode;
using DMCompiler.Compiler.DM;
using DMCompiler.Compiler.DMM;
using DMCompiler.Compiler.DMPreprocessor;
using DMCompiler.DM;
using DMCompiler.DM.Visitors;
using OpenDreamShared.Compiler;
using OpenDreamShared.Json;
using Robust.Shared.Utility;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using DMCompiler.Bytecode;
using Robust.Shared.Utility;

namespace DMCompiler;

Expand Down Expand Up @@ -51,10 +51,6 @@ public static bool Compile(DMCompilerSettings settings) {
ForcedWarning("Unimplemented proc & var warnings are currently suppressed");
}

if(OpcodeVerifier.AreOpcodesInvalid()) {
ForcedError("Some opcodes have the same byte value! Output assembly may be corrupted.");
}

DMPreprocessor preprocessor = Preprocess(settings.Files, settings.MacroDefines);
bool successfulCompile = preprocessor is not null && Compile(preprocessor);

Expand Down

0 comments on commit c7e8d71

Please sign in to comment.