Exemple #1
0
        public static void ImportExtensionMethodsIntoPackage(Package package, Type type)
        {
            VerifyNoMissingSymbols(package);

            var isbuiltin        = type.Assembly == Assembly.GetExecutingAssembly();
            var extendedType     = (Type)package.Dict["T"].CheckedValue;
            var restrictedImport = type.GetCustomAttributes(typeof(RestrictedImportAttribute), true).Length != 0;
            var names            = type.GetMethods(ImportBindingFlags).Select(x => x.Name).Distinct().ToArray();

            foreach (var name in names)
            {
                var methods = type.GetMember(name, ImportBindingFlags)
                              .Where(x => x is MethodInfo)
                              .Select(x => (MethodInfo)ResolveGenericMethod((MethodInfo)x))
                              .Where(x => ExtendsType(x, extendedType))
                              .ToList();

                if (methods.Count == 0)
                {
                    continue;
                }

                var importable = !restrictedImport || methods[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

                if (!importable)
                {
                    continue;
                }

                var sym     = package.FindOrCreate(name.LispName(), useMissing: true, excludeUseList: true, export: true);
                var builtin = sym.Value as ImportedFunction;

                if (builtin == null)
                {
                    sym.FunctionValue = builtin = new ImportedFunction(name, type);
                }

                if (isbuiltin)
                {
                    // Designed to go before other methods.
                    methods.AddRange(builtin.BuiltinExtensionMembers);
                    builtin.BuiltinExtensionMembers = methods.Distinct().ToArray();
                }
                else
                {
                    // todo: change order
                    // Goes after other methods as in c#.
                    methods.AddRange(builtin.ExternalExtensionMembers);
                    builtin.ExternalExtensionMembers = methods.Distinct().ToArray();
                }
            }
        }
Exemple #2
0
        public static void RestartBuiltins(Type type)
        {
            var flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public;
            var names = type.GetMethods(flags).Select(x => x.Name).Distinct();

            foreach (var name in names)
            {
                var members = type.GetMember(name, flags).OfType<MethodInfo>().Where(x => x.GetCustomAttributes(typeof(LispAttribute), false).Length != 0).ToArray();

                if (members.Length != 0)
                {
                    var pure = members[0].GetCustomAttributes(typeof(PureAttribute), false).Length != 0;
                    var lisp = members[0].GetCustomAttributes(typeof(LispAttribute), false);
                    var builtin = new ImportedFunction(name, type, members, pure);

                    foreach (string symbolName in ((LispAttribute)lisp[0]).Names)
                    {
                        var sym = FindSymbol(symbolName);
                        if (!sym.IsUndefined)
                        {
                            PrintWarning("Duplicate builtin name: ", sym.Name);
                        }
                        sym.FunctionValue = builtin;
                        sym.Package.Export(sym.Name);
                    }
                }
            }
        }
Exemple #3
0
 public ImportedFunctionMetaObject(Expression objParam, ImportedFunction runtimeModel)
     : base(objParam, BindingRestrictions.Empty, runtimeModel)
 {
     this.runtimeModel = runtimeModel;
 }
Exemple #4
0
        public static bool ImportMissingSymbol2(string name, Package package)
        {
            var members = package.ImportedType.GetMember(name, ImportBindingFlags).Select(x => ResolveGenericMethod(x)).ToArray();

            if (members.Length == 0)
            {
                return(false);
            }

            var importable = !package.RestrictedImport || members[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

            if (!importable)
            {
                return(false);
            }

            var field  = members[0] as FieldInfo;
            var ucName = members[0].Name.LispName().ToUpper();
            var lcName = members[0].Name.LispName();

            if (field != null)
            {
                if (field.IsLiteral || (field.IsStatic && field.IsInitOnly))
                {
                    var sym = package.FindOrCreate(ucName, excludeUseList: true, export: true);
                    sym.ConstantValue = field.GetValue(null);
                }
                return(true);
            }

            if (members[0] is EventInfo)
            {
                var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                sym.ConstantValue = members[0];
                return(true);
            }

            if (members[0] is ConstructorInfo)
            {
                var builtin = new ImportedConstructor(members.Cast <ConstructorInfo>().ToArray());
                var sym     = package.FindOrCreate("new", excludeUseList: true, export: true);
                sym.FunctionValue           = builtin;
                package.ImportedConstructor = builtin;
                return(true);
            }

            if (members[0] is MethodInfo)
            {
                //if ( !name.StartsWith( "get_" ) && !name.StartsWith( "set_" ) )
                {
                    var sym     = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, members.Cast <MethodInfo>().ToArray(), false);
                    sym.FunctionValue = builtin;
                }

                return(true);
            }

            if (members[0] is PropertyInfo)
            {
                var properties = members.Cast <PropertyInfo>().ToArray();
                var getters    = properties.Select(x => x.GetGetMethod()).Where(x => x != null).ToArray();
                var setters    = properties.Select(x => x.GetSetMethod()).Where(x => x != null).ToArray();

                if (getters.Length != 0)
                {
                    var sym     = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, getters, false);
                    sym.FunctionValue = builtin;
                }

                if (setters.Length != 0)
                {
                    // create getter symbol for setf/setq
                    package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    // use set-xxx
                    var sym     = package.FindOrCreate("set-" + lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, setters, false);
                    sym.FunctionValue = builtin;
                }

                return(true);
            }

            return(false);
        }
