Example #1
0
        public PyModuleSpec find_spec(FrameContext context, string name, string import_path, PyModule target)
        {
            PyModuleSpec found      = null;
            ClrContext   clrContext = null;

            if (context.HasVariable(ClrContext.FrameContextTokenName))
            {
                clrContext = (ClrContext)context.GetVariable(ClrContext.FrameContextTokenName);
                found      = findInAssemblies(name, clrContext.AddedReferences, clrContext);
                if (found != null)
                {
                    return(found);
                }
            }

            // If there isn't a CLR context yet, we still search the default assemblies and load from there.
            // Attach the context using the default modules as the added references.
            if (found == null)
            {
                found = findInAssemblies(name, DefaultAssemblies, clrContext);
                if (found != null)
                {
                    if (clrContext == null)
                    {
                        clrContext = new ClrContext();
                        clrContext.AddAssembliesToReferences(DefaultAssemblies);
                        context.AddVariable(ClrContext.FrameContextTokenName, clrContext);
                        found.LoaderState = clrContext;
                    }
                    return(found);
                }
            }

            return(null);
        }
Example #2
0
        static LastException()
        {
            if (myThreadPointerFieldInfo == null)
            {
                // Dont read the donĀ“t or you will get scared.
                // I prefer to read this more like: If you dont know the rules you will
                // get biten by the next .NET Framework update
                myThreadPointerFieldInfo = typeof(Thread).GetField("DONT_USE_InternalThread", BindingFlags.Instance | BindingFlags.NonPublic);
            }

            if (myThreadOffset == -1)
            {
                ClrContext context = ClrContext.None;
                context |= (IntPtr.Size == 8) ? ClrContext.Is64Bit : ClrContext.Is32Bit;
                context |= (Environment.Version.Major == 2) ? ClrContext.IsNet2 : ClrContext.None;
                context |= (Environment.Version.Major == 4) ? ClrContext.IsNet4 : ClrContext.None;
                context |= (Environment.Version.Major == 4 && Environment.Version.Revision == 42000) ? ClrContext.IsNet4_6 : ClrContext.None;

                switch (context)
                {
                case ClrContext.Is32Bit | ClrContext.IsNet4 | ClrContext.IsNet4_6:
                    myThreadOffset = 0x18C;
                    break;

                case ClrContext.Is32Bit | ClrContext.IsNet4:
                    myThreadOffset = 0x188;
                    break;

                case ClrContext.Is32Bit | ClrContext.IsNet2:
                    myThreadOffset = 0x180;
                    break;

                case ClrContext.Is64Bit | ClrContext.IsNet4 | ClrContext.IsNet4_6:
                    myThreadOffset = 0x258;
                    break;

                case ClrContext.Is64Bit | ClrContext.IsNet4:
                    myThreadOffset = 0x250;
                    break;

                case ClrContext.Is64Bit | ClrContext.IsNet2:
                    myThreadOffset = 0x240;
                    break;
                }
            }
        }
Example #3
0
        private PyModuleSpec findInAssemblies(string name, List <Assembly> assemblies, ClrContext loaderContext)
        {
            // TODO: Try to find a faster way to do these lookups.
            foreach (var assembly in assemblies)
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (type.Namespace == name)
                    {
                        var spec = PyModuleSpec.Create(name, loader, "", null);
                        spec.LoaderState = loaderContext;
                        return(spec);
                    }
                }

                // Still here? Maybe it's an actual type, not an assembly!
                foreach (var type in assembly.GetTypes())
                {
                    if (type.Name == name)
                    {
                        var spec = PyModuleSpec.Create(name, loader, "", null);
                        spec.LoaderState = loaderContext;
                        return(spec);
                    }
                }
            }
            return(null);
        }