Exemple #1
0
        public static SObject Replace(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(SString), typeof(SString) }))
            {
                var str     = ((SString)instance).Value;
                var replace = ((SString)parameters[0]).Value;
                var with    = ((SString)parameters[1]).Value;

                return(processor.CreateString(string.IsNullOrWhiteSpace(replace) ? str : str.Replace(replace, with)));
            }

            return(processor.Undefined);
        }
Exemple #2
0
        public static object Constructor(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { TypeContract.Number, TypeContract.Number }))
            {
                var x = (double)parameters[0];
                var y = (double)parameters[1];

                objLink.SetMember("x", x);
                objLink.SetMember("y", y);
            }

            return(NetUndefined.Instance);
        }
Exemple #3
0
        public static object GetData(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(string)))
            {
                var instance  = (EntityComponentWrapper)This;
                var entity    = instance.parent.GetEntity();
                var component = entity.GetComponent(instance.id);

                var dataKey = (string)parameters[0];

                return(component.GetDataOrDefault(dataKey, ""));
            }

            return(NetUndefined.Instance);
        }
Exemple #4
0
        public static SObject Remove(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(SNumber), typeof(SNumber) }))
            {
                var str    = ((SString)instance).Value;
                var start  = (int)((SNumber)parameters[0]).Value;
                var length = (int)((SNumber)parameters[1]).Value;

                if (start < 0)
                {
                    start = 0;
                }
                if (start > str.Length)
                {
                    return(processor.CreateString(str));
                }

                if (length <= 0)
                {
                    return(processor.CreateString(str));
                }
                if (length + start > str.Length)
                {
                    length = str.Length - start;
                }

                return(processor.CreateString(str.Remove(start, length)));
            }
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                var str   = ((SString)instance).Value;
                var start = (int)((SNumber)parameters[0]).Value;

                if (start < 0)
                {
                    start = 0;
                }
                if (start > str.Length)
                {
                    return(processor.CreateString(str));
                }

                return(processor.CreateString(str.Remove(start)));
            }

            return(processor.Undefined);
        }
Exemple #5
0
        public static SObject LastIndexOf(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                var str    = (SString)instance;
                var search = (SString)parameters[0];

                if (!str.Value.Contains(search.Value) || search.Value == "")
                {
                    return(processor.CreateNumber(-1));
                }

                return(processor.CreateNumber(str.Value.LastIndexOf(search.Value, StringComparison.Ordinal)));
            }

            return(processor.Undefined);
        }
Exemple #6
0
        public static object Constructor(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(string), typeof(int), typeof(int) }, 1))
            {
                var helper = new ParameterHelper(parameters);

                string id = helper.Pop <string>();
                objLink.SetMember("id", id);

                var moveModel = GetMoveModel(This);

                objLink.SetMember("PP", helper.Pop(moveModel.PP));
                objLink.SetMember("maxPP", helper.Pop(moveModel.PP));
            }

            return(NetUndefined.Instance);
        }
Exemple #7
0
        public static SObject IndexOf(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                var str    = instance as SString;
                var search = parameters[0] as SString;

                if (!str.Value.Contains(search.Value) || search.Value == "")
                {
                    return(processor.CreateNumber(-1));
                }

                return(processor.CreateNumber(str.Value.IndexOf(search.Value)));
            }

            return(processor.Undefined);
        }
Exemple #8
0
        public static SObject StartsWith(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                var str      = (SString)instance;
                var includes = (SString)parameters[0];

                if (includes.Value == "")
                {
                    return(processor.CreateBool(str.Value == ""));
                }

                return(processor.CreateBool(str.Value.StartsWith(includes.Value)));
            }

            return(processor.Undefined);
        }
Exemple #9
0
        public static object GetComponent(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(string) }))
            {
                var wrapper = (EntityWrapper)This;
                var entity  = wrapper.GetEntity();

                var component = new EntityComponentWrapper
                {
                    parent = wrapper,
                    id     = parameters[0] as string
                };

                return(component);
            }

            return(NetUndefined.Instance);
        }
Exemple #10
0
        public static SObject Repeat(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                var repeatStr = ((SString)instance).Value;
                var times     = (int)((SNumber)parameters[0]).Value;
                var str       = "";

                for (int i = 0; i < times; i++)
                {
                    str += repeatStr;
                }

                return(processor.CreateString(str));
            }

            return(processor.CreateString(((SString)instance).Value));
        }
