Ejemplo n.º 1
0
        public ArrayFunction(IContext context)
            : base(null, null, context)
        {
            var prototype = new DynamicObject();

            if (context != null)
            {
                var obj = context.GetValue("Object");

                if (obj != null && obj is DynamicObject)
                {
                    var superproto = ((DynamicObject)obj).GetValue("prototype");
                    if (superproto != null && superproto is DynamicObject)
                        prototype.SetValue("prototype", superproto);
                }
            }

            this.SetValue("isArray", isArrayFunction);
            this.SetValue("prototype", prototype);
            prototype.SetValue("push", pushFunction);
            prototype.SetValue("pop", popFunction);
            prototype.SetValue("unshift", unshiftFunction);
            prototype.SetValue("shift", shiftFunction);
            prototype.SetValue("join", joinFunction);
            prototype.SetValue("slice", sliceFunction);
        }
Ejemplo n.º 2
0
        public ObjectFunction(IContext context)
            : base(null, null, context)
        {
            var prototype = new DynamicObject();

            this.SetValue("prototype", prototype);
            prototype.SetValue("toString", toStringFunction);
        }
Ejemplo n.º 3
0
 public void GetValueFromPrototypeAndSetValueInObject()
 {
     DynamicObject prototype = new DynamicObject();
     this.function.SetValue("prototype", prototype);
     prototype.SetValue("x", 10);
     this.dynobj.SetValue("x", 20);
     Assert.AreEqual(20, this.dynobj.GetValue("x"));
     Assert.AreEqual(10, prototype.GetValue("x"));
 }
Ejemplo n.º 4
0
        public object Evaluate(IContext context)
        {
            DynamicObject obj = new DynamicObject((IFunction)context.RootContext.GetValue("Object"));

            for (int k = 0; k < this.names.Count; k++)
                obj.SetValue(this.names[k], this.expressions[k].Evaluate(context));

            return obj;
        }
Ejemplo n.º 5
0
        public void EvaluateWithThisObject()
        {
            Context context = new Context();
            ICommand body = new ReturnCommand(new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("x"), new DotExpression(new VariableExpression("this"), "Age")));
            Function function = new Function(new string[] { "x" }, body);
            DynamicObject person = new DynamicObject();
            person.SetValue("Age", 30);

            Assert.AreEqual(40, function.Invoke(context, person, new object[] { 10 }));
        }
Ejemplo n.º 6
0
 public void GetValueFromPrototypeAndSetValueInObject()
 {
     Function function = new Function(null, null);
     DynamicObject dynobj = new DynamicObject(function);
     DynamicObject prototype = new DynamicObject();
     function.SetValue("prototype", prototype);
     prototype.SetValue("x", 10);
     dynobj.SetValue("x", 20);
     Assert.AreEqual(20, dynobj.GetValue("x"));
     Assert.AreEqual(10, prototype.GetValue("x"));
 }
Ejemplo n.º 7
0
        public void DefineMethod()
        {
            DynamicObject dynobj = new DynamicObject();

            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            dynobj.SetValue("GetName", function);

            object result = dynobj.GetValue("GetName");

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ICallable));
            Assert.IsTrue(result == function);
        }
Ejemplo n.º 8
0
        public void DetectNativeMethods()
        {
            DynamicObject dynobj = new DynamicObject();

            Assert.IsTrue(dynobj.IsNativeMethod("ToString"));
            Assert.IsTrue(dynobj.IsNativeMethod("GetHashCode"));
            Assert.IsTrue(dynobj.IsNativeMethod("Equals"));

            Assert.IsTrue(dynobj.IsNativeMethod("GetValue"));
            Assert.IsTrue(dynobj.IsNativeMethod("SetValue"));
            Assert.IsTrue(dynobj.IsNativeMethod("GetNames"));
            Assert.IsTrue(dynobj.IsNativeMethod("Invoke"));

            Assert.IsFalse(dynobj.IsNativeMethod("Foo"));
        }
Ejemplo n.º 9
0
        public void GetNames()
        {
            DynamicObject dynobj = new DynamicObject();

            dynobj.SetValue("FirstName", "Adam");
            dynobj.SetValue("LastName", "Genesis");

            ICollection<string> names = dynobj.GetNames();

            Assert.IsNotNull(names);
            Assert.AreEqual(2, names.Count);

            Assert.IsTrue(names.Contains("FirstName"));
            Assert.IsTrue(names.Contains("LastName"));
        }
Ejemplo n.º 10
0
        public void EvaluateArrayDotExpression()
        {
            Context context = new Context();

            DynamicObject data = new DynamicObject();
            data.SetValue("Numbers", new string[] { "one", "two", "three" });

            context.SetValue("data", data);

            IExpression expression = new IndexedExpression(new DotExpression(new VariableExpression("data"), "Numbers"), new IExpression[] { new ConstantExpression(1) });

            object result = expression.Evaluate(context);

            Assert.IsNotNull(result);
            Assert.AreEqual("two", result);
        }
Ejemplo n.º 11
0
        public void ExecuteDeleteCommand()
        {
            DynamicObject dynobj = new DynamicObject();
            dynobj.SetValue("name", "Adam");
            dynobj.SetValue("age", 800);

            DeleteCommand cmd = new DeleteCommand(new DotExpression(new VariableExpression("adam"), "name"));

            var context = new Context();
            context.SetValue("adam", dynobj);

            cmd.Execute(context);

            Assert.IsTrue(dynobj.HasName("age"));
            Assert.IsFalse(dynobj.HasName("name"));
        }