Exemple #5
0
 public ImportedFunctionMetaObject(Expression objParam, ImportedFunction runtimeModel)
     : base(objParam, BindingRestrictions.Empty, runtimeModel)
 {
     this.runtimeModel = runtimeModel;
 }
Exemple #6
0
        public static bool ImportMissingSymbol2(string name, Package package)
        {
            var members = package.ImportedType.GetMember(name, ImportBindingFlags).Select(x => ResolveGenericMethod(x)).ToArray();

            if (members.Length == 0)
            {
                return false;
            }

            var importable = !package.RestrictedImport || members[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

            if (!importable)
            {
                return false;
            }

            var field = members[0] as FieldInfo;
            var ucName = members[0].Name.LispName().ToUpper();
            var lcName = members[0].Name.LispName();

            if (field != null)
            {
                if (field.IsLiteral || (field.IsStatic && field.IsInitOnly))
                {
                    var sym = package.FindOrCreate(ucName, excludeUseList: true, export: true);
                    sym.ConstantValue = field.GetValue(null);
                }
                return true;
            }

            if (members[0] is EventInfo)
            {
                var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                sym.ConstantValue = members[0];
                return true;
            }

            if (members[0] is ConstructorInfo)
            {
                var builtin = new ImportedConstructor(members.Cast<ConstructorInfo>().ToArray());
                var sym = package.FindOrCreate("new", excludeUseList: true, export: true);
                sym.FunctionValue = builtin;
                package.ImportedConstructor = builtin;
                return true;
            }

            if (members[0] is MethodInfo)
            {
                //if ( !name.StartsWith( "get_" ) && !name.StartsWith( "set_" ) )
                {
                    var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, members.Cast<MethodInfo>().ToArray(), false);
                    sym.FunctionValue = builtin;
                }

                return true;
            }

            if (members[0] is PropertyInfo)
            {
                var properties = members.Cast<PropertyInfo>().ToArray();
                var getters = properties.Select(x => x.GetGetMethod()).Where(x => x != null).ToArray();
                var setters = properties.Select(x => x.GetSetMethod()).Where(x => x != null).ToArray();

                if (getters.Length != 0)
                {
                    var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, getters, false);
                    sym.FunctionValue = builtin;
                }

                if (setters.Length != 0)
                {
                    // create getter symbol for setf/setq
                    package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    // use set-xxx
                    var sym = package.FindOrCreate("set-" + lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, setters, false);
                    sym.FunctionValue = builtin;
                }

                return true;
            }

            return false;
        }