Exemple #11
0
        public static object SetData(object This, ScriptObjectLink objLink, object[] parameters)
        {
            if (TypeContract.Ensure(parameters, new[] { typeof(string), typeof(string) }))
            {
                var instance  = (EntityComponentWrapper)This;
                var entity    = instance.parent.GetEntity();
                var component = entity.GetComponent(instance.id);

                var dataKey   = (string)parameters[0];
                var dataValue = (string)parameters[1];

                component.SetData(dataKey, dataValue);

                return(dataValue);
            }

            return(NetUndefined.Instance);
        }
Exemple #12
0
        public static SObject Split(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            int limit = -1;

            string[] delimiters;
            string   str = ((SString)instance).Value;

            if (TypeContract.Ensure(parameters, new[] { typeof(SArray), typeof(SNumber) }))
            {
                delimiters = ((SArray)parameters[0]).ArrayMembers.Select(m => m.ToString(processor).Value).ToArray();
                limit      = (int)((SNumber)parameters[1]).Value;
            }
            else if (TypeContract.Ensure(parameters, typeof(SArray)))
            {
                delimiters = ((SArray)parameters[0]).ArrayMembers.Select(m => m.ToString(processor).Value).ToArray();
            }
            else if (TypeContract.Ensure(parameters, new[] { typeof(SString), typeof(SNumber) }))
            {
                delimiters = new[] { ((SString)parameters[0]).Value };
                limit      = (int)((SNumber)parameters[1]).Value;
            }
            else if (TypeContract.Ensure(parameters, typeof(SString)))
            {
                delimiters = new[] { ((SString)parameters[0]).Value };
            }
            else
            {
                return(processor.CreateArray(new SObject[] { processor.CreateString(str) }));
            }

            var split = str.Split(delimiters, StringSplitOptions.None);

            if (limit >= 0 && split.Length > limit)
            {
                var result = new string[limit];
                Array.Copy(split, result, limit);
                split = result;
            }

            return(processor.CreateArray(split.Select(processor.CreateString).ToArray <SObject>()));
        }
Exemple #13
0
        protected FieldDefinition CreateStateField(NamedTypeDefinition typeDefinition, TypeContract typeContract)
        {
            var host = CciHostEnvironment.GetInstance();

            var field = new FieldDefinition
            {
                Name       = host.NameTable.GetNameFor("$state"),
                Type       = host.PlatformType.SystemInt32,
                Visibility = TypeMemberVisibility.Private,
                ContainingTypeDefinition = typeDefinition,
                InternFactory            = typeDefinition.InternFactory,
                CompileTimeValue         = new CompileTimeConstant
                {
                    Type  = host.PlatformType.SystemInt32,
                    Value = 0
                }
            };

            // Como el $state es int, necesito el invariante ya que no puede ser negativo.
            // Se usa int en vez de uint, para que no haya problemas con la traduccion de BCT
            if (typeContract == null)
            {
                typeContract = new TypeContract();
            }

            typeContract.Invariants.Add(new TypeInvariant
            {
                Condition = new GreaterThanOrEqual
                {
                    LeftOperand = new BoundExpression {
                        Definition = field, Instance = new ThisReference(), Type = field.Type
                    },
                    RightOperand = new CompileTimeConstant {
                        Type = host.PlatformType.SystemInt32, Value = 0
                    }
                }
            });

            return(field);
        }
Exemple #14
0
        public PrintContract(Contract contract, TypeContract tc)
        {
            pd_Contract = new PrintDocument();

            this.contract = contract;

            typeContract = tc;

            // Устанавливаем принтер по умолчанию(если есть)
            pd_Contract.PrinterSettings.PrinterName =
                PrinterSettings.InstalledPrinters
                .Cast <string>()
                .FirstOrDefault(s => (new PrinterSettings()
            {
                PrinterName = s
            }).IsDefaultPrinter);

            if (pd_Contract.PrinterSettings.PrinterName != null)
            {
                pd_Contract.PrintPage += PrintPage; // подключаем событие печати документа на принтер по умолчанию
            }
        }
Exemple #15
0
        public static SObject PadStart(ScriptProcessor processor, SObject instance, SObject This, SObject[] parameters)
        {
            var    str         = ((SString)instance).Value;
            int    totalLength = 0;
            var    padStr      = " ";
            string newPadded   = "";

            if (TypeContract.Ensure(parameters, new[] { typeof(SNumber), typeof(SString) }))
            {
                totalLength = (int)((SNumber)parameters[0]).Value;
                padStr      = ((SString)parameters[1]).Value;
            }
            if (TypeContract.Ensure(parameters, typeof(SNumber)))
            {
                totalLength = (int)((SNumber)parameters[0]).Value;
            }

            if (padStr == "" || totalLength <= str.Length)
            {
                return(processor.CreateString(str));
            }

            while (newPadded.Length + str.Length < totalLength)
            {
                newPadded += padStr;
            }

            if (newPadded.Length + str.Length > totalLength)
            {
                newPadded = newPadded.Remove(totalLength - str.Length);
            }

            str = newPadded + str;

            return(processor.CreateString(str));
        }
        public void Ctor()
        {
            var contract = new TypeContract();

            Assert.Null(contract.Value);
        }
