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); }
public ObjectFunction(IContext context) : base(null, null, context) { var prototype = new DynamicObject(); this.SetValue("prototype", prototype); prototype.SetValue("toString", toStringFunction); }
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")); }
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; }
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 })); }
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")); }
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); }
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")); }
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")); }
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); }
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")); }
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; }
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; }
public virtual object NewInstance(object[] parameters) { DynamicObject obj = new DynamicObject(this); return(this.Invoke(this.context, obj, parameters)); }
public void GetUndefinedForUndefinedValue() { DynamicObject dynobj = new DynamicObject(); Assert.AreSame(Undefined.Instance, dynobj.GetValue("Foo")); }
public virtual object NewInstance(object[] parameters) { DynamicObject obj = new DynamicObject(this); return this.Invoke(this.context, obj, parameters); }
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")); }
public void SetAndGetValue() { DynamicObject dynobj = new DynamicObject(); dynobj.SetValue("Foo", "Bar"); Assert.AreEqual("Bar", dynobj.GetValue("Foo")); }
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")); }
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); }
public void HasUndefinedName() { DynamicObject dynobj = new DynamicObject(); Assert.IsFalse(dynobj.HasName("name")); }
public void UsePrototypeForGetValue() { DynamicObject prototype = new DynamicObject(); this.function.SetValue("prototype", prototype); prototype.SetValue("x", 10); Assert.AreEqual(10, this.dynobj.GetValue("x")); }
public void HasName() { DynamicObject dynobj = new DynamicObject(); dynobj.SetValue("name", "Adam"); Assert.IsTrue(dynobj.HasName("name")); }
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; }
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); }
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")); }
public void SetupDynamicObject() { this.function = new Function(null, null); this.dynobj = new DynamicObject(this.function); }