Skip to content

Commit

Permalink
List append self (#2153)
Browse files Browse the repository at this point in the history
Co-authored-by: ritorizo <ritorizo@localhost>
Co-authored-by: wixoa <[email protected]>
  • Loading branch information
3 people authored Jan 6, 2025
1 parent 70b036f commit e73af64
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Content.Tests/DMProject/Tests/List/ListAppendSelf.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/proc/RunTest()
var/L1 = list(1, 2)
L1 += L1
ASSERT(L1 ~= list(1, 2, 1, 2))

var/L2 = list(a=1, b=2)
L2 += L2
ASSERT(L2 ~= list(a=1, b=2, "a", "b"))
23 changes: 13 additions & 10 deletions OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,14 @@ public override DreamValue OperatorOr(DreamValue b, DMProcState state) {

public override DreamValue OperatorAppend(DreamValue b) {
if (b.TryGetValueAsDreamList(out var bList)) {
foreach (DreamValue value in bList.GetValues()) {
if (bList._associativeValues?.TryGetValue(value, out var assocValue) is true) {
SetValue(value, assocValue);
} else {
AddValue(value);
var values = bList.GetValues();
var valueCount = values.Count; // Some lists return a reference to their internal values list which could change with each loop
for (int i = 0; i < valueCount; i++) {
var value = values[i];
AddValue(value); // Always add the value
if (bList._associativeValues?.TryGetValue(value, out var assocValue) is true) { // Ensure the associated value is correct
_associativeValues ??= new();
_associativeValues[value] = assocValue;
}
}
} else {
Expand Down Expand Up @@ -574,9 +577,9 @@ public override int GetLength() {
// atom's verbs list
// Keeps track of an appearance's verbs (atom.verbs, mutable_appearance.verbs, etc)
public sealed class VerbsList(DreamObjectTree objectTree, AtomManager atomManager, ServerVerbSystem? verbSystem, DreamObjectAtom atom) : DreamList(objectTree.List.ObjectDefinition, 0) {
public override DreamValue GetValue(DreamValue key) {
public override DreamValue GetValue(DreamValue key) {
if (verbSystem == null)
return DreamValue.Null;
return DreamValue.Null;
if (!key.TryGetValueAsInteger(out var index))
throw new Exception($"Invalid index into verbs list: {key}");

Expand Down Expand Up @@ -814,12 +817,12 @@ public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth
public override void AddValue(DreamValue value) {
EntityUid entity;
if (value.TryGetValueAsDreamObject<DreamObjectMovable>(out var movable)) {
if(_visContents.Contains(movable))
if (_visContents.Contains(movable))
return; // vis_contents cannot contain duplicates
_visContents.Add(movable);
entity = movable.Entity;
} else if (value.TryGetValueAsDreamObject<DreamObjectTurf>(out var turf)) {
if(_visContents.Contains(turf))
if (_visContents.Contains(turf))
return; // vis_contents cannot contain duplicates
_visContents.Add(turf);
entity = EntityUid.Invalid; // TODO: Support turfs in vis_contents
Expand Down Expand Up @@ -934,7 +937,7 @@ public override List<DreamValue> GetValues() {
}

public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth = false) {
if (!value.TryGetValueAsDreamObject<DreamObjectFilter>(out var filterObject) &&!value.IsNull)
if (!value.TryGetValueAsDreamObject<DreamObjectFilter>(out var filterObject) && !value.IsNull)
throw new Exception($"Cannot set value of filter list to {value}");
if (!key.TryGetValueAsInteger(out var filterIndex) || filterIndex < 1)
throw new Exception($"Invalid index into filter list: {key}");
Expand Down

0 comments on commit e73af64

Please sign in to comment.