Ejemplo n.º 1
0
        public void OverloadTest_WithoutObjects()
        {
            Script s = new Script();

            // Create an instance of the overload resolver
            var ov = new OverloadedMethodMemberDescriptor("Method1", this.GetType());

            // Iterate over the two methods through reflection
            foreach (var method in this.GetType().GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                     .Where(mi => mi.Name == "Method1"))
            {
                ov.AddOverload(new MethodMemberDescriptor(method));
            }

            // Creates the callback over the 'this' object
            DynValue callback = DynValue.NewCallback(ov.GetCallbackFunction(s, this));

            s.Globals.Set("func", callback);

            // Execute and check the results.
            DynValue result = s.DoString("return func(), func(17)");

            Assert.AreEqual(DataType.Tuple, result.Type);
            Assert.AreEqual(DataType.Number, result.Tuple[0].Type);
            Assert.AreEqual(DataType.Number, result.Tuple[1].Type);
            Assert.AreEqual(1, result.Tuple[0].Number);
            Assert.AreEqual(22, result.Tuple[1].Number);
        }
Ejemplo n.º 2
0
        void AddOverloadedMethod(Script s, string name)
        {
            var ov = new OverloadedMethodMemberDescriptor(name, Type);

            foreach (var method in Type.GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                     .Where(mi => mi.Name == name))
            {
                ov.AddOverload(new MethodMemberDescriptor(method));
            }
            vars.Add(DynValue.NewString(name), DynValue.NewCallback(ov.GetCallbackFunction(s)));
        }
Ejemplo n.º 3
0
        private void AddMemberTo(Dictionary <string, IMemberDescriptor> members, string name, IMemberDescriptor desc)
        {
            IOverloadableMemberDescriptor odesc = desc as IOverloadableMemberDescriptor;

            if (odesc != null)
            {
                if (members.ContainsKey(name))
                {
                    OverloadedMethodMemberDescriptor overloads = members[name] as OverloadedMethodMemberDescriptor;

                    if (overloads != null)
                    {
                        overloads.AddOverload(odesc);
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Multiple members named {0} are being added to type {1} and one or more of these members do not support overloads.", name, this.Type.FullName));
                    }
                }
                else
                {
                    members.Add(name, new OverloadedMethodMemberDescriptor(name, this.Type, odesc));
                }
            }
            else
            {
                if (members.ContainsKey(name))
                {
                    throw new ArgumentException(string.Format("Multiple members named {0} are being added to type {1} and one or more of these members do not support overloads.", name, this.Type.FullName));
                }
                else
                {
                    members.Add(name, desc);
                }
            }
        }
Ejemplo n.º 4
0
		public void OverloadTest_WithoutObjects()
		{
			Script s = new Script();

			// Create an instance of the overload resolver
			var ov = new OverloadedMethodMemberDescriptor("Method1", this.GetType());

			// Iterate over the two methods through reflection
			foreach(var method in this.GetType().GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
				.Where(mi => mi.Name == "Method1"))
			{
				ov.AddOverload(new MethodMemberDescriptor(method));
			}

			// Creates the callback over the 'this' object
			DynValue callback = DynValue.NewCallback(ov.GetCallbackFunction(s, this)); 
			s.Globals.Set("func", callback);

			// Execute and check the results.
			DynValue result = s.DoString("return func(), func(17)");

			Assert.AreEqual(DataType.Tuple, result.Type);
			Assert.AreEqual(DataType.Number, result.Tuple[0].Type);
			Assert.AreEqual(DataType.Number, result.Tuple[1].Type);
			Assert.AreEqual(1, result.Tuple[0].Number);
			Assert.AreEqual(22, result.Tuple[1].Number);
		}