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 2 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
16 changes: 15 additions & 1 deletion 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
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
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,10 +86,20 @@ internal ExecutionContext Clone()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Instruction GetInstruction(int ip) => Script.GetInstruction(ip);

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

internal bool MoveNext()
{
InstructionPointer += CurrentInstruction.Size;
return InstructionPointer < Script.Length;
}

public void SetState<T>(T state)
{
states[typeof(T)] = state;
}
}
}
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 @@ -297,11 +294,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 @@ -1074,12 +1071,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