Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move duplicate opcode check to a unit test #1563

Merged
merged 3 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading