CreateLoadError() публичный статический Метод

public static CreateLoadError ( Exception innerException ) : Exception
innerException System.Exception
Результат System.Exception
Пример #1
0
        internal Assembly ResolveAssembly(string /*!*/ fullName)
        {
            Utils.Log(String.Format("Resolving assembly: '{0}'", fullName), "RESOLVE_ASSEMBLY");

            AssemblyName assemblyName = new AssemblyName(fullName);
            ResolvedFile file         = FindFile(assemblyName.Name, true, ArrayUtils.EmptyStrings).FirstOrDefault();

            if (file == null || file.SourceUnit != null)
            {
                return(null);
            }

            Utils.Log(String.Format("Assembly '{0}' resolved: found in '{1}'", fullName, file.Path), "RESOLVE_ASSEMBLY");
            try {
                Assembly assembly = Platform.LoadAssemblyFromPath(file.Path);

#if !SILVERLIGHT
                // TODO: this API is broken
                if (AssemblyName.ReferenceMatchesDefinition(assemblyName, assembly.GetName()))
#endif
                {
                    Utils.Log(String.Format("Assembly '{0}' loaded for '{1}'", assembly.FullName, fullName), "RESOLVE_ASSEMBLY");
                    DomainManager.LoadAssembly(assembly);
                    return(assembly);
                }
            } catch (Exception e) {
                throw RubyExceptions.CreateLoadError(e);
            }

#if !SILVERLIGHT
            return(null);
#endif
        }
Пример #2
0
        /// <exception cref="LoadError"></exception>
        private void LoadLibrary(Type /*!*/ initializerType, bool builtin)
        {
            lock (_loadedTypes) {
                if (_loadedTypes.Contains(initializerType))
                {
                    return;
                }
            }

            LibraryInitializer initializer;

            try {
                initializer = Activator.CreateInstance(initializerType) as LibraryInitializer;
            } catch (TargetInvocationException e) {
                throw RubyExceptions.CreateLoadError(e.InnerException);
            } catch (Exception e) {
                throw RubyExceptions.CreateLoadError(e);
            }

            if (initializer == null)
            {
                throw RubyExceptions.CreateLoadError(String.Format("Specified type {0} is not a subclass of {1}",
                                                                   initializerType.FullName,
                                                                   typeof(LibraryInitializer).FullName)
                                                     );
            }

            // Propagate exceptions from initializers (do not wrap them to LoadError).
            // E.g. TypeError (can't modify frozen module) can be thrown.
            initializer.LoadModules(_context, builtin);

            lock (_loadedTypes) {
                _loadedTypes.Add(initializerType);
            }
        }
