private static void AssertGetBytes <TInstruction>(Opcode opcode, params object[] args) where TInstruction : IInstruction, new()
        {
            var instruction   = new TInstruction();
            var expectedBytes = InstructionByteBuilder.Create().Opcode(opcode, args).AsSpan();

            Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes()));
        }
Esempio n. 2
0
        private static void AssertCreatedInstruction <TInstruction>(Opcode opcode, params object[] args) where TInstruction : IInstruction
        {
            var bytes = InstructionByteBuilder.Create()
                        .Opcode(opcode, args)
                        .ToArray();

            AssertBytesDecodedAs <TInstruction>(bytes);
        }
        public void GetBytes_LoadStringInstruction()
        {
            var instruction   = new LoadStringInstruction("helloWorld");
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Load, LoadKind.String, "helloWorld")
                                .AsSpan();

            Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes()));
        }
        public void GetBytes_LoadLibraryInstruction()
        {
            var instruction   = new LoadLibraryInstruction("MyLib");
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Load, LoadKind.Library, "MyLib")
                                .AsSpan();

            Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes()));
        }
        public void GetBytes_CompareInstruction(CompareKind compareKind)
        {
            var instruction   = new CompareInstruction(compareKind);
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Compare, compareKind)
                                .AsSpan();

            Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes()));
        }
        public void GetBytes_BranchInstruction(BranchKind branchKind)
        {
            var instruction   = new BranchInstruction(branchKind, 42);
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Branch, branchKind, 42)
                                .AsSpan();

            Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes()));
        }
        public void GetBytes_LoadConstantInstruction(LoadKind loadKind, object value)
        {
            var instruction   = new LoadConstantInstruction(loadKind, value);
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Load, loadKind, value)
                                .AsSpan();

            Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes()));
        }
Esempio n. 8
0
        public void Decode_CompareInstruction_FromBytesArray(CompareKind compareKind)
        {
            var bytes = InstructionByteBuilder.Create()
                        .Opcode(Opcode.Compare, compareKind)
                        .ToArray();
            var compareInstruction = AssertBytesDecodedAs <CompareInstruction>(bytes);

            Assert.Equal(compareKind, compareInstruction.CompareKind);
        }
Esempio n. 9
0
        public void Decode_BranchInstruction_FromByteArray(BranchKind branchKind)
        {
            var bytes = InstructionByteBuilder.Create()
                        .Opcode(Opcode.Branch, branchKind, 42)
                        .ToArray();

            var branchInstruction = AssertBytesDecodedAs <BranchInstruction>(bytes);

            Assert.Equal(branchKind, branchInstruction.BranchKind);
        }
Esempio n. 10
0
        public void GetBytes_CallInstruction()
        {
            const string methodCall    = "void HelloWorld()";
            var          instruction   = new CallInstruction(methodCall);
            var          expectedBytes = InstructionByteBuilder.Create()
                                         .Opcode(Opcode.Call, methodCall)
                                         .AsSpan();

            Assert.True(expectedBytes.SequenceEqual(instruction.GetBytes()));
        }
Esempio n. 11
0
        public void GetBytes_LoadPlaceInstruction(LoadKind loadKind)
        {
            var instruction   = new LoadPlaceInstruction(loadKind, (byte)0x03);
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Load, loadKind, (byte)0x03)
                                .AsSpan();

            var actualBytes = instruction.GetBytes();

            Assert.True(expectedBytes.SequenceEqual(actualBytes));
        }
Esempio n. 12
0
        public void Decode_LoadLibraryInstruction_FromByteArray()
        {
            var bytes = InstructionByteBuilder.Create()
                        .Opcode(Opcode.Load, LoadKind.Library, "MyLibraryCore")
                        .ToArray();

            var loadInstruction = AssertBytesDecodedAs <LoadLibraryInstruction>(bytes);

            Assert.Equal(LoadKind.Library, loadInstruction.LoadKind);
            Assert.Equal("MyLibraryCore", loadInstruction.LibraryName);
        }
Esempio n. 13
0
        public void Decode_LoadConstantInstruction_FromByteArray(LoadKind loadKind, object expectedValue)
        {
            var bytes = InstructionByteBuilder.Create()
                        .Opcode(Opcode.Load, loadKind, expectedValue)
                        .ToArray();

            var loadInstruction = AssertBytesDecodedAs <LoadConstantInstruction>(bytes);

            Assert.Equal(loadKind, loadInstruction.LoadKind);
            Assert.Equal(expectedValue, loadInstruction.Value);
        }
Esempio n. 14
0
        public void Decode_CallInstruction_FromByteArray()
        {
            const string methodCall = "void HelloWorld()";
            var          bytes      = InstructionByteBuilder.Create()
                                      .Opcode(Opcode.Call, methodCall)
                                      .ToArray();

            var callInstruction = AssertBytesDecodedAs <CallInstruction>(bytes);

            Assert.Equal(methodCall, callInstruction.MethodName);
        }
