예제 #1
0
        internal bool Require(string s)
        {
            RThread th = ruby.GetCurrentContext();

            ////ruby.CheckSafeString(th, s);
            s = s.Replace('/', Path.DirectorySeparatorChar);
            string fname  = null;
            bool   script = true;
            string ext    = Path.GetExtension(s);

            if (ext != String.Empty)
            {
                if (String.Compare(ext, ".rb", true) == 0)
                {
                    fname = FindFile(s);
                }
                else if (String.Compare(ext, ".dll", true) == 0 ||
                         String.Compare(ext, ".so", true) == 0)
                {
                    fname  = FindFile(s);
                    script = false;
                }
            }
            else
            {
                for (int i = 0; i < exts.Length; i++)
                {
                    fname = FindFile(s + exts[i]);
                    if (fname != null)
                    {
                        if (i != 0)
                        {
                            script = false;
                        }
                        break;
                    }
                }
            }
            if (fname == null)
            {
                throw new eLoadError("No such file to load -- " + s);
            }

            string fileName = Path.GetFileName(fname);

            if (featureCheck(fileName))
            {
                return(false);
            }

            if (script == false)
            {
                try
                {
                    AssemblyName asm = AssemblyName.GetAssemblyName(fname);
                    Assembly     a   = Assembly.Load(asm);
                    Type[]       tps = a.GetTypes();
                    foreach (Type t in tps)
                    {
                        AddType(t, th);
                    }
                    features.Add(fileName);
                }
                catch (FileLoadException)
                {
                    return(false);
                }
                catch (BadImageFormatException)
                {
                    throw new eLoadError("Not valid file image to load -- " + fname);
                }
            }
            else
            {
                ////int oldSafeLevel = th.safeLevel;
                ////th.safeLevel = 0;
                ////th.PushTag(Tag.TAG.PROT_NONE);
                try
                {
                    ruby.Load(fname, false);
                    features.Add(fileName);
                }
                catch (Exception e)
                {
#if _DEBUG
                    System.Console.Error.WriteLine(e.Message);
                    System.Console.Error.WriteLine(e.StackTrace);
#endif
                    throw e;
                }
                finally
                {
                    ////th.PopTag(true);
                    ////th.safeLevel = oldSafeLevel;
                }
            }
            return(true);
        }