Exemple #7
0
        public static void ImportExtensionMethodsIntoPackage(Package package, Type type)
        {
            VerifyNoMissingSymbols(package);

            var isbuiltin = type.Assembly == Assembly.GetExecutingAssembly();
            var extendedType = (Type)package.Dict["T"].CheckedValue;
            var restrictedImport = type.GetCustomAttributes(typeof(RestrictedImportAttribute), true).Length != 0;
            var names = type.GetMethods(ImportBindingFlags).Select(x => x.Name).Distinct().ToArray();

            foreach (var name in names)
            {
                var methods = type.GetMember(name, ImportBindingFlags)
                                  .Where(x => x is MethodInfo)
                                  .Select(x => (MethodInfo)ResolveGenericMethod((MethodInfo)x))
                                  .Where(x => ExtendsType(x, extendedType))
                                  .ToList();

                if (methods.Count == 0)
                {
                    continue;
                }

                var importable = !restrictedImport || methods[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

                if (!importable)
                {
                    continue;
                }

                var sym = package.FindOrCreate(name.LispName(), useMissing: true, excludeUseList: true, export: true);
                var builtin = sym.Value as ImportedFunction;

                if (builtin == null)
                {
                    sym.FunctionValue = builtin = new ImportedFunction(name, type);
                }

                if (isbuiltin)
                {
                    // Designed to go before other methods.
                    methods.AddRange(builtin.BuiltinExtensionMembers);
                    builtin.BuiltinExtensionMembers = methods.Distinct().ToArray();
                }
                else {
                    // todo: change order
                    // Goes after other methods as in c#.
                    methods.AddRange(builtin.ExternalExtensionMembers);
                    builtin.ExternalExtensionMembers = methods.Distinct().ToArray();
                }

            }
        }
Exemple #8
0
        public static bool ImportMembers(MemberInfo[] members, Package package)
        {
            if (members.Length == 0)
            {
                return(false);
            }

            var member     = members[0];
            var importable = !package.RestrictedImport || member.GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

            if (!importable)
            {
                return(false);
            }

            var name   = member.Name;
            var ucName = name.LispName().ToUpper();
            var lcName = name.LispName();

            if (member is FieldInfo)
            {
                var field = member as FieldInfo;
                if (field.IsLiteral || (field.IsStatic && field.IsInitOnly))
                {
                    var sym = package.Create(ucName, ispublic: true);
                    sym.ConstantValue = field.GetValue(null);
                }
                return(true);
            }

            if (member is EventInfo)
            {
                var sym = package.Create(lcName, ispublic: true);
                sym.ConstantValue = member;
                return(true);
            }

            if (member is ConstructorInfo)
            {
                var builtin = new ImportedConstructor(members.Cast <ConstructorInfo>().ToArray());
                var sym     = package.Create("new", ispublic: true);
                sym.FunctionValue = builtin;
                return(true);
            }

            if (member is MethodInfo)
            {
                var sym     = package.Create(lcName, ispublic: true);
                var builtin = new ImportedFunction(name, member.DeclaringType, members.Cast <MethodInfo>().ToArray(), false);
                sym.FunctionValue = builtin;
                return(true);
            }

            if (member is PropertyInfo)
            {
                var properties = members.Cast <PropertyInfo>().ToArray();
                var getters    = properties.Select(x => x.GetGetMethod()).Where(x => x != null).ToArray();
                var setters    = properties.Select(x => x.GetSetMethod()).Where(x => x != null).ToArray();

                if (getters.Length != 0)
                {
                    var sym     = package.Create(lcName, ispublic: true);
                    var builtin = new ImportedFunction(name, member.DeclaringType, getters, false);
                    sym.FunctionValue = builtin;
                }

                if (setters.Length != 0)
                {
                    // create getter symbol for setf/setq
                    package.Create(lcName, ispublic: true);
                    // use set-xxx
                    var sym     = package.Create("set-" + lcName, ispublic: true);
                    var builtin = new ImportedFunction(name, member.DeclaringType, setters, false);
                    sym.FunctionValue = builtin;
                }

                return(true);
            }

            return(false);
        }