示例#1
0
        public IEnumerable <IVariable> GetChildVariables(IValue value)
        {
            var presenter = new DefaultValueVisitor();

            if (VariableHasType(value, DataType.Object))
            {
                var objectValue = value.AsObject();
                if (objectValue is IDebugPresentationAcceptor customPresenter)
                {
                    customPresenter.Accept(presenter);
                }
                else
                {
                    if (HasProperties(objectValue))
                    {
                        presenter.ShowProperties(objectValue);
                    }

                    if (HasIndexes(objectValue as ICollectionContext))
                    {
                        var context = value.AsObject();
                        if (context is IEnumerable <IValue> collection)
                        {
                            presenter.ShowCollectionItems(collection);
                        }
                    }
                }
            }

            return(presenter.Result);
        }
 public static object ConvertToDynamicCLRObject(IValue value)
 {
     if (value.DataType == DataType.Object && !(value.AsObject() is IObjectWrapper))
     {
         return(new DynamicContextWrapper(value.AsObject()));
     }
     else
     {
         return(ContextValuesMarshaller.ConvertToCLRObject(value));
     }
 }
示例#3
0
        public static DataReader Constructor(IValue dataSource, IValue textEncoding = null, ByteOrderEnum?byteOrder = null, string lineSplitter = "\n", string convertibleSplitterOfLines = null)
        {
            if (dataSource.DataType == DataType.String)
            {
                var stream = new FileStream(dataSource.AsString(), FileMode.Open, FileAccess.Read, FileShare.Read);
                return(new DataReader(stream, textEncoding, byteOrder, lineSplitter, convertibleSplitterOfLines));
            }
            else
            {
                var    obj = dataSource.AsObject();
                Stream stream;
                if (obj is BinaryDataContext)
                {
                    stream = ((BinaryDataContext)obj).GetStream();
                }
                else if (obj is IStreamWrapper)
                {
                    stream = ((IStreamWrapper)obj).GetUnderlyingStream();
                }
                else
                {
                    throw RuntimeException.InvalidArgumentType("dataSource");
                }

                return(new DataReader(stream, textEncoding, byteOrder, lineSplitter, convertibleSplitterOfLines));
            }
        }
示例#4
0
        public static object ConvertParam(IValue value, Type type)
        {
            object valueObj;

            if (value == null || value.DataType == DataType.NotAValidValue)
            {
                return(null);
            }

            if (Nullable.GetUnderlyingType(type) != null)
            {
                return(ConvertParam(value, Nullable.GetUnderlyingType(type)));
            }

            if (type == typeof(IValue))
            {
                valueObj = value;
            }
            else if (type == typeof(IVariable))
            {
                valueObj = value;
            }
            else if (type == typeof(string))
            {
                valueObj = value.AsString();
            }
            else if (type == typeof(int) || type == typeof(uint) || type == typeof(short) || type == typeof(ushort) || type == typeof(byte) || type == typeof(sbyte))
            {
                valueObj = (int)value.AsNumber();
            }
            else if (type == typeof(long) || type == typeof(ulong))
            {
                valueObj = (long)value.AsNumber();
            }
            else if (type == typeof(double) || type == typeof(decimal))
            {
                valueObj = value.AsNumber();
            }
            else if (type == typeof(DateTime))
            {
                valueObj = value.AsDate();
            }
            else if (type == typeof(bool))
            {
                valueObj = value.AsBoolean();
            }
            else if (typeof(IRuntimeContextInstance).IsAssignableFrom(type))
            {
                valueObj = value.AsObject();
            }
            else
            {
                valueObj = CastToCLRObject(value);
            }

            return(valueObj);
        }
