Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Easy access to the stack items value (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Aug 16, 2018
1 parent d4b6691 commit a2b5950
Showing 1 changed file with 88 additions and 2 deletions.
90 changes: 88 additions & 2 deletions src/NeoSharp.VM/Collections/IStackItemsStack.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Numerics;

namespace NeoSharp.VM
{
Expand All @@ -13,12 +14,15 @@ public abstract class IStackItemsStack : IStack<IStackItem>
public TStackItem Peek<TStackItem>(int index = 0) where TStackItem : IStackItem
{
if (!TryPeek(index, out IStackItem obj))
{
throw new ArgumentOutOfRangeException();
}

if (obj is TStackItem ts) return ts;

throw (new FormatException());
throw new FormatException();
}

/// <summary>
/// Pop object casting to this type
/// </summary>
Expand All @@ -28,8 +32,9 @@ public abstract class IStackItemsStack : IStack<IStackItem>
{
if (Pop() is TStackItem ts) return ts;

throw (new FormatException());
throw new FormatException();
}

/// <summary>
/// Try Pop object casting to this type
/// </summary>
Expand All @@ -38,6 +43,87 @@ public abstract class IStackItemsStack : IStack<IStackItem>
/// <returns>Return false if it is something wrong</returns>
public abstract bool TryPop<TStackItem>(out TStackItem item) where TStackItem : IStackItem;

/// <summary>
/// Try pop byte array
/// </summary>
/// <param name="value">Value</param>
/// <returns>Return false if is something wrong or is not convertible to ByteArray</returns>
public bool TryPop(out byte[] value)
{
if (TryPop<IStackItem>(out var item))
{
using (item)
{
if (item.CanConvertToByteArray)
{
value = item.ToByteArray();
return true;
}
}
}

value = null;
return false;
}

/// <summary>
/// Try pop BigInteger
/// </summary>
/// <param name="value">Value</param>
/// <returns>Return false if is something wrong or is not convertible to BigInteger</returns>
public bool TryPop(out BigInteger value)
{
if (TryPop<IStackItem>(out var item))
{
using (item)
{
if (item is IIntegerStackItem integer)
{
value = integer.Value;
return true;
}
else if (item.CanConvertToByteArray)
{
value = new BigInteger(item.ToByteArray());
return true;
}
}
}

value = BigInteger.Zero;
return false;
}

/// <summary>
/// Try pop bool
/// </summary>
/// <param name="value">Value</param>
/// <returns>Return false if is something wrong or is not convertible to bool</returns>
public bool TryPop(out bool value)
{
if (TryPop<IStackItem>(out var item))
{
using (item)
{
if (item is IBooleanStackItem integer)
{
value = integer.Value;
return true;
}
else if (item.CanConvertToByteArray)
{
var ret = item.ToByteArray();
value = ret != null && ret.Length != 0;

return true;
}
}
}

value = false;
return false;
}

/// <summary>
/// Constructor
/// </summary>
Expand Down

0 comments on commit a2b5950

Please sign in to comment.