Esempio n. 1
0
 static JObject MapToJson(Neo.VM.Types.Map map)
 {
     var json = new JObject();
     foreach (var (key, value) in map)
     {
         json.Add(PrimitiveTypeToString(key), value.ToJson());
     }
     return json;
 }
Esempio n. 2
0
        public static Variable Create(IVariableContainerSession session, Neo.VM.Types.Map map, string?name)
        {
            var container   = new NeoMapContainer(session, map);
            var containerID = session.AddVariableContainer(container);

            return(new Variable()
            {
                Name = name,
                Type = $"Map[{map.Count}]",
                VariablesReference = containerID,
                IndexedVariables = map.Count,
            });
        }
Esempio n. 3
0
 public static JToken ToJson(this StackItem item)
 {
     return item switch
     {
         Neo.VM.Types.Boolean _ => item.GetBoolean(),
         Neo.VM.Types.Buffer buffer => buffer.GetSpan().ToHexString(),
         Neo.VM.Types.ByteString byteString => byteString.GetSpan().ToHexString(),
         Neo.VM.Types.Integer @int => @int.GetInteger().ToString(),
         // Neo.VM.Types.InteropInterface _ => MakeVariable("InteropInterface"),
         Neo.VM.Types.Map map => MapToJson(map),
         Neo.VM.Types.Null _ => new JValue((object?)null),
         // Neo.VM.Types.Pointer _ => MakeVariable("Pointer"),
         Neo.VM.Types.Array array => new JArray(array.Select(i => i.ToJson())),
         _ => throw new NotSupportedException(),
     };
        private static StackItem DeserializeStackItem(BinaryReader reader, ExecutionEngine engine)
        {
            Stack <StackItem> deserialized = new Stack <StackItem>();
            int undeserialized             = 1;

            while (undeserialized-- > 0)
            {
                StackItemType type = (StackItemType)reader.ReadByte();
                switch (type)
                {
                case StackItemType.ByteArray:
                    deserialized.Push(new Neo.VM.Types.ByteArray(reader.ReadVarBytes()));
                    break;

                case StackItemType.Boolean:
                    deserialized.Push(new Neo.VM.Types.Boolean(reader.ReadBoolean()));
                    break;

                case StackItemType.Integer:
                    deserialized.Push(new Neo.VM.Types.Integer(new System.Numerics.BigInteger(reader.ReadVarBytes())));
                    break;

                case StackItemType.Array:
                case StackItemType.Struct:
                {
                    int count = (int)reader.ReadVarInt(engine.MaxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count;
                }
                break;

                case StackItemType.Map:
                {
                    int count = (int)reader.ReadVarInt(engine.MaxArraySize);
                    deserialized.Push(new ContainerPlaceholder
                        {
                            Type         = type,
                            ElementCount = count
                        });
                    undeserialized += count * 2;
                }
                break;

                default:
                    throw new FormatException();
                }
            }
            Stack <StackItem> stack_temp = new Stack <StackItem>();

            while (deserialized.Count > 0)
            {
                StackItem item = deserialized.Pop();
                if (item is ContainerPlaceholder placeholder)
                {
                    switch (placeholder.Type)
                    {
                    case StackItemType.Array:
                        var array = new Neo.VM.Types.Array();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            array.Add(stack_temp.Pop());
                        }
                        item = array;
                        break;

                    case StackItemType.Struct:
                        var @struct = new Neo.VM.Types.Struct();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            @struct.Add(stack_temp.Pop());
                        }
                        item = @struct;
                        break;

                    case StackItemType.Map:
                        var map = new Neo.VM.Types.Map();
                        for (int i = 0; i < placeholder.ElementCount; i++)
                        {
                            StackItem key   = stack_temp.Pop();
                            StackItem value = stack_temp.Pop();
                            map.Add(key, value);
                        }
                        item = map;
                        break;
                    }
                }
                stack_temp.Push(item);
            }
            return(stack_temp.Peek());
        }
Esempio n. 5
0
 public NeoMapContainer(IVariableContainerSession session, Neo.VM.Types.Map map)
 {
     this.session = session;
     this.map     = map;
 }
        public StackItem Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            var count = reader.ReadArrayHeader();

            if (count != 2)
            {
                throw new MessagePackSerializationException($"Invalid StackItem Array Header {count}");
            }

            var type = options.Resolver.GetFormatterWithVerify <StackItemType>().Deserialize(ref reader, options);

            switch (type)
            {
            case StackItemType.Any:
                reader.ReadNil();
                return(StackItem.Null);

            case StackItemType.Boolean:
                return(reader.ReadBoolean() ? StackItem.True : StackItem.False);

            case StackItemType.Buffer:
            {
                var bytes = options.Resolver.GetFormatter <byte[]>().Deserialize(ref reader, options);
                return(new NeoBuffer(bytes));
            }

            case StackItemType.ByteString:
            {
                var bytes = options.Resolver.GetFormatter <byte[]>().Deserialize(ref reader, options);
                return(new NeoByteString(bytes));
            }

            case StackItemType.Integer:
            {
                var integer = options.Resolver.GetFormatterWithVerify <BigInteger>().Deserialize(ref reader, options);
                return(new NeoInteger(integer));
            }

            case StackItemType.InteropInterface:
            {
                var typeName = options.Resolver.GetFormatterWithVerify <string>().Deserialize(ref reader, options);
                return(new TraceInteropInterface(typeName));
            }

            case StackItemType.Pointer:
                reader.ReadNil();
                return(new NeoPointer(Array.Empty <byte>(), 0));

            case StackItemType.Map:
            {
                var map      = new NeoMap();
                var mapCount = reader.ReadMapHeader();
                for (int i = 0; i < mapCount; i++)
                {
                    var key = (PrimitiveType)Deserialize(ref reader, options);
                    map[key] = Deserialize(ref reader, options);
                }
                return(map);
            }

            case StackItemType.Array:
            case StackItemType.Struct:
            {
                var array = type == StackItemType.Array
                            ? new NeoArray()
                            : new NeoStruct();
                var arrayCount = reader.ReadArrayHeader();
                for (int i = 0; i < arrayCount; i++)
                {
                    array.Add(Deserialize(ref reader, options));
                }
                return(array);
            }
            }

            throw new MessagePackSerializationException($"Invalid StackItem {type}");
        }
Esempio n. 7
0
 public MapStackItem()
 {
     _item = new Neo.VM.Types.Map();
 }
Esempio n. 8
0
 public MapStackItem(Neo.VM.Types.Map item)
 {
     _item = item;
 }