Esempio n. 1
0
        public byte[] Serialize(BuildingContext context)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new BigEndianStreamWriter(stream);

                writer.Write(MaxStack);
                writer.Write(MaxLocals);
                writer.Write(Code.Length);
                writer.Write(Code);

                writer.Write((ushort)ExceptionHandlers.Count);
                foreach (var handler in ExceptionHandlers)
                {
                    handler.Write(writer);
                }

                writer.Write((ushort)Attributes.Count);
                var writingContext = new WritingContext(writer);
                foreach (var attribute in Attributes)
                {
                    attribute.Write(writingContext);
                }

                return(stream.ToArray());
            }
        }
Esempio n. 2
0
 /// <inheritdoc />
 public byte[] Serialize(BuildingContext context)
 {
     using (var stream = new MemoryStream())
     {
         var writer = new BigEndianStreamWriter(stream);
         writer.Write((ushort)LocalVariables.Count);
         foreach (var variable in LocalVariables)
         {
             variable.Write(writer);
         }
         return(stream.ToArray());
     }
 }
 /// <inheritdoc />
 public byte[] Serialize(BuildingContext context)
 {
     using (var stream = new MemoryStream())
     {
         var writer = new BigEndianStreamWriter(stream);
         writer.Write((ushort)BootstrapMethods.Count);
         foreach (var method in BootstrapMethods)
         {
             method.Write(writer);
         }
         return(stream.ToArray());
     }
 }
        public void NoChange()
        {
            var reader = new MemoryBigEndianReader(Resources.HelloWorld);
            var classFile = JavaClassFile.FromReader(reader);

            using (var stream = new MemoryStream())
            {
                var writer = new BigEndianStreamWriter(stream);
                classFile.Write(new WritingContext(writer));
                Assert.Equal(Resources.HelloWorld, stream.ToArray());
            }

        }
Esempio n. 5
0
        /// <summary>
        /// Builds the class image and verifies the output of the file.
        /// </summary>
        /// <param name="image">The class to compile.</param>
        /// <param name="expectedOutput">The expected output of the file.</param>
        /// <param name="regex">Specifies if <paramref name="expectedOutput"/> should be interpreted as a regular expression pattern.</param>
        public void WriteAndVerifyOutput(JavaClassFile classFile, string expectedOutput, bool regex = false)
        {
            var    classInfo = (ClassInfo)classFile.ConstantPool.ResolveConstant(classFile.ThisClass);
            string name      = classFile.ConstantPool.ResolveString(classInfo.NameIndex);

            using (var fs = File.Create(Path.Combine(OutputDirectory, name + ".class")))
            {
                var writer  = new BigEndianStreamWriter(fs);
                var context = new WritingContext(writer);
                classFile.Write(context);
            }

            VerifyOutput(name, expectedOutput, regex);
        }
        public void WriteTest()
        {
            using (var stream = new MemoryStream())
            {
                var writer = new BigEndianStreamWriter(stream);

                writer.Write((byte)8);
                writer.Write((ushort)8);
                writer.Write((uint)8);
                writer.Write((ulong)8);

                Assert.Equal(Bytes, stream.ToArray());
            }
        }
Esempio n. 7
0
        /// <inheritdoc />
        public byte[] Serialize(BuildingContext context)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new BigEndianStreamWriter(stream);

                writer.Write((ushort)InnerClasses.Count);
                foreach (var @class in InnerClasses)
                {
                    @class.Write(writer);
                }

                return(stream.ToArray());
            }
        }
        /// <inheritdoc />
        public byte[] Serialize(BuildingContext context)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new BigEndianStreamWriter(stream);
                writer.Write((ushort)Exceptions.Count);

                foreach (var exception in Exceptions)
                {
                    writer.Write(exception);
                }

                return(stream.ToArray());
            }
        }
Esempio n. 9
0
        /// <inheritdoc />
        public byte[] Serialize(BuildingContext context)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new BigEndianStreamWriter(stream);
                writer.Write((ushort)Entries.Count);
                foreach (var entry in Entries)
                {
                    writer.Write(entry.Key);
                    writer.Write(entry.Value);
                }

                return(stream.ToArray());
            }
        }
Esempio n. 10
0
        private byte[] GenerateRawCode(BuildingContext context)
        {
            byte[] code = null;
            using (var stream = new MemoryStream())
            {
                var writer    = new BigEndianStreamWriter(stream);
                var assembler = new ByteCodeAssembler(writer)
                {
                    OperandBuilder = new DefaultOperandBuilder(context.Builder)
                };

                Instructions.CalculateOffsets();
                foreach (var instruction in Instructions)
                {
                    assembler.Write(instruction);
                }

                code = stream.ToArray();
            }

            return(code);
        }