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

Add: print out the stack #3033

Merged
merged 6 commits into from
Dec 25, 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
28 changes: 24 additions & 4 deletions src/Neo.VM/EvaluationStack.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2016-2023 The Neo Project.
//
// The neo-vm is free software distributed under the MIT software license,
//
// The neo-vm is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand Down Expand Up @@ -35,6 +35,26 @@ internal EvaluationStack(ReferenceCounter referenceCounter)
/// </summary>
public int Count => innerList.Count;

public override string ToString()
{
return $@"[{string.Join(", ", innerList.Select(p =>
{
var value = p.Type switch
{
StackItemType.Pointer => $"({((Pointer)p).Position})",
StackItemType.Boolean => $"({p.GetBoolean()})",
StackItemType.Integer => $"({p.GetInteger()})",
shargon marked this conversation as resolved.
Show resolved Hide resolved
StackItemType.ByteString => $"(\"{p.GetString()}\")",
StackItemType.Array
or StackItemType.Map
or StackItemType.Struct => $"({((CompoundType)p).Count})",
_ => ""
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
};
return $"{p.Type.ToString()}{value}";
}
))}]";
}

internal void Clear()
{
foreach (StackItem item in innerList)
Expand Down
13 changes: 13 additions & 0 deletions tests/Neo.VM.Tests/UtEvaluationStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,18 @@ public void TestReverse()
Assert.IsTrue(stack.Pop<Integer>().Equals(1));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => stack.Pop<Integer>().Equals(0));
}

[TestMethod]
public void TestToString()
{
var stack = new EvaluationStack(new ReferenceCounter());

stack.Insert(0, 3);
stack.Insert(1, 1);
stack.Insert(2, "test");
stack.Insert(3, true);

Assert.AreEqual("[Boolean(True), ByteString(\"test\"), Integer(1), Integer(3)]", stack.ToString());
}
}
}