Example #1
0
        /// <summary>
        /// Parses the values of <see cref="InnerXml"/>, <see cref="Value"/> and <see cref="Attribute"/> into <see cref="IUpdateOperation"/>s.
        /// </summary>
        protected IUpdateOperation[] GetOperations()
        {
            if (!IsNullOrEmpty(InnerXml))
            {
                var ops = new IUpdateOperation[InnerXml.Length];
                for (int n = 0; n < InnerXml.Length; n++)
                {
                    ops[n] = new SetInnerXmlOperation(InnerXml[n], UseEmptyTag);
                }
                return(ops);
            }
            else if (!IsNullOrEmpty(Value))
            {
                var ops = new IUpdateOperation[Value.Length];
                if (!IsNullOrEmpty(Attribute))
                {
                    if (Attribute.Length > 1 && Attribute.Length != Value.Length)
                    {
                        throw new ArgumentException("Attribute must have only 1 item or be in 1:1 correspondance with Value");
                    }

                    var namespaceUri = !IsNullOrEmpty(AttributeNamespace) ? AttributeNamespace : new[] { "" };
                    if (namespaceUri.Length > 1 && namespaceUri.Length != Attribute.Length)
                    {
                        throw new ArgumentException("AttributeNamespace must have only 1 item or be in 1:1 correspondance with Attribute");
                    }

                    for (int n = 0; n < Value.Length; n++)
                    {
                        ops[n] = new SetAttributeOperation(Value[n], Attribute.Length == 1 ? Attribute[0] : Attribute[n], namespaceUri.Length == 1 ? namespaceUri[0] : namespaceUri[n]);
                    }
                }
                else
                {
                    for (int n = 0; n < Value.Length; n++)
                    {
                        ops[n] = new SetValueOperation(Value[n], UseEmptyTag);
                    }
                }
                return(ops);
            }
            else
            {
                return(new[] { new SetValueOperation(string.Empty, UseEmptyTag) });
            }
        }
Example #2
0
        public void MachineExection()
        {
            IScriptContext context = new ScriptContext();

            //Example 0: Machine

            ExecutableMachine machine = ExecutableMachine.Create();
            SetValueOperation sv      = machine.CreateOperation <SetValueOperation>();

            sv.Id = "a";

            machine.AX = "Hello World";
            machine.CreateOperation <RetOperation>();
            machine.Execute(context);

            object rez = context.GetItem("a", true);

            Assert.Equal("Hello World", rez);
        }
Example #3
0
        public void Benchmark()
        {
            IScriptContext context = new ScriptContext();

            //Example 0: Machine

            ExecutableMachine machine = ExecutableMachine.Create();

            int iterations = 2;// 10000000;

            //loops = 10000000
            ValueOperation op0 = machine.CreateOperation <ValueOperation>();

            op0.Value = iterations;
            SetValueOperation op1 = machine.CreateOperation <SetValueOperation>();

            op1.Id = "loops";

            // counter = 0
            ValueOperation op2 = machine.CreateOperation <ValueOperation>();

            op2.Value = 0;
            SetValueOperation op3 = machine.CreateOperation <SetValueOperation>();

            op3.Id = "counter";

            //while (loops > 0)
            RegisterOperation op4 = machine.CreateOperation <RegisterOperation>();

            op4.Destination = MachineRegisters.BX;
            op4.Value       = 0;
            GetValueOperation op5 = machine.CreateOperation <GetValueOperation>();

            op5.Id = "loops";
            CmpOperation   op6 = machine.CreateOperation <CmpOperation>();
            JmpIfOperation op7 = machine.CreateOperation <JmpIfOperation>();

            op7.Offset = 8;
            //loops = loops -1;
            GetValueOperation op8 = machine.CreateOperation <GetValueOperation>();

            op8.Id = "loops";
            DecOperation      op9  = machine.CreateOperation <DecOperation>();
            SetValueOperation op10 = machine.CreateOperation <SetValueOperation>();

            op10.Id = "loops";

            //counter = counter + 1;
            GetValueOperation op11 = machine.CreateOperation <GetValueOperation>();

            op11.Id = "counter";
            IncOperation      op12 = machine.CreateOperation <IncOperation>();
            SetValueOperation op13 = machine.CreateOperation <SetValueOperation>();

            op13.Id = "counter";

            JmpOperation op14 = machine.CreateOperation <JmpOperation>();

            op14.Offset = -10;

            machine.CreateOperation <RetOperation>();
            machine.Execute(context);

            object rez = context.GetItem("counter", true);

            Assert.Equal(iterations, rez);
        }