Пример #1
0
 public Optimizer(double startValue, double mutationAmount, SetValueMethod setValueMethod)
 {
     this.currValue      = startValue;
     this.lastValue      = startValue;
     this.mutationAmount = mutationAmount;
     this.setValueMethod = setValueMethod;
 }
Пример #2
0
        public void SetEncodedValue_KnownHeaders_ThrowIf_IncorrectValue(SetValueMethod method)
        {
            if (method == SetValueMethod.AddShortcut)
            {
                return;
            }

            var writer = new CborWriter();

            writer.WriteNull();
            byte[] encodedNullValue = writer.Encode();

            var map = new CoseHeaderMap();

            // only accepts int or tstr
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.Algorithm, encodedNullValue, method));
            // [ +label ] (non-empty array)
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, encodedNullValue, method));
            writer.Reset();
            writer.WriteStartArray(0);
            writer.WriteEndArray();
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, writer.Encode(), method));
            // tstr / uint
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.ContentType, encodedNullValue, method));
            // bstr
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.KeyIdentifier, encodedNullValue, method));
        }
Пример #3
0
 /// <summary>
 ///
 /// </summary>
 private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
 {
     if (__editable && __object != null)
     {
         SetValueMethod _method = __objectEditor.GetMethod(__object.GetType());
         if (_method != null)
         {
             _method.Invoke(ref __object, __textBox.Text);
         }
     }
 }
Пример #4
0
        public void SetValue_KnownHeaders_ThrowIf_IncorrectValue(SetValueMethod method)
        {
            var map = new CoseHeaderMap();

            // only accepts int or tstr
            Assert.Throws <ArgumentException>("value", () => SetValue(map, CoseHeaderLabel.Algorithm, ReadOnlySpan <byte> .Empty, method));
            // [ +label ] (non-empty array)
            Assert.Throws <ArgumentException>("value", () => SetValue(map, CoseHeaderLabel.CriticalHeaders, ReadOnlySpan <byte> .Empty, method));
            // tstr / uint
            Assert.Throws <ArgumentException>("value", () => SetValue(map, CoseHeaderLabel.ContentType, -1, method));
            // bstr
            Assert.Throws <ArgumentException>("value", () => SetValue(map, CoseHeaderLabel.KeyIdentifier, "foo", method));
        }
Пример #5
0
        public PropertyMap(ISerializer serializer, PropertyDefinition propertyDefinition, ITypeMap typeMap, IEnumerable <string> availableFilters)
        {
            this.serializer = serializer;

            this.typeMap = typeMap is IContentTypeMap contentTypeMap && propertyDefinition.Content.UseXop
                ? contentTypeMap.GetOptimizedContentTypeMap()
                : typeMap;

            Definition = propertyDefinition;

            getValueMethod = Definition.PropertyInfo.CreateGetValueMethod();
            setValueMethod = Definition.PropertyInfo.CreateSetValueMethod();

            if (availableFilters == null)
            {
                return;
            }

            foreach (var availableFilter in availableFilters.Where(f => Definition.DeclaringTypeDefinition.Type.IsFilterableField(Definition.RuntimeName, f)))
            {
                filters.Add(availableFilter);
            }
        }
Пример #6
0
        public void SetValue_GetValue_KnownCoseHeaderLabel(SetValueMethod setMethod, GetValueMethod getMethod)
        {
            var map = new CoseHeaderMap();

            SetValue(map, CoseHeaderLabel.Algorithm, (int)ECDsaAlgorithm.ES256, setMethod);

            if (setMethod != SetValueMethod.AddShortcut)
            {
                SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, GetDummyCritHeaderValue(), setMethod);
            }

            SetValue(map, CoseHeaderLabel.ContentType, ContentTypeDummyValue, setMethod);
            SetValue(map, CoseHeaderLabel.KeyIdentifier, s_sampleContent, setMethod);

            Assert.Equal((int)ECDsaAlgorithm.ES256, GetValue <int>(map, CoseHeaderLabel.Algorithm, getMethod));

            if (getMethod != GetValueMethod.GetValueShortcut)
            {
                AssertExtensions.SequenceEqual(GetDummyCritHeaderValue(), GetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, getMethod));
            }

            Assert.Equal(ContentTypeDummyValue, GetValue <string>(map, CoseHeaderLabel.ContentType, getMethod));
            AssertExtensions.SequenceEqual(s_sampleContent, GetValue <byte[]>(map, CoseHeaderLabel.KeyIdentifier, getMethod));
        }