示例#5
0
        public void WriteXML(XmlWriterImpl xmlWriter,
                             IValue value,
                             string name,
                             XMLTypeAssignment typeAssigment = XMLTypeAssignment.Implicit,
                             XMLForm form = XMLForm.Element)
        {
            XMLExpandedName xmlType;

            switch (value.DataType)
            {
            case DataType.Undefined:

                WriteXMLUndefined(xmlWriter, name, form);
                break;

            case DataType.String:

                xmlType = new XMLExpandedName(XmlSchema.InstanceNamespace, "string");
                WriteXMLSimpleData(xmlWriter, name, value, xmlType, typeAssigment, form);
                break;

            case DataType.Number:

                xmlType = new XMLExpandedName(XmlSchema.InstanceNamespace, "decimal");
                WriteXMLSimpleData(xmlWriter, name, value, xmlType, typeAssigment, form);
                break;

            case DataType.Boolean:

                xmlType = new XMLExpandedName(XmlSchema.InstanceNamespace, "boolean");
                WriteXMLSimpleData(xmlWriter, name, value, xmlType, typeAssigment, form);
                break;

            case DataType.Date:

                xmlType = new XMLExpandedName(XmlSchema.InstanceNamespace, "dateTime");
                WriteXMLSimpleData(xmlWriter, name, value, xmlType, typeAssigment, form);
                break;

            case DataType.Object:

                IRuntimeContextInstance valueObject = value.AsObject();
                if (valueObject is IXDTOSerializableXML seriazable)
                {
                    seriazable.WriteXML(xmlWriter, this);
                }
                else
                {
                    throw RuntimeException.InvalidArgumentType();
                }
                break;

            default:
                throw RuntimeException.InvalidArgumentType();
            }
        }
示例#6
0
        private static bool HasProperties(IValue variable)
        {
            if (!VariableHasType(variable, DataType.Object))
            {
                return(false);
            }
            var obj = variable.AsObject();

            return(obj.GetPropCount() > 0);
        }
        private static bool HasProperties(IValue variable)
        {
            if (variable.DataType == DataType.Object)
            {
                var obj = variable.AsObject();
                return(obj.GetPropCount() > 0);
            }

            return(false);
        }
示例#8
0
        public static T ConvertParam <T>(IValue value)
        {
            object valueObj;
            var    type = typeof(T);

            if (value == null)
            {
                valueObj = default(T);
            }
            else if (type == typeof(IValue))
            {
                valueObj = value;
            }
            else if (type == typeof(IVariable))
            {
                valueObj = value;
            }
            else if (type == typeof(string))
            {
                valueObj = value.AsString();
            }
            else if (type == typeof(int))
            {
                valueObj = (int)value.AsNumber();
            }
            else if (type == typeof(double) || type == typeof(decimal))
            {
                valueObj = value.AsNumber();
            }
            else if (type == typeof(DateTime))
            {
                valueObj = value.AsDate();
            }
            else if (type == typeof(bool))
            {
                valueObj = value.AsBoolean();
            }
            else if (typeof(IRuntimeContextInstance).IsAssignableFrom(type))
            {
                valueObj = value.AsObject();
            }
            else
            {
                valueObj = default(T);
            }

            try
            {
                return((T)valueObj);
            }
            catch (InvalidCastException)
            {
                throw RuntimeException.InvalidArgumentType();
            }
        }
示例#9
0
        public static IRuntimeContextInstance Constructor1(IValue stream, IValue textEncoding = null, ByteOrderEnum?byteOrder = null, string lineSplitter = null, string convertibleSplitterOfLines = null, bool writeBOM = false)
        {
            var streamObj = stream.AsObject() as IStreamWrapper;

            if (streamObj == null)
            {
                throw RuntimeException.InvalidArgumentType(nameof(stream));
            }

            return(new DataWriter(streamObj, textEncoding, byteOrder, lineSplitter, convertibleSplitterOfLines, writeBOM));
        }
示例#10
0
        public void Write(IValue binaryDataOrReadResult)
        {
            var binData = binaryDataOrReadResult.AsObject() as BinaryDataContext;

            if (binData == null) //TODO: Поддержкать класс РезультатЧтенияДанных
            {
                throw RuntimeException.InvalidArgumentType();
            }

            _binaryWriter.Write(binData.Buffer, 0, binData.Size());
        }
