├── .gitignore ├── ConsoleApp1 ├── ConsoleApp1.sln └── ConsoleApp1 │ ├── ConsoleApp1.csproj │ └── Program.cs ├── Cvl.VirtualMachine ├── Cvl.VirtualMachine.Debugger │ ├── App.xaml │ ├── App.xaml.cs │ ├── AssemblyInfo.cs │ ├── Base │ │ └── ViewModelBase.cs │ ├── Cvl.VirtualMachine.Debugger.csproj │ ├── MainWindow.xaml │ ├── MainWindow.xaml.cs │ └── Views │ │ ├── CallStack │ │ ├── CallStackControl.xaml │ │ ├── CallStackControl.xaml.cs │ │ └── CallStackMV.cs │ │ ├── Debugger │ │ ├── DebuggerControl.xaml │ │ ├── DebuggerControl.xaml.cs │ │ └── DebuggerVM.cs │ │ ├── EvaluationStack │ │ └── EvaluationStackVM.cs │ │ ├── Instructions │ │ ├── InstructionsControl.xaml │ │ ├── InstructionsControl.xaml.cs │ │ ├── InstructionsVM.cs │ │ └── StyleSelectors │ │ │ └── ExecutedInstructionStyleSelector.cs │ │ ├── LocalArguments │ │ └── LocalArgumentsVM.cs │ │ ├── LocalVariables │ │ └── LocalVariablesVM.cs │ │ ├── TryCatchBlocks │ │ ├── TryCatchBlocksControl.xaml │ │ ├── TryCatchBlocksControl.xaml.cs │ │ └── TryCatchBlocksVM.cs │ │ └── Variables │ │ ├── VariableVM.cs │ │ ├── VariablesControl.xaml │ │ ├── VariablesControl.xaml.cs │ │ └── VariablesVM.cs ├── Cvl.VirtualMachine.UnitTest │ ├── Basic │ │ ├── ArithmeticUniteTest.cs │ │ ├── ArrayTest.cs │ │ ├── AsyncTest.cs │ │ ├── BranchTest.cs │ │ ├── CastingTestProcess.cs │ │ ├── ConvertTest.cs │ │ ├── EnumTest.cs │ │ ├── ExceptionsTest.cs │ │ ├── InheritanceTest.cs │ │ ├── InstanceTest.cs │ │ ├── IntShitftTest.cs │ │ ├── InterfacesTest.cs │ │ ├── LogicTest.cs │ │ ├── NullThisTest.cs │ │ ├── StaticVariablesTest.cs │ │ ├── SwitchTableTest.cs │ │ └── SwitchTest.cs │ ├── Cvl.VirtualMachine.UnitTest.csproj │ ├── Proces │ │ ├── ArithmeticProcessTest.cs │ │ ├── DwaParametryProcesTest.cs │ │ ├── FluentUIProcessTest.cs │ │ ├── FromLife │ │ │ ├── CheclRulesTest.cs │ │ │ └── StringConstrudtionTest.cs │ │ ├── Hibernate │ │ │ ├── HibernateProcessTest.cs │ │ │ └── HibernateWithParamsProcesTest.cs │ │ ├── LinquInProcessTest.cs │ │ ├── Model │ │ │ ├── ApplicationMonitor.cs │ │ │ ├── DataFormFactory.cs │ │ │ ├── ObiektBiznesowy.cs │ │ │ ├── Person.cs │ │ │ └── Repozytorium.cs │ │ ├── MonitoringProcessTest.cs │ │ ├── RequestContract.cs │ │ └── ResponseContract.cs │ └── UnitTest1.cs ├── Cvl.VirtualMachine.sln ├── Cvl.VirtualMachine │ ├── Class1.cs │ ├── Core │ │ ├── Attributes │ │ │ └── InterpretAttribute.cs │ │ ├── CallStack.cs │ │ ├── ElementBase.cs │ │ ├── Enums │ │ │ └── StepExecutionResultEnum.cs │ │ ├── EvaluationStack.cs │ │ ├── ExceptionHandlingStateMachine.cs │ │ ├── ExecutionPoints.cs │ │ ├── Extensions │ │ │ └── MonoTypeExtension.cs │ │ ├── MethodData.cs │ │ ├── MethodState.cs │ │ ├── Serializers │ │ │ ├── IDeserializedObject.cs │ │ │ ├── ISerializer.cs │ │ │ ├── InstanceCreator.cs │ │ │ └── XmlSerializer.cs │ │ ├── ThreadOfControl.cs │ │ ├── Tools │ │ │ ├── ILogMonitor.cs │ │ │ └── Serializer.cs │ │ ├── TryCatchStack.cs │ │ ├── Variables │ │ │ ├── Addresses │ │ │ │ ├── ArgumentAddress.cs │ │ │ │ ├── FieldAddress.cs │ │ │ │ ├── LocalVariableAddress.cs │ │ │ │ └── ObjectAddressWraper.cs │ │ │ ├── ObjectWraperBase.cs │ │ │ └── Values │ │ │ │ ├── BoxWraper.cs │ │ │ │ └── ObjectWraper.cs │ │ └── VirtualMachineState.cs │ ├── Cvl.VirtualMachine.csproj │ ├── Instructions │ │ ├── Arithmetic │ │ │ ├── Add.cs │ │ │ ├── ArithmeticInstructionsFactory.cs │ │ │ ├── Div.cs │ │ │ ├── Mul.cs │ │ │ ├── Rem.cs │ │ │ └── Sub.cs │ │ ├── Arrays │ │ │ ├── ArrayInstructionFactory.cs │ │ │ └── Stelem.cs │ │ ├── Base │ │ │ ├── IndexedInstruction.cs │ │ │ ├── InstructionBase.cs │ │ │ └── OperandInstruction.cs │ │ ├── Boxing │ │ │ ├── Box.cs │ │ │ ├── BoxingFactory.cs │ │ │ ├── Unbox.cs │ │ │ └── Unbox_Any.cs │ │ ├── Calls │ │ │ ├── Call.cs │ │ │ ├── CallStart.cs │ │ │ ├── CallsInstructionFactory.cs │ │ │ ├── Callvirt.cs │ │ │ ├── Constrained.cs │ │ │ └── Ret.cs │ │ ├── Casts │ │ │ ├── Castclass.cs │ │ │ └── CastsInstructionsFactory.cs │ │ ├── Conditional │ │ │ ├── Beq.cs │ │ │ ├── Bge.cs │ │ │ ├── Bgt.cs │ │ │ ├── Ble.cs │ │ │ ├── Blt.cs │ │ │ ├── Bne.cs │ │ │ ├── Br.cs │ │ │ ├── Brfalse.cs │ │ │ ├── Brinst.cs │ │ │ ├── Brnull.cs │ │ │ ├── Brtrue.cs │ │ │ ├── Brzero.cs │ │ │ ├── Ceq.cs │ │ │ ├── Cgt.cs │ │ │ ├── Cgt_Un.cs │ │ │ ├── Clt.cs │ │ │ ├── ConditionalInstrictionsFactory.cs │ │ │ ├── Isinst.cs │ │ │ └── Switch.cs │ │ ├── Converts │ │ │ ├── Conv.cs │ │ │ └── ConvertInstructionFactory.cs │ │ ├── Exceptions │ │ │ ├── Endfinally.cs │ │ │ ├── ExceptionsInstrutionsFactory.cs │ │ │ ├── Leave.cs │ │ │ ├── Rethrow.cs │ │ │ └── Throw.cs │ │ ├── Initialization │ │ │ ├── InitializationInstructionsFactory.cs │ │ │ ├── Initobj.cs │ │ │ ├── Newarr.cs │ │ │ └── Newobj.cs │ │ ├── InstructionsFactory.cs │ │ ├── Logic │ │ │ ├── And.cs │ │ │ ├── LogicInstructionFactory.cs │ │ │ ├── Neg.cs │ │ │ ├── Not.cs │ │ │ ├── Or.cs │ │ │ └── Xor.cs │ │ ├── Shift │ │ │ ├── ShiftInstructionsFactory.cs │ │ │ ├── Shl.cs │ │ │ └── Shr.cs │ │ ├── Special │ │ │ ├── Nop.cs │ │ │ └── SpecialInstrictionsFactory.cs │ │ ├── Stacks │ │ │ ├── Dup.cs │ │ │ ├── Pop.cs │ │ │ └── StacksInstructionsFactory.cs │ │ └── Storage │ │ │ ├── Ldarg.cs │ │ │ ├── Ldarga.cs │ │ │ ├── Ldc.cs │ │ │ ├── Ldelem.cs │ │ │ ├── Ldfld.cs │ │ │ ├── Ldflda.cs │ │ │ ├── Ldftn.cs │ │ │ ├── Ldind.cs │ │ │ ├── Ldlen.cs │ │ │ ├── Ldloc.cs │ │ │ ├── Ldloca.cs │ │ │ ├── Ldobj.cs │ │ │ ├── Ldsfld.cs │ │ │ ├── Ldstr.cs │ │ │ ├── Ldtoken.cs │ │ │ ├── Starg.cs │ │ │ ├── Stelem.cs │ │ │ ├── Stfld.cs │ │ │ ├── Stind.cs │ │ │ ├── Stloc.cs │ │ │ ├── Stobj.cs │ │ │ ├── Stsfld.cs │ │ │ └── ldnull.cs │ ├── Test │ │ ├── TestProces.cs │ │ └── VirtualMachineDebug.cs │ ├── VirtualMachine.cs │ └── cvlogo.png └── TestApp │ ├── Program.cs │ └── TestApp.csproj ├── LICENSE └── README.md /ConsoleApp1/ConsoleApp1.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31912.275 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{03971D1F-629E-44C9-AFF7-F6E88102D144}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {03971D1F-629E-44C9-AFF7-F6E88102D144}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {03971D1F-629E-44C9-AFF7-F6E88102D144}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {03971D1F-629E-44C9-AFF7-F6E88102D144}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {03971D1F-629E-44C9-AFF7-F6E88102D144}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {89AD0613-7B8A-4D89-9AFD-03E73D071757} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /ConsoleApp1/ConsoleApp1/ConsoleApp1.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /ConsoleApp1/ConsoleApp1/Program.cs: -------------------------------------------------------------------------------- 1 | // See https://aka.ms/new-console-template for more information 2 | 3 | using System.Runtime.CompilerServices; 4 | 5 | Console.WriteLine("Hello, World!"); 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/App.xaml: -------------------------------------------------------------------------------- 1 |  6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace Cvl.VirtualMachine.Debugger 10 | { 11 | /// 12 | /// Logika interakcji dla klasy App.xaml 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | [assembly: ThemeInfo( 4 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 5 | //(used if a resource is not found in the page, 6 | // or application resource dictionaries) 7 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 8 | //(used if a resource is not found in the page, 9 | // app, or any theme specific resource dictionaries) 10 | )] 11 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Base/ViewModelBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Cvl.VirtualMachine.Debugger.Base 8 | { 9 | public class ViewModelBase : Telerik.Windows.Controls.ViewModelBase 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | 16 | namespace Cvl.VirtualMachine.Debugger 17 | { 18 | /// 19 | /// Logika interakcji dla klasy MainWindow.xaml 20 | /// 21 | public partial class MainWindow : Window 22 | { 23 | public MainWindow() 24 | { 25 | InitializeComponent(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/CallStack/CallStackControl.xaml: -------------------------------------------------------------------------------- 1 |  10 | 11 | 20 | 21 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/CallStack/CallStackControl.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | 16 | namespace Cvl.VirtualMachine.Debugger.Views.CallStack 17 | { 18 | /// 19 | /// Logika interakcji dla klasy CallStackControl.xaml 20 | /// 21 | public partial class CallStackControl : UserControl 22 | { 23 | public CallStackControl() 24 | { 25 | InitializeComponent(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/CallStack/CallStackMV.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using Cvl.VirtualMachine.Debugger.Base; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Collections.ObjectModel; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Cvl.VirtualMachine.Debugger.Views.Stack 11 | { 12 | public class CallStackItemMV : ViewModelBase 13 | { 14 | public string MemberName { get; set; } 15 | 16 | } 17 | 18 | public class CallStackMV : ViewModelBase 19 | { 20 | public ObservableCollection Methods { get; set; } = new ObservableCollection(); 21 | 22 | internal void Load(ThreadOfControl thread) 23 | { 24 | Methods.Clear(); 25 | 26 | foreach (var item in thread.CallStack.StosSerializowany) 27 | { 28 | var vm = new CallStackItemMV(); 29 | vm.MemberName = $"{item.NazwaTypu}.{item.NazwaMetody}"; 30 | //item.LocalArguments 31 | 32 | Methods.Add(vm); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Debugger/DebuggerControl.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | 16 | namespace Cvl.VirtualMachine.Debugger.Views.Debugger 17 | { 18 | /// 19 | /// Logika interakcji dla klasy DebuggerControl.xaml 20 | /// 21 | public partial class DebuggerControl : UserControl 22 | { 23 | public DebuggerVM ViewModel => DataContext as DebuggerVM; 24 | 25 | public DebuggerControl() 26 | { 27 | InitializeComponent(); 28 | } 29 | 30 | private void UserControl_Loaded(object sender, RoutedEventArgs e) 31 | { 32 | ViewModel.Load(); 33 | } 34 | 35 | 36 | private void UserControl_KeyDown(object sender, KeyEventArgs e) 37 | { 38 | if (e.SystemKey == Key.F10 39 | && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))) 40 | { 41 | ViewModel.StepToCursor(); 42 | ViewModel.Refresh(); 43 | e.Handled = true; 44 | } else if (e.SystemKey == Key.F10) 45 | { 46 | ViewModel.StepOver(); 47 | e.Handled = true; 48 | } else if (e.Key == Key.F11) 49 | { 50 | ViewModel.Step(); 51 | e.Handled = true; 52 | } 53 | 54 | } 55 | 56 | 57 | 58 | private void btnStep_Click(object sender, RoutedEventArgs e) 59 | { 60 | ViewModel.Step(); 61 | } 62 | 63 | private void btnStepOver_Click(object sender, RoutedEventArgs e) 64 | { 65 | ViewModel.StepOver(); 66 | } 67 | 68 | private void btnExecute_Click(object sender, RoutedEventArgs e) 69 | { 70 | ViewModel.Execute(); 71 | ViewModel.Refresh(); 72 | } 73 | 74 | private void btnExecuteToSelected_Click(object sender, RoutedEventArgs e) 75 | { 76 | ViewModel.StepToCursor(); 77 | ViewModel.Refresh(); 78 | } 79 | 80 | private void btnExecuteToIter_Click(object sender, RoutedEventArgs e) 81 | { 82 | ViewModel.ExecuteToIteration(); 83 | ViewModel.Refresh(); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/EvaluationStack/EvaluationStackVM.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using Cvl.VirtualMachine.Debugger.Views.Variables; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Collections.ObjectModel; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Cvl.VirtualMachine.Debugger.Views.EvaluationStack 11 | { 12 | 13 | public class EvaluationStackVM : VariablesVM 14 | { 15 | internal void Load(MethodState aktualnaMetoda) 16 | { 17 | Variables.Clear(); 18 | 19 | var i = 0; 20 | foreach (var item in aktualnaMetoda.EvaluationStack.StosSerializowany) 21 | { 22 | var vm = new VariableVM(); 23 | vm.SetVariable(item); 24 | vm.Index = i++; 25 | 26 | Variables.Add(vm); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Instructions/InstructionsControl.xaml: -------------------------------------------------------------------------------- 1 |  11 | 12 | 13 | 14 | 17 | 18 | 19 | 21 | 22 | 23 | 24 | 25 | 36 | 37 | 39 | 41 | 43 | 45 | 47 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Instructions/InstructionsControl.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | 16 | namespace Cvl.VirtualMachine.Debugger.Views.Instructions 17 | { 18 | /// 19 | /// Logika interakcji dla klasy InstructionsControl.xaml 20 | /// 21 | public partial class InstructionsControl : UserControl 22 | { 23 | public InstructionsControl() 24 | { 25 | InitializeComponent(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Instructions/StyleSelectors/ExecutedInstructionStyleSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | 9 | namespace Cvl.VirtualMachine.Debugger.Views.Instructions.StyleSelectors 10 | { 11 | public class ExecutedInstructionStyleSelector : StyleSelector 12 | { 13 | public override Style SelectStyle(object item, DependencyObject container) 14 | { 15 | if (item is InstructionVM inst) 16 | { 17 | if (inst.IsExecuted) 18 | { 19 | return IsExecutedStyle; 20 | } 21 | else 22 | { 23 | return NormalStyle; 24 | } 25 | } 26 | return null; 27 | } 28 | public Style IsExecutedStyle { get; set; } 29 | public Style NormalStyle { get; set; } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/LocalArguments/LocalArgumentsVM.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using Cvl.VirtualMachine.Debugger.Views.Variables; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Collections.ObjectModel; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Cvl.VirtualMachine.Debugger.Views.LocalArguments 11 | { 12 | 13 | public class LocalArgumentsVM : VariablesVM 14 | { 15 | 16 | internal void Load(MethodState aktualnaMetoda) 17 | { 18 | Variables.Clear(); 19 | 20 | int i =0; 21 | foreach (var item in aktualnaMetoda.LocalArguments.Obiekty.Values) 22 | { 23 | var vm = new VariableVM(); 24 | vm.SetVariable(item); 25 | 26 | vm.Index = i; 27 | 28 | Variables.Add(vm); 29 | i++; 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/LocalVariables/LocalVariablesVM.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using Cvl.VirtualMachine.Debugger.Views.Variables; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Collections.ObjectModel; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Cvl.VirtualMachine.Debugger.Views.LocalVariables 11 | { 12 | 13 | public class LocalVariablesVM : VariablesVM 14 | { 15 | internal void Load(MethodState aktualnaMetoda) 16 | { 17 | Variables.Clear(); 18 | 19 | int i = 0; 20 | foreach (var item in aktualnaMetoda.LocalVariables.Obiekty.Values) 21 | { 22 | var vm = new VariableVM(); 23 | vm.SetVariable(item); 24 | vm.Index = i; 25 | 26 | Variables.Add(vm); 27 | 28 | i++; 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/TryCatchBlocks/TryCatchBlocksControl.xaml: -------------------------------------------------------------------------------- 1 |  10 | 11 | 20 | 21 | 23 | 25 | 27 | 29 | 31 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/TryCatchBlocks/TryCatchBlocksControl.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | 16 | namespace Cvl.VirtualMachine.Debugger.Views.TryCatchBlocks 17 | { 18 | /// 19 | /// Logika interakcji dla klasy TryCatchBlocksControl.xaml 20 | /// 21 | public partial class TryCatchBlocksControl : UserControl 22 | { 23 | public TryCatchBlocksControl() 24 | { 25 | InitializeComponent(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/TryCatchBlocks/TryCatchBlocksVM.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Debugger.Base; 2 | using Cvl.VirtualMachine.Debugger.Views.Variables; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Collections.ObjectModel; 6 | using System.Linq; 7 | using System.Reflection; 8 | using System.Text; 9 | using System.Threading.Tasks; 10 | 11 | namespace Cvl.VirtualMachine.Debugger.Views.TryCatchBlocks 12 | { 13 | public class TryCatchBlockVM : ViewModelBase 14 | { 15 | public int TryOffset { get; set; } 16 | public int TryLength { get; set; } 17 | public int HandlerOffset { get; set; } 18 | public int HandlerLength { get; set; } 19 | public ExceptionHandlingClauseOptions Flags { get; set; } 20 | public string MethodFullName { get; set; } 21 | } 22 | 23 | public class TryCatchBlocksVM : ViewModelBase 24 | { 25 | public ObservableCollection TryCatchBlocks { get; set; } = new ObservableCollection(); 26 | 27 | internal void Load(ThreadOfControl thread) 28 | { 29 | TryCatchBlocks.Clear(); 30 | 31 | foreach (var item in thread.AktualnaMetoda.TryCatchStack.TryCatchBlocks) 32 | { 33 | var vm = new TryCatchBlockVM(); 34 | vm.TryOffset = item.ExceptionHandlingClause.TryOffset; 35 | vm.TryLength = item.ExceptionHandlingClause.TryLength; 36 | vm.HandlerOffset = item.ExceptionHandlingClause.HandlerOffset; 37 | vm.HandlerLength = item.ExceptionHandlingClause.HandlerLength; 38 | vm.Flags = item.ExceptionHandlingClause.Flags; 39 | vm.MethodFullName = item.MethodFullName; 40 | 41 | TryCatchBlocks.Add(vm); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Variables/VariableVM.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Variables.Values; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Cvl.VirtualMachine.Debugger.Views.Variables 9 | { 10 | public class VariableVM 11 | { 12 | public int Index { get; set; } 13 | public string ValueString { get; set; } 14 | public object Value { get; set; } 15 | 16 | public string TypeName { get; set; } 17 | public string TypeFullName { get; set; } 18 | 19 | internal void SetVariable(object item) 20 | { 21 | var ob = item; 22 | if (item is ObjectWraper objectWraper) 23 | { 24 | ob = objectWraper.Warosc; 25 | this.ValueString = ob?.ToString(); 26 | this.Value = item; 27 | this.TypeFullName = $"ObjectWraper<{ob?.GetType().FullName}>"; 28 | this.TypeName = $"OW<{ob?.GetType().Name}>"; 29 | } 30 | else 31 | { 32 | 33 | this.ValueString = item?.ToString(); 34 | this.Value = item; 35 | this.TypeFullName = item?.GetType().FullName; 36 | this.TypeName = item?.GetType().Name; 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Variables/VariablesControl.xaml: -------------------------------------------------------------------------------- 1 |  10 | 11 | 21 | 22 | 23 | 25 | 27 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Variables/VariablesControl.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows; 7 | using System.Windows.Controls; 8 | using System.Windows.Data; 9 | using System.Windows.Documents; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Windows.Media.Imaging; 13 | using System.Windows.Navigation; 14 | using System.Windows.Shapes; 15 | 16 | namespace Cvl.VirtualMachine.Debugger.Views.Variables 17 | { 18 | /// 19 | /// Logika interakcji dla klasy VariablesControl.xaml 20 | /// 21 | public partial class VariablesControl : UserControl 22 | { 23 | public VariablesControl() 24 | { 25 | InitializeComponent(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.Debugger/Views/Variables/VariablesVM.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Debugger.Base; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Collections.ObjectModel; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Cvl.VirtualMachine.Debugger.Views.Variables 10 | { 11 | public class VariablesVM : ViewModelBase 12 | { 13 | public ObservableCollection Variables { get; set; } = new ObservableCollection(); 14 | 15 | private VariableVM selectedVariable; 16 | public VariableVM SelectedVariable 17 | { 18 | get => selectedVariable; 19 | set 20 | { 21 | if( value != selectedVariable) 22 | { 23 | selectedVariable = value; 24 | base.RaisePropertyChanged(); 25 | } 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/ArrayTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NUnit.Framework; 7 | 8 | namespace Cvl.VirtualMachine.UnitTest.Basic 9 | { 10 | public class ArrayTest 11 | { 12 | [SetUp] 13 | public void Setup() { } 14 | 15 | [Test] 16 | public void Test1() 17 | { 18 | var vm = new VirtualMachine(); 19 | var process = new ArrayTestProcess(); 20 | 21 | int[] testArray1 = { 2, 4, 6, 8, 10, 15, 20 }; 22 | 23 | Assert.AreEqual(process.ReadIndex(testArray1, 3), vm.StartTestExecution("ReadIndex", process, testArray1, 3)); 24 | var ex = Assert.Throws(() => vm.StartTestExecution("ReadIndex", process, testArray1, 20)); 25 | Assert.That(ex.InnerException, Is.TypeOf()); 26 | 27 | Assert.AreEqual(process.ChangeValueAtIndex(testArray1, 5, 30), vm.StartTestExecution("ChangeValueAtIndex", process, testArray1, 5, 30)); 28 | ex = Assert.Throws(() => vm.StartTestExecution("ChangeValueAtIndex", process, testArray1, 10, 30)); 29 | Assert.That(ex.InnerException, Is.TypeOf()); 30 | } 31 | 32 | } 33 | 34 | public class ArrayTestProcess 35 | { 36 | public object ReadIndex(int[] array, int index) 37 | { 38 | return array[index]; 39 | } 40 | 41 | public object ChangeValueAtIndex(int[] array, int index, int value) 42 | { 43 | array[index] = value; 44 | return array[index]; 45 | } 46 | 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/AsyncTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Cvl.VirtualMachine.Core.Attributes; 6 | using NUnit.Framework; 7 | 8 | namespace Cvl.VirtualMachine.UnitTest.Basic 9 | { 10 | public class AsyncTest 11 | { 12 | //[Test] 13 | //public void Test1() 14 | //{ 15 | // var vm = new VirtualMachine(); 16 | // var process = new ProcessWithAsync(); 17 | 18 | // Assert.AreEqual(process.SynchronicStart(), vm.StartTestExecution("SynchronicStart", process)); 19 | 20 | 21 | //} 22 | } 23 | 24 | public class ProcessWithAsync 25 | { 26 | public string SynchronicStart() 27 | { 28 | var ret = GetStringAsync().Result; 29 | return ret; 30 | } 31 | 32 | [Interpret] 33 | public async Task GetStringAsync() 34 | { 35 | await Task.Delay(1000); 36 | return "stringTestowy"; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/EnumTest.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Basic 7 | { 8 | public class EnumTest 9 | { 10 | [Test] 11 | public void Test1() 12 | { 13 | var vm = new VirtualMachine(); 14 | var process = new EnumProces(); 15 | 16 | Assert.AreEqual(process.GetEnum1(1), vm.StartTestExecution("GetEnum1", process, 1)); 17 | Assert.AreEqual(process.GetEnum1(-1), vm.StartTestExecution("GetEnum1", process, -1)); 18 | 19 | } 20 | 21 | [Test] 22 | public void Test2() 23 | { 24 | var vm = new VirtualMachine(); 25 | var process = new EnumProces(); 26 | 27 | var ret = vm.StartTestExecution("Test2", process,2); 28 | Assert.AreEqual(process.Test2(2), ret); 29 | 30 | Assert.AreEqual(process.Test3(RuleTypeEnum.ToChooseProcess), vm.StartTestExecution("Test3", process, RuleTypeEnum.ToChooseProcess)); 31 | } 32 | } 33 | 34 | public class EnumProces 35 | { 36 | public EnumTestStatus GetEnum1(int i) 37 | { 38 | var status = EnumTestStatus.Init; 39 | 40 | status = (EnumTestStatus)i; 41 | 42 | if (status == EnumTestStatus.Init) 43 | { 44 | return EnumTestStatus.Init; 45 | } 46 | 47 | //if(status == EnumTestStatus.Progress) 48 | //{ 49 | // status = EnumTestStatus.Init; 50 | //} 51 | 52 | //if(status == EnumTestStatus.Complite) 53 | //{ 54 | // return EnumTestStatus.Complite; 55 | //} 56 | 57 | var stat2 = getStat(); 58 | if(stat2 != EnumTestStatus.Init) 59 | { 60 | return stat2; 61 | } else if(stat2 == EnumTestStatus.Complite) 62 | { 63 | return status; 64 | } 65 | 66 | 67 | return status; 68 | } 69 | 70 | 71 | public EnumTestStatus getStat() 72 | { 73 | return EnumTestStatus.Init; 74 | } 75 | 76 | 77 | public int Test2(int ruleTypeId) 78 | { 79 | var t1 = ruleTypeId == (int) RuleTypeEnum.ToDecission ? (int) RuleTypeEnum.ToChooseProcess : ruleTypeId; 80 | return t1; 81 | } 82 | 83 | 84 | public int Test3(RuleTypeEnum ruleTypeEnum) 85 | { 86 | if(ruleTypeEnum != RuleTypeEnum.Initial ) 87 | { 88 | return (int)RuleTypeEnum.Initial; 89 | } else 90 | { 91 | return (int)RuleTypeEnum.ToChooseProcess; 92 | } 93 | } 94 | 95 | } 96 | 97 | public enum RuleTypeEnum 98 | { 99 | Empty = 0, 100 | Initial = 1, 101 | ToDecission = 2, 102 | ToChooseProcess = 3 103 | } 104 | 105 | public enum EnumTestStatus 106 | { 107 | Init, 108 | Progress, 109 | Complite 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/InheritanceTest.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Attributes; 2 | using NUnit.Framework; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.UnitTest.Basic 8 | { 9 | public class InheritanceTest 10 | { 11 | [Test] 12 | public void Test1() 13 | { 14 | var vm = new VirtualMachine(); 15 | var process = new InheritanceProcesTest(); 16 | 17 | var r1 = vm.StartTestExecution("Start", process); 18 | 19 | Assert.AreEqual(process.Start(), (object)r1); 20 | } 21 | } 22 | 23 | public class InheritanceProcesTest 24 | { 25 | public int Start() 26 | { 27 | int i = 1; 28 | 29 | A a = null; 30 | B b = new B(); 31 | C c = new C(); 32 | 33 | i += b.GetValue(); 34 | i += c.GetValue(); 35 | 36 | a = b; 37 | i += a.GetValue(); 38 | 39 | a = c; 40 | i += a.GetValue(); 41 | 42 | b = c; 43 | i += b.GetValue(); 44 | 45 | return i; 46 | } 47 | } 48 | 49 | public abstract class A 50 | { 51 | [Interpret] 52 | public abstract int GetValue(); 53 | } 54 | 55 | public class B : A 56 | { 57 | public override int GetValue() 58 | { 59 | return 2; 60 | } 61 | } 62 | 63 | public class C : B 64 | { 65 | public override int GetValue() 66 | { 67 | return 3; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/InstanceTest.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Runtime.CompilerServices; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Cvl.VirtualMachine.UnitTest.Basic 10 | { 11 | public class InstanceTest 12 | { 13 | [Test] 14 | public void Test1() 15 | { 16 | var vm = new VirtualMachine(); 17 | Cvl.VirtualMachine.Test.VirtualMachineDebug.VirtualMachine = vm; 18 | var process = new ServiceTest(); 19 | 20 | var r1 = process.Start(); 21 | var r2 = vm.StartTestExecution("Start", process); 22 | 23 | Assert.AreEqual(r1, r2); 24 | } 25 | } 26 | 27 | public class ServiceTest 28 | { 29 | private LoggerFactory loggerFactory { get; set; } = new LoggerFactory(); 30 | public int iter { get; set; } 31 | 32 | public int Start() 33 | { 34 | iter = 1; 35 | var request = new Request(); 36 | 37 | using (var logger = GetLogger("", "")) 38 | { 39 | logger.AddParameter(request, "request"); 40 | } 41 | 42 | return iter; 43 | } 44 | 45 | public Logger GetLogger(object externalId1 = null, object external2 = null, object external3 = null, object external4 = null, string message = null, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) 46 | { 47 | 48 | //jeśli brak logera to tworzę nowego logera 49 | var logger = loggerFactory.GetLogger(externalId1?.ToString(), external2?.ToString(), external3?.ToString(), external4?.ToString(), message, memberName, sourceFilePath, 50 | sourceLineNumber); 51 | 52 | iter += sourceLineNumber; 53 | 54 | return logger; 55 | } 56 | } 57 | 58 | public class LoggerFactory 59 | { 60 | internal Logger GetLogger(string v1, string v2, string v3, string v4, string message, string memberName, string sourceFilePath, int sourceLineNumber) 61 | { 62 | return new Logger(); 63 | } 64 | } 65 | 66 | public class Logger : IDisposable 67 | { 68 | public string TestPrperty { get; set; } 69 | 70 | public void Dispose() 71 | { 72 | } 73 | 74 | internal Logger AddParameter(Request request, string v) 75 | { 76 | return this; 77 | } 78 | } 79 | 80 | public class Request 81 | { 82 | public string TestPrperty1 { get; set; } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/IntShitftTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using NUnit.Framework; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Basic 7 | { 8 | public class IntShitftTest 9 | { 10 | [SetUp] 11 | public void Start() { } 12 | 13 | [Test] 14 | public void Test1() 15 | { 16 | var vm = new VirtualMachine(); 17 | var process = new ShiftTestProcess(); 18 | 19 | //shl 20 | Assert.AreEqual(process.ShiftL(0b0010, 2), vm.StartTestExecution("ShiftL", process, 0b0010, 2)); 21 | Assert.AreEqual(process.ShiftL(-0b0010, -2), vm.StartTestExecution("ShiftL", process, -0b0010, -2)); 22 | 23 | //shr 24 | Assert.AreEqual(process.ShiftR(0b1000, 1), vm.StartTestExecution("ShiftR", process, 0b1000, 1)); 25 | Assert.AreEqual(process.ShiftR(-0b1000, -1), vm.StartTestExecution("ShiftR", process, -0b1000, -1)); 26 | } 27 | } 28 | 29 | public class ShiftTestProcess 30 | { 31 | //Shift left 32 | public int ShiftL(int value, int amount) 33 | { 34 | return value << amount; 35 | } 36 | public int ShiftR(int value, int amount) 37 | { 38 | return value >> amount; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/InterfacesTest.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Attributes; 2 | using NUnit.Framework; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.UnitTest.Basic 8 | { 9 | public class InterfacesTest 10 | { 11 | [Test] 12 | public void Test1() 13 | { 14 | var vm = new VirtualMachine(); 15 | Cvl.VirtualMachine.Test.VirtualMachineDebug.VirtualMachine = vm; 16 | var process = new InterfacesProcessTest(); 17 | 18 | var r1 = process.Start(); 19 | var r2 = vm.StartTestExecution("Start", process); 20 | 21 | Assert.AreEqual(r1, r2); 22 | } 23 | } 24 | 25 | public class InterfacesProcessTest 26 | { 27 | public int Start() 28 | { 29 | int i = 1; 30 | string data = "sdfsdf"; 31 | IA a = null; 32 | a = new A1(); 33 | i += a.GetValue(data , 1); 34 | 35 | a = new A2(); 36 | i += a.GetValue(data, 1); 37 | 38 | return i; 39 | } 40 | } 41 | 42 | public interface IA 43 | { 44 | [Interpret] 45 | int GetValue(string data, int i); 46 | } 47 | 48 | public class A1 : IA 49 | { 50 | [Interpret] 51 | public int GetValue(string data, int i) 52 | { 53 | return data.Length + i + 1; 54 | } 55 | } 56 | 57 | public class A2 : IA 58 | { 59 | [Interpret] 60 | public int GetValue(string data, int i) 61 | { 62 | return data.Length + i + 2; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/LogicTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using NUnit.Framework; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Basic 7 | { 8 | public class LogicTest 9 | { 10 | [SetUp] 11 | public void Setup() { } 12 | 13 | [Test] 14 | public void Test1() 15 | { 16 | var vm = new VirtualMachine(); 17 | var process = new LogicTestProcess(); 18 | 19 | //AND 20 | Assert.AreEqual(process.And(true, false), vm.StartTestExecution("And", process, true, false)); 21 | //OR 22 | Assert.AreEqual(process.Or(true, false), vm.StartTestExecution("Or", process, true, false)); 23 | //XOR 24 | Assert.AreEqual(process.Xor(true, false), vm.StartTestExecution("Xor", process, true, false)); 25 | //NEG 26 | Assert.AreEqual(process.Neg(true), vm.StartTestExecution("Neg", process, true)); 27 | 28 | } 29 | } 30 | 31 | internal class LogicTestProcess 32 | { 33 | public bool And(bool a, bool b) 34 | { 35 | return a & b; 36 | } 37 | public bool Or(bool a, bool b) 38 | { 39 | return a | b; 40 | } 41 | public bool Xor(bool a, bool b) 42 | { 43 | return a ^ b; 44 | } 45 | public bool Neg(bool a) 46 | { 47 | return !a; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/StaticVariablesTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using NUnit.Framework; 7 | 8 | namespace Cvl.VirtualMachine.UnitTest.Basic 9 | { 10 | public class StaticVariablesTest 11 | { 12 | [SetUp] 13 | public void Setup() { } 14 | 15 | [Test] 16 | public void Test1() 17 | { 18 | var vm = new VirtualMachine(); 19 | var process = new StaticVariablesTestProcess(); 20 | 21 | 22 | 23 | Assert.AreEqual(process.ReadStatic(), vm.StartTestExecution("ReadStatic", process)); 24 | Assert.AreEqual(process.SaveStatic(5), vm.StartTestExecution("SaveStatic", process, 5)); 25 | } 26 | } 27 | 28 | public class StaticVariablesTestProcess 29 | { 30 | public object ReadStatic() 31 | { 32 | var stvar = new StaticVariable(); 33 | var result = stvar.GetVar(); 34 | return result; 35 | } 36 | 37 | public object SaveStatic(int value) 38 | { 39 | var stvar = new StaticVariable(); 40 | var result = stvar.SaveVar(value); 41 | return result; 42 | } 43 | } 44 | 45 | public class StaticVariable 46 | { 47 | public static int stvar = 10; 48 | public int GetVar() 49 | { 50 | return stvar; 51 | } 52 | public int SaveVar(int value) 53 | { 54 | stvar = value; 55 | return stvar; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/SwitchTableTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using NUnit.Framework; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Basic 7 | { 8 | public class SwitchTableTest 9 | { 10 | [SetUp] 11 | public void Start() { } 12 | 13 | [Test] 14 | public void Test1() 15 | { 16 | var vm = new VirtualMachine(); 17 | var process = new SwitchTableTestProcess(); 18 | 19 | Assert.AreEqual(process.Switch(2, new int[] { 1, 2, 3, 4, 5 }), vm.StartTestExecution("Switch", process, 2, new int[] { 1, 2, 3, 4, 5 })); 20 | 21 | } 22 | } 23 | 24 | public class SwitchTableTestProcess 25 | { 26 | public int Switch(int i, int[] arr) 27 | { 28 | return arr[i]; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Basic/SwitchTest.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Basic 7 | { 8 | public class SwitchTest 9 | { 10 | [SetUp] 11 | public void Setup() { } 12 | 13 | [Test] 14 | public void Test1() 15 | { 16 | var vm = new VirtualMachine(); 17 | var process = new SwitchTestProcess(); 18 | 19 | Assert.AreEqual(process.Switch(), vm.StartTestExecution("Switch", process)); 20 | } 21 | } 22 | 23 | public class SwitchTestProcess 24 | { 25 | public int Switch() 26 | { 27 | int ret = 0; 28 | for (int i = 0; i < 10; i++) 29 | { 30 | switch (i) 31 | { 32 | case 1: 33 | ret += 1; 34 | break; 35 | case 2: 36 | ret += 2; 37 | break; 38 | case 3: 39 | ret += 3; 40 | break; 41 | case 4: 42 | ret += 4; 43 | break; 44 | case 5: 45 | ret += 5; 46 | break; 47 | case 6: 48 | ret += 6; 49 | break; 50 | case 7: 51 | ret += 7; 52 | break; 53 | case 8: 54 | ret += 8; 55 | break; 56 | case 9: 57 | ret += 9; 58 | break; 59 | } 60 | } 61 | return ret; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Cvl.VirtualMachine.UnitTest.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/ArithmeticProcessTest.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Proces 7 | { 8 | public class ArithmeticProcess 9 | { 10 | public int X { get; set; } 11 | public int Y { get; set; } 12 | public double XX { get; set; } 13 | 14 | public object Start() 15 | { 16 | int i = 2; 17 | XX = 1; 18 | double sum = 0; 19 | 20 | for (int j = 0; j < 10; j++) 21 | { 22 | X = j * Y + j / i; 23 | XX = 2.0 * j + XX / 2.0; 24 | XX = XX / (XX + 2.0); 25 | sum += XX; 26 | } 27 | 28 | return sum; 29 | } 30 | } 31 | 32 | 33 | public class AritheticProcessUniteTest 34 | { 35 | [Test] 36 | public void ArithmeticTest() 37 | { 38 | var proces = new ArithmeticProcess(); 39 | var vm = new VirtualMachine(); 40 | var vmWynik = vm.StartTestExecution("Start", proces); 41 | 42 | proces = new ArithmeticProcess(); 43 | var wynik = proces.Start(); 44 | Assert.AreEqual(wynik, vmWynik); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/FluentUIProcessTest.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.UnitTest.Proces.Model; 2 | using NUnit.Framework; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq.Expressions; 6 | using System.Text; 7 | 8 | namespace Cvl.VirtualMachine.UnitTest.Proces 9 | { 10 | public class FluentUITest 11 | { 12 | [Test] 13 | public void TestMethod1() 14 | { 15 | var proces = new FluentUIProcess(); 16 | var vm = new VirtualMachine(); 17 | var vmWynik = vm.StartTestExecution("Start",proces); 18 | 19 | proces = new FluentUIProcess(); 20 | var wynik = proces.Start(); 21 | Assert.AreEqual(wynik, vmWynik); 22 | } 23 | } 24 | 25 | public class FluentUIProcess 26 | { 27 | public object Wynik { get; set; } 28 | 29 | public object Start() 30 | { 31 | var view = DataFormView(); 32 | view.DataForm(df => 33 | { 34 | df.AddField(p => p.Name); 35 | df.AddField(p => p.Surname); 36 | df.AddField(p => p.Age); 37 | }); 38 | 39 | Wynik = view; 40 | 41 | return "OK"; 42 | } 43 | 44 | public DataFormFactory DataFormView() 45 | { 46 | var view = new DataFormFactory(); 47 | 48 | return view; 49 | } 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/FromLife/CheclRulesTest.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Proces.FromLife 7 | { 8 | public class CheclRulesTest 9 | { 10 | [Test] 11 | public void Test1() 12 | { 13 | var proces = new ChcekRulesProcessTest(); 14 | var vm = new VirtualMachine(); 15 | var vmWynik = vm.StartTestExecution("Start", proces); 16 | 17 | proces = new ChcekRulesProcessTest(); 18 | var wynik = proces.Start(); 19 | Assert.AreEqual(wynik, vmWynik); 20 | } 21 | } 22 | 23 | public class ChcekRulesProcessTest 24 | { 25 | public string Start() 26 | { 27 | var request = new RequestTest(){Id = 3244}; 28 | return GetCheck(request); 29 | } 30 | 31 | public string GetCheck(RequestTest request) 32 | { 33 | return CheckRules(request.Id, 8, 3, 3); 34 | } 35 | 36 | public string CheckRules(int? applicationEngineId, int applicationId, int applicationTypeId, int ruleTypeId) 37 | { 38 | var str1 = $"ruleTypeId:{ruleTypeId}"; 39 | 40 | var str2 = $"{this} {applicationEngineId} {applicationId} {applicationTypeId} {ruleTypeId}"; 41 | 42 | return str1 + str2; 43 | } 44 | } 45 | 46 | public class RequestTest 47 | { 48 | public int Id { get; set; } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/FromLife/StringConstrudtionTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Cvl.VirtualMachine.Core.Attributes; 7 | using NUnit.Framework; 8 | 9 | namespace Cvl.VirtualMachine.UnitTest.Proces.FromLife 10 | { 11 | public class StringConstrudtionTest 12 | { 13 | //[Test] 14 | //public void Test1() 15 | //{ 16 | // var proces = new StringConstrudtionTestProcess(); 17 | // var vm = new VirtualMachine(); 18 | // var vmWynik = vm.StartTestExecution("Start", proces); 19 | 20 | // proces = new StringConstrudtionTestProcess(); 21 | // var wynik = proces.Start(); 22 | // Assert.AreEqual(wynik, vmWynik); 23 | //} 24 | } 25 | 26 | public class StringConstrudtionTestProcess 27 | { 28 | [Interpret] 29 | public string Start() 30 | { 31 | int i = 4; 32 | var text = $"Some text with {i} value"; 33 | return text; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/Hibernate/HibernateProcessTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Xml.Serialization; 5 | using Cvl.VirtualMachine.Core; 6 | using NUnit.Framework; 7 | 8 | namespace Cvl.VirtualMachine.UnitTest.Proces.Hibernate 9 | { 10 | class HibernateProcessTest 11 | { 12 | [Test] 13 | public void DwaParametryProcesTest() 14 | { 15 | var proces = new HibernateTestProcess(); 16 | var vm = new VirtualMachine(); 17 | Cvl.VirtualMachine.Test.VirtualMachineDebug.VirtualMachine = vm; 18 | vm.Instance = proces; 19 | 20 | var xml = VirtualMachine.SerializeVirtualMachine(vm); 21 | 22 | var vmWynik = vm.Start("Start", proces); 23 | Assert.True(vmWynik.State == VirtualMachineState.Hibernated); 24 | Assert.True(proces.State == 1); 25 | 26 | //vm.Thread.AktualnaMetoda = null; 27 | 28 | xml = VirtualMachine.SerializeVirtualMachine(vm); 29 | var vm2 = VirtualMachine.DeserializeVirtualMachine(xml); 30 | vm = vm2; 31 | 32 | vmWynik = vm.Resume("param1"); 33 | Assert.True(vmWynik.State == VirtualMachineState.Hibernated); 34 | //Assert.True(((HibernateTestProcess)vm.Instance).State == 7); 35 | 36 | vmWynik = vm.Resume(); 37 | Assert.True(vmWynik.State == VirtualMachineState.Executed); 38 | //Assert.True(((HibernateTestProcess)vm.Instance).State == 8); 39 | 40 | proces = new HibernateTestProcess(); 41 | var wynik = proces.Start(); 42 | Assert.AreEqual(wynik, vmWynik.Result); 43 | } 44 | 45 | } 46 | 47 | public class HibernateTestProcess 48 | { 49 | public int State { get; set; } 50 | 51 | public string TestString { get; set; } = "Test string"; 52 | 53 | public object Start() 54 | { 55 | int i = 2; 56 | State++; 57 | object testObject = 3; 58 | TestString = $"asdfasd { testObject}"; 59 | var result1 = VirtualMachine.Hibernate("param1"); 60 | CheckHibernationResult(result1); 61 | 62 | //TestString = $"asdfasd {3}"; 63 | 64 | State += TestString.Length; 65 | 66 | VirtualMachine.Hibernate(); 67 | State++; 68 | 69 | 70 | return State; 71 | } 72 | 73 | private void CheckHibernationResult(object o) 74 | { 75 | 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/Hibernate/HibernateWithParamsProcesTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Cvl.VirtualMachine.Core; 5 | using NUnit.Framework; 6 | 7 | namespace Cvl.VirtualMachine.UnitTest.Proces.Hibernate 8 | { 9 | 10 | class HibernateWithParamsProcesTest 11 | { 12 | [Test] 13 | public void DwaParametryProcesTest() 14 | { 15 | var proces = new HibernateWithPrarametersTestProcess(); 16 | var vm = new VirtualMachine(); 17 | Cvl.VirtualMachine.Test.VirtualMachineDebug.VirtualMachine = vm; 18 | var vmWynik = vm.Start("Start", proces); 19 | Assert.True(vmWynik.State == VirtualMachineState.Hibernated); 20 | Assert.True(proces.State == 1); 21 | var p1 = vm.GetHibernateParams(); 22 | Assert.True(p1[0].Equals("parameter from vw process")); 23 | Assert.True(p1[1].Equals(proces.State)); 24 | 25 | 26 | vmWynik = vm.Resume("Parameter from host to vw process"); 27 | Assert.True(vmWynik.State == VirtualMachineState.Hibernated); 28 | Assert.True(proces.State == 2); 29 | 30 | var p2 = vm.GetHibernateParams(); 31 | Assert.True(p2[0].Equals("Parameter from host to vw process")); 32 | 33 | vmWynik = vm.Resume(); 34 | Assert.True(vmWynik.State == VirtualMachineState.Executed); 35 | Assert.True(proces.State == 3); 36 | 37 | proces = new HibernateWithPrarametersTestProcess(); 38 | var wynik = proces.Start(); 39 | Assert.AreEqual(wynik, vmWynik.Result); 40 | } 41 | } 42 | 43 | public class HibernateWithPrarametersTestProcess 44 | { 45 | public int State { get; set; } 46 | 47 | public object Start() 48 | { 49 | int i = 2; 50 | State++; 51 | var ret =VirtualMachine.Hibernate("parameter from vw process", State); 52 | State++; 53 | VirtualMachine.Hibernate(ret); 54 | State++; 55 | 56 | 57 | return State; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/LinquInProcessTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Cvl.VirtualMachine.Core.Attributes; 6 | using Cvl.VirtualMachine.UnitTest.Proces.Model; 7 | using NUnit.Framework; 8 | 9 | namespace Cvl.VirtualMachine.UnitTest.Proces 10 | { 11 | public class LinquInProcessTest 12 | { 13 | [Test] 14 | public void TestMethod1() 15 | { 16 | var proces = new LinquInProcess(); 17 | var vm = new VirtualMachine(); 18 | var vmWynik = vm.StartTestExecution("Start", proces); 19 | 20 | proces = new LinquInProcess(); 21 | var wynik = proces.Start(); 22 | Assert.AreEqual(wynik, vmWynik); 23 | } 24 | } 25 | 26 | public class LinquInProcess 27 | { 28 | 29 | public int Start() 30 | { 31 | var list = createDataList(); 32 | var person1 = list.First(x => x.Age == 34); 33 | var personLast = list.Where(x => x.Age == 34).OrderBy(x=> x.Name).Last(); 34 | var listOlder = list.Where(x => x.Age <= person1.Age); 35 | listOlder = listOlder.OrderByDescending(x => x.Name); 36 | listOlder = listOlder.Where(x => x.Id < 10); 37 | 38 | var olders = listOlder.ToList(); 39 | 40 | foreach (var person in listOlder) 41 | { 42 | person.Age++; 43 | } 44 | 45 | return person1.Age; 46 | } 47 | 48 | [Interpret] 49 | private List createDataList() 50 | { 51 | var persons = new List(); 52 | persons.Add(new Person(){ Id =1, Name = "Jan", Surname = "Kowalski", Age = 32}); 53 | persons.Add(new Person() { Id=2, Name = "Adam", Surname = "Nowak", Age = 34 }); 54 | persons.Add(new Person() { Id=3, Name = "Jan", Surname = "Nowowsky", Age = 24 }); 55 | 56 | return persons; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/Model/ApplicationMonitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | using System.Runtime.CompilerServices; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.UnitTest.Proces.Model 8 | { 9 | public class Logger 10 | { 11 | public string LogData { get; set; } 12 | } 13 | public class ApplicationMonitor 14 | { 15 | public Logger StartLogs( 16 | Expression> param1 = null, 17 | Expression> param2 = null, 18 | Expression> param3 = null, 19 | object param4 = null, 20 | object param5 = null, 21 | [CallerMemberName] string memberName = "", 22 | [CallerFilePath] string sourceFilePath = "", 23 | [CallerLineNumber] int sourceLineNumber = 0, 24 | string message = "", 25 | string externalId = null) 26 | { 27 | Logger logger = new Logger(); 28 | var sb = new StringBuilder(); 29 | sb.AppendLine(externalId); 30 | sb.AppendLine(memberName); 31 | sb.AppendLine(sourceFilePath); 32 | sb.AppendLine(sourceLineNumber.ToString()); 33 | sb.AppendLine(message); 34 | sb.AppendLine(param4?.ToString()); 35 | sb.AppendLine(param5?.ToString()); 36 | 37 | return logger; 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/Model/DataFormFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.UnitTest.Proces.Model 7 | { 8 | public class DataFormFactory 9 | { 10 | public DataFormFactory DataForm(Action> panel) 11 | { 12 | var factory = new DataFormFactory(); 13 | panel(factory); 14 | return this; 15 | } 16 | 17 | public DataFormFactory AddField(Expression> nazwaPola, string tooltip = null) 18 | { 19 | 20 | return this; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/Model/ObiektBiznesowy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.UnitTest.Proces.Model 6 | { 7 | public class ObiektBiznesowy 8 | { 9 | public int Id { get; set; } 10 | 11 | public override int GetHashCode() 12 | { 13 | return base.GetHashCode(); 14 | } 15 | 16 | public override bool Equals(object obj) 17 | { 18 | var ob = obj as ObiektBiznesowy; 19 | if (ob != null) 20 | { 21 | return ob.Id == Id && ob.GetType() == this.GetType(); 22 | } 23 | else 24 | { 25 | return base.Equals(obj); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/Model/Person.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.UnitTest.Proces.Model 6 | { 7 | public class Person : ObiektBiznesowy 8 | { 9 | public string Surname { get; set; } 10 | public string Name { get; set; } 11 | public int Age { get; set; } 12 | public string Address { get; set; } 13 | 14 | public override bool Equals(object obj) 15 | { 16 | var p = obj as Person; 17 | return Surname == p.Surname && Name == p.Name && Age == p.Age && Address == p.Address; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/Model/Repozytorium.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.UnitTest.Proces.Model 6 | { 7 | /// 8 | /// Testowe repozytorium 9 | /// 10 | public class Repozytorium 11 | { 12 | public virtual List PobierzObiekty() 13 | { 14 | return null; 15 | } 16 | 17 | public virtual List PobierzObiekty(string filtry) 18 | { 19 | return null; 20 | } 21 | 22 | public virtual List PobierzObiekty(string filtry, DateTime od) 23 | { 24 | return null; 25 | } 26 | 27 | public virtual List PobierzObiekty(string filtry, DateTime? od) 28 | { 29 | return null; 30 | } 31 | 32 | public virtual List PobierzObiekty(string filtry, DateTime? od, DateTime? _do) 33 | { 34 | return null; 35 | } 36 | } 37 | 38 | 39 | /// 40 | /// Testowe repozytorium typowane 41 | /// 42 | /// 43 | public class RepozytoriumTypowane : Repozytorium 44 | where T : ObiektBiznesowy, new() 45 | { 46 | public virtual List PobierzObiektyTypowane() 47 | { 48 | var lista = new List(); 49 | lista.Add(new T()); 50 | lista.Add(new T()); 51 | return lista; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/MonitoringProcessTest.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Attributes; 2 | using Cvl.VirtualMachine.UnitTest.Proces.Model; 3 | using NUnit.Framework; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Text; 7 | using System.Threading; 8 | 9 | namespace Cvl.VirtualMachine.UnitTest.Proces 10 | { 11 | public class MonitoringProcessTest 12 | { 13 | [Test] 14 | public void TestMethod1() 15 | { 16 | var proces = new MonitoringProcess(); 17 | var vm = new VirtualMachine(); 18 | var vmWynik = vm.StartTestExecution("Start", proces); 19 | 20 | proces = new MonitoringProcess(); 21 | var wynik = proces.Start(); 22 | Assert.AreEqual(wynik, vmWynik); 23 | } 24 | } 25 | 26 | public class MonitoringProcess 27 | { 28 | private ApplicationMonitor monitor; 29 | public string Start() 30 | { 31 | monitor = new ApplicationMonitor(); 32 | 33 | var reqest = new RequestContract(); 34 | var response = GetVerification(reqest); 35 | return "Ok"; 36 | } 37 | 38 | [Interpret] 39 | public ResponseContract GetVerification(RequestContract request) 40 | { 41 | var log = monitor.StartLogs(() => request, param4: IPHelper.CheckIp(), 42 | externalId: request?.ApplicationId.ToString()); 43 | 44 | int i = 0; 45 | i += request.ApplicationId; 46 | 47 | return new ResponseContract(); 48 | } 49 | 50 | } 51 | 52 | internal class IPHelper 53 | { 54 | internal static object CheckIp() 55 | { 56 | return "localhost"; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/RequestContract.cs: -------------------------------------------------------------------------------- 1 | namespace Cvl.VirtualMachine.UnitTest.Proces 2 | { 3 | public class RequestContract 4 | { 5 | public int ApplicationId { get; set; } = 3; 6 | } 7 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/Proces/ResponseContract.cs: -------------------------------------------------------------------------------- 1 | namespace Cvl.VirtualMachine.UnitTest.Proces 2 | { 3 | public class ResponseContract 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.UnitTest/UnitTest1.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace Cvl.VirtualMachine.UnitTest 4 | { 5 | public class Tests 6 | { 7 | [SetUp] 8 | public void Setup() 9 | { 10 | } 11 | 12 | [Test] 13 | public void Test1() 14 | { 15 | Assert.Pass(); 16 | 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31912.275 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cvl.VirtualMachine", "Cvl.VirtualMachine\Cvl.VirtualMachine.csproj", "{91A26610-1363-45F0-BF98-79870ECA8A50}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.csproj", "{61DD8F0B-E810-43E0-AB46-B2DF9729D9BB}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cvl.VirtualMachine.UnitTest", "Cvl.VirtualMachine.UnitTest\Cvl.VirtualMachine.UnitTest.csproj", "{FD82AA64-E595-4181-BB68-E288BC23B837}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cvl.VirtualMachine.Debugger", "Cvl.VirtualMachine.Debugger\Cvl.VirtualMachine.Debugger.csproj", "{816D3161-B762-426B-BC60-AF15801F7272}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {91A26610-1363-45F0-BF98-79870ECA8A50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {91A26610-1363-45F0-BF98-79870ECA8A50}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {91A26610-1363-45F0-BF98-79870ECA8A50}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {91A26610-1363-45F0-BF98-79870ECA8A50}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {61DD8F0B-E810-43E0-AB46-B2DF9729D9BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {61DD8F0B-E810-43E0-AB46-B2DF9729D9BB}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {61DD8F0B-E810-43E0-AB46-B2DF9729D9BB}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {61DD8F0B-E810-43E0-AB46-B2DF9729D9BB}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {FD82AA64-E595-4181-BB68-E288BC23B837}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {FD82AA64-E595-4181-BB68-E288BC23B837}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {FD82AA64-E595-4181-BB68-E288BC23B837}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {FD82AA64-E595-4181-BB68-E288BC23B837}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {816D3161-B762-426B-BC60-AF15801F7272}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {816D3161-B762-426B-BC60-AF15801F7272}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {816D3161-B762-426B-BC60-AF15801F7272}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {816D3161-B762-426B-BC60-AF15801F7272}.Release|Any CPU.Build.0 = Release|Any CPU 36 | EndGlobalSection 37 | GlobalSection(SolutionProperties) = preSolution 38 | HideSolutionNode = FALSE 39 | EndGlobalSection 40 | GlobalSection(ExtensibilityGlobals) = postSolution 41 | SolutionGuid = {57D91F88-28DF-41DA-81A1-EBE8EF7758DD} 42 | EndGlobalSection 43 | EndGlobal 44 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Class1.cs: -------------------------------------------------------------------------------- 1 | namespace Cvl.VirtualMachine 2 | { 3 | public class Class1 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Attributes/InterpretAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Attributes 6 | { 7 | /// 8 | /// Atrybut oznacza że dana metoda powinna być interpretowana przez wirutalną maszynę 9 | /// Metody bez tego znacznika będą wykonane 10 | /// 11 | [AttributeUsage(AttributeTargets.All)] 12 | public class InterpretAttribute : System.Attribute 13 | { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/CallStack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Cvl.VirtualMachine.Core.Variables.Values; 6 | 7 | namespace Cvl.VirtualMachine.Core 8 | { 9 | 10 | /// 11 | /// Stos wirtualnej maszyny 12 | /// 13 | public class CallStack 14 | { 15 | public CallStack() 16 | { 17 | stosWewnetrzny = new Stack(); 18 | } 19 | 20 | public Stack PobierzStos() 21 | { 22 | return stosWewnetrzny; 23 | } 24 | 25 | private Stack stosWewnetrzny; 26 | 27 | /// 28 | /// Używany tylko do serializacji stosu (zapisuje go jako lista) 29 | /// 30 | public List StosSerializowany 31 | { 32 | get 33 | { 34 | return stosWewnetrzny.ToList(); 35 | } 36 | set 37 | { 38 | var l = value.ToList(); 39 | l.Reverse(); 40 | foreach (var item in l) 41 | { 42 | stosWewnetrzny.Push(item); 43 | } 44 | } 45 | } 46 | 47 | public void Push(MethodState obiekt) 48 | { 49 | stosWewnetrzny.Push(obiekt); 50 | } 51 | 52 | public MethodState Pop() 53 | { 54 | return stosWewnetrzny.Pop(); 55 | } 56 | 57 | public bool IsEmpty() 58 | { 59 | return stosWewnetrzny.Count == 0; 60 | } 61 | 62 | public override string ToString() 63 | { 64 | var str = stosWewnetrzny.Count + ": "; 65 | 66 | foreach (var item in stosWewnetrzny.ToList()) 67 | { 68 | if (item != null) 69 | { 70 | str += $"{item};\n "; 71 | } 72 | else 73 | { 74 | str += "null;\n"; 75 | } 76 | } 77 | 78 | return str; 79 | } 80 | 81 | public MethodState PobierzNastepnaMetodeZeStosu() 82 | { 83 | var o = Pop(); 84 | return o; 85 | } 86 | 87 | public MethodState PobierzTopMethodState() 88 | { 89 | var tablicaElementowStosu = stosWewnetrzny.ToArray(); 90 | return tablicaElementowStosu[0]; 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/ElementBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core 6 | { 7 | public class ElementBase 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Enums/StepExecutionResultEnum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Enums 6 | { 7 | public enum StepExecutionResultEnum 8 | { 9 | Continue, 10 | EndExecution, 11 | Breakpoint 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/EvaluationStack.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Variables.Values; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Core 8 | { 9 | /// 10 | /// Stos wirtualnej maszyny 11 | /// 12 | public class EvaluationStack 13 | { 14 | public EvaluationStack() 15 | { 16 | stosWewnetrzny = new Stack(); 17 | } 18 | 19 | public Stack PobierzStos() 20 | { 21 | return stosWewnetrzny; 22 | } 23 | 24 | private Stack stosWewnetrzny; 25 | 26 | /// 27 | /// Używany tylko do serializacji stosu (zapisuje go jako lista) 28 | /// 29 | public List StosSerializowany 30 | { 31 | get 32 | { 33 | return stosWewnetrzny.ToList(); 34 | } 35 | set 36 | { 37 | var l = value.ToList(); 38 | l.Reverse(); 39 | foreach (var item in l) 40 | { 41 | stosWewnetrzny.Push(item); 42 | } 43 | } 44 | } 45 | 46 | public void PushObject(object obiekt) 47 | { 48 | var w = new ObjectWraper(obiekt); 49 | Push(w); 50 | } 51 | 52 | public void Push(ElementBase obiekt) 53 | { 54 | stosWewnetrzny.Push(obiekt); 55 | } 56 | 57 | public object Pop() 58 | { 59 | return stosWewnetrzny.Pop(); 60 | } 61 | 62 | public bool IsEmpty() 63 | { 64 | return stosWewnetrzny.Count == 0; 65 | } 66 | 67 | public override string ToString() 68 | { 69 | var str = stosWewnetrzny.Count + ": "; 70 | 71 | foreach (var item in stosWewnetrzny.ToList()) 72 | { 73 | if (item != null) 74 | { 75 | str += item.ToString() + "; \n"; 76 | } 77 | else 78 | { 79 | str += "null;\n"; 80 | } 81 | } 82 | 83 | return str; 84 | } 85 | 86 | public MethodState PobierzNastepnaMetodeZeStosu() 87 | { 88 | while (stosWewnetrzny.Count > 0) 89 | { 90 | var o = Pop(); 91 | if(o is ObjectWraper wo) 92 | { 93 | o = wo.Warosc; 94 | } 95 | 96 | if (o is MethodState) 97 | { 98 | return o as MethodState; 99 | } 100 | } 101 | 102 | return null; 103 | } 104 | 105 | public object PobierzElementZeStosu(int numerElementuOdSzczytu) 106 | { 107 | var tablicaElementowStosu = stosWewnetrzny.ToArray(); 108 | return tablicaElementowStosu[numerElementuOdSzczytu]; 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/ExecutionPoints.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Xml.Serialization; 5 | 6 | namespace Cvl.VirtualMachine.Core 7 | { 8 | public class ExecutionPoint 9 | { 10 | /// 11 | /// numer instrukcji która jest wywoływana 12 | /// 13 | public int ExecutionInstructionIndex { get; set; } 14 | } 15 | 16 | public class ExecutionPoints 17 | { 18 | [XmlIgnore] 19 | public Stack ExecutionPointsStack { get; set; } = new Stack(); 20 | public int CurrentInstructionIndex => ExecutionPointsStack.Peek().ExecutionInstructionIndex; 21 | 22 | public List ExecutionPointsStackSerializowany 23 | { 24 | get 25 | { 26 | return ExecutionPointsStack.ToList(); 27 | } 28 | set 29 | { 30 | var l = value.ToList(); 31 | l.Reverse(); 32 | foreach (var item in l) 33 | { 34 | ExecutionPointsStack.Push(item); 35 | } 36 | } 37 | } 38 | 39 | public ExecutionPoints() 40 | { 41 | ExecutionPointsStack.Push(new ExecutionPoint()); 42 | } 43 | 44 | internal void Pop() 45 | { 46 | ExecutionPointsStack.Pop(); 47 | } 48 | 49 | public static ExecutionPoints operator ++(ExecutionPoints c1) 50 | { 51 | c1.ExecutionPointsStack.Peek().ExecutionInstructionIndex++; 52 | return c1; 53 | } 54 | 55 | internal void SetCurrentInstructionIndex(int v) 56 | { 57 | ExecutionPointsStack.Peek().ExecutionInstructionIndex = v; 58 | } 59 | 60 | internal void PushExecutionPoint(int handlerIndex) 61 | { 62 | ExecutionPointsStack.Push(new ExecutionPoint() { ExecutionInstructionIndex = handlerIndex }); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/MethodData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core 6 | { 7 | /// 8 | /// Kontener na dane metody - do abstrakcji parametrów i argumentów 9 | /// 10 | public class MethodData 11 | { 12 | public MethodData() 13 | { 14 | Obiekty = new Dictionary(); 15 | } 16 | 17 | public void Ustaw(int index, object obiekt) 18 | { 19 | Obiekty[index] = obiekt; 20 | } 21 | 22 | public object Pobierz(int index) 23 | { 24 | if (Obiekty.ContainsKey(index) == false) 25 | { 26 | Obiekty[index] = null; 27 | } 28 | return Obiekty[index]; 29 | } 30 | 31 | public Dictionary Obiekty { get; set; } 32 | //public object[] Obiekty { get; set; } 33 | 34 | public override string ToString() 35 | { 36 | var str = Obiekty.Count + ": "; 37 | foreach (var item in Obiekty.Keys) 38 | { 39 | string wartosc = ""; 40 | if (Obiekty.ContainsKey(item) != false) 41 | { 42 | wartosc = Obiekty[item].ToString(); 43 | } 44 | 45 | str += item.ToString() + "=" + wartosc + "; \n"; 46 | } 47 | return str; 48 | } 49 | 50 | public void Wczytaj(object[] lista) 51 | { 52 | Obiekty.Clear(); 53 | int i = 0; 54 | foreach (var item in lista) 55 | { 56 | Obiekty[i] = item; 57 | i++; 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Serializers/IDeserializedObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Cvl.VirtualMachine.Core.Serializers 8 | { 9 | public interface IDeserializedObject 10 | { 11 | void AfterDeserialization(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Serializers/ISerializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Cvl.VirtualMachine.Core.Serializers 8 | { 9 | public interface ISerializer 10 | { 11 | string Serialize(object? obj); 12 | 13 | T? Deserialize(string serializedForm); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Serializers/InstanceCreator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Polenter.Serialization.Core; 7 | 8 | namespace Cvl.VirtualMachine.Core.Serializers 9 | { 10 | public class SimpleInstanceCreator : IInstanceCreator 11 | { 12 | private readonly IServiceProvider _serviceProvider; 13 | private List deserializedObjects = new List(); 14 | 15 | public SimpleInstanceCreator(IServiceProvider serviceProvider) 16 | { 17 | _serviceProvider = serviceProvider; 18 | } 19 | 20 | public object CreateInstance(Type type) 21 | { 22 | object instance = null; 23 | 24 | if (_serviceProvider != null) 25 | { 26 | instance = _serviceProvider.GetService(type); 27 | } 28 | if (instance == null) 29 | { 30 | instance = Activator.CreateInstance(type); 31 | } 32 | 33 | if (instance is IDeserializedObject deserializedObject) 34 | { 35 | deserializedObjects.Add(deserializedObject); 36 | } 37 | 38 | return instance; 39 | } 40 | 41 | public void RunDeserializationInicaializer() 42 | { 43 | deserializedObjects.Reverse(); 44 | foreach (var deserializedObject in deserializedObjects) 45 | { 46 | deserializedObject.AfterDeserialization(); 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Serializers/XmlSerializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using System.Xml.Serialization; 8 | using Polenter.Serialization; 9 | using Polenter.Serialization.Core; 10 | 11 | namespace Cvl.VirtualMachine.Core.Serializers 12 | { 13 | public class XmlSerializer : ISerializer 14 | { 15 | private readonly IServiceProvider _serviceProvider; 16 | 17 | public XmlSerializer(IServiceProvider serviceProvider = null) 18 | { 19 | _serviceProvider = serviceProvider; 20 | } 21 | 22 | public T? Deserialize(string xmlOfAnObject) 23 | { 24 | if (xmlOfAnObject.StartsWith("?")) 25 | { 26 | xmlOfAnObject = xmlOfAnObject.Remove(0, 1); 27 | } 28 | 29 | if (xmlOfAnObject.Equals("")) 30 | { 31 | return default; 32 | } 33 | 34 | //return JsonConvert.DeserializeObject(xmlOfAnObject); 35 | 36 | 37 | var serializer = new SharpSerializer(); 38 | var instanceCreator = new SimpleInstanceCreator(_serviceProvider); 39 | serializer.InstanceCreator = instanceCreator; 40 | serializer.PropertyProvider.AttributesToIgnore.Clear(); 41 | // remove default ExcludeFromSerializationAttribute for performance gain 42 | serializer.PropertyProvider.AttributesToIgnore.Add(typeof(XmlIgnoreAttribute)); 43 | byte[] bajty = Encoding.UTF8.GetBytes(xmlOfAnObject); 44 | using (var ms = new MemoryStream(bajty)) 45 | { 46 | object obiekt = serializer.Deserialize(ms); 47 | 48 | instanceCreator.RunDeserializationInicaializer(); 49 | return (T)obiekt; 50 | } 51 | } 52 | 53 | public string Serialize(object? obj) 54 | { 55 | if (obj == null) 56 | { 57 | return ""; 58 | } 59 | 60 | //string preserveReferenacesAll = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings 61 | //{ 62 | // PreserveReferencesHandling = PreserveReferencesHandling.All 63 | //}); 64 | 65 | //return preserveReferenacesAll; 66 | 67 | //return JsonConvert.SerializeObject(obj); 68 | 69 | var serializer = new SharpSerializer(); 70 | serializer.PropertyProvider.AttributesToIgnore.Clear(); 71 | // remove default ExcludeFromSerializationAttribute for performance gain 72 | serializer.PropertyProvider.AttributesToIgnore.Add(typeof(XmlIgnoreAttribute)); 73 | 74 | using (var ms = new MemoryStream()) 75 | { 76 | serializer.Serialize(obj, ms); 77 | ms.Position = 0; 78 | byte[] bajty = ms.ToArray(); 79 | return Encoding.UTF8.GetString(bajty, 0, bajty.Length); 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Tools/ILogMonitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Core.Tools 7 | { 8 | public interface ILogMonitor 9 | { 10 | void EventRet(object ret, long iterationNumber); 11 | void EventCall(MethodBase method, List parameters, int callLevel, long iterationNumber); 12 | } 13 | 14 | public class ConsoleLogMonitor : ILogMonitor 15 | { 16 | public void EventCall(MethodBase method, List parameters, int callLevel, long iterationNumber) 17 | { 18 | for (int i = 0; i < callLevel; i++) 19 | { 20 | Console.Write(" "); 21 | } 22 | Console.WriteLine($"{method.Name}({string.Join(",", parameters)})"); 23 | } 24 | 25 | public void EventRet(object ret, long iterationNumber) 26 | { 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/TryCatchStack.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Core 7 | { 8 | public class TryCatchBlock 9 | { 10 | public ExceptionHandlingClause ExceptionHandlingClause { get; internal set; } 11 | public string MethodFullName { get; internal set; } 12 | } 13 | 14 | public class TryCatchStack 15 | { 16 | public Stack TryCatchBlocks { get; set; } = new Stack(); 17 | 18 | internal void PushTryBolcks(List tryBlocks, MethodState aktualnaMetoda) 19 | { 20 | var methodDesc = aktualnaMetoda.PobierzOpisMetody(); 21 | var methodFullName = $"{methodDesc.DeclaringType.FullName}.{methodDesc.Name}"; 22 | 23 | tryBlocks.Reverse(); //liste wrzuce na stron, dla tego musze zamienić kolejność 24 | foreach (var item in tryBlocks) 25 | { 26 | var b = new TryCatchBlock(); 27 | b.ExceptionHandlingClause = item; 28 | b.MethodFullName = methodFullName; 29 | 30 | TryCatchBlocks.Push(b); 31 | } 32 | } 33 | 34 | internal TryCatchBlock PeekTryBlock() 35 | { 36 | return TryCatchBlocks.Peek(); 37 | } 38 | 39 | internal bool IsEmptyTryBlock() 40 | { 41 | return TryCatchBlocks.Count <= 0; 42 | } 43 | 44 | internal TryCatchBlock PopTryBlock() 45 | { 46 | return TryCatchBlocks.Pop(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Variables/Addresses/ArgumentAddress.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Variables.Addresses 6 | { 7 | public class ArgumentAddress : ObjectAddressWraper 8 | { 9 | public int Indeks { get; set; } 10 | public MethodData LokalneArgumenty { get; set; } 11 | 12 | public override object GetValue() 13 | { 14 | if (LokalneArgumenty.Obiekty.ContainsKey(Indeks) == false) 15 | { 16 | LokalneArgumenty.Obiekty[Indeks] = null; 17 | } 18 | var obj= LokalneArgumenty.Obiekty[Indeks]; 19 | 20 | if(obj is ObjectWraperBase objectWraper) 21 | { 22 | //mamy referencje do obieku opakowującego - pobieramy jego wartość 23 | return objectWraper.GetValue(); 24 | } else 25 | { 26 | //mamy normalny obiekt, zwracamy go 27 | return obj; 28 | } 29 | } 30 | 31 | public override void SetValue(object ret) 32 | { 33 | LokalneArgumenty.Obiekty[Indeks] = ret; 34 | } 35 | 36 | public override void SetNull() 37 | { 38 | LokalneArgumenty.Obiekty[Indeks] = null; 39 | } 40 | 41 | public override string ToString() 42 | { 43 | return "Adres arg " + Indeks; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Variables/Addresses/FieldAddress.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Core.Variables.Addresses 7 | { 8 | public class FieldAddress : ObjectAddressWraper 9 | { 10 | public FieldInfo Field { get; set; } 11 | public object Object { get; set; } 12 | 13 | public override object GetValue() 14 | { 15 | var val = Field.GetValue(Object); 16 | return val; 17 | } 18 | 19 | public override void SetValue(object ret) 20 | { 21 | Field.SetValue(Object, ret); 22 | } 23 | 24 | public override void SetNull() 25 | { 26 | Field.SetValue(Object, null); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Variables/Addresses/LocalVariableAddress.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Variables.Addresses 6 | { 7 | public class LocalVariableAddress : ObjectAddressWraper 8 | { 9 | public int Indeks { get; set; } 10 | public MethodData LokalneZmienne { get; set; } 11 | 12 | public override object GetValue() 13 | { 14 | if (LokalneZmienne.Obiekty.ContainsKey(Indeks) == false) 15 | { 16 | LokalneZmienne.Obiekty[Indeks] = null; 17 | } 18 | return LokalneZmienne.Obiekty[Indeks]; 19 | } 20 | 21 | public override void SetValue(object ret) 22 | { 23 | LokalneZmienne.Obiekty[Indeks] = ret; 24 | } 25 | 26 | public override void SetNull() 27 | { 28 | LokalneZmienne.Obiekty[Indeks] = null; 29 | } 30 | 31 | public override string ToString() 32 | { 33 | return "Adres zmiennej lok. " + Indeks; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Variables/Addresses/ObjectAddressWraper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Variables.Addresses 6 | { 7 | public class ObjectAddressWraper : ObjectWraperBase 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Variables/ObjectWraperBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Variables 6 | { 7 | public class ObjectWraperBase : ElementBase 8 | { 9 | public virtual object GetValue() 10 | { 11 | return null; 12 | } 13 | 14 | public virtual void SetNull() 15 | { 16 | 17 | } 18 | 19 | public virtual void SetValue(object ret) 20 | { 21 | throw new NotImplementedException(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Variables/Values/BoxWraper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Variables.Values 6 | { 7 | public class BoxWraper : ObjectWraper 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/Variables/Values/ObjectWraper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core.Variables.Values 6 | { 7 | public class ObjectWraper : ObjectWraperBase 8 | { 9 | public object Warosc { get; set; } 10 | 11 | public ObjectWraper(object o) 12 | { 13 | Warosc = o; 14 | } 15 | 16 | public ObjectWraper() 17 | { 18 | 19 | } 20 | 21 | public override void SetValue(object ret) 22 | { 23 | Warosc = ret; 24 | } 25 | 26 | public override object GetValue() 27 | { 28 | return Warosc; 29 | } 30 | 31 | public override string ToString() 32 | { 33 | return $"OW:{Warosc}"; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Core/VirtualMachineState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Core 6 | { 7 | public enum VirtualMachineState 8 | { 9 | Stoped, 10 | Executing, 11 | Exception, 12 | Executed, 13 | Hibernated, 14 | ExceptionFromVWCore 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Cvl.VirtualMachine.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 6.0.1 8 | 6.0.1 9 | 6.0.4 10 | cvlogo.png 11 | MIT 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | True 23 | \ 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arithmetic/Add.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Arithmetic 6 | { 7 | public class Add : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a + b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arithmetic/ArithmeticInstructionsFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Arithmetic 7 | { 8 | public class ArithmeticInstructionsFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "add": 15 | return CreateInstruction(instrukcja); 16 | case "sub": 17 | return CreateInstruction(instrukcja); 18 | case "mul": 19 | return CreateInstruction(instrukcja); 20 | case "div.un": 21 | case "div": 22 | return CreateInstruction
(instrukcja); 23 | case "rem.un": 24 | case "rem": 25 | return CreateInstruction(instrukcja); 26 | } 27 | return null; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arithmetic/Div.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Arithmetic 6 | { 7 | public class Div : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a / b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arithmetic/Mul.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Arithmetic 6 | { 7 | public class Mul : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a * b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arithmetic/Rem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Arithmetic 6 | { 7 | public class Rem : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a % b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arithmetic/Sub.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Arithmetic 6 | { 7 | public class Sub : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a - b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arrays/ArrayInstructionFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Arrays 7 | { 8 | public class ArrayInstructionFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "stelem.ref": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Arrays/Stelem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Arrays 6 | { 7 | public class Stelem : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | var val = PopObject(); 12 | var index = (int)PopObject(); 13 | var array = (Array)PopObject(); 14 | 15 | array.SetValue(val, index); 16 | 17 | WykonajNastepnaInstrukcje(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Base/IndexedInstruction.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Base 7 | { 8 | public class IndexedInstruction: InstructionBase 9 | { 10 | public int Index { get; set; } 11 | 12 | public void Inicialize(Instruction instruction, int? index = null) 13 | { 14 | base.Inicialize(instruction); 15 | 16 | if(index != null) 17 | { 18 | Index = index.Value; 19 | } else 20 | { 21 | if(instruction.Operand is System.Reflection.LocalVariableInfo vr) 22 | { 23 | Index = vr.LocalIndex; 24 | } 25 | else if (instruction.Operand is System.Reflection.ParameterInfo parameterInfo) 26 | { 27 | Index = parameterInfo.Position + 1; 28 | 29 | } 30 | else if (instruction.Operand is Int32 i) 31 | { 32 | Index = i; 33 | } 34 | else 35 | { 36 | throw new Exception($"brak obsugi instrukcji {instruction} dla operanda {instruction.Operand}"); 37 | } 38 | } 39 | } 40 | 41 | protected int GetIndex() 42 | { 43 | var index = Index; 44 | 45 | if(Instruction.Operand is System.Reflection.ParameterInfo parametrInfo) 46 | { 47 | var methodInfo = (System.Reflection.MethodInfo)parametrInfo.Member; 48 | if(methodInfo.CallingConvention.HasFlag(System.Reflection.CallingConventions.HasThis)) 49 | { 50 | index++; 51 | } 52 | } 53 | 54 | return index; 55 | } 56 | 57 | public override string ToString() 58 | { 59 | return $"{Instruction} {Index}"; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Base/OperandInstruction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Base 6 | { 7 | public class OperandInstruction : InstructionBase 8 | { 9 | public object Operand { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Boxing/Box.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Boxing 6 | { 7 | /// 8 | /// Converts a value type to an object reference (type O). 9 | /// https://msdn.microsoft.com/pl-pl/library/system.reflection.emit.opcodes.box(v=vs.110).aspx 10 | /// 11 | public class Box : InstructionBase 12 | { 13 | public override void Wykonaj() 14 | { 15 | //nic nie robię - box i unbox jest robiony przez środowisko wykonujące 16 | //nie muszę tego emulować 17 | WykonajNastepnaInstrukcje(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Boxing/BoxingFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Boxing 7 | { 8 | public class BoxingFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "box": 15 | return CreateInstruction(instrukcja); 16 | case "unbox": 17 | return CreateInstruction(instrukcja); 18 | case "unbox.any": 19 | return CreateInstruction(instrukcja); 20 | } 21 | return null; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Boxing/Unbox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Boxing 6 | { 7 | /// 8 | /// Converts the boxed representation of a value type to its unboxed form. 9 | /// https://msdn.microsoft.com/pl-pl/library/system.reflection.emit.opcodes.unbox(v=vs.110).aspx 10 | /// 11 | public class Unbox : InstructionBase 12 | { 13 | public override void Wykonaj() 14 | { 15 | //nic nie robię - box i unbox jest robiony przez środowisko wykonujące 16 | //nie muszę tego emulować 17 | WykonajNastepnaInstrukcje(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Boxing/Unbox_Any.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Boxing 6 | { 7 | /// 8 | /// Converts the boxed representation of a type specified in the instruction to its unboxed form. 9 | /// 10 | public class Unbox_Any : InstructionBase 11 | { 12 | 13 | public override void Wykonaj() 14 | { 15 | //nic nie robię - box i unbox jest robiony przez środowisko wykonujące 16 | //nie muszę tego emulować 17 | WykonajNastepnaInstrukcje(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Calls/CallStart.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Calls 7 | { 8 | public class CallStart : InstructionBase 9 | { 10 | public CallStart(MethodState metoda) 11 | { 12 | Metoda = metoda; 13 | } 14 | 15 | public MethodState Metoda { get; set; } 16 | 17 | public override void Wykonaj() 18 | { 19 | WczytajLokalneArgumenty(1); 20 | var instancja = PobierzLokalnyArgument(0); 21 | 22 | var metodaDoWykonania = new MethodState(); 23 | metodaDoWykonania.WyczyscInstrukcje(); 24 | metodaDoWykonania.WczytajInstrukcje(); 25 | 26 | this.MethodContext = metodaDoWykonania; 27 | this.HardwareContext.PushAktualnaMetode(metodaDoWykonania); //TODO: do usuniecia 28 | } 29 | 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Calls/CallsInstructionFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Calls 7 | { 8 | public class CallsInstructionFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "call": 15 | return CreateInstruction(instrukcja); 16 | case "callvirt": 17 | return CreateInstruction(instrukcja); 18 | case "ret": 19 | return CreateInstruction(instrukcja); 20 | case "constrained.": 21 | return CreateInstruction(instrukcja); 22 | } 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Calls/Callvirt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Calls 6 | { 7 | /// 8 | /// Calls a late-bound method on an object, pushing the return value onto the evaluation stack. 9 | /// https://www.ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf page 422 10 | /// 11 | public class Callvirt : Call 12 | { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Calls/Constrained.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Calls 6 | { 7 | /// 8 | /// Constrains the type on which a virtual method call is made. 9 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.constrained?view=netframework-4.8 10 | /// https://www.ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf page 342. 11 | /// 12 | public class Constrained : InstructionBase 13 | { 14 | public override void Wykonaj() 15 | { 16 | MethodContext.ConstrainedType = (Type)this.Instruction.Operand; 17 | //throw new NotImplementedException(); 18 | WykonajNastepnaInstrukcje(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Calls/Ret.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Calls 8 | { 9 | public class Ret : InstructionBase 10 | { 11 | public override void Wykonaj() 12 | { 13 | //mamy wynik 14 | object wynik = "null"; 15 | 16 | //sprawdzam czy jest coś jeszcze na stosie, jeśli jet to jest to wynik, który trzeba zwrócić 17 | if (MethodContext.EvaluationStack.IsEmpty()) 18 | { 19 | EventRet(); 20 | //mamy koniec wykonywania procedury (bez wyniku) 21 | //MethodContext.CzyWykonywacInstrukcje = false; 22 | //WirtualnaMaszyna.Status = VirtualMachineState.Executed; 23 | // return; 24 | } 25 | else 26 | { 27 | // mamy wynik metody, pobieram ze stosu 28 | wynik = PopObject(); 29 | 30 | //loguje wykonanie ret 31 | EventRet(wynik); 32 | } 33 | 34 | 35 | //mamy inne metody na stosie wywołań - przekazuje ostatniej wynik 36 | //ściągam ze stosu obecną metodę 37 | HardwareContext.PopMethodState(); 38 | 39 | //sprawdzam czy koniec wykonania VM - jest coś jeszcze na stosie wywołań - jeśli nie to kończymy wątek i wirtualną maszynę 40 | if (HardwareContext.CallStack.IsEmpty()) 41 | { 42 | //mamy koniec wykonywania funkcji (zwracającej wynik) 43 | MethodContext.CzyWykonywacInstrukcje = false; 44 | HardwareContext.Status = VirtualMachineState.Executed; 45 | HardwareContext.Result = wynik; //zwracam wynik na stosie 46 | return; 47 | } 48 | 49 | var metodaDoWznowienia = HardwareContext.AktualnaMetoda; 50 | if (wynik == "null") 51 | { 52 | //metoda bez wyniku 53 | } 54 | else 55 | { 56 | metodaDoWznowienia.PushObject(wynik); 57 | } 58 | 59 | metodaDoWznowienia.NumerWykonywanejInstrukcji++; 60 | 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Casts/Castclass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Casts 8 | { 9 | /// 10 | /// Attempts to cast an object passed by reference to the specified class. 11 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.castclass?view=net-6.0 12 | /// 13 | public class Castclass : InstructionBase 14 | { 15 | public override void Wykonaj() 16 | { 17 | dynamic a = PopObject(); 18 | 19 | var type = (Type)this.Instruction.Operand; 20 | if (a == null) 21 | { 22 | PushObject(null); 23 | } 24 | else if (type.IsAssignableFrom(a.GetType())) 25 | { 26 | PushObject(a); 27 | } 28 | else 29 | { 30 | throw new InvalidCastException($"Invalid casting {a} to {type}"); 31 | } 32 | WykonajNastepnaInstrukcje(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Casts/CastsInstructionsFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Mono.Reflection; 7 | 8 | namespace Cvl.VirtualMachine.Instructions.Casts 9 | { 10 | public class CastsInstructionsFactory : InstructionFactory 11 | { 12 | public override InstructionBase CreateInstruction(Instruction instrukcja) 13 | { 14 | switch (instrukcja.OpCode.Name) 15 | { 16 | case "castclass": 17 | return CreateInstruction(instrukcja); 18 | } 19 | return null; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Beq.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | /// 9 | /// Transfers control to a target instruction (short form) if two values are equal. 10 | /// https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.beq_s(v=vs.110).aspx 11 | /// 12 | public class Beq : InstructionBase 13 | { 14 | 15 | public override void Wykonaj() 16 | { 17 | var value2 = PopObject(); 18 | var value1 = PopObject(); 19 | 20 | if ((value1==null && value2== null) || value2.Equals(value1)) 21 | { 22 | var op = Instruction.Operand as Instruction; 23 | var nextOffset = op.Offset; 24 | WykonajSkok(nextOffset); 25 | } else 26 | { 27 | WykonajNastepnaInstrukcje(); 28 | } 29 | } 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Bge.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | /// 9 | /// Transfers control to a target instruction if the first value is greater than or equal to the second value. 10 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.bge 11 | /// 12 | public class Bge : InstructionBase 13 | { 14 | public override void Wykonaj() 15 | { 16 | var value2 = PopObject() as dynamic; 17 | var value1 = PopObject() as dynamic; 18 | 19 | var r = value1 > value2; 20 | 21 | if (r == true) 22 | { 23 | var op = Instruction.Operand as Instruction; 24 | var nextOffset = op.Offset; 25 | WykonajSkok(nextOffset); 26 | } 27 | else 28 | { 29 | WykonajNastepnaInstrukcje(); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Bgt.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | /// 9 | /// Transfers control to a target instruction (short form) if the first value is greater than the second value, when comparing unsigned integer values or unordered float values. 10 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.bgt_un_s 11 | /// 12 | public class Bgt : InstructionBase 13 | { 14 | 15 | public override void Wykonaj() 16 | { 17 | var value2 = PopObject() as dynamic; 18 | var value1 = PopObject() as dynamic; 19 | dynamic d = value1 > value2; 20 | 21 | if (d) 22 | { 23 | var op = Instruction.Operand as Instruction; 24 | var nextOffset = op.Offset; 25 | WykonajSkok(nextOffset); 26 | } 27 | else 28 | { 29 | WykonajNastepnaInstrukcje(); 30 | } 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Ble.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | /// 9 | /// Transfers control to a target instruction if the first value is less than or equal to the second value. 10 | /// 11 | /// 12 | public class Ble : InstructionBase 13 | { 14 | public override void Wykonaj() 15 | { 16 | var value2 = PopObject() as dynamic; 17 | var value1 = PopObject() as dynamic; 18 | 19 | var r = value1 <= value2; 20 | 21 | if (r == true) 22 | { 23 | var op = Instruction.Operand as Instruction; 24 | var nextOffset = op.Offset; 25 | WykonajSkok(nextOffset); 26 | } 27 | else 28 | { 29 | WykonajNastepnaInstrukcje(); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Blt.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | /// 9 | /// Transfers control to a target instruction (short form) if the first value is less than the second value. 10 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.blt_s 11 | /// 12 | public class Blt : InstructionBase 13 | { 14 | public override void Wykonaj() 15 | { 16 | var value2 = PopObject() as dynamic; 17 | var value1 = PopObject() as dynamic; 18 | 19 | var r = value1 < value2; 20 | 21 | if (r == true) 22 | { 23 | var op = Instruction.Operand as Instruction; 24 | var nextOffset = op.Offset; 25 | WykonajSkok(nextOffset); 26 | } 27 | else 28 | { 29 | WykonajNastepnaInstrukcje(); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Bne.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | /// 9 | /// Transfers control to a target instruction (short form) when two unsigned integer values or unordered float values are not equal. 10 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.bne_un_s 11 | /// 12 | public class Bne : InstructionBase 13 | { 14 | public override void Wykonaj() 15 | { 16 | var value2 = PopObject(); 17 | var value1 = PopObject(); 18 | 19 | if (value2 != value1) 20 | { 21 | var op = Instruction.Operand as Instruction; 22 | var nextOffset = op.Offset; 23 | WykonajSkok(nextOffset); 24 | } 25 | else 26 | { 27 | WykonajNastepnaInstrukcje(); 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Br.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | /// 9 | /// Unconditionally transfers control to a target instruction. 10 | /// 11 | public class Br : InstructionBase 12 | { 13 | public override void Wykonaj() 14 | { 15 | var op = Instruction.Operand as Instruction; 16 | var nextOffset = op.Offset; 17 | WykonajSkok(nextOffset); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Brfalse.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Variables; 2 | using Cvl.VirtualMachine.Core.Variables.Values; 3 | using Mono.Reflection; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Text; 7 | 8 | namespace Cvl.VirtualMachine.Instructions.Conditional 9 | { 10 | //Transfers control to a target instruction if value is false, a null reference, or zero. 11 | public class Brfalse : InstructionBase 12 | { 13 | public override void Wykonaj() 14 | { 15 | var wynik = false; 16 | dynamic a = PopObject(); 17 | 18 | if (a == null) 19 | { 20 | wynik = true; 21 | } 22 | else if (a is bool b) 23 | { 24 | wynik = b == false; 25 | } 26 | else if (a is int i) 27 | { 28 | wynik = i == 0; 29 | } 30 | else 31 | { 32 | wynik = false; 33 | } 34 | 35 | 36 | if (wynik == true) 37 | { 38 | var op = Instruction.Operand as Instruction; 39 | var nextOffset = op.Offset; 40 | WykonajSkok(nextOffset); 41 | } 42 | else 43 | { 44 | WykonajNastepnaInstrukcje(); 45 | } 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Brinst.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | //Transfers control to a target instruction (short form) if value is not null. 9 | public class Brinst : InstructionBase 10 | { 11 | public override void Wykonaj() 12 | { 13 | var wynik = false; 14 | dynamic a = PopObject(); 15 | if (a != null) 16 | { 17 | wynik = true; 18 | } 19 | else 20 | { 21 | wynik = false; 22 | } 23 | 24 | 25 | if (wynik) 26 | { 27 | var op = Instruction.Operand as Instruction; 28 | var nextOffset = op.Offset; 29 | WykonajSkok(nextOffset); 30 | } 31 | else 32 | { 33 | WykonajNastepnaInstrukcje(); 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Brnull.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | //Transfers control to a target instruction if value is null. 9 | public class Brnull : InstructionBase 10 | { 11 | public override void Wykonaj() 12 | { 13 | var wynik = false; 14 | dynamic a = PopObject(); 15 | if (a == null) 16 | { 17 | wynik = false; 18 | } 19 | else 20 | { 21 | wynik = true; 22 | } 23 | 24 | 25 | if (wynik == false) 26 | { 27 | var op = Instruction.Operand as Instruction; 28 | var nextOffset = op.Offset; 29 | WykonajSkok(nextOffset); 30 | } 31 | else 32 | { 33 | WykonajNastepnaInstrukcje(); 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Brtrue.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | //Transfers control to a target instruction (short form) if value is true, not null, or non-zero. 9 | public class Brtrue : InstructionBase 10 | { 11 | public override void Wykonaj() 12 | { 13 | var wynik = false; 14 | dynamic a = PopObject(); 15 | if (a == null) 16 | { 17 | wynik = false; 18 | } 19 | else if (a is bool) 20 | { 21 | wynik = (bool)a; 22 | } 23 | else if (a is int) 24 | { 25 | wynik = a == 1; 26 | } 27 | else 28 | { 29 | wynik = true; 30 | } 31 | 32 | 33 | if (wynik) 34 | { 35 | var op = Instruction.Operand as Instruction; 36 | var nextOffset = op.Offset; 37 | WykonajSkok(nextOffset); 38 | } 39 | else 40 | { 41 | WykonajNastepnaInstrukcje(); 42 | } 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Brzero.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | //Transfers control to a target instruction if value is zero. 9 | public class Brzero : InstructionBase 10 | { 11 | public override void Wykonaj() 12 | { 13 | var wynik = false; 14 | dynamic a = PopObject(); 15 | if (a == 0) 16 | { 17 | wynik = false; 18 | } 19 | else 20 | { 21 | wynik = true; 22 | } 23 | 24 | 25 | if (wynik == false) 26 | { 27 | var op = Instruction.Operand as Instruction; 28 | var nextOffset = op.Offset; 29 | WykonajSkok(nextOffset); 30 | } 31 | else 32 | { 33 | WykonajNastepnaInstrukcje(); 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Ceq.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Conditional 6 | { 7 | //Compares two values. If they are equal, the integer value 1 (int32) is pushed onto the evaluation stack; otherwise 0 (int32) is pushed onto the evaluation stack. 8 | public class Ceq : InstructionBase 9 | { 10 | 11 | public override void Wykonaj() 12 | { 13 | dynamic b = PopObject(); 14 | dynamic a = PopObject(); 15 | if (a is int && b is bool) 16 | { 17 | b = b ? 1 : 0; 18 | } 19 | else if (b is int && a is bool) 20 | { 21 | a = a ? 1 : 0; 22 | } else if( a is Enum && b is int) 23 | { 24 | a = (int)a; 25 | } 26 | 27 | dynamic wynik = a == b; 28 | PushObject(wynik); 29 | WykonajNastepnaInstrukcje(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Cgt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Conditional 6 | { 7 | ///Compares two values. If the first value is greater than the second, 8 | /// the integer value 1 (int32) is pushed onto the evaluation stack; otherwise 0 (int32) is 9 | /// pushed onto the evaluation stack. 10 | public class Cgt : InstructionBase 11 | { 12 | public override void Wykonaj() 13 | { 14 | dynamic b = PopObject(); 15 | dynamic a = PopObject(); 16 | 17 | dynamic wynik = a > b ? 1 : 0; 18 | PushObject(wynik); 19 | WykonajNastepnaInstrukcje(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Cgt_Un.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Conditional 6 | { 7 | /// 8 | /// Compares two unsigned or unordered values. 9 | /// If the first value is greater than the second, the integer value 1 (int32) is pushed onto the evaluation stack; 10 | /// otherwise 0 (int32) is pushed onto the evaluation stack. 11 | /// 12 | public class Cgt_Un : InstructionBase 13 | { 14 | public override void Wykonaj() 15 | { 16 | dynamic b = PopObject(); 17 | dynamic a = PopObject(); 18 | 19 | if(a is null && !(b is null)) 20 | { 21 | PushObject(0); 22 | } else 23 | if(b is null && !(a is null)) 24 | { 25 | PushObject(1); 26 | } else 27 | if (b is int && a is Enum ) 28 | { 29 | var wynik = (int)a > b ? 1 : 0; 30 | PushObject(wynik); 31 | } 32 | else 33 | if (b is int || a is int) 34 | { 35 | dynamic wynik = a > b ? 1 : 0; 36 | PushObject(wynik); 37 | } 38 | else if (b is double || a is double) 39 | { 40 | dynamic wynik = a > b ? 1 : 0; 41 | PushObject(wynik); 42 | } 43 | else if (b is float || a is float) 44 | { 45 | dynamic wynik = a > b ? 1 : 0; 46 | PushObject(wynik); 47 | } 48 | else 49 | { 50 | //mamy jakiś obiekt więc sprawdzamy czy jest różny 51 | dynamic wynik = a != b ? 1 : 0; 52 | PushObject(wynik); 53 | } 54 | 55 | WykonajNastepnaInstrukcje(); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Clt.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Conditional 6 | { 7 | /// 8 | /// Compares two values. If the first value is less than the second, the integer value 1 (int32) is pushed onto the evaluation stack; otherwise 0 (int32) is pushed onto the evaluation stack. 9 | /// 10 | public class Clt : InstructionBase 11 | { 12 | public override void Wykonaj() 13 | { 14 | dynamic b = PopObject(); 15 | dynamic a = PopObject(); 16 | 17 | int one = 1; 18 | int zero = 0; 19 | 20 | int wynik = a < b ? one : zero; 21 | PushObject(wynik); 22 | WykonajNastepnaInstrukcje(); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/ConditionalInstrictionsFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Conditional 7 | { 8 | public class ConditionalInstrictionsFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "beq": 15 | case "beq.s": 16 | return CreateInstruction(instrukcja); 17 | case "br.s": 18 | case "br": 19 | return CreateInstruction
(instrukcja); 20 | case "cgt": 21 | return CreateInstruction(instrukcja); 22 | case "cgt.un": 23 | return CreateInstruction(instrukcja); 24 | case "clt": 25 | return CreateInstruction(instrukcja); 26 | case "ceq": 27 | return CreateInstruction(instrukcja); 28 | case "brzero": 29 | case "brzero.s": 30 | return CreateInstruction(instrukcja); 31 | case "brfalse": 32 | case "brfalse.s": 33 | return CreateInstruction(instrukcja); 34 | case "brnull": 35 | return CreateInstruction(instrukcja); 36 | case "brtrue": 37 | case "brtrue.s": 38 | return CreateInstruction(instrukcja); 39 | case "brinst": 40 | return CreateInstruction(instrukcja); 41 | case "isinst": 42 | return CreateInstruction(instrukcja); 43 | case "bne.un.s": 44 | case "bne.un": 45 | return CreateInstruction(instrukcja); 46 | case "bgt.s": 47 | case "bgt.un.s": 48 | case "bgt.un": 49 | return CreateInstruction(instrukcja); 50 | case "blt": 51 | case "blt.s": 52 | case "blt.un": 53 | case "blt.un.s": 54 | return CreateInstruction(instrukcja); 55 | case "bge": 56 | case "bge.s": 57 | case "bge.un.s": //TODO: dodać test 58 | return CreateInstruction(instrukcja); 59 | case "ble": 60 | case "ble.s": 61 | case "ble.un": 62 | case "ble.un.s": //TODO: dodać test 63 | return CreateInstruction(instrukcja); 64 | case "switch": 65 | return CreateInstruction(instrukcja); 66 | } 67 | return null; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Isinst.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Variables.Addresses; 2 | using Cvl.VirtualMachine.Core.Variables.Values; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Conditional 8 | { 9 | /// 10 | /// Tests whether an object reference (type O) is an instance of a particular class. 11 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.isinst?view=netframework-4.8 12 | /// 13 | public class Isinst : InstructionBase 14 | { 15 | public override void Wykonaj() 16 | { 17 | dynamic b = PopObject(); 18 | if (b != null) 19 | { 20 | var typ = b.GetType(); 21 | var typOperanda = (Type)Instruction.Operand; 22 | if (typOperanda.IsAssignableFrom(typ)) 23 | { 24 | //mamy ten sam typ 25 | 26 | 27 | if (typOperanda.IsByRef == false) 28 | { 29 | //musimy zrobić sztuczny boxing - 30 | //Push(new BoxWraper() { Warosc = b }); 31 | Push(new BoxWraper() { Warosc = true }); 32 | } 33 | else 34 | { 35 | //wrzucamy normalnya obiekt 36 | PushObject(b); 37 | } 38 | 39 | 40 | //wrzucamy normalnya obiekt 41 | //PushObject(b); 42 | } 43 | else 44 | { 45 | PushObject(null); 46 | } 47 | } 48 | else 49 | { 50 | PushObject(null); 51 | } 52 | 53 | WykonajNastepnaInstrukcje(); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Conditional/Switch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Conditional 6 | { 7 | public class Switch : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | var index = (int)PopObject(); 12 | 13 | if(index <0) 14 | { 15 | WykonajNastepnaInstrukcje(); 16 | } else 17 | { 18 | var instructions = (Mono.Reflection.Instruction[])Instruction.Operand; 19 | var selectedInstruction = instructions[index]; 20 | WykonajSkok(selectedInstruction.Offset); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Converts/Conv.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Converts 6 | { 7 | public enum ConvertType 8 | { 9 | i4, 10 | r8, 11 | i8, 12 | i, 13 | i1, 14 | i2, 15 | r, 16 | r4, 17 | u8, 18 | u4, 19 | u2, 20 | u1, 21 | u 22 | } 23 | 24 | /// 25 | /// Converts the value on top of the evaluation stack to ... 26 | /// 27 | public class Conv : InstructionBase 28 | { 29 | public ConvertType ConvertType; 30 | public override void Wykonaj() 31 | { 32 | dynamic a = PopObject(); 33 | 34 | switch(this.ConvertType) 35 | { 36 | case ConvertType.i: 37 | PushObject((int)a); 38 | break; 39 | case ConvertType.i1: 40 | PushObject((SByte)a); 41 | break; 42 | case ConvertType.i2: 43 | PushObject((short)a); 44 | break; 45 | case ConvertType.i4: 46 | PushObject((int)a); 47 | break; 48 | case ConvertType.i8: 49 | PushObject((int)a); 50 | break; 51 | case ConvertType.r: 52 | PushObject((float)a); 53 | break; 54 | case ConvertType.r4: 55 | PushObject((float)a); 56 | break; 57 | case ConvertType.r8: 58 | PushObject((double)a); 59 | break; 60 | case ConvertType.u: 61 | PushObject((uint)a); 62 | break; 63 | case ConvertType.u1: 64 | PushObject((byte)a); 65 | break; 66 | case ConvertType.u2: 67 | PushObject((ushort)a); 68 | break; 69 | case ConvertType.u4: 70 | PushObject((UInt32)a); 71 | break; 72 | case ConvertType.u8: 73 | PushObject((UInt64)a); 74 | break; 75 | 76 | } 77 | 78 | WykonajNastepnaInstrukcje(); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Converts/ConvertInstructionFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Converts 7 | { 8 | public class ConvertInstructionFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "conv.ovf.i": //TODO:: zrobić obsługę dla ovf i un 15 | case "conv.ovf.i.un": 16 | case "conv.i": 17 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.i); 18 | case "conv.ovf.i1": 19 | case "conv.ovf.i1.un": 20 | case "conv.i1": 21 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.i1); 22 | case "conv.ovf.i2": 23 | case "conv.ovf.i2.un": 24 | case "conv.i2": 25 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.i2); 26 | case "conv.ovf.i4": 27 | case "conv.ovf.i4.un": 28 | case "conv.i4": 29 | return CreateInstruction(instrukcja, i=> i.ConvertType = ConvertType.i4); 30 | case "conv.ovf.i8": 31 | case " conv.ovf.i8.un": 32 | case "conv.i8": 33 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.i8); 34 | case "conv.r.un": 35 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.r); 36 | case "conv.r4": 37 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.r4); 38 | case "conv.r8": 39 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.r8); 40 | case "conv.ovf.u": 41 | case "conv.ovf.u.un": 42 | case "conv.u": 43 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.u); 44 | case "conv.ovf.u1": 45 | case "conv.ovf.u1.un": 46 | case "conv.u1": 47 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.u1); 48 | case "conv.ovf.u2": 49 | case "conv.ovf.u2.un": 50 | case "conv.u2": 51 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.u2); 52 | case "conv.ovf.u4": 53 | case "conv.ovf.u4.un": 54 | case "conv.u4": 55 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.u4); 56 | case "conv.ovf.u8": 57 | case "conv.ovf.u8.un": 58 | case "conv.u8": 59 | return CreateInstruction(instrukcja, i => i.ConvertType = ConvertType.u8); 60 | 61 | 62 | 63 | 64 | } 65 | return null; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Exceptions/Endfinally.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Exceptions 7 | { 8 | public class Endfinally : InstructionBase 9 | { 10 | public override void Wykonaj() 11 | { 12 | HardwareContext.ExceptionHandling.FromEndFinally(); 13 | 14 | //if (HardwareContext.AktualnaMetoda.NumerWykonywanejInstrukcji.ExecutionPointsStack.Count > 1) 15 | //{ 16 | // HardwareContext.AktualnaMetoda.NumerWykonywanejInstrukcji.Pop(); 17 | //} else 18 | //{ 19 | // WykonajNastepnaInstrukcje(); 20 | //} 21 | 22 | //var isEmpty = HardwareContext.TryCatchStack.IsEmptyTryBlock(); 23 | 24 | //if(isEmpty == false) 25 | //{ 26 | // ////sciagam ze stosu jeden blok trycatch 27 | // //HardwareContext.TryCatchStack.PopTryBlock(); 28 | // HardwareContext.AktualnaMetoda.NumerWykonywanejInstrukcji.Pop(); 29 | 30 | // if (HardwareContext.ThrowedException != null) 31 | // { 32 | // //jestem w trakcie wyjątku, przechodzę przez stos do obsługi wyjątku 33 | 34 | // //sciagam ze stosu jeden blok trycatch, bo nie został wywołene zakończenie try -> leave 35 | // HardwareContext.TryCatchStack.PopTryBlock(); 36 | 37 | // Throw.ObslugaRzuconegoWyjatku(HardwareContext.WirtualnaMaszyna, HardwareContext.ThrowedException); 38 | // } 39 | // else 40 | // { 41 | // //jesteśmy w miejscu bez wyjątku, 42 | 43 | // if(HardwareContext.TryCatchStack.PeekTryBlock().ExceptionHandlingClause.Flags == System.Reflection.ExceptionHandlingClauseOptions.Finally) 44 | // { 45 | // //jestemy bez wyjątku i w finally 46 | // HardwareContext.TryCatchStack.PopTryBlock(); 47 | // } 48 | 49 | // WykonajNastepnaInstrukcje(); 50 | // } 51 | //} 52 | //else 53 | //{ 54 | // WykonajNastepnaInstrukcje(); 55 | //} 56 | 57 | 58 | 59 | //var rzuconyWyjatek = PopObject(); 60 | //if (HardwareContext.Status == VirtualMachineState.Exception) 61 | //{ 62 | // //jestem w trakcie wyjątku, przechodzę przez stos do obsługi wyjątku 63 | // Throw.ObslugaRzuconegoWyjatku(MethodContext.WirtualnaMaszyna, rzuconyWyjatek); 64 | //} 65 | //else 66 | //{ 67 | // WykonajNastepnaInstrukcje(); 68 | //} 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Exceptions/ExceptionsInstrutionsFactory.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Special; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Exceptions 8 | { 9 | public class ExceptionsInstrutionsFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "throw": 16 | return CreateInstruction(instrukcja); 17 | case "rethrow": 18 | return CreateInstruction(instrukcja); 19 | case "endfinally": 20 | return CreateInstruction(instrukcja); 21 | case "leave": 22 | case "leave.s": 23 | return CreateInstruction(instrukcja); 24 | } 25 | return null; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Exceptions/Rethrow.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Exceptions 8 | { 9 | /// 10 | /// Rethrows the current exception. 11 | /// https://docs.microsoft.com/pl-pl/dotnet/api/system.reflection.emit.opcodes.rethrow?view=net-5.0 12 | /// 13 | public class Rethrow : InstructionBase 14 | { 15 | public override void Wykonaj() 16 | { 17 | HardwareContext.ExceptionHandling.FromRethrow(); 18 | 19 | //var block = HardwareContext.AktualnaMetoda.TryCatchStack.PopTryBlock(); 20 | 21 | //HardwareContext.Status = VirtualMachineState.Exception; 22 | //var rzuconyWyjatek = MethodContext.WirtualnaMaszyna.Thread.ThrowedException; 23 | //MethodContext.WirtualnaMaszyna.EventThrowException(rzuconyWyjatek as Exception); 24 | 25 | //Throw.ObslugaRzuconegoWyjatku(MethodContext.WirtualnaMaszyna, rzuconyWyjatek); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Initialization/InitializationInstructionsFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Initialization 7 | { 8 | public class InitializationInstructionsFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "initobj": 15 | return CreateInstruction(instrukcja); 16 | case "newobj": 17 | return CreateInstruction(instrukcja); 18 | case "newarr": 19 | return CreateInstruction(instrukcja); 20 | } 21 | return null; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Initialization/Initobj.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Variables.Addresses; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Initialization 7 | { 8 | /// 9 | /// Initializes each field of the value type at a specified address to a null reference or a 0 of the appropriate primitive type. 10 | /// 11 | public class Initobj : InstructionBase 12 | { 13 | public override void Wykonaj() 14 | { 15 | //throw new NotImplementedException("instrukcja Initobj"); 16 | var ob = Pop(); 17 | 18 | if (ob == null) 19 | { 20 | //nic nie robimy 21 | } 22 | else if (ob is ObjectAddressWraper) 23 | { 24 | //mamy adres do zmiennej 25 | var adres = ob as ObjectAddressWraper; 26 | var zmienna = adres.GetValue(); 27 | 28 | if (zmienna != null) 29 | { 30 | //TODO: coś trzeba zrobić , pewnie zerować wszystkie propercje 31 | var typInstancji = this.Instruction.Operand as Type; 32 | //if (insturkcjaGeneryczna != null && insturkcjaGeneryczna.FullName.StartsWith("System.Nullable")) 33 | //Wcześniej tylko nullable ustawialiśmy na null - teraz wszystko co można nulujemy 34 | if (typInstancji.IsValueType == false) 35 | { 36 | adres.SetNull(); 37 | } 38 | else 39 | { 40 | var v = Activator.CreateInstance(typInstancji); 41 | adres.SetValue(v); 42 | } 43 | } 44 | } 45 | 46 | WykonajNastepnaInstrukcje(); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Initialization/Newarr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Initialization 6 | { 7 | /// 8 | /// Pushes an object reference to a new zero-based, one-dimensional array whose elements are of a specific type onto the evaluation stack. 9 | /// 10 | public class Newarr : InstructionBase 11 | { 12 | public override void Wykonaj() 13 | { 14 | //throw new NotImplementedException("instrukcja Newarr"); 15 | var tr = Instruction.Operand as Type; 16 | //var td = tr; 17 | 18 | var typ = tr; 19 | var n = (int)PopObject(); 20 | object arr = Array.CreateInstance(typ, n); 21 | PushObject(arr); 22 | WykonajNastepnaInstrukcje(); 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/InstructionsFactory.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Cvl.VirtualMachine.Instructions.Calls; 3 | using Cvl.VirtualMachine.Instructions.Special; 4 | using Cvl.VirtualMachine.Instructions.Storage; 5 | using Mono.Reflection; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | 11 | namespace Cvl.VirtualMachine.Instructions 12 | { 13 | public interface IInstructionFactory 14 | { 15 | InstructionBase CreateInstruction(Instruction instrukcja); 16 | VirtualMachine WirtualnaMaszyna { get; set; } 17 | } 18 | 19 | public class InstructionFactory : IInstructionFactory 20 | { 21 | public VirtualMachine WirtualnaMaszyna { get; set; } 22 | 23 | public virtual InstructionBase CreateInstruction(Instruction instrukcja) 24 | { 25 | return null; 26 | } 27 | 28 | public T CreateInstruction(Instruction instruction, Action a = null) 29 | where T : InstructionBase, new() 30 | { 31 | var intst = new T(); 32 | intst.HardwareContext = WirtualnaMaszyna.Thread; 33 | intst.MethodContext = intst.HardwareContext.AktualnaMetoda; 34 | intst.Inicialize(instruction); 35 | if(a != null) 36 | { 37 | a.Invoke(intst); 38 | } 39 | return intst; 40 | } 41 | 42 | public T CreateIndexedInstruction(Instruction instruction, int? index = null) 43 | where T : IndexedInstruction, new() 44 | { 45 | var intst = new T(); 46 | intst.Inicialize(instruction, index); 47 | return intst; 48 | } 49 | } 50 | 51 | public class InstructionsFactory 52 | { 53 | public InstructionBase UtworzInstrukcje(Instruction instrukcja, VirtualMachine wirtualnaMaszyna) 54 | { 55 | if(instructionFactories == null) 56 | { 57 | RegisterInstructionFactory(); 58 | } 59 | 60 | foreach (var facotry in instructionFactories) 61 | { 62 | facotry.WirtualnaMaszyna = wirtualnaMaszyna; 63 | var inst = facotry.CreateInstruction(instrukcja); 64 | if(inst != null) 65 | { 66 | return inst; 67 | } 68 | } 69 | 70 | return null; 71 | } 72 | 73 | 74 | private List instructionFactories = null; 75 | 76 | public void RegisterInstructionFactory() 77 | { 78 | instructionFactories = new List(); 79 | foreach (Type mytype in System.Reflection.Assembly.GetExecutingAssembly().GetTypes() 80 | .Where(mytype => mytype.GetInterfaces().Contains(typeof(IInstructionFactory)))) 81 | { 82 | instructionFactories.Add((IInstructionFactory)Activator.CreateInstance(mytype)); 83 | } 84 | } 85 | 86 | 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Logic/And.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Logic 6 | { 7 | public class And : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a & b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Logic/LogicInstructionFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Logic 7 | { 8 | public class LogicInstructionFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "and": 15 | return CreateInstruction(instrukcja); 16 | case "neg": 17 | return CreateInstruction(instrukcja); 18 | case "or": 19 | return CreateInstruction(instrukcja); 20 | case "xor": 21 | return CreateInstruction(instrukcja); 22 | case "not": 23 | return CreateInstruction(instrukcja); 24 | } 25 | return null; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Logic/Neg.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Logic 6 | { 7 | /// 8 | /// Negates a value and pushes the result onto the evaluation stack. 9 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.neg 10 | /// 11 | public class Neg : InstructionBase 12 | { 13 | public override void Wykonaj() 14 | { 15 | dynamic a = PopObject(); 16 | 17 | //TODO:// sprawdzić czy napewno to jest to czy nie - 18 | dynamic wynik = -a; 19 | PushObject(wynik); 20 | WykonajNastepnaInstrukcje(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Logic/Not.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Logic 6 | { 7 | /// 8 | /// Computes the bitwise complement of the integer value on top of the stack and pushes the result onto the evaluation stack as the same type. 9 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.not?view=netcore-3.1 10 | /// 11 | public class Not : InstructionBase 12 | { 13 | public override void Wykonaj() 14 | { 15 | dynamic a = PopObject(); 16 | 17 | dynamic wynik = !a; 18 | PushObject(wynik); 19 | WykonajNastepnaInstrukcje(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Logic/Or.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Logic 6 | { 7 | public class Or : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a | b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Logic/Xor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Logic 6 | { 7 | public class Xor : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a ^ b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Shift/ShiftInstructionsFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Reflection; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Shift 7 | { 8 | public class ShiftInstructionsFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "shl": 15 | case "shl.un": 16 | return CreateInstruction(instrukcja); 17 | case "shr": 18 | case "shr.un": 19 | return CreateInstruction(instrukcja); 20 | } 21 | return null; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Shift/Shl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Shift 6 | { 7 | class Shl : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a << b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Shift/Shr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Shift 6 | { 7 | class Shr : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | dynamic b = PopObject(); 12 | dynamic a = PopObject(); 13 | 14 | dynamic wynik = a >> b; 15 | PushObject(wynik); 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Special/Nop.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Special 7 | { 8 | 9 | /// 10 | /// Fills space if opcodes are patched. No meaningful operation is performed although a processing cycle can be consumed. 11 | /// 12 | public class Nop : InstructionBase 13 | { 14 | public override void Wykonaj() 15 | { 16 | WykonajNastepnaInstrukcje(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Special/SpecialInstrictionsFactory.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Special 7 | { 8 | public class SpecialInstrictionsFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "nop": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Stacks/Dup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Stacks 6 | { 7 | /// 8 | /// Copies the current topmost value on the evaluation stack, and then pushes the copy onto the evaluation stack. 9 | /// 10 | public class Dup : InstructionBase 11 | { 12 | public override void Wykonaj() 13 | { 14 | var o = PopObject(); 15 | PushObject(o); 16 | PushObject(o); 17 | 18 | WykonajNastepnaInstrukcje(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Stacks/Pop.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Instructions.Special 6 | { 7 | public class Pop : InstructionBase 8 | { 9 | public override void Wykonaj() 10 | { 11 | PopObject(); 12 | WykonajNastepnaInstrukcje(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Stacks/StacksInstructionsFactory.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Special; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Stacks 8 | { 9 | public class StacksInstructionsFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "dup": 16 | return CreateInstruction(instrukcja); 17 | case "pop": 18 | return CreateInstruction(instrukcja); 19 | } 20 | return null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldarg.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class LdargFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "ldarg.0": 16 | return CreateIndexedInstruction(instrukcja, 0); 17 | case "ldarg.1": 18 | return CreateIndexedInstruction(instrukcja, 1); 19 | case "ldarg.2": 20 | return CreateIndexedInstruction(instrukcja, 2); 21 | case "ldarg.3": 22 | return CreateIndexedInstruction(instrukcja, 3); 23 | case "ldarg.s": 24 | return CreateIndexedInstruction(instrukcja); 25 | } 26 | return null; 27 | } 28 | } 29 | 30 | /// 31 | /// Loads an argument (referenced by a specified index value) onto the stack. 32 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.ldarg_s?view=netcore-3.1 33 | /// 34 | public class Ldarg : IndexedInstruction 35 | { 36 | public override void Wykonaj() 37 | { 38 | var o = PobierzLokalnyArgument(Index); 39 | PushObject(o); 40 | WykonajNastepnaInstrukcje(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldarga.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class LdargaFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "ldarga.s": 16 | return CreateIndexedInstruction(instrukcja); 17 | } 18 | return null; 19 | } 20 | } 21 | 22 | /// 23 | /// Load an argument address onto the evaluation stack. 24 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.ldarga 25 | /// 26 | public class Ldarga : IndexedInstruction 27 | { 28 | public override void Wykonaj() 29 | { 30 | var index = GetIndex(); 31 | 32 | var o = PobierzAdresArgumentu(index); 33 | Push(o); 34 | WykonajNastepnaInstrukcje(); 35 | } 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldc.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | /// 10 | /// Pushes a supplied value of type int32 onto the evaluation stack as an int32. 11 | /// https://msdn.microsoft.com/pl-pl/library/system.reflection.emit.opcodes.ldc_i4(v=vs.110).aspx 12 | /// 13 | public class Ldc : InstructionBase 14 | { 15 | public int? ConstValue { get; set; } 16 | public override void Wykonaj() 17 | { 18 | if (ConstValue != null) 19 | { 20 | PushObject(ConstValue); 21 | } 22 | else 23 | { 24 | var constVal = Instruction.Operand; 25 | PushObject(constVal); 26 | } 27 | WykonajNastepnaInstrukcje(); 28 | } 29 | } 30 | 31 | public class LdcFactory : InstructionFactory 32 | { 33 | public override InstructionBase CreateInstruction(Instruction instrukcja) 34 | { 35 | switch (instrukcja.OpCode.Name) 36 | { 37 | case "ldc.i4.m1": 38 | return CreateInstruction(instrukcja, i => i.ConstValue = -1); 39 | case "ldc.i4": 40 | return CreateInstruction(instrukcja, i => i.ConstValue = (int)instrukcja.Operand); 41 | case "ldc.i4.0": 42 | return CreateInstruction(instrukcja, i => i.ConstValue = 0); 43 | case "ldc.i4.1": 44 | return CreateInstruction(instrukcja, i=> i.ConstValue=1); 45 | case "ldc.i4.2": 46 | return CreateInstruction(instrukcja, i => i.ConstValue =2); 47 | case "ldc.i4.3": 48 | return CreateInstruction(instrukcja, i => i.ConstValue =3); 49 | case "ldc.i4.4": 50 | return CreateInstruction(instrukcja, i => i.ConstValue =4); 51 | case "ldc.i4.5": 52 | return CreateInstruction(instrukcja, i => i.ConstValue = 5); 53 | case "ldc.i4.6": 54 | return CreateInstruction(instrukcja, i => i.ConstValue = 6); 55 | case "ldc.i4.7": 56 | return CreateInstruction(instrukcja, i => i.ConstValue = 7); 57 | case "ldc.i4.8": 58 | return CreateInstruction(instrukcja, i => i.ConstValue = 8); 59 | case "ldc.i4.s": 60 | case "ldc.r8": 61 | case "ldc.i8": 62 | case "ldc.r4": 63 | return CreateInstruction(instrukcja); 64 | 65 | } 66 | 67 | if(instrukcja.OpCode.Name.Contains("ldc.i4")) 68 | { 69 | 70 | var vs = instrukcja.OpCode.Name.Split('.').Last(); 71 | var v = int.Parse(vs); 72 | 73 | return CreateInstruction(instrukcja, i => i.ConstValue = v); 74 | } 75 | 76 | 77 | return null; 78 | } 79 | } 80 | 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldelem.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdelemFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldelem"://TODO:Testy 15 | case "ldelem.i": 16 | case "ldelem.i2": 17 | case "ldelem.i4": 18 | case "ldelem.i8": 19 | case "ldelem.r4": 20 | case "ldelem.r8": 21 | case "ldelem.ref": 22 | case "ldelem.u1": 23 | case "ldelem.u2": 24 | case "ldelem.u4": 25 | case "ldelem.u8": 26 | return CreateInstruction(instrukcja); 27 | } 28 | return null; 29 | } 30 | } 31 | 32 | /// 33 | /// Loads the element at a specified array index onto the top of the evaluation stack as the type specified in the instruction. 34 | /// https://docs.microsoft.com/pl-pl/dotnet/api/system.reflection.emit.opcodes.ldelem?view=net-5.0 35 | /// 36 | public class Ldelem : InstructionBase 37 | { 38 | 39 | public override void Wykonaj() 40 | { 41 | //pobieram obiekt ze stosu 42 | var index = (int)PopObject(); 43 | var ar = PopObject() as Array; 44 | 45 | var v = ar.GetValue(index); 46 | PushObject(v); 47 | 48 | WykonajNastepnaInstrukcje(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldfld.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class LdfldFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "ldfld": 16 | return CreateInstruction(instrukcja); 17 | } 18 | return null; 19 | } 20 | } 21 | 22 | /// 23 | /// Finds the value of a field in the object whose reference is currently on the evaluation stack. 24 | /// 25 | public class Ldfld : InstructionBase 26 | { 27 | 28 | public override void Wykonaj() 29 | { 30 | //pobieram obiekt ze stosu 31 | var obj = PopObject(); 32 | 33 | //szukam pola 34 | var field = (System.Reflection.FieldInfo)Instruction.Operand; 35 | var val = field.GetValue(obj); 36 | 37 | //kładę na stos wartość pola 38 | PushObject(val); 39 | 40 | WykonajNastepnaInstrukcje(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldflda.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Variables.Addresses; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class LdfldaFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "ldflda": 16 | return CreateInstruction(instrukcja); 17 | } 18 | return null; 19 | } 20 | } 21 | 22 | /// 23 | /// Finds the address of a field in the object whose reference is currently on the evaluation stack. 24 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.ldflda 25 | /// 26 | public class Ldflda : InstructionBase 27 | { 28 | 29 | public override void Wykonaj() 30 | { 31 | //pobieram obiekt ze stosu 32 | var obj = PopObject(); 33 | 34 | //szukam pola 35 | var field = (System.Reflection.FieldInfo)Instruction.Operand; 36 | 37 | var fieldAddress = new FieldAddress(); 38 | fieldAddress.Field = field; 39 | fieldAddress.Object = obj; 40 | 41 | //kładę na stos wartość pola 42 | Push(fieldAddress); 43 | 44 | WykonajNastepnaInstrukcje(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldftn.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdftnFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldftn": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | 21 | /// 22 | /// Pushes an unmanaged pointer (type native int) to the native code implementing a specific method onto the evaluation stack. 23 | /// https://msdn.microsoft.com/pl-pl/library/system.reflection.emit.opcodes.ldftn(v=vs.110).aspx 24 | /// 25 | public class Ldftn : InstructionBase 26 | { 27 | public override void Wykonaj() 28 | { 29 | var methodDefiniton = Instruction.Operand as System.Reflection.MethodInfo; 30 | 31 | PushObject(methodDefiniton.MethodHandle.GetFunctionPointer()); 32 | WykonajNastepnaInstrukcje(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldind.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdindFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldind"://TODO:Testy 15 | case "ldind.i": 16 | case "ldind.i2": 17 | case "ldind.i4": 18 | case "ldind.i8": 19 | case "ldind.r4": 20 | case "ldind.r8": 21 | case "ldind.ref": 22 | case "ldind.u1": 23 | case "ldind.u2": 24 | case "ldind.u4": 25 | case "ldind.u8": 26 | return CreateInstruction(instrukcja); 27 | } 28 | return null; 29 | } 30 | } 31 | 32 | /// 33 | /// load value of type native int into memory at address. 34 | /// https://en.wikipedia.org/wiki/List_of_CIL_instructions 35 | /// 36 | public class Ldind : InstructionBase 37 | { 38 | 39 | public override void Wykonaj() 40 | { 41 | WykonajNastepnaInstrukcje(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldlen.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdlenFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldlen"://TODO:Testy 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | 21 | /// 22 | /// Pushes the number of elements of a zero-based, one-dimensional array onto the evaluation stack. 23 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.ldlen 24 | /// 25 | public class Ldlen : InstructionBase 26 | { 27 | 28 | public override void Wykonaj() 29 | { 30 | //pobieram obiekt ze stosu 31 | var ar = PopObject() as Array; 32 | var v= ar.Length; 33 | PushObject(v); 34 | 35 | WykonajNastepnaInstrukcje(); 36 | } 37 | } 38 | 39 | class ldlen 40 | { 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldloc.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class LdlocFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "ldloc": 16 | case "ldloc.s": 17 | return CreateIndexedInstruction(instrukcja); 18 | case "ldloc.0": 19 | return CreateIndexedInstruction(instrukcja, 0); 20 | case "ldloc.1": 21 | return CreateIndexedInstruction(instrukcja, 1); 22 | case "ldloc.2": 23 | return CreateIndexedInstruction(instrukcja, 2); 24 | case "ldloc.3": 25 | return CreateIndexedInstruction(instrukcja, 3); 26 | } 27 | return null; 28 | } 29 | } 30 | 31 | /// 32 | /// Loads the local variable at a specific index onto the evaluation stack. 33 | /// 34 | public class Ldloc : IndexedInstruction 35 | { 36 | 37 | public override void Wykonaj() 38 | { 39 | 40 | var a = Instruction.Operand as System.Reflection.LocalVariableInfo; 41 | if (a != null) 42 | { 43 | var o = PobierzLokalnaZmienna(a.LocalIndex); 44 | PushObject(o); 45 | } 46 | else 47 | { 48 | var o = PobierzLokalnaZmienna(Index); 49 | PushObject(o); 50 | } 51 | WykonajNastepnaInstrukcje(); 52 | } 53 | 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldloca.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class LdlocaFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "ldloca": 16 | case "ldloca.s": 17 | return CreateIndexedInstruction(instrukcja); 18 | } 19 | return null; 20 | } 21 | } 22 | 23 | /// 24 | /// Loads the address of the local variable at a specific index onto the evaluation stack. 25 | /// 26 | public class Ldloca : IndexedInstruction 27 | { 28 | public override void Wykonaj() 29 | { 30 | var index = GetIndex(); 31 | 32 | var o = PobierzAdresZmiennejLokalnej(index); 33 | Push(o); 34 | WykonajNastepnaInstrukcje(); 35 | } 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldobj.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdobjFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldobj": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | 21 | /// 22 | /// Kopiuje obiekt typu wartości wskazywany przez adres na początku stosu oceny. 23 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.ldobj?view=net-5.0 24 | /// 25 | public class Ldobj : InstructionBase 26 | { 27 | public override void Wykonaj() 28 | { 29 | //TODO: napisać test 30 | //tu nic nie musimy robić - C# sam sobie to obsłuży 31 | WykonajNastepnaInstrukcje(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldsfld.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdsfldFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldsfld": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | 21 | /// 22 | /// Pushes the value of a static field onto the evaluation stack. 23 | /// https://msdn.microsoft.com/pl-pl/library/system.reflection.emit.opcodes.ldsfld(v=vs.110).aspx 24 | /// 25 | public class Ldsfld : InstructionBase 26 | { 27 | public override void Wykonaj() 28 | { 29 | //pobieram statyczną zmienną 30 | var fieldDefinition = Instruction.Operand as System.Reflection.FieldInfo; 31 | var val = fieldDefinition.GetValue(null); 32 | //var typ = fieldDefinition.DeclaringType;//.GetSystemType(); 33 | //var field = typ.GetField(fieldDefinition.Name); 34 | //var val = field.GetValue(null); 35 | 36 | PushObject(val); 37 | WykonajNastepnaInstrukcje(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldstr.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdstrFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldstr": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | 21 | public class Ldstr : InstructionBase 22 | { 23 | public override void Wykonaj() 24 | { 25 | string str = Instruction.Operand as string; 26 | PushObject(str); 27 | WykonajNastepnaInstrukcje(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Ldtoken.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Mono.Reflection; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdtokenFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldtoken": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | 21 | /// 22 | /// Converts a metadata token to its runtime representation, pushing it onto the evaluation stack. 23 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.ldtoken 24 | /// 25 | public class Ldtoken : InstructionBase 26 | { 27 | public override void Wykonaj() 28 | { 29 | if (Instruction.Operand is Type typ) 30 | { 31 | PushObject(typ.TypeHandle); 32 | } 33 | else if(Instruction.Operand is System.Reflection.FieldInfo fieldInfo) 34 | { 35 | PushObject(fieldInfo.FieldHandle); 36 | } 37 | else 38 | { 39 | throw new NotImplementedException($"Ldtoken: operand in not known type {Instruction.Operand.ToString()}"); 40 | } 41 | WykonajNastepnaInstrukcje(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Starg.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class StargFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "starg.s": 16 | return CreateIndexedInstruction(instrukcja); 17 | } 18 | return null; 19 | } 20 | } 21 | 22 | /// 23 | /// Stores the value on top of the evaluation stack in the argument slot at a specified index. 24 | /// 25 | public class Starg : IndexedInstruction 26 | { 27 | public override void Wykonaj() 28 | { 29 | var o = PopObject(); 30 | ZapiszLokalnyArgument(o, Index); 31 | WykonajNastepnaInstrukcje(); 32 | } 33 | 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Stelem.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class StelemFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "stelem"://TODO:Testy 15 | case "stelem.i": 16 | case "stelem.i2": 17 | case "stelem.i4": 18 | case "stelem.i8": 19 | case "stelem.r4": 20 | case "stelem.r8": 21 | case "stelem.ref": 22 | return CreateInstruction(instrukcja); 23 | } 24 | return null; 25 | } 26 | } 27 | 28 | /// 29 | /// Zastępuje element Array w danym indeksie wartością na stosie ewaluacyjnym, którego typ jest określony w instrukcji. 30 | /// https://docs.microsoft.com/pl-pl/dotnet/api/system.reflection.emit.opcodes.Stelem?view=net-5.0 31 | /// 32 | public class Stelem : InstructionBase 33 | { 34 | 35 | public override void Wykonaj() 36 | { 37 | //pobieram obiekt ze stosu 38 | var v = PopObject(); 39 | var index = (int)PopObject(); 40 | var ar = PopObject() as Array; 41 | 42 | ar.SetValue(v, index); 43 | 44 | WykonajNastepnaInstrukcje(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Stfld.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class StfldFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "stfld": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | 21 | /// 22 | /// Replaces the value stored in the field of an object reference or pointer with a new value. 23 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.stfld?view=netframework-4.8 24 | /// 25 | public class Stfld : InstructionBase 26 | { 27 | 28 | public override void Wykonaj() 29 | { 30 | //pobieram vartosc ze stosu 31 | var objVal = PopObject(); 32 | var obj = PopObject(); 33 | 34 | var field = (System.Reflection.FieldInfo)Instruction.Operand; 35 | field.SetValue(obj, objVal); 36 | 37 | WykonajNastepnaInstrukcje(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Stind.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class StindFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "stind"://TODO:Testy 15 | case "stind.i": 16 | case "stind.i1": 17 | case "stind.i2": 18 | case "stind.i4": 19 | case "stind.i8": 20 | case "stind.r4": 21 | case "stind.r8": 22 | case "stind.ref": 23 | return CreateInstruction(instrukcja); 24 | } 25 | return null; 26 | } 27 | } 28 | 29 | /// 30 | ///Store value of type native int into memory at address. 31 | /// https://en.wikipedia.org/wiki/List_of_CIL_instructions 32 | /// 33 | public class Stind : InstructionBase 34 | { 35 | 36 | public override void Wykonaj() 37 | { 38 | WykonajNastepnaInstrukcje(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Stloc.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class StlocFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "stloc": 16 | case "stloc.s": 17 | return CreateIndexedInstruction(instrukcja); 18 | case "stloc.0": 19 | return CreateIndexedInstruction(instrukcja, 0); 20 | case "stloc.1": 21 | return CreateIndexedInstruction(instrukcja, 1); 22 | case "stloc.2": 23 | return CreateIndexedInstruction(instrukcja, 2); 24 | case "stloc.3": 25 | return CreateIndexedInstruction(instrukcja, 3); 26 | } 27 | return null; 28 | } 29 | } 30 | 31 | /// 32 | /// Pops the current value from the top of the evaluation stack and stores it in a the local variable list at a specified index. 33 | /// 34 | public class Stloc : IndexedInstruction 35 | { 36 | 37 | public override void Wykonaj() 38 | { 39 | var o = PopObject(); 40 | 41 | var a = Instruction.Operand as System.Reflection.LocalVariableInfo; 42 | if (a != null) 43 | { 44 | ZapiszLokalnaZmienna(o, a.LocalIndex); 45 | } 46 | else 47 | { 48 | ZapiszLokalnaZmienna(o, Index); 49 | } 50 | WykonajNastepnaInstrukcje(); 51 | } 52 | 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Stobj.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Instructions.Base; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class StobjFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "stobj": 16 | case "stobj.s": 17 | return CreateInstruction(instrukcja); 18 | } 19 | return null; 20 | } 21 | } 22 | 23 | /// 24 | /// Copies a value of a specified type from the evaluation stack into a supplied memory address. 25 | /// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.opcodes.stobj?view=net-5.0 26 | /// 27 | public class Stobj : IndexedInstruction 28 | { 29 | 30 | public override void Wykonaj() 31 | { 32 | //nic nie robię 33 | WykonajNastepnaInstrukcje(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/Stsfld.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | using System.Text; 6 | 7 | namespace Cvl.VirtualMachine.Instructions.Storage 8 | { 9 | public class StsfldFactory : InstructionFactory 10 | { 11 | public override InstructionBase CreateInstruction(Instruction instrukcja) 12 | { 13 | switch (instrukcja.OpCode.Name) 14 | { 15 | case "stsfld": 16 | return CreateInstruction(instrukcja); 17 | } 18 | return null; 19 | } 20 | } 21 | 22 | /// 23 | /// Replaces the value of a static field with a value from the evaluation stack. 24 | /// https://msdn.microsoft.com/pl-pl/library/system.reflection.emit.opcodes.stsfld(v=vs.110).aspx 25 | /// 26 | public class Stsfld : InstructionBase 27 | { 28 | public override void Wykonaj() 29 | { 30 | var o = PopObject(); 31 | 32 | //ustawiam statyczną zmienną wartością ze stosu 33 | var fieldDefinition = Instruction.Operand as FieldInfo; 34 | 35 | //var typ = fieldDefinition.DeclaringType.GetSystemType(); 36 | // var field = typ.GetField(fieldDefinition.Name); 37 | fieldDefinition.SetValue(null, o); 38 | 39 | WykonajNastepnaInstrukcje(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Instructions/Storage/ldnull.cs: -------------------------------------------------------------------------------- 1 | using Mono.Reflection; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Instructions.Storage 7 | { 8 | public class LdnullFactory : InstructionFactory 9 | { 10 | public override InstructionBase CreateInstruction(Instruction instrukcja) 11 | { 12 | switch (instrukcja.OpCode.Name) 13 | { 14 | case "ldnull": 15 | return CreateInstruction(instrukcja); 16 | } 17 | return null; 18 | } 19 | } 20 | public class Ldnull : InstructionBase 21 | { 22 | public override void Wykonaj() 23 | { 24 | PushObject(null); 25 | WykonajNastepnaInstrukcje(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Test/TestProces.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Core.Attributes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Cvl.VirtualMachine.Test 7 | { 8 | public class ProcesTest 9 | { 10 | public string JakasPropercja { get; set; } 11 | private int zmienna = 4; 12 | public int Wynik { get; set; } 13 | 14 | public int Start() 15 | { 16 | Wynik= Funkcja1(1, 2, "3"); 17 | return Wynik; 18 | } 19 | 20 | 21 | [Interpret] 22 | public int Funkcja1(int p1, int p2, string p3) 23 | { 24 | var zmienalokaln1 = p1; 25 | var zmienalokalna2 = p2; 26 | var zmiennaLokaln2 = p2.ToString() + p1.ToString() + zmienalokalna2; 27 | 28 | Metoda1(); 29 | Metoda2(4, new decimal(399.99)); 30 | 31 | var zmienna3 = zmienalokaln1 + zmiennaLokaln2; 32 | 33 | return zmienna + p1 + p2.ToString().Length + zmienna3.Length; 34 | } 35 | 36 | [Interpret] 37 | public void Metoda1() 38 | { 39 | zmienna += 3; 40 | } 41 | 42 | [Interpret] 43 | public void Metoda2(decimal ilosc, decimal cenaJedn) 44 | { 45 | var wartosc = ilosc * cenaJedn; 46 | zmienna += (int)wartosc; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/Test/VirtualMachineDebug.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Cvl.VirtualMachine.Test 6 | { 7 | public class VirtualMachineDebug 8 | { 9 | public static VirtualMachine VirtualMachine; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Cvl.VirtualMachine/Cvl.VirtualMachine/cvlogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cv-lang/virtual-machine/f8dbc173096e59894ba6bf03c44b20a7d300a8af/Cvl.VirtualMachine/Cvl.VirtualMachine/cvlogo.png -------------------------------------------------------------------------------- /Cvl.VirtualMachine/TestApp/Program.cs: -------------------------------------------------------------------------------- 1 | using Cvl.VirtualMachine.Test; 2 | using Mono.Reflection; 3 | using System; 4 | using System.Windows; 5 | using Cvl.VirtualMachine.Core.Tools; 6 | using Cvl.VirtualMachine.UnitTest.Proces.FromLife; 7 | 8 | namespace Cvl.VirtualMachine.TestApp 9 | { 10 | 11 | class Program 12 | { 13 | [STAThread] 14 | static void Main(string[] args) 15 | { 16 | 17 | 18 | 19 | //var factoryLogger = new ApplicationServer.Logs.Factory.LoggerFactory(new ApplicationServer.Logs.Storage.FileLogStorage(), "test-vm"); 20 | 21 | var p = new StringConstrudtionTestProcess(); 22 | 23 | var vm = new Cvl.VirtualMachine.VirtualMachine(); 24 | vm.ActionToExecute(() => 25 | { 26 | p.Start(); 27 | }); 28 | 29 | //using (vm.Logger = factoryLogger.GetLogger()) 30 | { 31 | 32 | vm.LogMonitor = new ConsoleLogMonitor(); 33 | vm.InterpreteFullNameTypes = "Cvl.VirtualMachine.Test"; 34 | 35 | 36 | var app = new Application(); 37 | var debuggerVM = new Debugger.Views.Debugger.DebuggerVM(); 38 | debuggerVM.VirtualMachine = vm; 39 | var window = new Cvl.VirtualMachine.Debugger.MainWindow(); 40 | window.DataContext = debuggerVM; 41 | app.Run(window); 42 | 43 | 44 | 45 | } 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Cvlang Organization 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # .NET virtual-machine 2 | virtualmachine-logo 3 | 4 | .NET Virtual machine written in C #. It allows you to perform the compiled .net code (IL). The code is executed instruction by instruction. (Virtual machine emulates all instructions from IL). 5 | Execute code can be hibernated and then restarted (even on another machine after serialization). 6 | 7 | 8 | ## Allow to: 9 | - Application checkpointing - allowing to stop execution (suspending/hibernation) save execution state to disk and later load state from disk and resume execution. 10 | - Hibernation and process recovery 11 | - Thread serialization 12 | 13 | ## Example 14 | 15 | ```CSharp 16 | public class HibernateWorkflow 17 | { 18 | public int InputParametr { get; set; } 19 | public string Start() 20 | { 21 | //do some work 22 | for (int i = 0; i < 10; i++) 23 | { 24 | SomeInterpretedFunction(); 25 | } 26 | 27 | //after restore (in another thread/computer) 28 | //do some work 29 | for (int i = 0; i < 10; i++) 30 | { 31 | SomeInterpretedFunction(); 32 | } 33 | 34 | return "Helow World " + InputParametr; 35 | } 36 | 37 | [Interpret] 38 | public void SomeInterpretedFunction() 39 | { 40 | //do some work 41 | InputParametr++; 42 | 43 | //hibernate executed method 44 | VirtualMachine.VirtualMachine.Hibernate(); 45 | } 46 | } 47 | ``` 48 | 49 | launching code 50 | ```CSharp 51 | var proces = new HibernateWorkflowSimple() { InputParametr = 10 }; 52 | var vm = new VirtualMachine.VirtualMachine(); 53 | vm.Start(proces); 54 | var serializedVMXml = VirtualMachine.SerializeVirtualMachine(vm); 55 | object retFromSerializedVM = ""; 56 | 57 | while (vm.Status == VirtualMachineState.Hibernated) 58 | { 59 | vm = VirtualMachine.DeserializeVirtualMachine(serializedVMXml); 60 | retFromSerializedVM = vm.Resume(); 61 | serializedVMXml = vm.Serialize(); 62 | } 63 | 64 | var inProcProces = new HibernateWorkflowSimple() { InputParametr = 10 }; 65 | var retInProcProces = inProcProces.Start(); 66 | Assert.AreEqual(retInProcProces, retFromSerializedVM); 67 | ``` 68 | 69 | ## NuGet 'Cvl.VirtualMachine' 70 | PM> Install-Package Cvl.VirtualMachine -Version 0.9.1 71 | 72 | [NuGet package Cvl.VirtualMachine](https://www.nuget.org/packages/Cvl.VirtualMachine/) 73 | 74 | ## Dependencies 75 | 76 | [Mono.Reflection](https://github.com/jbevain/mono.reflection) 77 | 78 | [SharpSerializer](http://sharpserializer.com/en/index.html) 79 | 80 | --------------------------------------------------------------------------------