Exemple #17
0
 /// <summary>
 /// Visits the specified type contract.
 /// </summary>
 /// <param name="typeContract">The type contract.</param>
 public virtual ITypeContract Visit(TypeContract typeContract)
 {
     typeContract.ContractFields = this.Visit(typeContract.ContractFields);
       typeContract.ContractMethods = this.Visit(typeContract.ContractMethods);
       typeContract.Invariants = this.Visit(typeContract.Invariants);
       return typeContract;
 }
Exemple #18
0
 /// <summary>
 /// Rewrites the children of the given type contract.
 /// </summary>
 public virtual void RewriteChildren(TypeContract typeContract)
 {
     typeContract.ContractFields = this.Rewrite(typeContract.ContractFields);
       typeContract.ContractMethods = this.Rewrite(typeContract.ContractMethods);
       typeContract.Invariants = this.Rewrite(typeContract.Invariants);
 }
 public EventingVisitor(Action<TypeContract> visitTypeContract) { VisitedTypeContract += visitTypeContract; } public event Action<TypeContract> VisitedTypeContract; public override TypeContract VisitTypeContract(TypeContract contract) { if (VisitedTypeContract != null) VisitedTypeContract(contract); return base.VisitTypeContract(contract); }
 public ParameterContract(XElement elementParameter)
 {
     this.Name = elementParameter.Element("Name").Value;
     this.Type = new TypeContract(elementParameter.Element("Type").Value);
 }
Exemple #21
0
 /// <summary>
 /// Visits the specified type contract.
 /// </summary>
 /// <param name="typeContract">The type contract.</param>
 protected virtual ITypeContract DeepCopy(TypeContract typeContract)
 {
     typeContract.ContractFields = this.DeepCopy(typeContract.ContractFields);
       typeContract.ContractMethods = this.DeepCopy(typeContract.ContractMethods);
       typeContract.Invariants = this.DeepCopy(typeContract.Invariants);
       return typeContract;
 }
Exemple #22
0
 public override TypeContract VisitTypeContract(TypeContract contract) {
   // We *don't* want to walk into the contract; there is no code to generate for the
   // invariants here. That is done in ImplementInvariantHoldsMethod.
   return contract;
 }
Exemple #23
0
 public override TypeContract VisitTypeContract(TypeContract contract){
   if (this.RecordOriginalAsTemplate) return null; //A type template instance does not need invariants
   if (contract == null) return null;
   contract = (TypeContract)contract.Clone();
   contract.DeclaringType = this.VisitTypeReference(contract.DeclaringType);
   contract.Invariants = this.VisitInvariantList(contract.invariants);
   return contract;
 }
Exemple #24
0
 /// <summary>
 /// Visits the specified type contract.
 /// </summary>
 /// <param name="typeContract">The type contract.</param>
 public virtual ITypeContract Visit(ITypeContract typeContract)
 {
     TypeContract mutableTypeContract = typeContract as TypeContract;
       if (!this.copyOnlyIfNotAlreadyMutable || mutableTypeContract == null)
     mutableTypeContract = new TypeContract(typeContract);
       return this.Visit(mutableTypeContract);
 }