示例#11
0
        public void SetError(IValue target)
        {
            if (!(target.AsObject() is IStreamWrapper stream))
            {
                throw RuntimeException.InvalidArgumentType(nameof(target));
            }

            var writer = new StreamWriter(stream.GetUnderlyingStream());

            Console.SetError(writer);
        }
 public bool Equals(IValue other)
 {
     if (other.SystemType.Equals(this.SystemType))
     {
         return(Object.ReferenceEquals(this.AsObject(), other.AsObject()));
     }
     else
     {
         return(false);
     }
 }
        public static object ConvertToCLRObject(IValue val)
        {
            object result;

            if (val == null)
            {
                return(val);
            }

            switch (val.DataType)
            {
            case Machine.DataType.Boolean:
                result = val.AsBoolean();
                break;

            case Machine.DataType.Date:
                result = val.AsDate();
                break;

            case Machine.DataType.Number:
                result = val.AsNumber();
                break;

            case Machine.DataType.String:
                result = val.AsString();
                break;

            case Machine.DataType.Undefined:
                result = null;
                break;

            default:
                if (val.DataType == DataType.Object)
                {
                    result = val.AsObject();
                }

                result = val.GetRawValue();
                if (result is IObjectWrapper)
                {
                    result = ((IObjectWrapper)result).UnderlyingObject;
                }
                else
                {
                    throw new ValueMarshallingException($"Тип {val.GetType()} не поддерживает преобразование в CLR-объект");
                }

                break;
            }

            return(result);
        }
示例#14
0
        public bool MethodExists(IValue target, string methodName)
        {
            if (target.DataType == DataType.Object)
            {
                return(MethodExistsForObject(target.AsObject(), methodName));
            }

            if (target.DataType == DataType.Type)
            {
                return(MethodExistsForType(target.GetRawValue() as TypeTypeValue, methodName));
            }

            throw RuntimeException.InvalidArgumentType("target");
        }
示例#15
0
        public void SetOutput(IValue target)
        {
            if (!(target.AsObject() is IStreamWrapper stream))
            {
                throw RuntimeException.InvalidArgumentType(nameof(target));
            }

            var writer = new StreamWriter(stream.GetUnderlyingStream(), Console.OutputEncoding)
            {
                AutoFlush = true,
            };

            Console.SetOut(writer);
        }
示例#16
0
 private static bool IsStream(IValue input, out IStreamWrapper wrapper)
 {
     wrapper = null;
     if (input.DataType == DataType.Object)
     {
         var obj = input.AsObject();
         if (obj is IStreamWrapper wrap)
         {
             wrapper = wrap;
             return(true);
         }
     }
     return(false);
 }
示例#17
0
        public void WriteXML(XmlWriterImpl xmlWriter,
                             IValue value,
                             XMLTypeAssignment typeAssigment = XMLTypeAssignment.Implicit,
                             XMLForm form = XMLForm.Element)
        {
            switch (value.DataType)
            {
            case DataType.Undefined:

                WriteXML(xmlWriter, value, "Undefined", typeAssigment, form);
                break;

            case DataType.String:

                WriteXML(xmlWriter, value, "string", typeAssigment, form);
                break;

            case DataType.Number:

                WriteXML(xmlWriter, value, "decimal", typeAssigment, form);
                break;

            case DataType.Boolean:

                WriteXML(xmlWriter, value, "boolean", typeAssigment, form);
                break;

            case DataType.Date:

                WriteXML(xmlWriter, value, "dateTime", typeAssigment, form);
                break;

            case DataType.Object:

                IRuntimeContextInstance valueObject = value.AsObject();
                if (valueObject is IXDTOSerializableXML seriazable)
                {
                    seriazable.WriteXML(xmlWriter, this);
                }
                else
                {
                    throw RuntimeException.InvalidArgumentType();
                }
                break;

            default:
                throw RuntimeException.InvalidArgumentType();
            }
        }
