PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Backend/Runtime/ReflectedPackage.cs

https://bitbucket.org/AdamMil/boaold
C# | 119 lines | 82 code | 16 blank | 21 comment | 10 complexity | 641007b4539e2e73d4259a0682ddd633 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Boa is the reference implementation for a language similar to Python,
  3. also called Boa. This implementation is both interpreted and compiled,
  4. targeting the Microsoft .NET Framework.
  5. http://www.adammil.net/
  6. Copyright (C) 2004-2005 Adam Milazzo
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. using System.Collections;
  21. using System.Reflection;
  22. namespace Boa.Runtime
  23. {
  24. // FIXME: handle nested classes
  25. [BoaType("package")]
  26. public sealed class ReflectedPackage : IHasAttributes
  27. { public ReflectedPackage(string name) { __name__=name; __dict__=new Dict(); }
  28. #region IHasAttributes Members
  29. public List __attrs__() { return __dict__.keys(); }
  30. public object __getattr__(string key)
  31. { object obj = __dict__[key];
  32. return obj!=null || __dict__.Contains(key) ? obj : Ops.Missing;
  33. }
  34. public void __setattr__(string key, object value) { __dict__[key]=value; }
  35. public void __delattr__(string key) { __dict__.Remove(key); }
  36. #endregion
  37. public override string ToString() { return string.Format("<namespace '{0}'>", __name__); }
  38. public string __name__;
  39. public Dict __dict__;
  40. public static ReflectedPackage FromNamespace(string ns)
  41. { string[] bits = ns.Split('.');
  42. ReflectedPackage rns;
  43. lock(dict) rns = (ReflectedPackage)dict[bits[0]];
  44. if(rns==null)
  45. { rns = new ReflectedPackage(bits[0]);
  46. lock(dict) dict[bits[0]] = rns;
  47. }
  48. ns = bits[0];
  49. for(int i=1; i<bits.Length; i++)
  50. { ns = ns+"."+bits[i];
  51. ReflectedPackage tp = (ReflectedPackage)rns.__dict__[bits[i]];
  52. if(tp==null) rns.__dict__[bits[i]] = tp = new ReflectedPackage(ns);
  53. rns = tp;
  54. }
  55. return rns;
  56. }
  57. public static ReflectedPackage GetPackage(string name)
  58. { Initialize(); // do we want to do this?
  59. lock(dict) return (ReflectedPackage)dict[name];
  60. }
  61. public static Assembly LoadAssemblyByName(string name) { return LoadAssemblyByName(name, true); }
  62. public static Assembly LoadAssemblyByName(string name, bool throwOnError)
  63. { Assembly a=null;
  64. try { a=Assembly.LoadWithPartialName(name); }
  65. catch
  66. { if(!throwOnError) return null;
  67. throw Ops.RuntimeError("Could not load assembly {0}", name);
  68. }
  69. InitAssembly(a);
  70. return a;
  71. }
  72. public static Assembly LoadAssemblyFromFile(string filename) { return LoadAssemblyFromFile(filename, true); }
  73. public static Assembly LoadAssemblyFromFile(string filename, bool throwOnError)
  74. { Assembly a=null;
  75. try { a = Assembly.LoadFrom(filename); }
  76. catch
  77. { if(!throwOnError) return null;
  78. throw Ops.RuntimeError("Could not load assembly from {0}", filename);
  79. }
  80. InitAssembly(a);
  81. return a;
  82. }
  83. static void InitAssembly(Assembly a)
  84. { ReflectedPackage p=null;
  85. foreach(Type type in a.GetTypes())
  86. { if(type.Namespace==null) continue;
  87. if(p==null || p.__name__!=type.Namespace) p = ReflectedPackage.FromNamespace(type.Namespace);
  88. p.__dict__[type.Name] = ReflectedType.FromType(type); // do this lazily?
  89. }
  90. }
  91. static void Initialize()
  92. { if(!initialized)
  93. { initialized=true;
  94. LoadAssemblyByName("mscorlib", false);
  95. LoadAssemblyByName("System", false);
  96. }
  97. }
  98. static readonly Dict dict=new Dict();
  99. static bool initialized;
  100. }
  101. } // namespace Boa.Runtime