Exemplo n.º 1
0
        public void ShouldMaintainContextEvenWhenNestingCalls()
        {
            dynamic obj  = new ProtoObject();
            dynamic obj2 = new ProtoObject();

            obj.Prototype.PrintName = (Func <String>)(() => {
                return(Proto.CurrentContext.Name);
            });

            obj.Prototype.PrintAge = (Func <String>)(() => {
                return(Proto.CurrentContext.Age);
            });

            obj.Name  = "OBJ_1";
            obj.Age   = "32";
            obj2.Name = "OBJ_2";
            obj2.Age  = "12";

            obj.PrintAll = (Func <String>)(() => {
                return(String.Format("{0}|{1}|{2}|{3}",
                                     Proto.CurrentContext.PrintName(), obj2.PrintName(),
                                     Proto.CurrentContext.PrintAge(), obj2.PrintAge()));
            });

            Assert.Equal("OBJ_1|OBJ_2|32|12", obj.PrintAll());
        }
Exemplo n.º 2
0
        public void ShouldMaintainCorrectContextEvenWithMultipleThreads()
        {
            dynamic obj = new ProtoObject();

            obj.Prototype.Name = "";
            obj.Prototype.Say  = (Func <String>)(() => {
                dynamic @this = Proto.CurrentContext;

                return(String.Format("Say: {0}", @this.Name));
            });

            var objects = new List <dynamic>();

            //Using 1000 here to ensure at least 2 threads are used
            for (int i = 0; i < 1000; i++)
            {
                dynamic newObj = new ProtoObject();
                newObj.Name = i;
                objects.Add(newObj);
            }

            Parallel.ForEach(objects, target => {
                var expected = "Say: " + target.Name;
                Assert.Equal(expected, target.Say());
            });
        }
Exemplo n.º 3
0
        public void ShouldMaintainContextEvenWhenExceptionsAreThrownFromInnerCalls()
        {
            dynamic obj  = new ProtoObject();
            dynamic obj2 = new ProtoObject();

            obj.Prototype.PrintName = (Func <String>)(() => {
                return(Proto.CurrentContext.Name);
            });

            obj.Prototype.Throws = (Action)(() => {
                throw new InvalidOperationException(String.Format("{0}: Threw an exception", Proto.CurrentContext.Name));
            });

            obj.Prototype.CheckValues = (Action)(() => {
                var name = Proto.CurrentContext.PrintName();

                try
                {
                    Proto.CurrentContext.Throws();
                    Assert.True(false, "Previous line should prevent this from executing");
                }
                catch (Exception ex)
                {
                    Assert.Equal(Proto.CurrentContext.Name + ": Threw an exception", ex.Message);
                }

                Assert.Equal(name, Proto.CurrentContext.Name);
            });

            obj.Name  = "OBJ_1";
            obj2.Name = "OBJ_2";

            obj.CheckValues();
            obj2.CheckValues();
        }
Exemplo n.º 4
0
        public void PrototypeMembersShouldBeAvailableToAllObjects()
        {
            dynamic obj = new ProtoObject();
            dynamic obj2 = new ProtoObject();

            obj.Prototype.Name = "Josh";

            Assert.Equal("Josh", obj.Name);
            Assert.Equal("Josh", obj2.Name);
        }
Exemplo n.º 5
0
        public void ShouldClearCurrentContextEvenIfExceptionIsThrownDuringInvocation()
        {
            dynamic obj = new ProtoObject();

            obj.Prototype.Throw = (Action)(() => { throw new Exception("It Blew Up!"); });

            Assert.Null(Proto.CurrentContext);
            Assert.Throws <Exception>(() => obj.Throw());
            Assert.Null(Proto.CurrentContext);
        }
Exemplo n.º 6
0
        public void PrototypeMembersShouldBeAvailableToAllObjects()
        {
            dynamic obj  = new ProtoObject();
            dynamic obj2 = new ProtoObject();

            obj.Prototype.Name = "Josh";

            Assert.Equal("Josh", obj.Name);
            Assert.Equal("Josh", obj2.Name);
        }
Exemplo n.º 7
0
        public void ShouldBeAbleToOverwriteMembers()
        {
            dynamic obj = new ProtoObject();

            obj.Name = "Josh";

            Assert.Equal("Josh", obj.Name);

            obj.Name = 56;

            Assert.Equal(56, obj.Name);
        }
