예제 #1
0
        private static ClassBinding BindImpl(Type type)
        {
            var classAttrib = type.Attribute <MondClassAttribute>();

            if (classAttrib == null)
            {
                throw new MondBindingException(BindingError.TypeMissingAttribute, "MondClass");
            }

            var className = classAttrib.Name ?? type.Name;

            var functions = new Dictionary <string, MondInstanceFunction>();

            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);

            foreach (var method in MondFunctionBinder.BindInstance(className, methods, type))
            {
                var name = method.Item1;

                if (functions.ContainsKey(name))
                {
                    throw new MondBindingException(BindingError.DuplicateDefinition, name);
                }

                functions[name] = method.Item2;
            }

            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties.PropertyMethods())
            {
                var name = property.Item1;

                if (functions.ContainsKey(name))
                {
                    throw new MondBindingException(BindingError.DuplicateDefinition, name);
                }

                var propertyArray = new[] { property.Item2 };

                var propertyBinding = MondFunctionBinder.BindInstance(className, propertyArray, type, MondFunctionBinder.MethodType.Property, name)
                                      .FirstOrDefault();

                if (propertyBinding != null)
                {
                    functions[name] = propertyBinding.Item2;
                }
            }

            var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
            var constructor  = MondFunctionBinder.BindConstructor(className, constructors);

            return(new ClassBinding(className, constructor, functions));
        }
예제 #2
0
        private static IEnumerable <Tuple <string, MondInstanceFunction> > BindImpl(Type type)
        {
            var moduleAttrib = type.Attribute <MondModuleAttribute>();

            if (moduleAttrib == null)
            {
                throw new MondBindingException(BindingError.TypeMissingAttribute, "MondModule");
            }

            var moduleName = moduleAttrib.Name ?? type.Name;

            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);

            return(MondFunctionBinder.BindInstance(moduleName, methods));
        }
예제 #3
0
        public static MondFunction Bind(Type type, out MondValue prototype)
        {
            var classAttrib = type.Attribute <MondClassAttribute>();

            if (classAttrib == null)
            {
                throw new Exception("Type does not have the MondClass attribute");
            }

            var className = classAttrib.Name ?? type.Name;

            MondFunction constructor = null;

            prototype = new MondValue(MondValueType.Object);

            foreach (var method in type.GetMethods())
            {
                var functionAttrib = method.Attribute <MondFunctionAttribute>();

                if (functionAttrib == null)
                {
                    continue;
                }

                var name = functionAttrib.Name ?? method.Name;
                prototype[name] = MondFunctionBinder.BindInstance(className, name, type, method);
            }

            foreach (var property in type.GetProperties())
            {
                var functionAttrib = property.Attribute <MondFunctionAttribute>();

                if (functionAttrib == null)
                {
                    continue;
                }

                var name = functionAttrib.Name ?? property.Name;

                var getMethod = property.GetGetMethod();
                var setMethod = property.GetSetMethod();

                if (getMethod != null && getMethod.IsPublic)
                {
                    prototype["get" + name] = MondFunctionBinder.BindInstance(className, name, type, getMethod);
                }

                if (setMethod != null && setMethod.IsPublic)
                {
                    prototype["set" + name] = MondFunctionBinder.BindInstance(className, name, type, setMethod);
                }
            }

            foreach (var ctor in type.GetConstructors())
            {
                var constructorAttrib = ctor.Attribute <MondClassConstructorAttribute>();

                if (constructorAttrib == null)
                {
                    continue;
                }

                if (constructor != null)
                {
                    throw new Exception("Classes can not have multiple Mond constructors");
                }

                constructor = MondFunctionBinder.BindConstructor(className, ctor, prototype);
            }

            if (constructor == null)
            {
                throw new Exception("Classes must have one Mond constructor");
            }

            return(constructor);
        }