Пример #7
0
        internal override void Set(ILGenerator generator, OptimizationInfo optimizationInfo, bool rIU, Type valueType, SetValueMethod value)
        {
            if (_Type == null)
            {
                _Type = valueType;
            }
            else if (_Type != valueType)
            {
                // Essentially declaring a new variable.
                _Type = valueType;
                Store = null;
            }

            // Declare an IL local variable if no storage location has been allocated yet.
            if (Store == null)
            {
                Store = generator.DeclareVariable(valueType, Name);
            }

            // Load the value:
            value(rIU);

            // Store the value in the variable.
            generator.StoreVariable(Store);
        }
 public EnumListProperty(string name, GetValueMethod getValue, SetValueMethod setValue, CreateInstanceMethod createInstance = null) : base(name, getValue, setValue, createInstance)
 {
     Assert.IsTrue(typeof(TItem).IsEnum);
 }
Пример #9
0
 /// <summary>Sets this variable with the given value generator method.</summary>
 internal virtual void Set(ILGenerator generator, OptimizationInfo optimizationInfo, bool rIU, Type valueType, SetValueMethod value)
 {
 }
 public StructMutableContainerListProperty(string name, GetValueMethod getValue, SetValueMethod setValue, CreateInstanceMethod createInstanceMethod = null)
     : base(name, getValue, setValue, createInstanceMethod)
 {
 }
 public StructListProperty(string name, GetValueMethod getValue, SetValueMethod setValue, CreateInstanceMethod createInstance = null)
     : base(name, getValue, setValue)
 {
     m_CreateInstanceMethod = createInstance ?? DefaultCreateInstance;
 }
Пример #12
0
 public StructValueStructProperty(string name, GetValueMethod getValue, SetValueMethod setValue, GetValueRefMethod getValueRef) : base(name, getValue, setValue)
 {
     Assert.IsNotNull(getValueRef);
     m_GetValueRef = getValueRef;
 }
Пример #13
0
 public StructMutableContainerProperty(string name, GetValueMethod getValue, SetValueMethod setValue, RefAccessMethod refAccess) : base(name, getValue, setValue)
 {
     RefAccess = refAccess;
 }