Exemplo n.º 8
0
        public void InstanceMembersShouldOverridePrototypeMembers()
        {
            dynamic obj = new ProtoObject();
            dynamic obj2 = new ProtoObject();
            dynamic obj3 = new ProtoObject();

            obj.Prototype.Name = "Josh";
            obj2.Name = "Dan";
            obj3.Name = "Kathy";

            Assert.Equal("Josh", obj.Name);
            Assert.Equal("Dan", obj2.Name);
            Assert.Equal("Kathy", obj3.Name);
        }
Exemplo n.º 9
0
        public void InstanceMembersShouldOverridePrototypeMembers()
        {
            dynamic obj  = new ProtoObject();
            dynamic obj2 = new ProtoObject();
            dynamic obj3 = new ProtoObject();

            obj.Prototype.Name = "Josh";
            obj2.Name          = "Dan";
            obj3.Name          = "Kathy";

            Assert.Equal("Josh", obj.Name);
            Assert.Equal("Dan", obj2.Name);
            Assert.Equal("Kathy", obj3.Name);
        }
Exemplo n.º 10
0
        }                                                                       //全局变量


        public Script()
        {
            Global = new ScriptGlobal();

            TypeObject      = new ScriptTypeObject("Object");
            TypeObjectValue = new ScriptValue(TypeObject);
            Global.SetValue(TypeObject.TypeName, TypeObjectValue);

            TypeBoolean      = new ScriptBasicType("Bool", TypeObjectValue);
            TypeBooleanValue = new ScriptValue(TypeBoolean);
            Global.SetValue(TypeBoolean.TypeName, TypeBooleanValue);

            TypeNumber      = new ScriptBasicType("Number", TypeObjectValue);
            TypeNumberValue = new ScriptValue(TypeNumber);
            Global.SetValue(TypeNumber.TypeName, TypeNumberValue);

            TypeString      = new ScriptBasicType("String", TypeObjectValue);
            TypeStringValue = new ScriptValue(TypeString);
            Global.SetValue(TypeString.TypeName, TypeStringValue);

            TypeArray      = new ScriptBasicType("Array", TypeObjectValue);
            TypeArrayValue = new ScriptValue(TypeArray);
            Global.SetValue(TypeArray.TypeName, TypeArrayValue);

            TypeMap      = new ScriptBasicType("Map", TypeObjectValue);
            TypeMapValue = new ScriptValue(TypeMap);
            Global.SetValue(TypeMap.TypeName, TypeMapValue);

            TypeFunction      = new ScriptBasicType("Function", TypeObjectValue);
            TypeFunctionValue = new ScriptValue(TypeFunction);
            Global.SetValue(TypeFunction.TypeName, TypeFunctionValue);

            Global.SetValue(GLOBAL_NAME, new ScriptValue(Global));
            Global.SetValue(GLOBAL_SCRIPT, ScriptValue.CreateValue(this));
            Global.SetValue(GLOBAL_VERSION, ScriptValue.CreateValue(typeof(Version)));

            ProtoObject.Load(this, TypeObject);
            ProtoBoolean.Load(this, TypeBoolean);
            ProtoNumber.Load(this, TypeNumber);
            ProtoString.Load(this, TypeString);
            ProtoArray.Load(this, TypeArray);
            ProtoMap.Load(this, TypeMap);
            ProtoFunction.Load(this, TypeFunction);

            TypeManager.PushAssembly(typeof(object).Assembly);                        //mscorlib.dll
            TypeManager.PushAssembly(typeof(System.Net.Sockets.Socket).Assembly);     //System.dll
            TypeManager.PushAssembly(GetType().Assembly);                             //当前所在的程序集
            LoadLibrary();
        }
Exemplo n.º 11
0
        public void PrototypeMembersShouldBeAvailableToDerivedTypes()
        {
            dynamic obj = new ProtoObject();

            obj.Prototype.Name = "Josh";

            dynamic person = new Person();
            dynamic emp = new Employee();
            dynamic sales = new Salesman();
            dynamic mgr = new Manager();

            Assert.Equal("Josh", person.Name);
            Assert.Equal("Josh", emp.Name);
            Assert.Equal("Josh", sales.Name);
            Assert.Equal("Josh", mgr.Name);
        }
Exemplo n.º 12
0
        public void PrototypeMembersShouldBeAvailableToDerivedTypes()
        {
            dynamic obj = new ProtoObject();

            obj.Prototype.Name = "Josh";

            dynamic person = new Person();
            dynamic emp    = new Employee();
            dynamic sales  = new Salesman();
            dynamic mgr    = new Manager();

            Assert.Equal("Josh", person.Name);
            Assert.Equal("Josh", emp.Name);
            Assert.Equal("Josh", sales.Name);
            Assert.Equal("Josh", mgr.Name);
        }
