Ejemplo n.º 1
0
        /// <summary>
        /// Reads the child instructions of a WebAssembly block from the given reader.
        /// </summary>
        /// <param name="blockType">The type of value returned by the resulting block.</param>
        /// <param name="reader">The WebAssembly file reader.</param>
        /// <returns>A WebAssembly block instruction.</returns>
        public static IfElseInstruction ReadBlockContents(WasmType blockType, BinaryWasmReader reader)
        {
            var ifBranch = new List <Instruction>();
            List <Instruction> elseBranch = null;

            while (true)
            {
                byte opCode = reader.ReadByte();
                if (opCode == Operators.EndOpCode)
                {
                    return(new IfElseInstruction(blockType, ifBranch, elseBranch));
                }
                else if (opCode == Operators.ElseOpCode)
                {
                    if (elseBranch != null)
                    {
                        throw new WasmException("More than one 'else' opcode in an 'if' instruction");
                    }

                    elseBranch = new List <Instruction>();
                }
                else
                {
                    var op = Operators.GetOperatorByOpCode(opCode);
                    (elseBranch == null ? ifBranch : elseBranch).Add(op.ReadImmediates(reader));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads the child instructions of a WebAssembly block from the given reader.
        /// </summary>
        /// <param name="BlockType">The type of value returned by the resulting block.</param>
        /// <param name="Reader">The WebAssembly file reader.</param>
        /// <returns>A WebAssembly block instruction.</returns>
        public BlockInstruction ReadBlockContents(WasmType BlockType, BinaryWasmReader Reader)
        {
            var contents = new List <Instruction>();

            while (true)
            {
                byte opCode = Reader.ReadByte();
                if (opCode == Operators.EndOpCode)
                {
                    return(Create(BlockType, contents));
                }
                else
                {
                    var op = Operators.GetOperatorByOpCode(opCode);
                    contents.Add(op.ReadImmediates(Reader));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads the export section with the given header.
        /// </summary>
        /// <param name="header">The section header.</param>
        /// <param name="reader">A reader for a binary WebAssembly file.</param>
        /// <returns>The parsed section.</returns>
        public static ExportSection ReadSectionPayload(
            SectionHeader header, BinaryWasmReader reader)
        {
            long startPos = reader.Position;
            // Read the function indices.
            uint count        = reader.ReadVarUInt32();
            var  exportedVals = new List <ExportedValue>();

            for (uint i = 0; i < count; i++)
            {
                exportedVals.Add(
                    new ExportedValue(
                        reader.ReadString(),
                        (ExternalKind)reader.ReadByte(),
                        reader.ReadVarUInt32()));
            }

            // Skip any remaining bytes.
            var extraPayload = reader.ReadRemainingPayload(startPos, header);

            return(new ExportSection(exportedVals, extraPayload));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Reads an imported value from the given binary WebAssembly reader.
        /// </summary>
        /// <param name="Reader">The WebAssembly reader.</param>
        /// <returns>The imported value that was read.</returns>
        public static ImportedValue ReadFrom(BinaryWasmReader Reader)
        {
            string moduleName = Reader.ReadString();
            string fieldName  = Reader.ReadString();
            var    kind       = (ExternalKind)Reader.ReadByte();

            switch (kind)
            {
            case ExternalKind.Function:
                return(new ImportedFunction(moduleName, fieldName, Reader.ReadVarUInt32()));

            case ExternalKind.Global:
                return(new ImportedGlobal(moduleName, fieldName, GlobalType.ReadFrom(Reader)));

            case ExternalKind.Memory:
                return(new ImportedMemory(moduleName, fieldName, MemoryType.ReadFrom(Reader)));

            case ExternalKind.Table:
                return(new ImportedTable(moduleName, fieldName, TableType.ReadFrom(Reader)));

            default:
                throw new WasmException("Unknown imported value kind: " + kind);
            }
        }