Пример #14
0
        /// <summary>
        /// Stores the value on the top of the stack in the reference.
        /// </summary>
        /// <param name="generator"> The generator to output the CIL to. </param>
        /// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
        /// <param name="valueType"> The primitive type of the value that is on the top of the stack. </param>
        /// <param name="throwIfUnresolvable"> <c>true</c> to throw a ReferenceError exception if
        /// the name is unresolvable; <c>false</c> to create a new property instead. </param>
        public void GenerateSet(ILGenerator generator, OptimizationInfo optimizationInfo, bool rIU, Type valueType, SetValueMethod value, bool throwIfUnresolvable)
        {
            if (ResolvedProperty == null)
            {
                // Dynamic property access
                // -----------------------
                // xxx = object.Set(x)

                // Load the left-hand side:
                var lhs = this.GetOperand(0);

                // -- Begin args for Prototype.SetPropertyValue --

                // Script engine (engine):
                EmitHelpers.LoadEngine(generator);

                // Put LHS object onto stack now (thisObj):
                lhs.GenerateCode(generator, optimizationInfo);

                // What type have we now got on the stack? Typically expected to be 'object'.
                Type lhsType = lhs.GetResultType(optimizationInfo);

                // Ensure it's boxed (still thisObj):
                EmitConversion.ToAny(generator, lhsType);

                // Load the property name and convert to a string.
                var rhs = this.GetOperand(1);
                rhs.GenerateCode(generator, optimizationInfo);
                EmitConversion.ToString(generator, rhs.GetResultType(optimizationInfo));

                if (rIU)
                {
                    // Output the value now (twice):
                    value(true);

                    // We now have [obj][value][value] on the stack.
                    // Calling the set method would fail (as it'll operate on the duplicated value).
                    // So, we have to pop one off and re-add it after.

                    // In order for SetValue to work, we need to shove the 2nd copy into a temp variable.
                    ILLocalVariable localVar = generator.DeclareVariable(valueType);

                    // Store into the local:
                    generator.StoreVariable(localVar);

                    // Set the value:
                    generator.Call(ReflectionHelpers.Object_SetPropertyValue);

                    // Load from the local:
                    generator.LoadVariable(localVar);
                }
                else
                {
                    // Output the value now:
                    value(false);

                    // Set the value:
                    generator.Call(ReflectionHelpers.Object_SetPropertyValue);
                }
            }
            else if (isArrayIndex)
            {
                // Array indexer
                // -------------
                // object[index] = x

                // Load the left-hand side and convert to an object instance.
                var lhs = this.GetOperand(0);
                lhs.GenerateCode(generator, optimizationInfo);

                // Load the right-hand side and convert to int32/uint32/whatever the indexer function wants.
                var rhs = this.GetOperand(1);
                rhs.GenerateCode(generator, optimizationInfo);

                // Convert the index:
                EmitConversion.Convert(generator, rhs.GetResultType(optimizationInfo), ResolvedProperty.FirstIndexType);

                // Call set:
                ResolvedProperty.Set(generator, optimizationInfo, rIU, valueType, value);
            }
            else
            {
                // Named property modification (e.g. x.property = y)
                // -------------------------------------------------

                if (ResolvedProperty.HasEngine)
                {
                    // Emit the engine ref:
                    EmitHelpers.LoadEngine(generator);
                }

                if (ResolvedProperty.HasAccessor)
                {
                    // Load the left-hand side:
                    var lhs = GetOperand(0);
                    lhs.GenerateCode(generator, optimizationInfo);
                }

                // Target object is now on the stack.

                // Set it:
                ResolvedProperty.Set(generator, optimizationInfo, rIU, valueType, value);
            }
        }
Пример #15
0
        public void SetEncodedValue_GetEncodedValue_KnownCoseHeaderLabel(int knownHeader, byte[] encodedValue, SetValueMethod setMethod, GetValueMethod getMethod)
        {
            var map   = new CoseHeaderMap();
            var label = new CoseHeaderLabel(knownHeader);

            SetEncodedValue(map, label, encodedValue, setMethod);

            ReadOnlySpan <byte> returnedEncocedValue = GetEncodedValue(map, label, getMethod);

            AssertExtensions.SequenceEqual(encodedValue, returnedEncocedValue);
        }
Пример #16
0
 public ValueClassProperty(string name, GetValueMethod getValue, SetValueMethod setValue) : base(name, getValue, setValue)
 {
 }
Пример #17
0
 public ClassValueStructProperty(string name, GetValueMethod getValue, SetValueMethod setValue)
     : base(name, getValue, setValue)
 {
 }
Пример #18
0
 public Property(string name, GetValueMethod getValue, SetValueMethod setValue)
     : this(name)
 {
     m_GetValue = getValue;
     m_SetValue = setValue;
 }
 public StructEnumProperty(string name, GetValueMethod getValue, SetValueMethod setValue) : base(name, getValue, setValue)
 {
 }
Пример #20
0
 public StructContainerProperty(string name, GetValueMethod getValue, SetValueMethod setValue) : base(name, getValue, setValue)
 {
 }
Пример #21
0
        internal override void Set(ILGenerator generator, OptimizationInfo optimizationInfo, bool rIU, Type valueType, SetValueMethod value)
        {
            if (_Type == null)
            {
                _Type = valueType;
            }
            else if (valueType != _Type)
            {
                // Essentially declaring a new variable.
                // This overwrites the name reference in the scope to the new variable.

                Scope.AddVariable(Name, valueType, null).Set(generator, optimizationInfo, rIU, valueType, value);

                return;
            }

            // Load the value:
            value(rIU);

            // Store it in the arg:
            generator.StoreArgument(ArgumentID);
        }