Exemplo n.º 13
0
        public void ShouldBeAbleToAddMembersDynamically()
        {
            dynamic obj = new ProtoObject();

            obj.Name = "Josh";

            Assert.Equal("Josh", obj.Name);

            obj.Age = 34;

            Assert.Equal(34, obj.Age);

            obj.Is21 = (Func <Boolean>)(() => obj.Age >= 21);

            Assert.True(obj.Is21());
        }
Exemplo n.º 14
0
        public void ShouldBeAbleToAccessInstanceFromCurrentContext()
        {
            dynamic obj  = new ProtoObject();
            dynamic obj2 = new ProtoObject();
            dynamic obj3 = new ProtoObject();

            obj.Prototype.Name     = "Josh";
            obj.Prototype.SayHello = (Func <String>)(() => {
                dynamic @this = Proto.CurrentContext;

                return(String.Format("Hello, {0}!", @this.Name));
            });

            obj2.Name = "Dan";
            obj3.Name = "Kathy";

            Assert.Equal("Hello, Josh!", obj.SayHello());
            Assert.Equal("Hello, Dan!", obj2.SayHello());
            Assert.Equal("Hello, Kathy!", obj3.SayHello());
        }
Exemplo n.º 15
0
        public Script()
        {
            Global = new ScriptGlobal();

            TypeObject      = new ScriptTypeObject("Object");
            TypeObjectValue = new ScriptValue(TypeObject);
            Global.SetValue(TypeObject.TypeName, TypeObjectValue);

            AddPrimitivePrototype("Bool", ref m_TypeBool, ref m_TypeValueBool);
            AddPrimitivePrototype("Number", ref m_TypeNumber, ref m_TypeValueNumber);
            AddPrimitivePrototype("String", ref m_TypeString, ref m_TypeValueString);
            AddPrimitivePrototype("Function", ref m_TypeFunction, ref m_TypeValueFunction);

            AddBasicPrototype(m_TypeArray         = new ScriptTypeBasicArray(this, "Array", TypeObjectValue), ref m_TypeValueArray);
            AddBasicPrototype(m_TypeMap           = new ScriptTypeBasicMap(this, "Map", TypeObjectValue), ref m_TypeValueMap);
            AddBasicPrototype(m_TypeStringBuilder = new ScriptTypeBasicStringBuilder(this, "StringBuilder", TypeObjectValue), ref m_TypeValueStringBuilder);

            Global.SetValue(GLOBAL_NAME, new ScriptValue(Global));
            Global.SetValue(GLOBAL_SCRIPT, ScriptValue.CreateValue(this));
            Global.SetValue(GLOBAL_VERSION, ScriptValue.CreateValue(typeof(Version)));

            ProtoObject.Load(this, TypeObject);
            ProtoBoolean.Load(this, TypeBoolean);
            ProtoNumber.Load(this, TypeNumber);
            ProtoString.Load(this, TypeString);
            ProtoArray.Load(this, TypeArray);
            ProtoMap.Load(this, TypeMap);
            ProtoFunction.Load(this, TypeFunction);
            ProtoStringBuilder.Load(this, TypeStringBuilder);

            TypeManager.PushAssembly(typeof(object).Assembly);                        //mscorlib.dll
            TypeManager.PushAssembly(typeof(System.Net.Sockets.Socket).Assembly);     //System.dll
            TypeManager.PushAssembly(GetType().Assembly);                             //当前所在的程序集

            LibraryBasis.Load(this);
            LibraryJson.Load(this);
            LibraryMath.Load(this);
            LibraryUserdata.Load(this);
            LibraryIO.Load(this);
        }
Exemplo n.º 16
0
 public void ShouldBeAbleToCreateInstanceOfProtoObject()
 {
     var obj = new ProtoObject();
 }
Exemplo n.º 17
0
        public void ShouldBeAbleToOverwriteMembers()
        {
            dynamic obj = new ProtoObject();

            obj.Name = "Josh";

            Assert.Equal("Josh", obj.Name);

            obj.Name = 56;

            Assert.Equal(56, obj.Name);
        }
Exemplo n.º 18
0
        public void ShouldClearCurrentContextEvenIfExceptionIsThrownDuringInvocation()
        {
            dynamic obj = new ProtoObject();

            obj.Prototype.Throw = (Action)(() => { throw new Exception("It Blew Up!"); });

            Assert.Null(Proto.CurrentContext);
            Assert.Throws<Exception>(() => obj.Throw());
            Assert.Null(Proto.CurrentContext);
        }
