Example #1
0
 internal WasmModuleValidator(WasmModule module)
 {
     if (null == module)
     {
         throw new ArgumentNullException("module");
     }
     _module = module;
 }
Example #2
0
 internal ValidationContext(WasmModule module)
 {
     _module = module;
     Errors  = new List <string>();
     Labels  = new List <Tuple <BuiltinLanguageType, int, bool> >();
     _stack  = new List <BuiltinLanguageType>();
     InitializeGlobalVariables();
     InitializeFunctions();
     InitializeMemoryMap();
     InitializeTables();
 }
Example #3
0
        /// <summary>Create a module from an input stream.</summary>
        /// <param name="from">A readable, seekable module.</param>
        /// <returns>The new module instance.</returns>
        public static WasmModule Create(Stream from)
        {
            if (null == from)
            {
                throw new ArgumentNullException("from");
            }
            if (!from.CanRead)
            {
                throw new ArgumentException("non readable stream");
            }
            if (!from.CanSeek)
            {
                throw new ArgumentException("non seekable stream");
            }
            WasmModule result = new WasmModule();

            return(result.Parse(from) ? result : null);
        }
 internal WasmModuleTextWriter(WasmModule module)
 {
     _module = module ?? throw new ArgumentNullException();
     return;
 }
Example #5
0
        public static WasmModule Read(Stream stream)
        {
            var file = new WasmModule();

            using (var br = new BinaryReader(stream, Encoding.UTF8, true)) {
                var magic = br.ReadBytes(4);
                if (!magic.SequenceEqual(Encoding.ASCII.GetBytes("\0asm")))
                {
                    throw new Exception("Invalid magic, expected '\\0asm'");
                }

                file.Version = br.ReadInt32();
                Debug.Assert(file.Version == 1);

                while (stream.Position < stream.Length)
                {
                    var  type   = (SectionType)br.ReadVarUint7();
                    uint size   = br.ReadVarUint32();
                    long oldPos = stream.Position;
                    Debug.Assert(size <= stream.Length - stream.Position);

                    switch (type)
                    {
                    // TODO: custom
                    case SectionType.Type:
                        file.FunctionTypes = br.ReadWasmArray <FunctionSignature>();
                        break;

                    case SectionType.Import:
                        file.Imports = br.ReadWasmArray <Import>();
                        break;

                    case SectionType.Function:
                        file.Functions = br.ReadVarUint32Array();
                        break;

                    // TODO: table
                    // TODO: memory
                    case SectionType.Global:
                        file.Globals = br.ReadWasmArray <Global>();
                        break;

                    case SectionType.Export:
                        file.Exports = br.ReadWasmArray <Export>();
                        break;

                    // TODO: start
                    case SectionType.Element:
                        file.Elements = br.ReadWasmArray <Element>();
                        break;

                    case SectionType.Code:
                        file.FunctionBodies = br.ReadWasmArray <FunctionBody>();
                        break;

                    case SectionType.Data:
                        file.DataSegments = br.ReadWasmArray <DataSegment>();
                        break;

                    default:
                        throw new NotImplementedException(type.ToString());
                    }

                    Debug.Assert(oldPos + size == stream.Position, $"Section size was {size}, but read {stream.Position - oldPos} bytes");
                }
            }

            file.ImportedFunctionCount = file.Imports.Count(x => x.Kind == ImportKind.TypeIndex);
            file.ImportedGlobals       = file.Imports
                                         .Where(x => x.Kind == ImportKind.GlobalType)
                                         .Select(x =>
                                                 x.GlobalType ??
                                                 throw new Exception(
                                                     $"{nameof(Import.GlobalType)} had no value, but {nameof(Import.Kind)} was {nameof(ImportKind.GlobalType)}"))
                                         .ToArray();

            return(file);
        }