Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Add PUSHT and PUSHF #497

Merged
merged 2 commits into from
Nov 1, 2022
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
10 changes: 10 additions & 0 deletions src/Neo.VM/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ private void ExecuteInstruction(Instruction instruction)
Push(new BigInteger(instruction.Operand.Span));
break;
}
case OpCode.PUSHT:
{
Push(StackItem.True);
break;
}
case OpCode.PUSHF:
{
Push(StackItem.False);
break;
}
case OpCode.PUSHA:
{
int position = checked(CurrentContext!.InstructionPointer + instruction.TokenI32);
Expand Down
8 changes: 8 additions & 0 deletions src/Neo.VM/OpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public enum OpCode : byte
[OperandSize(Size = 32)]
PUSHINT256 = 0x05,
/// <summary>
/// Pushes the boolean value <see langword="true"/> onto the stack.
/// </summary>
PUSHT = 0x08,
/// <summary>
/// Pushes the boolean value <see langword="false"/> onto the stack.
/// </summary>
PUSHF = 0x09,
/// <summary>
/// Converts the 4-bytes offset to an <see cref="Pointer"/>, and pushes it onto the stack.
/// </summary>
[OperandSize(Size = 4)]
Expand Down
3 changes: 1 addition & 2 deletions src/Neo.VM/ScriptBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public ScriptBuilder EmitPush(BigInteger value)
/// <returns>A reference to this instance after the emit operation has completed.</returns>
public ScriptBuilder EmitPush(bool value)
{
Emit(!value ? OpCode.PUSH1 : OpCode.PUSH0);
return Emit(OpCode.NOT);
return Emit(value ? OpCode.PUSHT : OpCode.PUSHF);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions tests/Neo.VM.Tests/UtScriptBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ public void TestEmitPushBool()
using (ScriptBuilder script = new())
{
script.EmitPush(true);
CollectionAssert.AreEqual(new byte[] { (byte)OpCode.PUSH0, (byte)OpCode.NOT }, script.ToArray());
CollectionAssert.AreEqual(new byte[] { (byte)OpCode.PUSHT }, script.ToArray());
}

using (ScriptBuilder script = new())
{
script.EmitPush(false);
CollectionAssert.AreEqual(new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.NOT }, script.ToArray());
CollectionAssert.AreEqual(new byte[] { (byte)OpCode.PUSHF }, script.ToArray());
}
}

Expand Down