Exemplo n.º 19
0
        public void ShouldMaintainContextEvenWhenExceptionsAreThrownFromInnerCalls()
        {
            dynamic obj = new ProtoObject();
            dynamic obj2 = new ProtoObject();

            obj.Prototype.PrintName = (Func<String>)(() => {
                return Proto.CurrentContext.Name;
            });

            obj.Prototype.Throws = (Action)(() => {
                throw new InvalidOperationException(String.Format("{0}: Threw an exception", Proto.CurrentContext.Name));
            });

            obj.Prototype.CheckValues = (Action)(() => {
                var name = Proto.CurrentContext.PrintName();

                try
                {
                    Proto.CurrentContext.Throws();
                    Assert.True(false, "Previous line should prevent this from executing");
                }
                catch(Exception ex)
                {
                    Assert.Equal(Proto.CurrentContext.Name + ": Threw an exception", ex.Message);
                }

                Assert.Equal(name, Proto.CurrentContext.Name);
            });

            obj.Name = "OBJ_1";
            obj2.Name = "OBJ_2";

            obj.CheckValues();
            obj2.CheckValues();
        }
Exemplo n.º 20
0
        public void ShouldReturnUndefinedForMembersThatDoNotExist()
        {
            dynamic obj = new ProtoObject();

            Assert.Equal(Proto.Undefined, obj.NoWhereElse);
        }
Exemplo n.º 21
0
        public void ShouldMaintainContextEvenWhenNestingCalls()
        {
            dynamic obj = new ProtoObject();
            dynamic obj2 = new ProtoObject();

            obj.Prototype.PrintName = (Func<String>)(() => {
                return Proto.CurrentContext.Name;
            });

            obj.Prototype.PrintAge = (Func<String>)(() => {
                return Proto.CurrentContext.Age;
            });

            obj.Name = "OBJ_1";
            obj.Age = "32";
            obj2.Name = "OBJ_2";
            obj2.Age = "12";

            obj.PrintAll = (Func<String>)(() => {
                return String.Format("{0}|{1}|{2}|{3}",
                    Proto.CurrentContext.PrintName(), obj2.PrintName(),
                    Proto.CurrentContext.PrintAge(), obj2.PrintAge());
            });

            Assert.Equal("OBJ_1|OBJ_2|32|12", obj.PrintAll());
        }
Exemplo n.º 22
0
        public void ShouldMaintainCorrectContextEvenWithMultipleThreads()
        {
            dynamic obj = new ProtoObject();
            obj.Prototype.Name = "";
            obj.Prototype.Say = (Func<String>)(() => {
                dynamic @this = Proto.CurrentContext;

                return String.Format("Say: {0}", @this.Name);
            });

            var objects = new List<dynamic>();

            //Using 1000 here to ensure at least 2 threads are used
            for (int i = 0; i < 1000; i++)
            {
                dynamic newObj = new ProtoObject();
                newObj.Name = i;
                objects.Add(newObj);
            }

            Parallel.ForEach(objects, target => {
                var expected = "Say: " + target.Name;
                Assert.Equal(expected, target.Say());
            });
        }
Exemplo n.º 23
0
 public void ShouldBeAbleToCreateInstanceOfProtoObject()
 {
     var obj = new ProtoObject();
 }
Exemplo n.º 24
0
        public void ShouldReturnUndefinedForMembersThatDoNotExist()
        {
            dynamic obj = new ProtoObject();

            Assert.Equal(Proto.Undefined, obj.NoWhereElse);
        }
Exemplo n.º 25
0
        public void ShouldBeAbleToAddMembersDynamically()
        {
            dynamic obj = new ProtoObject();

            obj.Name = "Josh";

            Assert.Equal("Josh", obj.Name);

            obj.Age = 34;

            Assert.Equal(34, obj.Age);

            obj.Is21 = (Func<Boolean>)(() => obj.Age >= 21);

            Assert.True(obj.Is21());
        }
Exemplo n.º 26
0
        public void ShouldBeAbleToAccessInstanceFromCurrentContext()
        {
            dynamic obj = new ProtoObject();
            dynamic obj2 = new ProtoObject();
            dynamic obj3 = new ProtoObject();

            obj.Prototype.Name = "Josh";
            obj.Prototype.SayHello = (Func<String>)(() => {
                dynamic @this = Proto.CurrentContext;

                return String.Format("Hello, {0}!", @this.Name);
            });

            obj2.Name = "Dan";
            obj3.Name = "Kathy";

            Assert.Equal("Hello, Josh!", obj.SayHello());
            Assert.Equal("Hello, Dan!", obj2.SayHello());
            Assert.Equal("Hello, Kathy!", obj3.SayHello());
        }