示例#18
0
        public static Encoding GetEncoding(IValue encoding, bool addBOM = true)
        {
            if (encoding.BaseType == ValueTypeEnum.STRING)
            {
                return(GetEncodingByName(encoding.AsString(), addBOM));
            }
            else
            {
                if (encoding.BaseType != ValueTypeEnum.OBJECT)
                {
                    throw new Exception("Неверный тип аргумента");
                }

                var encodingEnum = (TextEncodingEnumInner)encoding.AsObject();

                Encoding enc;
                if (encodingEnum == TextEncodingEnumInner.ANSI)
                {
                    enc = Encoding.GetEncoding(1251);
                }
                else if (encodingEnum == TextEncodingEnumInner.OEM)
                {
                    enc = Encoding.GetEncoding(866);
                }
                else if (encodingEnum == TextEncodingEnumInner.UTF16)
                {
                    enc = new UnicodeEncoding(false, addBOM);
                }
                else if (encodingEnum == TextEncodingEnumInner.UTF8)
                {
                    enc = new UTF8Encoding(addBOM);
                }
                else if (encodingEnum == TextEncodingEnumInner.UTF8NoBOM)
                {
                    enc = new UTF8Encoding(false);
                }
                else if (encodingEnum == TextEncodingEnumInner.System)
                {
                    enc = Encoding.Default;
                }
                else
                {
                    throw new Exception("Неверный тип аргумента");
                }

                return(enc);
            }
        }
示例#19
0
        public static IRuntimeContextInstance ConstructByOtherDescription(
            IValue typeDescription      = null,
            IValue addTypes             = null,
            IValue removeTypes          = null,
            IValue numberQualifiers     = null,
            IValue stringQualifiers     = null,
            IValue dateQualifiers       = null,
            IValue binaryDataQualifiers = null)
        {
            var td = typeDescription as TypeDescription;

            var removeTypesList = ConstructTypeList(removeTypes);

            if (removeTypesList == null)
            {
                throw RuntimeException.InvalidArgumentType(nameof(removeTypes));
            }


            var _types = new List <TypeTypeValue>();

            if (td != null)
            {
                foreach (var ivType in td.Types())
                {
                    var type = ivType as TypeTypeValue;
                    if (removeTypesList.IndexOf(type) == -1)
                    {
                        _types.Add(type);
                    }
                }
            }

            var addTypesList = ConstructTypeList(addTypes);

            if (addTypesList == null)
            {
                throw RuntimeException.InvalidArgumentType(nameof(addTypes));
            }
            _types.AddRange(addTypesList);

            var paramNumberQ     = numberQualifiers?.AsObject() as NumberQualifiers ?? td?.NumberQualifiers;
            var paramStringQ     = stringQualifiers?.AsObject() as StringQualifiers ?? td?.StringQualifiers;
            var paramDateQ       = dateQualifiers?.AsObject() as DateQualifiers ?? td?.DateQualifiers;
            var paramBinaryDataQ = binaryDataQualifiers?.AsObject() as BinaryDataQualifiers ?? td?.BinaryDataQualifiers;

            return(new TypeDescription(_types, paramNumberQ, paramStringQ, paramDateQ, paramBinaryDataQ));
        }
示例#20
0
 public IValue ReadIntoBinaryDataBuffer(IValue buffer, int positionInBuffer = 0, int number = 0)
 {
     if (buffer.DataType == DataType.Number && positionInBuffer == 0 && number == 0)
     {
         var stream = ReadSomeBytes(number);
         return(new BinaryDataBuffer(stream.ToArray()));
     }
     else
     {
         var binBuffer  = (BinaryDataBuffer)buffer.AsObject();
         var stream     = ReadSomeBytes(number);
         var bytesCount = number == 0 ? (int)stream.Length : number;
         stream.Read(binBuffer.Bytes, positionInBuffer, bytesCount);
         return(buffer);
     }
 }
        private bool HasIndexes(IValue variable)
        {
            if (variable.DataType == DataType.Object)
            {
                var obj = variable.AsObject();
                if (!(obj is IEnumerable <KeyAndValueImpl>) &&
                    obj is IRuntimeContextInstance cntx && cntx.IsIndexed)
                {
                    if (obj is ICollectionContext collection)
                    {
                        return(collection.Count() > 0);
                    }
                }
            }

            return(false);
        }
        public static HttpRequestContext Constructor(IValue resource, IValue headers = null)
        {
            var ctx = new HttpRequestContext();

            ctx.ResourceAddress = resource.AsString();
            if (headers != null)
            {
                var headersMap = headers.AsObject() as MapImpl;
                if (headersMap == null)
                {
                    throw RuntimeException.InvalidArgumentType();
                }

                ctx.Headers = headersMap;
            }

            return(ctx);
        }