Exemple #25
0
 /// <summary>
 /// Makes a copy of this contract, changing the containing block to the given block.
 /// </summary>
 public TypeContract MakeCopyFor(TypeDeclaration containingType)
 {
     if (this.containingType == containingType) return this;
       TypeContract result = new TypeContract(this);
       result.SetContainingType(containingType);
       return result;
 }
        /// <summary>
        /// Returns the type contract, if any, that has been associated with the given object. Returns null if no association exits.
        /// </summary>
        /// <param name="type">An object that might have been associated with a type contract. This can be any kind of object.</param>
        /// <returns></returns>
        public ITypeContract /*?*/ GetTypeContractFor(object type)
        {
            ITypeContract contract = this.underlyingContractProvider.GetTypeContractFor(type);

            if (contract != null)
            {
                return(contract == ContractDummy.TypeContract ? null : contract);
            }

            TypeContract  result          = new TypeContract();
            ITypeContract primaryContract = null;

            if (this.oobExtractors == null)
            {
                primaryContract = this.primaryExtractor.GetTypeContractFor(type);
            }
            bool found = false;

            if (primaryContract != null)
            {
                found = true;
                ContractHelper.AddTypeContract(result, primaryContract);
            }
            if (this.oobExtractors != null)
            {
                foreach (var oobProvider in this.oobExtractors)
                {
                    var oobUnit = oobProvider.Unit;

                    ITypeReference typeReference = type as ITypeReference;
                    if (typeReference == null || typeReference is Dummy)
                    {
                        continue;                                        // REVIEW: Is there anything else it could be and still find a contract for it?
                    }
                    MappingMutator primaryToOobMapper = this.mapperForPrimaryToOob[oobProvider];
                    var            oobType            = primaryToOobMapper.Map(typeReference);

                    if (oobType == null)
                    {
                        continue;
                    }

                    var oobContract = oobProvider.GetTypeContractFor(oobType);

                    if (oobContract == null)
                    {
                        continue;
                    }

                    MappingMutator oobToPrimaryMapper = this.mapperForOobToPrimary[oobProvider];
                    oobContract = oobToPrimaryMapper.Map(typeReference.ResolvedType, oobContract);
                    ContractHelper.AddTypeContract(result, oobContract);
                    found = true;
                }
            }

            // always cache so we don't try to extract more than once
            if (found)
            {
                this.underlyingContractProvider.AssociateTypeWithContract(type, result);
                return(result);
            }
            else
            {
                this.underlyingContractProvider.AssociateTypeWithContract(type, ContractDummy.TypeContract);
                return(null);
            }
        }
Exemple #27
0
    public virtual void VisitTypeContract(TypeContract contract)
    {
      if (contract == null) return;
      // don't visit contract.DeclaringType
      // don't visit contract.InheritedContracts
      this.VisitInvariantList(contract.Invariants);
#if ExtendedRuntime
      this.VisitModelfieldContractList(contract.ModelfieldContracts);
#endif
    }
  Expression IContractDeserializer.ParseContract (TypeContract tc, string text, ErrorNodeList errs)
  {
    Expression expression = null;
    currentMethodContract = null;
    currentMethod = null;
    currentType = tc.DeclaringType;
    try{
      Parser.ParseContract(this.assembly, text, out expression);
    }catch (Exception e){
      ErrorNodeList eList = errs != null ? errs : this.ErrorList;
      if (eList != null){
#if OLDERRORS
        ErrorHandler eh = new ErrorHandler(eList);
        eh.HandleError(tc,System.Compiler.Error.GenericError,"Deserializer error: " + e.Message);
#else
        this.assembly.MetadataImportErrors.Add(e);
#endif
      }
      throw e;
    }
    return expression;
  }
        public void Ctor()
        {
            var contract = new TypeContract();

            Assert.Null(contract.Value);
        }
Exemple #30
0
 /// <summary>
 /// A copy constructor that allocates an instance that is the same as the given template.
 /// </summary>
 /// <param name="template">The template to copy.</param>
 private TypeContract(TypeContract template)
 {
     if (template.contractFields != EmptyListOfFields)
     this.contractFields = new List<FieldDeclaration>(template.contractFields);
       else
     this.contractFields = template.contractFields;
       if (template.contractMethods != EmptyListOfMethods)
     this.contractMethods = new List<MethodDeclaration>(template.contractMethods);
       else
     this.contractMethods = template.contractMethods;
       if (template.invariants != EmptyListOfInvariants)
     this.invariants = new List<TypeInvariant>(template.invariants);
       else
     this.invariants = template.invariants;
 }
 public virtual TypeContract VisitTypeContract(TypeContract contract1, TypeContract contract2) {
   if (contract1 == null) return null;
   if (contract2 == null) {
     // don't visit contract.DeclaringType
     // don't visit contract.InheitedContracts
     contract1.Invariants = this.VisitInvariantList(contract1.Invariants, null);
   }else{
     // don't visit contract.DeclaringType
     // don't visit contract.InheitedContracts
     contract1.Invariants = this.VisitInvariantList(contract1.Invariants, contract2.Invariants);
   }
   return contract1;
 }
Exemple #32
0
 public virtual TypeContract VisitTypeContract(TypeContract contract){
   if (contract == null) return null;
   // don't visit contract.DeclaringType
   // don't visit contract.InheritedContracts
   contract.Invariants = this.VisitInvariantList(contract.Invariants);
   contract.ModelfieldContracts = this.VisitModelfieldContractList(contract.ModelfieldContracts);
   return contract;
 }
Exemple #33
0
 private async void TypeContractIdAsync(TypeContract id)
 {
     string x = id.ID.ToString();
     await Shell.Current.GoToAsync($"{nameof(EditTypeContractPage)}?{nameof(EditTypeContractViewModel.ItemId)}={x}");
 }