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

Add class Instruction to simplify ExecutionEngine #102

Merged
merged 5 commits into from
Mar 28, 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
56 changes: 28 additions & 28 deletions src/neo-vm/ExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Neo.VM
{
public class ExecutionContext : IDisposable
public class ExecutionContext
{
/// <summary>
/// Number of items to be returned
/// </summary>
internal readonly int RVCount;
private readonly Dictionary<int, Instruction> instructions = new Dictionary<int, Instruction>();

/// <summary>
/// Binary Reader of the script
/// Number of items to be returned
/// </summary>
internal readonly BinaryReader OpReader;
internal int RVCount { get; }

/// <summary>
/// Script
/// </summary>
public readonly Script Script;
public Script Script { get; }

/// <summary>
/// Evaluation stack
Expand All @@ -34,31 +30,24 @@ public class ExecutionContext : IDisposable
/// <summary>
/// Instruction pointer
/// </summary>
public int InstructionPointer
public int InstructionPointer { get; set; }

public Instruction CurrentInstruction
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return (int)OpReader.BaseStream.Position;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
OpReader.BaseStream.Seek(value, SeekOrigin.Begin);
return GetInstruction(InstructionPointer);
}
}

/// <summary>
/// Next instruction
/// </summary>
public OpCode NextInstruction
public Instruction NextInstruction
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
var position = (int)OpReader.BaseStream.Position;

return position >= Script.Length ? OpCode.RET : Script[position];
return GetInstruction(InstructionPointer + CurrentInstruction.Size);
}
}

Expand All @@ -83,12 +72,23 @@ internal ExecutionContext(Script script, int rvcount)
{
this.RVCount = rvcount;
this.Script = script;
this.OpReader = script.GetBinaryReader();
}

/// <summary>
/// Free resources
/// </summary>
public void Dispose() => OpReader.Dispose();
private Instruction GetInstruction(int ip)
{
if (ip >= Script.Length) return Instruction.RET;
if (!instructions.TryGetValue(ip, out Instruction instruction))
{
instruction = new Instruction(Script, ip);
instructions.Add(ip, instruction);
}
return instruction;
}

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