Пример #3
0
        /// <summary>
        /// Searches file in load directories and then appends extensions.
        /// </summary>
        private ResolvedFile FindFile(string /*!*/ path, bool appendExtensions, string[] sourceFileExtensions)
        {
            Assert.NotNull(path);
            bool isAbsolutePath;

#if SILVERLIGHT
            {
#else
            if (path.StartsWith("~/", StringComparison.Ordinal) || path.StartsWith("~\\", StringComparison.Ordinal))
            {
                path           = RubyUtils.ExpandPath(_context.Platform, path);
                isAbsolutePath = true;
            }
            else
            {
#endif
                try {
                    isAbsolutePath = Platform.IsAbsolutePath(path);
                } catch (ArgumentException e) {
                    throw RubyExceptions.CreateLoadError(e);
                }
            }

            string extension = RubyUtils.GetExtension(path);

            // Absolute path -> load paths not consulted.
            if (isAbsolutePath)
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            string[] loadPaths = GetLoadPathStrings();

            if (loadPaths.Length == 0)
            {
                return(null);
            }

            // If load paths are non-empty and the path starts with .\ or ..\ then MRI also ignores the load paths.
            if (path.StartsWith("./", StringComparison.Ordinal) ||
                path.StartsWith("../", StringComparison.Ordinal) ||
                path.StartsWith(".\\", StringComparison.Ordinal) ||
                path.StartsWith("..\\", StringComparison.Ordinal))
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            foreach (var dir in loadPaths)
            {
                ResolvedFile result = ResolveFile(RubyUtils.CombinePaths(dir, path), extension, appendExtensions, sourceFileExtensions);
                if (result != null)
                {
                    return(result);
                }
            }

            return(null);
        }
Пример #4
0
        /// <exception cref="LoadError"></exception>
        internal void LoadBuiltins()
        {
            Type initializerType;

            try {
                Assembly assembly = _context.DomainManager.Platform.LoadAssembly(GetIronRubyAssemblyLongName("IronRuby.Libraries"));
                initializerType = assembly.GetType(LibraryInitializer.GetBuiltinsFullTypeName());
            } catch (Exception e) {
                throw RubyExceptions.CreateLoadError(e);
            }

            LoadLibrary(initializerType, true);
        }
Пример #5
0
        /// <summary>
        /// Appends extensions if applicable.
        /// </summary>
        private ResolvedFile ResolveFile(string /*!*/ path, string /*!*/ extension, bool appendExtensions, string[] /*!*/ knownExtensions)
        {
            Debug.Assert(RubyUtils.GetExtension(path) == extension);

            string expandedPath = RubyUtils.ExpandPath(_context.Platform, path);

            // MRI doesn't load file w/o .rb extension:
            if (IsKnownExtension(extension, knownExtensions))
            {
                return(GetSourceUnit(path, expandedPath, extension, false));
            }

            if (_LibraryExtensions.IndexOf(extension, DlrConfiguration.FileExtensionComparer) != -1)
            {
                if (Platform.FileExists(expandedPath))
                {
                    return(new ResolvedFile(expandedPath, null));
                }
            }
            else if (!appendExtensions)
            {
                return(GetSourceUnit(path, expandedPath, extension, false));
            }

            if (appendExtensions)
            {
                List <string> matchingExtensions = GetExtensionsOfExistingFiles(expandedPath, knownExtensions);

                if (matchingExtensions.Count == 1)
                {
                    return(GetSourceUnit(path + matchingExtensions[0], expandedPath + matchingExtensions[0], matchingExtensions[0], true));
                }
                else if (matchingExtensions.Count > 1)
                {
                    Exception e = new AmbiguousFileNameException(expandedPath + matchingExtensions[0], expandedPath + matchingExtensions[1]);
                    throw RubyExceptions.CreateLoadError(e);
                }

                foreach (string libExtension in _LibraryExtensions)
                {
                    if (Platform.FileExists(expandedPath + libExtension))
                    {
                        return(new ResolvedFile(expandedPath + libExtension, libExtension));
                    }
                }
            }

            return(null);
        }
Пример #6
0
        private Assembly GetAssembly(string /*!*/ assemblyName, bool throwOnError, bool tryPartialName)
        {
#if SILVERLIGHT || WIN8 || WP75
            tryPartialName = false;
#endif
            try {
                return(Platform.LoadAssembly(assemblyName));
            } catch (Exception e) {
                if (!tryPartialName || !(e is FileNotFoundException))
                {
                    if (throwOnError)
                    {
                        throw RubyExceptions.CreateLoadError(e);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }

#if SILVERLIGHT || WIN8 || WP75
            throw Assert.Unreachable;
#else
#pragma warning disable 618,612 // csc, gmcs
            Assembly assembly;
            try {
                assembly = Assembly.LoadWithPartialName(assemblyName);
            } catch (Exception e) {
                if (throwOnError)
                {
                    throw RubyExceptions.CreateLoadError(e);
                }
                else
                {
                    return(null);
                }
            }
            if (assembly == null && throwOnError)
            {
                throw RubyExceptions.CreateLoadError(String.Format("Assembly '{0}' not found", assemblyName));
            }
#pragma warning restore 618,612
            return(assembly);
#endif
        }
Пример #7
0
        private bool LoadAssembly(Assembly /*!*/ assembly, string typeName, bool throwOnError)
        {
            Utils.Log(String.Format("Loading assembly '{0}' and type '{1}'", assembly, typeName), "LOADER");
            Type initializerType;

            if (typeName != null)
            {
                // load Ruby library:
                initializerType = assembly.GetType(typeName);
                if (initializerType == null)
                {
                    if (throwOnError)
                    {
                        throw new TypeLoadException(
                                  String.Format("Unhandled Exception: System.TypeLoadException: Could not load type '{0}' from assembly '{1}'.",
                                                typeName, assembly.FullName)
                                  );
                    }
                    return(false);
                }

                LoadLibrary(initializerType, false);
            }
            else
            {
                // load namespaces:
                try {
                    DomainManager.LoadAssembly(assembly);
                } catch (Exception e) {
                    if (throwOnError)
                    {
                        throw RubyExceptions.CreateLoadError(e);
                    }
                    return(false);
                }
            }

            return(true);
        }
Пример #8
0
        private bool LoadAssembly(Assembly /*!*/ assembly, string typeName, bool throwOnError)
        {
            Utils.Log(String.Format("Loading assembly '{0}' and type '{1}'", assembly, typeName), "LOADER");
            Type initializerType;

            if (typeName != null)
            {
                // load Ruby library:
                try {
                    initializerType = assembly.GetType(typeName, true);
                } catch (Exception e) {
                    if (throwOnError)
                    {
                        throw new LoadError(e.Message, e);
                    }
                    return(false);
                }

                LoadLibrary(initializerType, false);
            }
            else
            {
                // load namespaces:
                try {
                    DomainManager.LoadAssembly(assembly);
                } catch (Exception e) {
                    if (throwOnError)
                    {
                        throw RubyExceptions.CreateLoadError(e);
                    }
                    return(false);
                }
            }

            return(true);
        }
Пример #9
0
        private bool LoadFromPath(Scope globalScope, object self, string /*!*/ path, RubyEncoding /*!*/ pathEncoding, LoadFlags flags, out object loaded)
        {
            Assert.NotNull(pathEncoding, path);

            string[] sourceFileExtensions;
            if ((flags & LoadFlags.AnyLanguage) != 0)
            {
                sourceFileExtensions = DomainManager.Configuration.GetFileExtensions();
            }
            else
            {
                sourceFileExtensions = DomainManager.Configuration.GetFileExtensions(_context);
            }

            IList <ResolvedFile> files = FindFile(path, (flags & LoadFlags.AppendExtensions) != 0, sourceFileExtensions);

            if (files.Count == 0)
            {
                // MRI: doesn't throw an exception if the path is in $" (performs resolution first though):
                if (AlreadyLoaded(path, null, flags, sourceFileExtensions))
                {
                    loaded = null;
                    return(false);
                }
                throw RubyExceptions.CreateLoadError(String.Format("no such file to load -- {0}", path));
            }

            ResolvedFile file = files.First();

            string pathWithExtension = path;

            if (file.AppendedExtension != null)
            {
                pathWithExtension += file.AppendedExtension;
            }

            if (AlreadyLoaded(path, files, flags) || _unfinishedFiles.Contains(file.Path))
            {
                if ((flags & LoadFlags.ResolveLoaded) != 0)
                {
                    if (file.SourceUnit != null)
                    {
                        Scope loadedScope;
                        if (!LoadedScripts.TryGetValue(file.Path, out loadedScope))
                        {
                            throw RubyExceptions.CreateLoadError(String.Format("no such file to load -- {0}", file.Path));
                        }
                        loaded = loadedScope;
                    }
                    else
                    {
                        loaded = Platform.LoadAssemblyFromPath(file.Path);
                    }
                }
                else
                {
                    loaded = null;
                }
                return(false);
            }

            try {
                // save path as is, no canonicalization nor combination with an extension or directory:
                _unfinishedFiles.Push(file.Path);

                if (file.SourceUnit != null)
                {
                    AddScriptLines(file.SourceUnit);

                    ScriptCode compiledCode;
                    if (file.SourceUnit.LanguageContext == _context)
                    {
                        compiledCode = CompileRubySource(file.SourceUnit, flags);
                    }
                    else
                    {
                        compiledCode = file.SourceUnit.Compile();
                    }
                    loaded = Execute(globalScope, compiledCode);
                }
                else
                {
                    Debug.Assert(file.Path != null);
                    try {
                        Assembly assembly = Platform.LoadAssemblyFromPath(file.Path);
                        DomainManager.LoadAssembly(assembly);
                        loaded = assembly;
                    } catch (Exception e) {
                        throw RubyExceptions.CreateLoadError(e);
                    }
                }

                FileLoaded(MutableString.Create(file.Path, pathEncoding), flags);
            } finally {
                _unfinishedFiles.Pop();
            }

            return(true);
        }
Пример #10
0
        private ResolvedFile FindFile(string /*!*/ path, bool appendExtensions, string[] sourceFileExtensions)
        {
            Assert.NotNull(path);
            bool   isAbsolutePath;
            string extension;
            string home = null;

#if !SILVERLIGHT
            if (path.StartsWith("~/", StringComparison.Ordinal) || path.StartsWith("~\\", StringComparison.Ordinal))
            {
                try {
                    home = Environment.GetEnvironmentVariable("HOME");
                } catch (SecurityException) {
                    home = null;
                }

                if (home == null)
                {
                    throw RubyExceptions.CreateArgumentError(String.Format("couldn't find HOME environment -- expanding `{0}'", path));
                }
            }
#endif
            try {
                if (home != null)
                {
                    path = RubyUtils.CombinePaths(home, path.Substring(2));
                }

                isAbsolutePath = Platform.IsAbsolutePath(path);
                extension      = RubyUtils.GetExtension(path);
            } catch (ArgumentException e) {
                throw RubyExceptions.CreateLoadError(e);
            }

            // Absolute path -> load paths not consulted.
            if (isAbsolutePath)
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            string[] loadPaths = GetLoadPathStrings();

            if (loadPaths.Length == 0)
            {
                return(null);
            }

            // If load paths are non-empty and the path starts with .\ or ..\ then MRI also ignores the load paths.
            if (path.StartsWith("./", StringComparison.Ordinal) ||
                path.StartsWith("../", StringComparison.Ordinal) ||
                path.StartsWith(".\\", StringComparison.Ordinal) ||
                path.StartsWith("..\\", StringComparison.Ordinal))
            {
                return(ResolveFile(path, extension, appendExtensions, sourceFileExtensions));
            }

            foreach (var dir in loadPaths)
            {
                try {
                    ResolvedFile result = ResolveFile(RubyUtils.CombinePaths(dir, path), extension, appendExtensions, sourceFileExtensions);
                    if (result != null)
                    {
                        return(result);
                    }
                } catch (ArgumentException) {
                    // invalid characters in path
                }
            }

            return(null);
        }