示例#23
0
        public ValueTable.ValueTable GetMethodsTable(IValue target)
        {
            var result = new ValueTable.ValueTable();

            if (target.DataType == DataType.Object)
            {
                FillMethodsTableForObject(target.AsObject(), result);
            }
            else if (target.DataType == DataType.Type)
            {
                FillMethodsTableForType(target.GetRawValue() as TypeTypeValue, result);
            }
            else
            {
                throw RuntimeException.InvalidArgumentType();
            }

            return(result);
        }
示例#24
0
 public void Write(IValue filenameOrStream)
 {
     if (filenameOrStream.DataType == DataType.String)
     {
         var filename = filenameOrStream.AsString();
         using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
         {
             fs.Write(_buffer, 0, _buffer.Length);
         }
     }
     else if (filenameOrStream.AsObject() is IStreamWrapper stream)
     {
         stream.GetUnderlyingStream().Write(_buffer, 0, _buffer.Length);
     }
     else
     {
         throw RuntimeException.InvalidArgumentType("filenameOrStream");
     }
 }
示例#25
0
        public ValueTable.ValueTable GetPropertiesTable(IValue target)
        {
            ValueTable.ValueTable result = new ValueTable.ValueTable();

            if (target.DataType == DataType.Object)
            {
                FillPropertiesTable(result, target.AsObject().GetProperties());
            }
            else if (target.DataType == DataType.Type)
            {
                var type = target.GetRawValue() as TypeTypeValue;
                FillPropertiesTableForType(type, result);
            }
            else
            {
                throw RuntimeException.InvalidArgumentType();
            }

            return(result);
        }
示例#26
0
        public ValueTable.ValueTable GetPropertiesTable(IValue target)
        {
            ValueTable.ValueTable result = new ValueTable.ValueTable();

            if (target.DataType == DataType.Object)
            {
                FillPropertiesTable(result, target.AsObject().GetProperties());
            }
            else if (target.DataType == DataType.Type)
            {
                var type        = target.GetRawValue() as TypeTypeValue;
                var clrType     = GetReflectableClrType(type);
                var magicCaller = CreatePropertiesMapper(clrType);
                FillPropertiesTable(result, magicCaller.GetProperties());
            }
            else
            {
                throw RuntimeException.InvalidArgumentType();
            }

            return(result);
        }
示例#27
0
        public void Open(IValue filenameOrStream, string password = null, FileNamesEncodingInZipFile encoding = FileNamesEncodingInZipFile.Auto)
        {
            // f**k non-russian encodings on non-ascii files
            ZipFile.DefaultEncoding = Encoding.GetEncoding(866);

            if (filenameOrStream.DataType == DataType.String)
            {
                _zip = ZipFile.Read(filenameOrStream.AsString(), new ReadOptions {
                    Encoding = ChooseEncoding(encoding)
                });
            }
            else if (filenameOrStream.AsObject() is IStreamWrapper stream)
            {
                _zip = ZipFile.Read(stream.GetUnderlyingStream(), new ReadOptions {
                    Encoding = ChooseEncoding(encoding)
                });
            }
            else
            {
                throw RuntimeException.InvalidArgumentType(nameof(filenameOrStream));
            }

            _zip.Password = password;
        }
示例#28
0
 public bool Equals(IValue other)
 {
     if (other.SystemType.Equals(this.SystemType))
     {
         return Object.ReferenceEquals(this.AsObject(), other.AsObject());
     }
     else
     {
         return false;
     }
 }
        public virtual bool Equals(IValue other)
        {
            if (other == null)
            {
                return(false);
            }

            return(other.SystemType.Equals(this.SystemType) && Object.ReferenceEquals(this.AsObject(), other.AsObject()));
        }
示例#30
0
 public static CriticalSectionContext Create(IValue instance)
 {
     return(new CriticalSectionContext(instance.AsObject()));
 }