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

Extend ExecutionContext with states #193

Merged
merged 5 commits into from
Aug 20, 2019
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
30 changes: 28 additions & 2 deletions src/neo-vm/ExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Neo.VM
{
[DebuggerDisplay("RVCount={RVCount}, InstructionPointer={InstructionPointer}")]
public class ExecutionContext
public sealed class ExecutionContext
{
private readonly Dictionary<Type, object> states = new Dictionary<Type, object>();
erikzhang marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Number of items to be returned
/// </summary>
Expand Down Expand Up @@ -82,6 +86,28 @@ internal ExecutionContext Clone()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Instruction GetInstruction(int ip) => Script.GetInstruction(ip);

public T GetState<T>()
{
return (T)states[typeof(T)];
}

public bool TryGetState<T>(out T value)
{
if (states.TryGetValue(typeof(T), out var val))
{
value = (T)val;
return true;
}

value = default;
return false;
}

public void SetState<T>(T state)
{
states[typeof(T)] = state;
}

internal bool MoveNext()
{
InstructionPointer += CurrentInstruction.Size;
Expand Down
16 changes: 6 additions & 10 deletions src/neo-vm/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ public class ExecutionEngine : IDisposable
public ExecutionContext EntryContext => InvocationStack.Count > 0 ? InvocationStack.Peek(InvocationStack.Count - 1) : null;
public VMState State { get; internal protected set; } = VMState.BREAK;

#region Events

public event EventHandler<ExecutionContext> ContextLoaded;
public event EventHandler<ExecutionContext> ContextUnloaded;

#endregion

#region Limits

/// <summary>
Expand Down Expand Up @@ -170,6 +163,10 @@ private static int GetItemCount(IEnumerable<StackItem> items)

#endregion

protected virtual void ContextUnloaded(ExecutionContext context)
{
}

public virtual void Dispose()
{
InvocationStack.Clear();
Expand Down Expand Up @@ -298,11 +295,11 @@ private bool ExecuteInstruction()
context_pop.AltStack.CopyTo(CurrentContext.AltStack);
}
CheckStackSize(false, 0);
ContextUnloaded?.Invoke(this, context_pop);
if (InvocationStack.Count == 0)
{
State = VMState.HALT;
}
ContextUnloaded(context_pop);
return true;
}
case OpCode.SYSCALL:
Expand Down Expand Up @@ -1075,12 +1072,11 @@ private bool ExecuteInstruction()
return true;
}

private void LoadContext(ExecutionContext context)
protected virtual void LoadContext(ExecutionContext context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we still inherit from this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the unique way to set the states

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So event based may be better than this, if we can seal the class and use event to manage internals...

{
if (InvocationStack.Count >= MaxInvocationStackSize)
throw new InvalidOperationException();
InvocationStack.Push(context);
ContextLoaded?.Invoke(this, context);
}

public ExecutionContext LoadScript(byte[] script, int rvcount = -1)
Expand Down
21 changes: 21 additions & 0 deletions tests/neo-vm.Tests/UtExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.VM;

namespace Neo.Test
{
[TestClass]
public class UtExecutionContext
{
[TestMethod]
public void StateTest()
{
var context = new ExecutionContext(null, null, 0);

Assert.IsFalse(context.TryGetState<int>(out var i));
context.SetState(5);
Assert.AreEqual(5, context.GetState<int>());
Assert.IsTrue(context.TryGetState(out i));
Assert.AreEqual(5, i);
}
}
}