Пример #22
0
        /// <summary>
        /// Stores the value on the top of the stack in the reference.
        /// </summary>
        /// <param name="generator"> The generator to output the CIL to. </param>
        /// <param name="optimizationInfo"> Information about any optimizations that should be performed. </param>
        /// <param name="valueType"> The primitive type of the value that is on the top of the stack. </param>
        /// <param name="throwIfUnresolvable"> <c>true</c> to throw a ReferenceError exception if
        /// the name is unresolvable; <c>false</c> to create a new property instead. </param>
        public void GenerateSet(ILGenerator generator, OptimizationInfo optimizationInfo, bool rIU, Type valueType, SetValueMethod value, bool throwIfUnresolvable)
        {
            if (Variable == null)
            {
                // Create a new variable:
                Variable = Scope.AddVariable(Name, valueType, null);
            }
            else if (Variable.IsHoisted(Scope))
            {
                Wrench.Log.Add("Attempted to set a hoisted variable. This is slow and is usually unnecessary. Ignoring it by treating it as a local.");

                // This happens in this situation:
                // function a(){
                //  var b="hello!";
                //  var test=function(){
                //     b="overwriting a hoisted variable here (Nitrassic treats it as a new local in test's scope)";
                //   };
                //  // Nitrassic is wrong if this happens:
                //  test();
                //  console.log(b); // *should* be the value set from inside test
                // }

                // Create a new variable:
                Variable = Scope.AddVariable(Name, valueType, null);
            }

            // Output a set now:
            Variable.Set(generator, optimizationInfo, rIU, valueType, value);
        }
Пример #23
0
 protected DelegateValueClassPropertyBase(string name, GetValueMethod getValue, SetValueMethod setValue)
     : base(name)
 {
     m_GetValue = getValue;
     m_SetValue = setValue;
 }
Пример #24
0
 public void AddMethod(Type _type, SetValueMethod _method)
 {
     __methods[_type] = _method;
 }
Пример #25
0
        internal override void Set(ILGenerator generator, OptimizationInfo optimizationInfo, bool rIU, Type valueType, SetValueMethod value)
        {
            // Resolve if needed:
            if (Field == null && Property == null)
            {
                Resolve(valueType);
            }
            else
            {
                // Update type, potentially forcing the variable to change type
                // (unless it's a fixed property, in which case the value being set changes type instead):
                if (Property == null)
                {
                    // Fixed property - var changes type:
                    Type = valueType;
                }
            }

            ILLocalVariable localVar = null;

            // Is the return value of this set in use?
            // E.g. var x=obj.x=14;
            if (rIU)
            {
                // It's in use and 'obj.x' is not static.

                // Output the value twice:
                value(true);

                // If it's not static then we now have [obj][value][value] on the stack.
                // Calling the set method would fail (as it'll operate on the duplicated value).
                // So, we have to pop one off and re-add it after.

                if (!IsStatic)
                {
                    // Note that if it's static, no 'obj' reference goes onto the stack anyway.

                    // At this point, we have our target object followed by two copies of the value.
                    // For the following Call/StoreField to work correctly, we must store the 2nd copy into a local.
                    localVar = generator.DeclareVariable(valueType);

                    // Store into the local:
                    generator.StoreVariable(localVar);
                }
            }
            else
            {
                // Output the value just once:
                value(false);
            }

            if (_Type != valueType)
            {
                // Convert if needed:
                EmitConversion.Convert(generator, valueType, _Type, optimizationInfo);
            }

            if (Property != null)
            {
                // Call the set method:
                generator.Call(Property.SetMethod);
            }
            else
            {
                // Load the field:
                generator.StoreField(Field);
            }

            if (localVar != null)
            {
                // Reload the 2nd copy of the value:
                generator.LoadVariable(localVar);
            }
        }