Esempio n. 15
0
        public void GetBytes_StoreInstruction(StoreKind storeKind)
        {
            var instruction   = new StoreInstruction(storeKind, (byte)0x03);
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Store, storeKind, (byte)0x03)
                                .AsSpan();

            var actualBytes = instruction.GetBytes();

            Assert.True(expectedBytes.SequenceEqual(actualBytes));
        }
Esempio n. 16
0
        public void Decode_LoadString_FromByteArray()
        {
            const string value = "helloWorld";
            var          bytes = InstructionByteBuilder.Create()
                                 .Opcode(Opcode.Load, LoadKind.String, value)
                                 .ToArray();

            var loadInstruction = AssertBytesDecodedAs <LoadStringInstruction>(bytes);

            Assert.Equal(LoadKind.String, loadInstruction.LoadKind);
            Assert.Equal(value, loadInstruction.Value);
        }
Esempio n. 17
0
        public void Decode_StoreInstructionFromByteArray_RegistryIndexAndStoreTypeAreSet()
        {
            const StoreKind expectedStoreType = StoreKind.Field;
            const byte      expectedIndex     = 0x05;
            var             bytes             = InstructionByteBuilder.Create()
                                                .Opcode(Opcode.Store, expectedStoreType, expectedIndex)
                                                .ToArray();

            var storeInstruction = AssertBytesDecodedAs <StoreInstruction>(bytes);

            Assert.Equal(expectedIndex, storeInstruction.Index);
            Assert.Equal(expectedStoreType, storeInstruction.StoreKind);
        }
Esempio n. 18
0
        public void Decode_LoadPlaceInstruction_FromByteArray_RegistryIndexAndLoadTypeAreSet()
        {
            const LoadKind expectedLoadType = LoadKind.StaticField;
            const byte     expectedIndex    = 0x07;
            var            bytes            = InstructionByteBuilder.Create()
                                              .Opcode(Opcode.Load, expectedLoadType, expectedIndex)
                                              .ToArray();

            var loadInstruction = AssertBytesDecodedAs <LoadPlaceInstruction>(bytes);

            Assert.Equal(expectedIndex, loadInstruction.Index);
            Assert.Equal(expectedLoadType, loadInstruction.LoadKind);
        }
Esempio n. 19
0
        public void DecodeStream_StreamWithMultipleOpcode_DecodesInstructions()
        {
            var bytesStream = InstructionByteBuilder.Create()
                              .Opcode(Opcode.Load, LoadKind.Integer, 42)
                              .Opcode(Opcode.Load, LoadKind.Integer, 69)
                              .Opcode(Opcode.Add)
                              .Opcode(Opcode.Return)
                              .AsStream();

            var instructions = InstructionDecoder.DecodeStream(bytesStream).ToList();

            Assert.Equal(4, instructions.Count);
            Assert.Equal("add", instructions.ElementAt(2).ToString());
            Assert.Equal("ret", instructions.ElementAt(3).ToString());
        }
Esempio n. 20
0
        public void Disassemble()
        {
            var bytesStream = InstructionByteBuilder.Create()
                              .Opcode(Opcode.Load, LoadKind.Integer, 42)
                              .Opcode(Opcode.Load, LoadKind.Integer, 69)
                              .Opcode(Opcode.Add)
                              .Opcode(Opcode.Return)
                              .AsStream();

            var disassembler = new Disassembler();
            var disassembled = disassembler.Disassemble(bytesStream).ToArray();

            Assert.Equal(4, disassembled.Length);
            Assert.Equal("add", disassembled[2]);
            Assert.Equal("ret", disassembled[3]);
        }
Esempio n. 21
0
        public void Assemble_ToExpectedFormat()
        {
            var expectedBytes = InstructionByteBuilder.Create()
                                .Opcode(Opcode.Load, LoadKind.Integer, 42)
                                .Opcode(Opcode.Load, LoadKind.Integer, 69)
                                .Opcode(Opcode.Add)
                                .Opcode(Opcode.Return)
                                .ToArray();

            var instructions = new List <IInstruction> {
                new LoadConstantInstruction(LoadKind.Integer, 42),
                new LoadConstantInstruction(LoadKind.Integer, 69),
                new AddInstruction(),
                new ReturnInstruction()
            };

            var assembler = new Assembler();

            using var outputStream = new MemoryStream();
            assembler.Assemble(outputStream, instructions);

            Assert.Equal(expectedBytes, outputStream.ToArray());
        }
Esempio n. 22
0
        public void Decode_NotSupportedOpcode_ThrowOpcodeReadException(Opcode opcode)
        {
            var bytes = InstructionByteBuilder.Create().Opcode(opcode).ToArray();

            Assert.Throws <OpcodeDecodeException>(() => InstructionDecoder.Decode(bytes));
        }