Esempio 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);
        }
Esempio 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)));
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Script"/> class.
        /// </summary>
        /// <param name="coreModules">The core modules to be pre-registered in the default global table.</param>
        public Script(CoreModules coreModules)
        {
            Options          = new ScriptOptions(DefaultOptions);
            PerformanceStats = new PerformanceStatistics();
            Registry         = new Table(this);

            m_ByteCode      = new ByteCode(this);
            m_MainProcessor = new Processor(this, m_GlobalTable, m_ByteCode);
            m_GlobalTable   = new Table(this).RegisterCoreModules(coreModules);
            foreach (var t in TypeDescriptorRegistry.RegisteredTypes)
            {
                Type valType = t.Value.Type;
                if (valType.Assembly.FullName != typeof(Script).Assembly.FullName)
                {
                    // m_GlobalTable.RegisterModuleType(t.Value.Type);
                    if (t.Value is StandardUserDataDescriptor)
                    {
                        StandardUserDataDescriptor desc = (StandardUserDataDescriptor)t.Value;
                        foreach (var member in desc.Members)
                        {
                            if (member.Value is MethodMemberDescriptor)
                            {
                                MethodMemberDescriptor methDesc = (MethodMemberDescriptor)member.Value;
                                if (methDesc.IsConstructor)
                                {
                                    m_GlobalTable.Set(methDesc.Name, methDesc.GetCallbackAsDynValue(this));
                                }
                            }
                            else if (member.Value is OverloadedMethodMemberDescriptor)
                            {
                                OverloadedMethodMemberDescriptor methDesc = (OverloadedMethodMemberDescriptor)member.Value;
                                foreach (var overloadDesc in methDesc.m_Overloads)
                                {
                                    if (overloadDesc is MethodMemberDescriptor)
                                    {
                                        MethodMemberDescriptor actualDesc = (MethodMemberDescriptor)overloadDesc;
                                        if (actualDesc.IsConstructor)
                                        {
                                            m_GlobalTable.Set(desc.FriendlyName, DynValue.NewCallback(methDesc.GetCallbackFunction(this)));
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio 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);
		}