$ show neo-project/neo-vm
Neo.VM is a lightweight, deterministic, stack-based virtual machine library for executing Neo smart contract bytecode, written in C# targeting .NET 10.0. It implements a complete instruction set (opcodes) for arithmetic, control flow, stack manipulation, type conversion, arrays/structs/maps, and interop (syscalls). The VM features a pluggable jump table dispatch pattern, integrated reference counting for garbage collection of cyclic object graphs, and a debugger API supporting step-into/over/out. Extensively tested with JSON-driven specification files covering edge cases, plus performance benchmarks. Published as a NuGet package, it serves as the execution engine for the Neo blockchain's smart contract runtime.
The LLM identified 3 files worth highlighting for reuse:
Implements a thread-safe reference counter that tracks object references in a directed graph, with specialized logic to handle cycles (e.g., maps containing the map itself). It avoids full mark-and-sweep by only adding/removing references when the count transitions between 0 and 1, recursively scanning children. The design is self-contained (imports only System.Collections.Generic and System.Collections.Concurrent) and can be extracted for any .NET project requiring managed cyclic object lifetime.
A dispatch mechanism that maps opcode names via reflection to virtual method handlers in partial classes. The constructor scans the type hierarchy for methods matching the 'OpCode' naming convention and populates a delegate array. This architecture enables easy extension or customization (e.g., overriding SYSCALL behavior) without modifying the dispatch core. The same pattern can be reused for any state machine or command dispatcher needing runtime method resolution with minimal boilerplate.
A collection of JSON files that serve as executable specifications for each opcode. They define scripts (bytecode), expected intermediate VM states (stack, slots, references), and step-by-step execution actions (stepInto, execute). This format enables cross-implementation conformance testing (e.g., comparing Neo-go and Neo VM execution) and can be adopted by other VM projects to encode deterministic behavior tests independent of a specific test framework.