Ejemplo n.º 12
0
        public Function(string[] parameterNames, ICommand body, IContext context)
        {
            // TODO Review this cyclic reference
            this.Function = this;

            DynamicObject prototype = new DynamicObject();

            this.SetValue("prototype", functionPrototype);

            this.parameterNames = parameterNames;
            this.body = body;

            if (parameterNames == null)
                this.arity = 0;
            else
                this.arity = parameterNames.Length;

            this.context = context;
        }
Ejemplo n.º 13
0
        public override object NewInstance(object[] parameters)
        {
            var dynobj = new DynamicObject(this);

            if (parameters == null || parameters.Length == 0)
                return dynobj;

            var arg = parameters[0];

            if (arg == null)
                arg = "null";
            else
                arg = arg.ToString();

            var str = (string)arg;

            for (int k = 0; k < str.Length; k++)
                dynobj.SetValue(k.ToString(), str[k].ToString());

            dynobj.SetValue("length", str.Length, false);

            return dynobj;
        }
Ejemplo n.º 14
0
        public virtual object NewInstance(object[] parameters)
        {
            DynamicObject obj = new DynamicObject(this);

            return(this.Invoke(this.context, obj, parameters));
        }
Ejemplo n.º 15
0
 public void GetUndefinedForUndefinedValue()
 {
     DynamicObject dynobj = new DynamicObject();
     Assert.AreSame(Undefined.Instance, dynobj.GetValue("Foo"));
 }
Ejemplo n.º 16
0
 public virtual object NewInstance(object[] parameters)
 {
     DynamicObject obj = new DynamicObject(this);
     return this.Invoke(this.context, obj, parameters);
 }
Ejemplo n.º 17
0
        public void UsePrototypeForGetValue()
        {
            Function function = new Function(null, null);
            DynamicObject dynobj = new DynamicObject(function);

            DynamicObject prototype = new DynamicObject();
            function.SetValue("prototype", prototype);
            prototype.SetValue("x", 10);

            Assert.AreEqual(10, dynobj.GetValue("x"));
        }
Ejemplo n.º 18
0
        public void SetAndGetValue()
        {
            DynamicObject dynobj = new DynamicObject();

            dynobj.SetValue("Foo", "Bar");

            Assert.AreEqual("Bar", dynobj.GetValue("Foo"));
        }
Ejemplo n.º 19
0
 public void RemoveValue()
 {
     DynamicObject dynobj = new DynamicObject();
     dynobj.SetValue("name", "Adam");
     Assert.IsTrue(dynobj.HasName("name"));
     dynobj.RemoveValue("name");
     Assert.IsFalse(dynobj.HasName("name"));
     Assert.AreSame(Undefined.Instance, dynobj.GetValue("name"));
 }
Ejemplo n.º 20
0
        public void InvokeNativeMethod()
        {
            DynamicObject dynobj = new DynamicObject();

            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            dynobj.SetValue("Name", "Adam");

            object result = dynobj.Invoke("GetValue", new object[] { "Name" });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(string));
            Assert.AreEqual("Adam", result);
        }
Ejemplo n.º 21
0
 public void HasUndefinedName()
 {
     DynamicObject dynobj = new DynamicObject();
     Assert.IsFalse(dynobj.HasName("name"));
 }
Ejemplo n.º 22
0
        public void UsePrototypeForGetValue()
        {
            DynamicObject prototype = new DynamicObject();
            this.function.SetValue("prototype", prototype);
            prototype.SetValue("x", 10);

            Assert.AreEqual(10, this.dynobj.GetValue("x"));
        }
Ejemplo n.º 23
0
 public void HasName()
 {
     DynamicObject dynobj = new DynamicObject();
     dynobj.SetValue("name", "Adam");
     Assert.IsTrue(dynobj.HasName("name"));
 }
Ejemplo n.º 24
0
        private static object ResolveToObject(VariableExpression expression, IContext context)
        {
            string name = expression.Name;

            object obj = context.GetValue(name);

            if (obj == null || obj == Undefined.Instance)
            {
                obj = new DynamicObject();

                // TODO Review if Local or not
                context.SetValue(name, obj);
            }

            return obj;
        }
Ejemplo n.º 25
0
        private static object ResolveToObject(DotExpression expression, IContext context)
        {
            object obj = ResolveToObject(expression.Expression, context);

            if (obj is DynamicObject)
            {
                DynamicObject dynobj = (DynamicObject)obj;

                obj = dynobj.GetValue(expression.Name);

                if (obj == null || obj == Undefined.Instance)
                {
                    obj = new DynamicObject();
                    dynobj.SetValue(expression.Name, obj);
                }

                return obj;
            }

            return ObjectUtilities.GetValue(obj, expression.Name);
        }
Ejemplo n.º 26
0
 public void HasNameInPrototype()
 {
     Function function = new Function(null, null);
     DynamicObject dynobj = new DynamicObject(function);
     DynamicObject prototype = new DynamicObject();
     function.SetValue("prototype", prototype);
     prototype.SetValue("name", "Adam");
     Assert.IsTrue(dynobj.HasName("name"));
 }
Ejemplo n.º 27
0
 public void SetupDynamicObject()
 {
     this.function = new Function(null, null);
     this.dynobj = new DynamicObject(this.function);
 }